DatabaseConnectionFactory { public static IDatabaseConnection openConnection(final BasicDataSource ds) { try { final Connection connection = ds.getConnection(); for (final DbUnitConnectionFactory impl : SERVICE_LOADER) { if (impl.supportsDriver(ds.getDriverClassName())) { return impl.createConnection(connection, discoverSchema(connection)); } } return new DatabaseConnection(connection); } catch (final DatabaseUnitException | SQLException e) { throw new JpaUnitException(e); } } private  DatabaseConnectionFactory(); static IDatabaseConnection openConnection(final BasicDataSource ds);  }
@Test public void testOpenConnectionToH2DbHavingAllSupportedPersistenceProperties() throws ClassNotFoundException { final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(H2_DRIVER_CLASS_PROP_VALUE); ds.setUsername(USERNAME_PROP_VALUE); ds.setPassword(PASSWORD_PROP_VALUE); ds.setUrl(H2_CONNECTION_URL_PROP_VALUE); connection = DatabaseConnectionFactory.openConnection(ds); assertThat(connection, notNullValue()); assertThat(connection, instanceOf(H2Connection.class)); }
@Test public void testOpenConnectionToH2DbHavingUsernameAndPasswordEncodedIntoConnectionUrl() throws ClassNotFoundException { final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(H2_DRIVER_CLASS_PROP_VALUE); ds.setUrl(H2_CONNECTION_URL_PROP_VALUE + ";USER=" + USERNAME_PROP_VALUE + ";PASSWORD=" + PASSWORD_PROP_VALUE); connection = DatabaseConnectionFactory.openConnection(ds); assertThat(connection, notNullValue()); assertThat(connection, instanceOf(H2Connection.class)); }
@Test public void testOpenConnectionUsingUnknownDriver() throws ClassNotFoundException { final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("unknown"); try { connection = DatabaseConnectionFactory.openConnection(ds); fail("JpaTestException is expected"); } catch (final JpaUnitException e) { } }
@Test public void testOpenConnectionToSqliteDbHavingAllSupportedPersistenceProperties() throws Exception { final File dbFile = folder.newFile("test.db"); final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE); ds.setUsername(USERNAME_PROP_VALUE); ds.setPassword(PASSWORD_PROP_VALUE); ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath()); connection = DatabaseConnectionFactory.openConnection(ds); assertThat(connection, notNullValue()); assertThat(connection.getClass(), equalTo(DatabaseConnection.class)); }
@Test public void testOpenConnectionToSqliteDbWithoutHavingUsernameAndPasswordProperties() throws Exception { final File dbFile = folder.newFile("test.db"); final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE); ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath()); connection = DatabaseConnectionFactory.openConnection(ds); assertThat(connection, notNullValue()); assertThat(connection.getClass(), equalTo(DatabaseConnection.class)); }