20
20
import com .zaxxer .hikari .HikariConfig ;
21
21
import com .zaxxer .hikari .HikariDataSource ;
22
22
import 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 ;
23
26
import org .apache .shardingsphere .test .natived .commons .TestShardingService ;
24
27
import org .awaitility .Awaitility ;
25
28
import org .firebirdsql .management .FBManager ;
26
29
import 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 ;
29
32
import org .junit .jupiter .api .Test ;
30
33
import org .junit .jupiter .api .condition .EnabledInNativeImage ;
31
34
import org .testcontainers .containers .GenericContainer ;
47
50
@ Testcontainers
48
51
class FirebirdTest {
49
52
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." ;
51
54
52
- private static final String PASSWORD = "masterkey" ;
55
+ private final String password = "masterkey" ;
53
56
54
57
@ SuppressWarnings ("resource" )
55
58
@ 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 )
58
61
.withEnv ("FIREBIRD_USER" , "alice" )
59
- .withEnv ("FIREBIRD_PASSWORD" , PASSWORD )
62
+ .withEnv ("FIREBIRD_PASSWORD" , password )
60
63
.withEnv ("FIREBIRD_DATABASE" , "mirror.fdb" )
61
64
.withEnv ("FIREBIRD_DATABASE_DEFAULT_CHARSET" , "UTF8" )
62
65
.withExposedPorts (3050 );
63
66
64
- private static DataSource logicDataSource ;
67
+ private DataSource logicDataSource ;
65
68
66
69
private String jdbcUrlPrefix ;
67
70
68
71
private TestShardingService testShardingService ;
69
72
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 ()));
75
78
}
76
79
77
- @ AfterAll
78
- static void afterAll () throws SQLException {
80
+ @ AfterEach
81
+ void afterEach () throws SQLException {
79
82
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 ();
81
88
}
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" );
85
92
}
86
93
87
94
@ Test
88
95
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/" ;
90
97
logicDataSource = createDataSource ();
91
98
testShardingService = new TestShardingService (logicDataSource );
92
99
initEnvironment ();
@@ -109,7 +116,7 @@ private void initEnvironment() throws SQLException {
109
116
private Connection openConnection () throws SQLException {
110
117
Properties props = new Properties ();
111
118
props .setProperty ("user" , "alice" );
112
- props .setProperty ("password" , PASSWORD );
119
+ props .setProperty ("password" , password );
113
120
return DriverManager .getConnection (jdbcUrlPrefix + "mirror.fdb" , props );
114
121
}
115
122
@@ -131,22 +138,22 @@ private DataSource createDataSource() throws Exception {
131
138
try (FBManager fbManager = new FBManager ()) {
132
139
fbManager .setServer ("localhost" );
133
140
fbManager .setUserName ("alice" );
134
- fbManager .setPassword (PASSWORD );
141
+ fbManager .setPassword (password );
135
142
fbManager .setFileName ("/var/lib/firebird/data/mirror.fdb" );
136
143
fbManager .setPageSize (PageSizeConstants .SIZE_16K );
137
144
fbManager .setDefaultCharacterSet ("UTF8" );
138
- fbManager .setPort (CONTAINER .getMappedPort (3050 ));
145
+ fbManager .setPort (container .getMappedPort (3050 ));
139
146
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 );
143
150
}
144
151
HikariConfig config = new HikariConfig ();
145
152
config .setDriverClassName ("org.apache.shardingsphere.driver.ShardingSphereDriver" );
146
153
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" );
150
157
return new HikariDataSource (config );
151
158
}
152
159
}
0 commit comments