package LinkFuture;

import LinkFuture.Init.Extensions.PathExtension;
import org.junit.Before;
import org.junit.BeforeClass;
import org.postgresql.ds.PGPoolingDataSource;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;

import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Created by Cyokin
 * on 3/27/2015.
 */
public class TestCaseBase {
    public static final String MySQLConnection = "jdbc:mysql://localhost:3306/world?user=root&password=qazwsx123&generateSimpleParameterMetadata=true";
    public static final String MSSQLConnection = "jdbc:sqlserver://10.25.36.65:1433;databaseName=world;user=sa;password=bbh2gd";
    public static final String PostgreSQLConnection = "jdbc:postgresql://10.25.36.65:5432/world?user=postgres&password=qazwsx123";
    public static final String MyServerSQLConnection = "jdbc:postgresql://192.168.201.110:5432/postgres?user=postgres&password=qazwsx123";
    public static final String TendSQLConnection = "jdbc:postgresql://192.168.201.110:5432/tend?user=postgres&password=qazwsx123";
    public static final String QyouPostgreSQLConnection = "jdbc:postgresql://98.247.87.181:5432/postgres?user=postgres&password=pass123&charSet=utf-8";

    @BeforeClass
    public static void setup() throws NamingException, MalformedURLException {


        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        builder.bind("java:/comp/env/LinkFuture/config_filePath", "classpath:config.properties");
//        builder.bind("java:/comp/env/jdbc/mySqlDB", getMySql());
        builder.bind("java:/comp/env/jdbc/PostgreSQLDB", getPostgreSQL());
        builder.activate();

        //mock class path
        URLClassLoader urlClassLoader
                = new URLClassLoader(new URL[]{new File(PathExtension.FullPhysicalPath("Demo/src/main/resources")).toURL()},
                Thread.currentThread().getContextClassLoader());
        Thread.currentThread().setContextClassLoader(urlClassLoader);

        //mock domain
        //PathExtension.setApplicationPath(PathExtension.FullPhysicalPath("Demo/src/main/resources/"));
    }

    @Before
    public void init(){

    }
    private static DataSource getPostgreSQL(){
        PGPoolingDataSource source = new PGPoolingDataSource();
        source.setDataSourceName("postgres");
        source.setServerName("cyokin.ddns.net");
        source.setDatabaseName("postgres");
        source.setUser("postgres");
        source.setPassword("qazwsx123");
        source.setMaxConnections(10);
        return source;
    }
}
