package org.umlg.sqlg.test.datasource;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.umlg.sqlg.structure.SqlgGraph;
import org.umlg.sqlg.structure.ds.C3P0DataSource;

import javax.naming.Context;
import javax.naming.spi.InitialContextFactory;
import javax.naming.spi.NamingManager;
import javax.sql.DataSource;
import java.net.URL;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Lukas Krejci
 */
public class TestJNDIInitialization {

    private static final Logger logger = LoggerFactory.getLogger(TestJNDIInitialization.class);

    private static Configuration configuration;
    private static DataSource ds;

    @Rule
    public TestRule watcher = new TestWatcher() {
        protected void starting(Description description) {
            logger.info("Starting test: " + description.getClassName() + "." + description.getMethodName());
        }

        protected void finished(Description description) {
            logger.info("Finished test: " + description.getClassName() + "." + description.getMethodName());
        }
    };

    @BeforeClass
    public static void beforeClass() throws Exception {
        URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
        Configurations configs = new Configurations();
        configuration = configs.properties(sqlProperties);
        if (!configuration.containsKey("jdbc.url")) {
            throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
        }

        ds = C3P0DataSource.create(configuration).getDatasource();

        //change the connection url to be a JNDI one
        configuration.setProperty("jdbc.url", "jndi:testConnection");

        //set up the initial context
        NamingManager.setInitialContextFactoryBuilder(environment -> {
            InitialContextFactory mockFactory = mock(InitialContextFactory.class);
            Context mockContext = mock(Context.class);
            when(mockFactory.getInitialContext(any())).thenReturn(mockContext);

            when(mockContext.lookup("testConnection")).thenReturn(ds);

            return mockFactory;
        });
    }

    @Test
    public void testLoadingDatasourceFromJndi() throws Exception {
        SqlgGraph g = SqlgGraph.open(configuration);
        assertNotNull(g.getSqlDialect());
        Assert.assertEquals(configuration.getString("jdbc.url"), g.getJdbcUrl());
        assertNotNull(g.getConnection());
    }
}

@@ -182,90 +182,9 @@
 nit.
-Assert;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Rule;%0Aimport org.junit.Test
+*
 ;%0Aim
@@ -1019,16 +1019,66 @@
 urce ds;
+%0A    private static C3P0DataSource c3P0DataSource;
 %0A%0A    @R
@@ -1992,18 +1992,30 @@
         
-ds
+c3P0DataSource
  = C3P0D
@@ -2045,16 +2045,45 @@
 uration)
+;%0A        ds = c3P0DataSource
 .getData
@@ -2663,24 +2663,117 @@
  %7D);%0A    %7D%0A%0A
+    @AfterClass%0A    public static void afterClass() %7B%0A        c3P0DataSource.close();%0A    %7D%0A%0A
     @Test%0A  
@@ -2845,16 +2845,21 @@
         
+try (
 SqlgGrap
@@ -2885,34 +2885,40 @@
 n(configuration)
-;%0A
+) %7B%0A    
         assertNo
@@ -2951,16 +2951,20 @@
         
+   
+ 
 Assert.a
@@ -3029,32 +3029,36 @@
 Url());%0A        
+    
 assertNotNull(g.
@@ -3071,20 +3071,30 @@
 nection());%0A
+        %7D%0A
     %7D%0A%7D%0A

fix test to properly close the datasource