2020import com .zaxxer .hikari .HikariConfig ;
2121import com .zaxxer .hikari .HikariDataSource ;
2222import org .apache .shardingsphere .driver .jdbc .core .connection .ShardingSphereConnection ;
23+ import org .apache .shardingsphere .infra .database .core .DefaultDatabase ;
24+ import org .apache .shardingsphere .infra .metadata .database .resource .unit .StorageUnit ;
25+ import org .apache .shardingsphere .mode .manager .ContextManager ;
2326import org .apache .shardingsphere .test .natived .commons .TestShardingService ;
2427import org .awaitility .Awaitility ;
2528import org .firebirdsql .management .FBManager ;
2629import org .firebirdsql .management .PageSizeConstants ;
27- import org .junit .jupiter .api .AfterAll ;
28- import org .junit .jupiter .api .BeforeAll ;
30+ import org .junit .jupiter .api .AfterEach ;
31+ import org .junit .jupiter .api .BeforeEach ;
2932import org .junit .jupiter .api .Test ;
3033import org .junit .jupiter .api .condition .EnabledInNativeImage ;
3134import org .testcontainers .containers .GenericContainer ;
4750@ Testcontainers
4851class FirebirdTest {
4952
50- private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.database.firebird." ;
53+ private final String systemPropKeyPrefix = "fixture.test-native.yaml.database.firebird." ;
5154
52- private static final String PASSWORD = "masterkey" ;
55+ private final String password = "masterkey" ;
5356
5457 @ SuppressWarnings ("resource" )
5558 @ Container
56- private static final GenericContainer <?> CONTAINER = new GenericContainer <>("ghcr.io/fdcastel/firebird:5.0.1" )
57- .withEnv ("FIREBIRD_ROOT_PASSWORD" , PASSWORD )
59+ private final GenericContainer <?> container = new GenericContainer <>("ghcr.io/fdcastel/firebird:5.0.1" )
60+ .withEnv ("FIREBIRD_ROOT_PASSWORD" , password )
5861 .withEnv ("FIREBIRD_USER" , "alice" )
59- .withEnv ("FIREBIRD_PASSWORD" , PASSWORD )
62+ .withEnv ("FIREBIRD_PASSWORD" , password )
6063 .withEnv ("FIREBIRD_DATABASE" , "mirror.fdb" )
6164 .withEnv ("FIREBIRD_DATABASE_DEFAULT_CHARSET" , "UTF8" )
6265 .withExposedPorts (3050 );
6366
64- private static DataSource logicDataSource ;
67+ private DataSource logicDataSource ;
6568
6669 private String jdbcUrlPrefix ;
6770
6871 private TestShardingService testShardingService ;
6972
70- @ BeforeAll
71- static void beforeAll () {
72- assertThat (System .getProperty (SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url" ), is (nullValue ()));
73- assertThat (System .getProperty (SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url" ), is (nullValue ()));
74- assertThat (System .getProperty (SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url" ), is (nullValue ()));
73+ @ BeforeEach
74+ void beforeEach () {
75+ assertThat (System .getProperty (systemPropKeyPrefix + "ds0.jdbc-url" ), is (nullValue ()));
76+ assertThat (System .getProperty (systemPropKeyPrefix + "ds1.jdbc-url" ), is (nullValue ()));
77+ assertThat (System .getProperty (systemPropKeyPrefix + "ds2.jdbc-url" ), is (nullValue ()));
7578 }
7679
77- @ AfterAll
78- static void afterAll () throws SQLException {
80+ @ AfterEach
81+ void afterEach () throws SQLException {
7982 try (Connection connection = logicDataSource .getConnection ()) {
80- connection .unwrap (ShardingSphereConnection .class ).getContextManager ().close ();
83+ ContextManager contextManager = connection .unwrap (ShardingSphereConnection .class ).getContextManager ();
84+ for (StorageUnit each : contextManager .getStorageUnits (DefaultDatabase .LOGIC_NAME ).values ()) {
85+ each .getDataSource ().unwrap (HikariDataSource .class ).close ();
86+ }
87+ contextManager .close ();
8188 }
82- System .clearProperty (SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url" );
83- System .clearProperty (SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url" );
84- System .clearProperty (SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url" );
89+ System .clearProperty (systemPropKeyPrefix + "ds0.jdbc-url" );
90+ System .clearProperty (systemPropKeyPrefix + "ds1.jdbc-url" );
91+ System .clearProperty (systemPropKeyPrefix + "ds2.jdbc-url" );
8592 }
8693
8794 @ Test
8895 void assertShardingInLocalTransactions () throws Exception {
89- jdbcUrlPrefix = "jdbc:firebird://localhost:" + CONTAINER .getMappedPort (3050 ) + "//var/lib/firebird/data/" ;
96+ jdbcUrlPrefix = "jdbc:firebird://localhost:" + container .getMappedPort (3050 ) + "//var/lib/firebird/data/" ;
9097 logicDataSource = createDataSource ();
9198 testShardingService = new TestShardingService (logicDataSource );
9299 initEnvironment ();
@@ -109,7 +116,7 @@ private void initEnvironment() throws SQLException {
109116 private Connection openConnection () throws SQLException {
110117 Properties props = new Properties ();
111118 props .setProperty ("user" , "alice" );
112- props .setProperty ("password" , PASSWORD );
119+ props .setProperty ("password" , password );
113120 return DriverManager .getConnection (jdbcUrlPrefix + "mirror.fdb" , props );
114121 }
115122
@@ -131,22 +138,22 @@ private DataSource createDataSource() throws Exception {
131138 try (FBManager fbManager = new FBManager ()) {
132139 fbManager .setServer ("localhost" );
133140 fbManager .setUserName ("alice" );
134- fbManager .setPassword (PASSWORD );
141+ fbManager .setPassword (password );
135142 fbManager .setFileName ("/var/lib/firebird/data/mirror.fdb" );
136143 fbManager .setPageSize (PageSizeConstants .SIZE_16K );
137144 fbManager .setDefaultCharacterSet ("UTF8" );
138- fbManager .setPort (CONTAINER .getMappedPort (3050 ));
145+ fbManager .setPort (container .getMappedPort (3050 ));
139146 fbManager .start ();
140- fbManager .createDatabase ("/var/lib/firebird/data/demo_ds_0.fdb" , "alice" , PASSWORD );
141- fbManager .createDatabase ("/var/lib/firebird/data/demo_ds_1.fdb" , "alice" , PASSWORD );
142- fbManager .createDatabase ("/var/lib/firebird/data/demo_ds_2.fdb" , "alice" , PASSWORD );
147+ fbManager .createDatabase ("/var/lib/firebird/data/demo_ds_0.fdb" , "alice" , password );
148+ fbManager .createDatabase ("/var/lib/firebird/data/demo_ds_1.fdb" , "alice" , password );
149+ fbManager .createDatabase ("/var/lib/firebird/data/demo_ds_2.fdb" , "alice" , password );
143150 }
144151 HikariConfig config = new HikariConfig ();
145152 config .setDriverClassName ("org.apache.shardingsphere.driver.ShardingSphereDriver" );
146153 config .setJdbcUrl ("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/firebird.yaml?placeholder-type=system_props" );
147- System .setProperty (SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url" , jdbcUrlPrefix + "demo_ds_0.fdb" );
148- System .setProperty (SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url" , jdbcUrlPrefix + "demo_ds_1.fdb" );
149- System .setProperty (SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url" , jdbcUrlPrefix + "demo_ds_2.fdb" );
154+ System .setProperty (systemPropKeyPrefix + "ds0.jdbc-url" , jdbcUrlPrefix + "demo_ds_0.fdb" );
155+ System .setProperty (systemPropKeyPrefix + "ds1.jdbc-url" , jdbcUrlPrefix + "demo_ds_1.fdb" );
156+ System .setProperty (systemPropKeyPrefix + "ds2.jdbc-url" , jdbcUrlPrefix + "demo_ds_2.fdb" );
150157 return new HikariDataSource (config );
151158 }
152159}
0 commit comments