Skip to content

Commit b27a25b

Browse files
committed
test: isolate FileConfiguration regression coverage
1 parent c33e355 commit b27a25b

1 file changed

Lines changed: 75 additions & 65 deletions

File tree

config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java

Lines changed: 75 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
import java.util.concurrent.CountDownLatch;
2828
import java.util.concurrent.TimeUnit;
2929

30-
import static org.junit.jupiter.api.Assertions.assertFalse;
31-
import static org.junit.jupiter.api.Assertions.assertTrue;
32-
3330
class FileConfigurationTest {
3431

3532
Logger logger = LoggerFactory.getLogger(FileConfigurationTest.class);
@@ -99,7 +96,7 @@ void addConfigListener() throws InterruptedException {
9996
}
10097

10198
@Test
102-
void testDiffDefaultValue() throws Exception {
99+
void testDiffDefaultValue() {
103100
Configuration fileConfig = ConfigurationFactory.getInstance();
104101
int intValue1 = fileConfig.getInt("int.not.exist", 100);
105102
int intValue2 = fileConfig.getInt("int.not.exist", 200);
@@ -127,93 +124,93 @@ void testDiffDefaultValue() throws Exception {
127124
}
128125

129126
@Test
130-
void testGetConfigWithTimeout() throws Exception {
127+
void testGetConfigWithTimeout() {
131128
Configuration fileConfig = ConfigurationFactory.getInstance();
132129
String value = fileConfig.getConfig("test.key", "default-value", 1000);
133130
Assertions.assertNotNull(value);
134131
}
135132

136133
@Test
137-
void testGetIntWithTimeout() throws Exception {
134+
void testGetIntWithTimeout() {
138135
Configuration fileConfig = ConfigurationFactory.getInstance();
139136
int value = fileConfig.getInt("test.int.key", 100, 1000);
140-
assertTrue(value >= 0);
137+
Assertions.assertTrue(value >= 0);
141138
}
142139

143140
@Test
144-
void testGetBooleanWithTimeout() throws Exception {
141+
void testGetBooleanWithTimeout() {
145142
Configuration fileConfig = ConfigurationFactory.getInstance();
146143
boolean value = fileConfig.getBoolean("test.boolean.key", true, 1000);
147-
assertTrue(value || !value);
144+
Assertions.assertTrue(value || !value);
148145
}
149146

150147
@Test
151-
void testGetLongWithTimeout() throws Exception {
148+
void testGetLongWithTimeout() {
152149
Configuration fileConfig = ConfigurationFactory.getInstance();
153150
long value = fileConfig.getLong("test.long.key", 1000L, 1000);
154-
assertTrue(value >= 0);
151+
Assertions.assertTrue(value >= 0);
155152
}
156153

157154
@Test
158-
void testGetShortWithTimeout() throws Exception {
155+
void testGetShortWithTimeout() {
159156
Configuration fileConfig = ConfigurationFactory.getInstance();
160157
short value = fileConfig.getShort("test.short.key", (short) 10, 1000);
161-
assertTrue(value >= 0);
158+
Assertions.assertTrue(value >= 0);
162159
}
163160

164161
@Test
165-
void testPutConfig() throws Exception {
162+
void testPutConfig() {
166163
Configuration fileConfig = ConfigurationFactory.getInstance();
167164
boolean result = fileConfig.putConfig("test.put.key", "test-value");
168-
assertTrue(result || !result);
165+
Assertions.assertTrue(result || !result);
169166
}
170167

171168
@Test
172-
void testPutConfigWithTimeout() throws Exception {
169+
void testPutConfigWithTimeout() {
173170
Configuration fileConfig = ConfigurationFactory.getInstance();
174171
boolean result = fileConfig.putConfig("test.put.key", "test-value", 1000);
175-
assertTrue(result || !result);
172+
Assertions.assertTrue(result || !result);
176173
}
177174

178175
@Test
179-
void testPutConfigIfAbsent() throws Exception {
176+
void testPutConfigIfAbsent() {
180177
Configuration fileConfig = ConfigurationFactory.getInstance();
181178
boolean result = fileConfig.putConfigIfAbsent("test.absent.key", "test-value");
182-
assertTrue(result || !result);
179+
Assertions.assertTrue(result || !result);
183180
}
184181

185182
@Test
186-
void testPutConfigIfAbsentWithTimeout() throws Exception {
183+
void testPutConfigIfAbsentWithTimeout() {
187184
Configuration fileConfig = ConfigurationFactory.getInstance();
188185
boolean result = fileConfig.putConfigIfAbsent("test.absent.key", "test-value", 1000);
189-
assertTrue(result || !result);
186+
Assertions.assertTrue(result || !result);
190187
}
191188

192189
@Test
193-
void testRemoveConfig() throws Exception {
190+
void testRemoveConfig() {
194191
Configuration fileConfig = ConfigurationFactory.getInstance();
195192
fileConfig.putConfig("test.remove.key", "test-value");
196193
boolean result = fileConfig.removeConfig("test.remove.key");
197-
assertTrue(result || !result);
194+
Assertions.assertTrue(result || !result);
198195
}
199196

200197
@Test
201-
void testRemoveConfigWithTimeout() throws Exception {
198+
void testRemoveConfigWithTimeout() {
202199
Configuration fileConfig = ConfigurationFactory.getInstance();
203200
fileConfig.putConfig("test.remove.key", "test-value");
204201
boolean result = fileConfig.removeConfig("test.remove.key", 1000);
205-
assertTrue(result || !result);
202+
Assertions.assertTrue(result || !result);
206203
}
207204

208205
@Test
209-
void testGetLatestConfig() throws Exception {
206+
void testGetLatestConfig() {
210207
Configuration fileConfig = ConfigurationFactory.getInstance();
211208
String value = fileConfig.getLatestConfig("test.latest.key", "default-value", 1000);
212209
Assertions.assertNotNull(value);
213210
}
214211

215212
@Test
216-
void testRemoveConfigListener() throws Exception {
213+
void testRemoveConfigListener() {
217214
Configuration fileConfig = ConfigurationFactory.getInstance();
218215
ConfigurationChangeListener listener = new ConfigurationChangeListener() {
219216
@Override
@@ -228,7 +225,7 @@ public void onChangeEvent(ConfigurationChangeEvent event) {}
228225
}
229226

230227
@Test
231-
void testGetConfigListeners() throws Exception {
228+
void testGetConfigListeners() {
232229
Configuration fileConfig = ConfigurationFactory.getInstance();
233230
ConfigurationChangeListener listener = new ConfigurationChangeListener() {
234231
@Override
@@ -245,7 +242,7 @@ public void onChangeEvent(ConfigurationChangeEvent event) {}
245242
}
246243

247244
@Test
248-
void testMultipleListeners() throws Exception {
245+
void testMultipleListeners() {
249246
Configuration fileConfig = ConfigurationFactory.getInstance();
250247
ConfigurationChangeListener listener1 = new ConfigurationChangeListener() {
251248
@Override
@@ -275,52 +272,52 @@ public void onChangeEvent(ConfigurationChangeEvent event) {}
275272
}
276273

277274
@Test
278-
void testGetShort() throws Exception {
275+
void testGetShort() {
279276
Configuration fileConfig = ConfigurationFactory.getInstance();
280277
System.setProperty("test.short.value", "100");
281278
short value = fileConfig.getShort("test.short.value");
282279
Assertions.assertEquals((short) 100, value);
283280
}
284281

285282
@Test
286-
void testGetShortWithDefault() throws Exception {
283+
void testGetShortWithDefault() {
287284
Configuration fileConfig = ConfigurationFactory.getInstance();
288285
short value = fileConfig.getShort("test.short.not.exist", (short) 50);
289286
Assertions.assertEquals((short) 50, value);
290287
}
291288

292289
@Test
293-
void testGetLong() throws Exception {
290+
void testGetLong() {
294291
Configuration fileConfig = ConfigurationFactory.getInstance();
295292
System.setProperty("test.long.value", "10000");
296293
long value = fileConfig.getLong("test.long.value");
297294
Assertions.assertEquals(10000L, value);
298295
}
299296

300297
@Test
301-
void testGetLongWithDefault() throws Exception {
298+
void testGetLongWithDefault() {
302299
Configuration fileConfig = ConfigurationFactory.getInstance();
303300
long value = fileConfig.getLong("test.long.not.exist", 5000L);
304301
Assertions.assertEquals(5000L, value);
305302
}
306303

307304
@Test
308-
void testNullValues() throws Exception {
305+
void testNullValues() {
309306
Configuration fileConfig = ConfigurationFactory.getInstance();
310307
String value = fileConfig.getConfig("non.existent.key");
311308
Assertions.assertNull(value);
312309
}
313310

314311
@Test
315-
void testEmptyStringValue() throws Exception {
312+
void testEmptyStringValue() {
316313
Configuration fileConfig = ConfigurationFactory.getInstance();
317314
System.setProperty("test.empty.value", "");
318315
String value = fileConfig.getConfig("test.empty.value", "default");
319316
Assertions.assertNotNull(value);
320317
}
321318

322319
@Test
323-
void testSpecialCharacters() throws Exception {
320+
void testSpecialCharacters() {
324321
Configuration fileConfig = ConfigurationFactory.getInstance();
325322
String specialValue = "test@#$%^&*()";
326323
System.setProperty("test.special.chars", specialValue);
@@ -329,15 +326,15 @@ void testSpecialCharacters() throws Exception {
329326
}
330327

331328
@Test
332-
void testAddNullListener() throws Exception {
329+
void testAddNullListener() {
333330
Configuration fileConfig = ConfigurationFactory.getInstance();
334331
fileConfig.addConfigListener("test.null.listener", null);
335332
Set<ConfigurationChangeListener> listeners = fileConfig.getConfigListeners("test.null.listener");
336333
Assertions.assertNull(listeners);
337334
}
338335

339336
@Test
340-
void testAddListenerWithBlankDataId() throws Exception {
337+
void testAddListenerWithBlankDataId() {
341338
Configuration fileConfig = ConfigurationFactory.getInstance();
342339
ConfigurationChangeListener listener = new ConfigurationChangeListener() {
343340
@Override
@@ -351,13 +348,13 @@ public void onChangeEvent(ConfigurationChangeEvent event) {}
351348
}
352349

353350
@Test
354-
void testRemoveNullListener() throws Exception {
351+
void testRemoveNullListener() {
355352
Configuration fileConfig = ConfigurationFactory.getInstance();
356353
fileConfig.removeConfigListener("test.remove.null", null);
357354
}
358355

359356
@Test
360-
void testRemoveListenerWithBlankDataId() throws Exception {
357+
void testRemoveListenerWithBlankDataId() {
361358
Configuration fileConfig = ConfigurationFactory.getInstance();
362359
ConfigurationChangeListener listener = new ConfigurationChangeListener() {
363360
@Override
@@ -371,14 +368,14 @@ public void onChangeEvent(ConfigurationChangeEvent event) {}
371368
}
372369

373370
@Test
374-
void testGetListenersForNonExistentKey() throws Exception {
371+
void testGetListenersForNonExistentKey() {
375372
Configuration fileConfig = ConfigurationFactory.getInstance();
376373
Set<ConfigurationChangeListener> listeners = fileConfig.getConfigListeners("non.existent.listener.key");
377374
Assertions.assertNull(listeners);
378375
}
379376

380377
@Test
381-
void testRemoveLastListener() throws Exception {
378+
void testRemoveLastListener() {
382379
Configuration fileConfig = ConfigurationFactory.getInstance();
383380
ConfigurationChangeListener listener = new ConfigurationChangeListener() {
384381
@Override
@@ -396,32 +393,32 @@ public void onChangeEvent(ConfigurationChangeEvent event) {}
396393
Set<ConfigurationChangeListener> listeners = fileConfig.getConfigListeners(dataId);
397394
// due to configuration cache, may still return an empty set instead of null after removing listener
398395
// or may still contain cached listeners, this is normal behavior
399-
assertTrue(listeners == null || listeners.isEmpty() || !listeners.contains(listener));
396+
Assertions.assertTrue(listeners == null || listeners.isEmpty() || !listeners.contains(listener));
400397
}
401398

402399
@Test
403-
void testGetTypeName() throws Exception {
400+
void testGetTypeName() {
404401
FileConfiguration fileConfig = new FileConfiguration();
405402
String typeName = fileConfig.getTypeName();
406403
Assertions.assertEquals("file", typeName);
407404
}
408405

409406
@Test
410-
void testFileConfigurationWithCustomName() throws Exception {
407+
void testFileConfigurationWithCustomName() {
411408
FileConfiguration fileConfig = new FileConfiguration("file.conf");
412409
Assertions.assertNotNull(fileConfig);
413410
String typeName = fileConfig.getTypeName();
414411
Assertions.assertEquals("file", typeName);
415412
}
416413

417414
@Test
418-
void testFileConfigurationWithNonExistentFile() throws Exception {
415+
void testFileConfigurationWithNonExistentFile() {
419416
FileConfiguration fileConfig = new FileConfiguration("non-existent-file.conf");
420417
Assertions.assertNotNull(fileConfig);
421418
}
422419

423420
@Test
424-
void testMultipleConfigOperations() throws Exception {
421+
void testMultipleConfigOperations() {
425422
Configuration fileConfig = ConfigurationFactory.getInstance();
426423

427424
System.setProperty("test.multi.op.1", "value1");
@@ -438,49 +435,62 @@ void testMultipleConfigOperations() throws Exception {
438435
}
439436

440437
@Test
441-
void testPutAndGetConfig() throws Exception {
438+
void testPutAndGetConfig() {
442439
Configuration fileConfig = ConfigurationFactory.getInstance();
443440
boolean putResult = fileConfig.putConfig("test.put.get", "put-value");
444-
assertTrue(putResult || !putResult);
441+
Assertions.assertTrue(putResult || !putResult);
445442
}
446443

447444
@Test
448-
void testPutConfigIfAbsentWhenKeyExists() throws Exception {
445+
void testPutConfigIfAbsentWhenKeyExists() {
449446
Configuration fileConfig = ConfigurationFactory.getInstance();
450447
System.setProperty("test.put.if.absent", "existing-value");
451448
boolean result = fileConfig.putConfigIfAbsent("test.put.if.absent", "new-value");
452-
assertTrue(result || !result);
449+
Assertions.assertTrue(result || !result);
453450
}
454451

455452
@Test
456-
void testRemoveExistingConfig() throws Exception {
453+
void testRemoveExistingConfig() {
457454
Configuration fileConfig = ConfigurationFactory.getInstance();
458455
System.setProperty("test.remove.existing", "value");
459456
boolean result = fileConfig.removeConfig("test.remove.existing");
460-
assertTrue(result || !result);
457+
Assertions.assertTrue(result || !result);
461458
}
462459

463460
@Test
464-
void testGetConfigFromSystemProperty() throws Exception {
461+
void testGetConfigFromSystemProperty() {
465462
System.setProperty("test.sys.prop", "sys-prop-value");
466463
Configuration fileConfig = ConfigurationFactory.getInstance();
467464
String value = fileConfig.getConfig("test.sys.prop");
468465
Assertions.assertEquals("sys-prop-value", value);
469466
}
470467

471468
@Test
472-
void testGetConfigFromEnvironmentVariable() throws Exception {
469+
void testGetConfigFromEnvironmentVariable() {
473470
Configuration fileConfig = ConfigurationFactory.getInstance();
474471
String path = fileConfig.getConfigFromSys("PATH");
475472
Assertions.assertNotNull(path);
476473
}
477474

478475
@Test
479476
void shouldDelegateFileBackedReadsThroughConfigurationFactory() {
480-
Configuration fileConfig = ConfigurationFactory.getInstance();
481-
482-
Assertions.assertEquals("127.0.0.1:8091", fileConfig.getConfig("service.default.grouplist"));
483-
assertFalse(fileConfig.getBoolean("service.disableGlobalTransaction"));
477+
String dataId = "service.disableGlobalTransaction";
478+
String previousValue = System.getProperty(dataId);
479+
try {
480+
System.clearProperty(dataId);
481+
ConfigurationFactory.reload();
482+
Configuration fileConfig = ConfigurationFactory.getInstance();
483+
484+
Assertions.assertEquals("127.0.0.1:8091", fileConfig.getConfig("service.default.grouplist"));
485+
Assertions.assertFalse(fileConfig.getBoolean(dataId));
486+
} finally {
487+
if (previousValue == null) {
488+
System.clearProperty(dataId);
489+
} else {
490+
System.setProperty(dataId, previousValue);
491+
}
492+
ConfigurationFactory.reload();
493+
}
484494
}
485495

486496
@Test
@@ -497,12 +507,12 @@ void shouldReturnDefaultsForMissingConfigurationThroughConfigurationFactory() {
497507
void shouldReportMutationOperationOutcomeThroughConfigurationFactory() {
498508
Configuration fileConfig = ConfigurationFactory.getInstance();
499509

500-
assertTrue(fileConfig.putConfig("adopted.put.key", "value", 1000L));
501-
assertTrue(fileConfig.putConfigIfAbsent("adopted.put-if-absent.key", "value", 1000L));
502-
assertTrue(fileConfig.removeConfig("adopted.remove.key", 1000L));
510+
Assertions.assertTrue(fileConfig.putConfig("adopted.put.key", "value", 1000L));
511+
Assertions.assertTrue(fileConfig.putConfigIfAbsent("adopted.put-if-absent.key", "value", 1000L));
512+
Assertions.assertTrue(fileConfig.removeConfig("adopted.remove.key", 1000L));
503513

504-
assertFalse(fileConfig.putConfig("adopted.expired.put.key", "value", -1L));
505-
assertFalse(fileConfig.putConfigIfAbsent("adopted.expired.put-if-absent.key", "value", -1L));
506-
assertFalse(fileConfig.removeConfig("adopted.expired.remove.key", -1L));
514+
Assertions.assertFalse(fileConfig.putConfig("adopted.expired.put.key", "value", -1L));
515+
Assertions.assertFalse(fileConfig.putConfigIfAbsent("adopted.expired.put-if-absent.key", "value", -1L));
516+
Assertions.assertFalse(fileConfig.removeConfig("adopted.expired.remove.key", -1L));
507517
}
508518
}

0 commit comments

Comments
 (0)