01: package org.geotools.data.jdbc.ds;
02:
03: import java.io.IOException;
04: import java.sql.Connection;
05: import java.sql.PreparedStatement;
06: import java.sql.SQLException;
07: import java.sql.Statement;
08:
09: import org.apache.commons.dbcp.BasicDataSource;
10: import org.geotools.data.jdbc.datasource.DataSourceFinder;
11: import org.geotools.data.jdbc.datasource.UnWrapper;
12:
13: import junit.framework.TestCase;
14:
15: public class UnWrapperTest extends TestCase {
16:
17: public void testDBCPUnwrapper() throws SQLException, IOException {
18: BasicDataSource ds = new BasicDataSource();
19: ds.setDriverClassName("org.h2.Driver");
20: ds.setUrl("jdbc:h2:mem:test_mem");
21: ds.setAccessToUnderlyingConnectionAllowed(true);
22:
23: Connection conn = ds.getConnection();
24: UnWrapper uw = DataSourceFinder.getUnWrapper(conn);
25: assertNotNull(uw);
26: assertTrue(uw.canUnwrap(conn));
27: Connection unwrapped = uw.unwrap(conn);
28: assertNotNull(unwrapped);
29: assertTrue(unwrapped instanceof org.h2.jdbc.JdbcConnection);
30:
31: Statement st = conn.createStatement();
32: uw = DataSourceFinder.getUnWrapper(st);
33: assertNotNull(uw);
34: assertTrue(uw.canUnwrap(st));
35: Statement uwst = uw.unwrap(st);
36: assertNotNull(uwst);
37: assertTrue(uwst instanceof org.h2.jdbc.JdbcStatement);
38: st.close();
39:
40: PreparedStatement ps = conn
41: .prepareStatement("select curtime()");
42: uw = DataSourceFinder.getUnWrapper(ps);
43: assertNotNull(uw);
44: assertTrue(uw.canUnwrap(ps));
45: PreparedStatement uwps = (PreparedStatement) uw.unwrap(ps);
46: assertNotNull(uwps);
47: assertTrue(uwps instanceof org.h2.jdbc.JdbcPreparedStatement);
48: ps.close();
49:
50: conn.close();
51: ds.close();
52: }
53: }
|