package org.geotools.data.jdbc.ds;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import junit.framework.TestCase;

import org.apache.commons.dbcp.BasicDataSource;
import org.geotools.data.jdbc.datasource.DBCPDataSourceFactory;
import org.geotools.data.jdbc.datasource.DataSourceFinder;

public class DataSourceFinderTest extends TestCase {
    public void testDbcpFactory() throws IOException {
        assertTrue(new DBCPDataSourceFactory().isAvailable());
        DataSourceFinder.scanForPlugins();
        
        Map map = new HashMap();
        map.put(DBCPDataSourceFactory.DSTYPE.key, "DBCP");
        map.put(DBCPDataSourceFactory.DRIVERCLASS.key, "org.h2.Driver");
        map.put(DBCPDataSourceFactory.JDBC_URL.key, "jdbc:h2:mem:test_mem");
        map.put(DBCPDataSourceFactory.USERNAME.key, "admin");
        map.put(DBCPDataSourceFactory.PASSWORD.key, "");
        map.put(DBCPDataSourceFactory.MAXACTIVE.key, new Integer(10));
        map.put(DBCPDataSourceFactory.MAXIDLE.key, new Integer(0));
        
        DataSource source =  DataSourceFinder.getDataSource(map);
        assertNotNull(source);
        assertTrue(source instanceof BasicDataSource);
    }
    
//    public void testJNDIFactory() throws Exception {
        // can't make this work... there are dependencies from EJBMock to stuff
        // that's not in the maven repos
        
//        EJBMockObjectFactory ejbMock = new EJBMockObjectFactory();
//        ejbMock.initMockContextFactory();
//        Context mockContext = new MockContext();
//        InitialContext context = new InitialContext();
//        DataSource mockDataSource = new MockDataSource();
//        context.rebind("jdbc/pool", mockDataSource);
//        JNDI.init(context);
//        
//        assertTrue(new JNDIDataSourceFactory().isAvailable());
//        DataSourceFinder.scanForPlugins();
//        
//        
//        
//        Map map = new HashMap();
//        map.put(JNDIDataSourceFactory.JNDI_REFNAME.key, "jdbc/pool");
//        
//        DataSource source =  DataSourceFinder.getDataSource(map);
//        assertNotNull(source);
//        assertEquals(mockDataSource, source);
//    }
}
