Skip to content

Commit d03d098

Browse files
committed
Oracle Client Rule: do not start container in static block
This make it possible to use the rule without starting the shared instance. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 2add7ac commit d03d098

File tree

2 files changed

+43
-55
lines changed

2 files changed

+43
-55
lines changed

vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OraclePoolTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
package io.vertx.oracleclient.test;
1212

13-
import io.vertx.oracleclient.OracleConnectOptions;
1413
import io.vertx.oracleclient.OraclePool;
1514
import io.vertx.oracleclient.test.junit.OracleRule;
1615
import io.vertx.sqlclient.*;
@@ -22,7 +21,7 @@
2221
public class OraclePoolTest extends OracleTestBase {
2322

2423
@Rule
25-
public OracleRule oracle;
24+
public OracleRule oracle = OracleRule.SHARED_INSTANCE;
2625

2726
static final String DROP_TABLE = "DROP TABLE fruits";
2827
static final String CREATE_TABLE = "CREATE TABLE fruits (" +
@@ -33,12 +32,7 @@ public class OraclePoolTest extends OracleTestBase {
3332

3433
@Test
3534
public void test() {
36-
OraclePool pool = OraclePool.pool(vertx, new OracleConnectOptions()
37-
.setHost(OracleRule.getDatabaseHost())
38-
.setPort(OracleRule.getDatabasePort())
39-
.setUser(OracleRule.getUser())
40-
.setPassword(OracleRule.getPassword())
41-
.setDatabase(OracleRule.getDatabase()),
35+
OraclePool pool = OraclePool.pool(vertx, oracle.options(),
4236
new PoolOptions().setMaxSize(1)
4337
);
4438

vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/junit/OracleRule.java

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,45 @@
1616
import org.testcontainers.containers.GenericContainer;
1717
import org.testcontainers.containers.wait.strategy.Wait;
1818

19+
import java.io.IOException;
1920
import java.time.Duration;
2021

2122
public class OracleRule extends ExternalResource {
2223

24+
public static final OracleRule SHARED_INSTANCE = new OracleRule();
25+
2326
static final String IMAGE = "gvenzl/oracle-xe";
2427
static final String PASSWORD = "vertx";
2528
static final int PORT = 1521;
2629

27-
static final GenericContainer<?> ORACLE_DB;
30+
private GenericContainer<?> server;
31+
private OracleConnectOptions options;
2832

29-
static {
30-
String containerVersion = System.getProperty("oracle-container.version");
31-
if (containerVersion == null || containerVersion.isEmpty()) {
32-
containerVersion = "18-slim";
33+
@Override
34+
protected void before() throws IOException {
35+
if (server == null) {
36+
options = startOracle();
37+
}
38+
}
39+
40+
private boolean isNullOrEmpty(String s) {
41+
return s == null || s.isEmpty();
42+
}
43+
44+
@Override
45+
protected void after() {
46+
if (this != SHARED_INSTANCE) {
47+
stopOracle();
3348
}
49+
}
50+
51+
private OracleConnectOptions startOracle() throws IOException {
52+
String containerVersion = System.getProperty("oracle-container.version");
53+
containerVersion = isNullOrEmpty(containerVersion) ? "18-slim" : containerVersion;
3454

3555
String image = IMAGE + ":" + containerVersion;
3656

37-
ORACLE_DB = new GenericContainer<>(image)
57+
server = new GenericContainer<>(image)
3858
.withEnv("ORACLE_PASSWORD", PASSWORD)
3959
.withExposedPorts(PORT)
4060
.withClasspathResourceMapping("tck/import.sql", "/container-entrypoint-initdb.d/import.sql", BindMode.READ_ONLY)
@@ -44,53 +64,27 @@ public class OracleRule extends ExternalResource {
4464
)
4565
.withStartupTimeout(Duration.ofMinutes(15));
4666

47-
ORACLE_DB.start();
48-
}
49-
50-
public static String getPassword() {
51-
return PASSWORD;
52-
}
53-
54-
public static String getUser() {
55-
return "sys as sysdba";
56-
}
67+
server.start();
5768

58-
public static String getDatabaseHost() {
59-
return ORACLE_DB.getHost();
60-
}
61-
62-
public static int getDatabasePort() {
63-
return ORACLE_DB.getMappedPort(1521);
64-
}
65-
66-
public static String getDatabase() {
67-
return "xe";
69+
return new OracleConnectOptions()
70+
.setHost(server.getContainerIpAddress())
71+
.setPort(server.getMappedPort(PORT))
72+
.setUser("sys as sysdba")
73+
.setPassword(PASSWORD)
74+
.setDatabase("xe");
6875
}
6976

70-
private OracleConnectOptions options;
71-
72-
public static final OracleRule SHARED_INSTANCE = new OracleRule();
73-
74-
public synchronized OracleConnectOptions getOptions() throws Exception {
75-
return new OracleConnectOptions()
76-
.setPort(getDatabasePort())
77-
.setHost(getDatabaseHost())
78-
.setUser(getUser())
79-
.setPassword(getPassword())
80-
.setDatabase(getDatabase());
77+
private void stopOracle() {
78+
if (server != null) {
79+
try {
80+
server.stop();
81+
} finally {
82+
server = null;
83+
}
84+
}
8185
}
8286

8387
public OracleConnectOptions options() {
8488
return new OracleConnectOptions(options);
8589
}
86-
87-
@Override
88-
protected void before() throws Throwable {
89-
options = getOptions();
90-
}
91-
92-
@Override
93-
protected void after() {
94-
95-
}
9690
}

0 commit comments

Comments
 (0)