use of org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource in project tomee by apache.
the class DataSourceDefinitionJndiTest method check.
private void check(final DataSource ds, final String name) throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// the first "cast part" is not important, we just want to check the jdbc url is ok
assertThat(ds, instanceOf(DbcpManagedDataSource.class));
final DbcpManagedDataSource dbcp = (DbcpManagedDataSource) ds;
final Connection connection = dbcp.getConnection();
assertThat(connection, instanceOf(ManagedConnection.class));
final ManagedConnection mc = (ManagedConnection) connection;
final Method getInnermostDelegateInternal = DelegatingConnection.class.getDeclaredMethod("getInnermostDelegateInternal");
getInnermostDelegateInternal.setAccessible(true);
final Connection delegate = (Connection) getInnermostDelegateInternal.invoke(mc);
assertThat(delegate, instanceOf(JDBCConnection.class));
final Method getURL = JDBCConnection.class.getDeclaredMethod("getURL");
getURL.setAccessible(true);
assertEquals("jdbc:hsqldb:mem:" + name, getURL.invoke(delegate));
}
use of org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource in project tomee by apache.
the class DataSourceDefinitionTest method checkDs.
@Test
public void checkDs() throws SQLException {
final DataSource ds = persister.getDs();
assertNotNull(ds);
assertThat(ds, instanceOf(DbcpManagedDataSource.class));
final DbcpManagedDataSource castedDs = (DbcpManagedDataSource) ds;
final String driver = castedDs.getDriverClassName();
assertEquals("org.h2.jdbcx.JdbcDataSource", driver);
final String user = castedDs.getUserName();
assertEquals("sa", user);
final String url = castedDs.getUrl();
assertEquals("jdbc:h2:mem:persister", url);
final int initPoolSize = castedDs.getInitialSize();
assertEquals(1, initPoolSize);
final int maxIdle = castedDs.getMaxIdle();
assertEquals(3, maxIdle);
final Connection connection = ds.getConnection();
assertNotNull(connection);
execute(connection, "CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
execute(connection, "INSERT INTO TEST(ID, NAME) VALUES(1, 'foo')");
connection.commit();
final PreparedStatement statement = ds.getConnection().prepareStatement("SELECT NAME FROM TEST");
statement.execute();
final ResultSet set = statement.getResultSet();
assertTrue(set.next());
assertEquals("foo", set.getString("NAME"));
}
Aggregations