Search in sources :

Example 1 with DbcpManagedDataSource

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));
}
Also used : Connection(java.sql.Connection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) JDBCConnection(org.hsqldb.jdbc.JDBCConnection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) Method(java.lang.reflect.Method) DbcpManagedDataSource(org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource) JDBCConnection(org.hsqldb.jdbc.JDBCConnection)

Example 2 with DbcpManagedDataSource

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"));
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbcpManagedDataSource(org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource) DbcpManagedDataSource(org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)2 DbcpManagedDataSource (org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource)2 Method (java.lang.reflect.Method)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 DataSource (javax.sql.DataSource)1 DelegatingConnection (org.apache.commons.dbcp2.DelegatingConnection)1 ManagedConnection (org.apache.commons.dbcp2.managed.ManagedConnection)1 JDBCConnection (org.hsqldb.jdbc.JDBCConnection)1 Test (org.junit.Test)1