Skip to content

Commit f84dfba

Browse files
committed
Start image only once per test URL by moving datasource to test method
1 parent c06c7e5 commit f84dfba

1 file changed

Lines changed: 50 additions & 54 deletions

File tree

src/test/java/org/firebirdsql/testcontainers/JDBCDriverTest.java

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import org.junit.runner.RunWith;
1010
import org.junit.runners.Parameterized;
1111
import org.junit.runners.Parameterized.Parameter;
12-
import org.testcontainers.jdbc.ConnectionUrl;
1312
import org.testcontainers.jdbc.ContainerDatabaseDriver;
1413

14+
import javax.sql.DataSource;
1515
import java.sql.Connection;
1616
import java.sql.SQLException;
1717
import java.util.EnumSet;
@@ -43,6 +43,7 @@ public static Iterable<Object[]> data() {
4343
});
4444
}
4545

46+
@SuppressWarnings("unused")
4647
public static void sampleInitFunction(Connection connection) throws SQLException {
4748
connection.createStatement().execute("CREATE TABLE bar (\n" +
4849
" foo VARCHAR(255)\n" +
@@ -60,69 +61,64 @@ public static void testCleanup() {
6061

6162
@Test
6263
public void test() throws SQLException {
63-
performSimpleTest(jdbcUrl);
64+
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
65+
performSimpleTest(dataSource);
6466

65-
if (options.contains(Options.ScriptedSchema)) {
66-
performTestForScriptedSchema(jdbcUrl);
67-
}
67+
if (options.contains(Options.ScriptedSchema)) {
68+
performTestForScriptedSchema(dataSource);
69+
}
6870

69-
if (options.contains(Options.JDBCParams)) {
70-
performTestForJDBCParamUsage(jdbcUrl);
71+
if (options.contains(Options.JDBCParams)) {
72+
performTestForJDBCParamUsage(dataSource);
73+
}
7174
}
7275
}
7376

74-
private void performSimpleTest(String jdbcUrl) throws SQLException {
75-
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
76-
boolean result = new QueryRunner(dataSource).query("SELECT 1 FROM RDB$DATABASE", rs -> {
77-
rs.next();
78-
int resultSetInt = rs.getInt(1);
79-
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
80-
return true;
81-
});
77+
private void performSimpleTest(DataSource dataSource) throws SQLException {
78+
boolean result = new QueryRunner(dataSource).query("SELECT 1 FROM RDB$DATABASE", rs -> {
79+
rs.next();
80+
int resultSetInt = rs.getInt(1);
81+
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
82+
return true;
83+
});
8284

83-
assertTrue("The database returned a record as expected", result);
84-
}
85+
assertTrue("The database returned a record as expected", result);
8586
}
8687

87-
private void performTestForScriptedSchema(String jdbcUrl) throws SQLException {
88-
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
89-
boolean result = new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%world'", rs -> {
90-
rs.next();
91-
String resultSetString = rs.getString(1);
92-
assertEquals("A basic SELECT query succeeds where the schema has been applied from a script", "hello world", resultSetString);
93-
return true;
94-
});
95-
}
88+
private void performTestForScriptedSchema(DataSource dataSource) throws SQLException {
89+
new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%world'", rs -> {
90+
rs.next();
91+
String resultSetString = rs.getString(1);
92+
assertEquals("A basic SELECT query succeeds where the schema has been applied from a script", "hello world", resultSetString);
93+
return true;
94+
});
9695
}
9796

98-
private void performTestForJDBCParamUsage(String jdbcUrl) throws SQLException {
99-
final String databaseType = ConnectionUrl.newInstance(jdbcUrl).getDatabaseType();
100-
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
101-
boolean result = new QueryRunner(dataSource).query("select CURRENT_USER FROM RDB$DATABASE", rs -> {
102-
rs.next();
103-
String resultUser = rs.getString(1);
104-
// Not all databases (eg. Postgres) return @% at the end of user name. We just need to make sure the user name matches.
105-
if (resultUser.endsWith("@%")) {
106-
resultUser = resultUser.substring(0, resultUser.length() - 2);
107-
}
108-
assertEquals("User from query param is created.", "SOMEUSER", resultUser);
109-
return true;
110-
});
111-
112-
assertTrue("The database returned a record as expected", result);
113-
114-
String databaseQuery = "select rdb$get_context('SYSTEM', 'DB_NAME') from RDB$DATABASE";
115-
116-
result = new QueryRunner(dataSource).query(databaseQuery, rs -> {
117-
rs.next();
118-
String resultDB = rs.getString(1);
119-
// Firebird reports full path
120-
assertThat("Database name from URL String is used.", resultDB, CoreMatchers.endsWith("/databasename"));
121-
return true;
122-
});
123-
124-
assertTrue("The database returned a record as expected", result);
125-
}
97+
private void performTestForJDBCParamUsage(DataSource dataSource) throws SQLException {
98+
boolean result = new QueryRunner(dataSource).query("select CURRENT_USER FROM RDB$DATABASE", rs -> {
99+
rs.next();
100+
String resultUser = rs.getString(1);
101+
// Not all databases (eg. Postgres) return @% at the end of user name. We just need to make sure the user name matches.
102+
if (resultUser.endsWith("@%")) {
103+
resultUser = resultUser.substring(0, resultUser.length() - 2);
104+
}
105+
assertEquals("User from query param is created.", "SOMEUSER", resultUser);
106+
return true;
107+
});
108+
109+
assertTrue("The database returned a record as expected", result);
110+
111+
String databaseQuery = "select rdb$get_context('SYSTEM', 'DB_NAME') from RDB$DATABASE";
112+
113+
result = new QueryRunner(dataSource).query(databaseQuery, rs -> {
114+
rs.next();
115+
String resultDB = rs.getString(1);
116+
// Firebird reports full path
117+
assertThat("Database name from URL String is used.", resultDB, CoreMatchers.endsWith("/databasename"));
118+
return true;
119+
});
120+
121+
assertTrue("The database returned a record as expected", result);
126122
}
127123

128124
private HikariDataSource getDataSource(String jdbcUrl, int poolSize) {

0 commit comments

Comments
 (0)