import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.postgresql.ds.PGConnectionPoolDataSource;

import junit.framework.Assert;


//master 127.0.0.1:5432
//slave 127.0.0.1:5433

public class MutiHostsConnectionTest {
    
    public static String getServerInfo(Connection con) throws SQLException{
        ResultSet rs = con.createStatement().executeQuery("select inet_server_addr(),inet_server_port()");
        rs.next();
        String hostspec = rs.getString(1)+":"+rs.getString(2);
        return hostspec;
        
    }
    public static void testFailOver() throws SQLException{
        System.out.println("");
        System.out.println("");
        System.out.println("test()");
        Connection con;
        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        
        String url = "jdbc:postgresql://127.0.0.1:5433,notahost:0001/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertEquals("127.0.0.1:5433", getServerInfo(con));
        con.close();
        
        url = "jdbc:postgresql://notahost:0002,127.0.0.1:5433/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertEquals("127.0.0.1:5433", getServerInfo(con));
        con.close();
        
        
        url = "jdbc:postgresql://127.0.0.1:5432,127.0.0.1:5433/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        
      //test targetServerType=Any
        props.setProperty("targetServerType", "Any");
        url = "jdbc:postgresql://127.0.0.1:5432,127.0.0.1:5433/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        
        //test targetServerType=Master
        props.setProperty("targetServerType", "Master");
        url = "jdbc:postgresql://127.0.0.1:5432,127.0.0.1:5433/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertTrue(isMaster(con));
        con.close();
        
        //test targetServerType=Slave
        props.setProperty("targetServerType", "Slave");
        url = "jdbc:postgresql://127.0.0.1:5433,127.0.0.1:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertFalse(isMaster(con));
        con.close();
        
        //test targetServerType=SlaveFirst
        props.setProperty("targetServerType", "SlaveFirst");
        url = "jdbc:postgresql://127.0.0.1:5433,127.0.0.1:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertFalse(isMaster(con));
        con.close();
        
        props.setProperty("targetServerType", "SlaveFirst");
        url = "jdbc:postgresql://notahost:0001,127.0.0.1:5433/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertEquals("127.0.0.1:5433", getServerInfo(con));
        con.close();
        
        props.setProperty("targetServerType", "SlaveFirst");
        url = "jdbc:postgresql://notahost:0001,127.0.0.1:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertEquals("127.0.0.1:5432", getServerInfo(con));
        con.close();
    }
    
    public static void testLoadBalance() throws SQLException{
        System.out.println("");
        System.out.println("");
        System.out.println("testLoadBalance()");
        Connection con;
        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        props.setProperty("enableLoadBalance", "true");
        
        String url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001,127.0.0.1:5432,localhost:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        
        //test targetServerType=Any
        props.setProperty("targetServerType", "Any");
        url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001,127.0.0.1:5432,localhost:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        
        //test targetServerType=Master
        props.setProperty("targetServerType", "Master");
        url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001,127.0.0.1:5432,localhost:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertTrue(isMaster(con));
        con.close();
        
        //test targetServerType=Slave
        props.setProperty("targetServerType", "Slave");
        url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001,127.0.0.1:5432,localhost:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertFalse(isMaster(con));
        con.close();
        
        //test targetServerType=Slave
        props.setProperty("targetServerType", "Slave");
        url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001,localhost:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertFalse(isMaster(con));
        con.close();
        
        
        //test targetServerType=SlaveFirst
        props.setProperty("targetServerType", "SlaveFirst");
        url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001,127.0.0.1:5433,localhost:5432/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertFalse(isMaster(con));
        con.close();
        
        props.setProperty("targetServerType", "SlaveFirst");
        url = "jdbc:postgresql://127.0.0.1:5433,localhost:5433,notahost:0001/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertEquals("127.0.0.1:5433", getServerInfo(con));
        con.close();
        
        props.setProperty("targetServerType", "SlaveFirst");
        url = "jdbc:postgresql://127.0.0.1:5432,localhost:5432,notahost:0001/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+url);
        Assert.assertEquals("127.0.0.1:5432", getServerInfo(con));
        con.close();
        
    }
    
    public static void testLoadBalanceWithFailedReCheck() throws SQLException, IOException{
        
        System.out.println("");
        System.out.println("");
        System.out.println("testLoadBalanceWithFailedReCheck()");
        Connection con;
        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        props.setProperty("enableLoadBalance", "true");
        props.setProperty("failedHostCheckPeriod", "3");
        props.setProperty("targetServerType", "SlaveFirst");
        
        System.out.println("Stop the slave and press enter");
        System.in.read();
        System.in.read();
        String url = "jdbc:postgresql://127.0.0.1:5432,127.0.0.1:5433/postgres"+protocolVersion;
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        Assert.assertTrue(isMaster(con));
        con.close();
        
        System.out.println("Start the slave and press enter after 5 seconds");
        System.in.read();
        System.in.read();
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        con.close();
        
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        Assert.assertFalse(isMaster(con));
        con.close();

    }
    
    public static void testURL() throws SQLException{
        System.out.println("");
        System.out.println("");
        System.out.println("testURL()");
        Connection con;
        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        
        String url = "jdbc:postgresql://127.0.0.1:5432,127.0.0.1:5433/postgres?targetServerType=master";
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        Assert.assertTrue(isMaster(con));
        con.close();
        
        url = "jdbc:postgresql://127.0.0.1:5432,127.0.0.1:5433/postgres?targetServerType=slave";
        con = DriverManager.getConnection(url,props);
        System.out.println("Connect OK:"+getServerInfo(con)+":"+url);
        Assert.assertFalse(isMaster(con));
        con.close();
    }
    
    public static boolean isMaster(Connection con) throws SQLException{
        ResultSet rs = con.createStatement().executeQuery("show transaction_read_only");
        rs.next();
        return "off".equalsIgnoreCase(rs.getString(1));
    }
    
    public static String protocolVersion = "";
    public static String user = "chj";
    public static String password = "postgres";
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Class.forName("org.postgresql.Driver");
            protocolVersion = "?protocolVersion=3";
            
            testFailOver();
            testLoadBalance();
            testURL();
           //testLoadBalanceWithFailedReCheck();
            System.out.println("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
