Skip to content

Commit 0fcccbb

Browse files
Copilotmercyblitz
andcommitted
Add test cases to enhance coverage for JSONArray, JSONObject, StandardFileWatchService, and ArtifactDetector
Co-authored-by: mercyblitz <533114+mercyblitz@users.noreply.github.com>
1 parent ad6511d commit 0fcccbb

4 files changed

Lines changed: 175 additions & 0 deletions

File tree

microsphere-java-core/src/test/java/io/microsphere/classloading/ArtifactDetectorTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ void testDetectOnEmptySet() {
5454
ArtifactDetector instance = new ArtifactDetector(null);
5555
assertTrue(instance.detect(emptySet()).isEmpty());
5656
}
57+
58+
@Test
59+
void testDetectWithIncludedJdkLibraries() {
60+
ArtifactDetector instance = new ArtifactDetector();
61+
List<Artifact> artifacts = instance.detect(true);
62+
assertNotNull(artifacts);
63+
}
64+
65+
@Test
66+
void testDetectWithExcludedJdkLibraries() {
67+
ArtifactDetector instance = new ArtifactDetector();
68+
List<Artifact> artifacts = instance.detect(false);
69+
assertNotNull(artifacts);
70+
}
5771
}

microsphere-java-core/src/test/java/io/microsphere/io/StandardFileWatchServiceTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static io.microsphere.util.ArrayUtils.ofArray;
5353
import static io.microsphere.util.ClassLoaderUtils.getResource;
5454
import static io.microsphere.util.ExceptionUtils.wrap;
55+
import static io.microsphere.collection.Lists.ofList;
5556
import static java.nio.charset.StandardCharsets.UTF_8;
5657
import static java.nio.file.Files.copy;
5758
import static java.nio.file.Files.write;
@@ -265,4 +266,51 @@ private void async(ThrowableAction task) {
265266
}
266267
}
267268

269+
@Test
270+
void testIsStartedBeforeStart() {
271+
StandardFileWatchService fileWatchService = new StandardFileWatchService();
272+
assertFalse(fileWatchService.isStarted());
273+
}
274+
275+
@Test
276+
void testCloseWithoutStart() throws Exception {
277+
StandardFileWatchService fileWatchService = new StandardFileWatchService();
278+
assertFalse(fileWatchService.isStarted());
279+
fileWatchService.close();
280+
assertFalse(fileWatchService.isStarted());
281+
}
282+
283+
@Test
284+
void testWatchWithMultipleListeners() throws Exception {
285+
AtomicReference<File> fileReference = new AtomicReference<>();
286+
287+
try (StandardFileWatchService fileWatchService = new StandardFileWatchService()) {
288+
289+
FileChangedListener listener1 = new FileChangedListener() {
290+
@Override
291+
public void onFileCreated(FileChangedEvent event) {
292+
fileReference.set(event.getFile());
293+
}
294+
};
295+
296+
FileChangedListener listener2 = new LoggingFileChangedListener();
297+
298+
fileWatchService.watch(this.testDir, ofList(listener1, listener2), CREATED);
299+
300+
fileWatchService.start();
301+
302+
assertTrue(fileWatchService.isStarted());
303+
304+
File testFile = createRandomFile(testDir);
305+
306+
while (!testFile.equals(fileReference.get())) {
307+
// spin
308+
}
309+
310+
fileWatchService.stop();
311+
312+
assertFalse(fileWatchService.isStarted());
313+
}
314+
}
315+
268316
}

microsphere-java-core/src/test/java/io/microsphere/json/JSONArrayTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,66 @@ void testHashCode() {
446446
assertEquals(jsonArray.hashCode(), jsonArray.hashCode());
447447
assertEquals(ofList(1, 2, 3).hashCode(), jsonArray.hashCode());
448448
}
449+
450+
@Test
451+
void testPutDoubleOnInvalidValue() {
452+
assertThrows(JSONException.class, () -> jsonArray.put(NaN));
453+
assertThrows(JSONException.class, () -> jsonArray.put(Double.POSITIVE_INFINITY));
454+
assertThrows(JSONException.class, () -> jsonArray.put(Double.NEGATIVE_INFINITY));
455+
}
456+
457+
@Test
458+
void testPutDoubleWithIndexOnInvalidValue() {
459+
assertThrows(JSONException.class, () -> jsonArray.put(0, NaN));
460+
assertThrows(JSONException.class, () -> jsonArray.put(0, Double.POSITIVE_INFINITY));
461+
assertThrows(JSONException.class, () -> jsonArray.put(0, Double.NEGATIVE_INFINITY));
462+
}
463+
464+
@Test
465+
void testOptBooleanWithFallback() {
466+
jsonArray.put(true);
467+
jsonArray.put("false");
468+
assertTrue(jsonArray.optBoolean(0, false));
469+
assertFalse(jsonArray.optBoolean(1, true));
470+
assertTrue(jsonArray.optBoolean(2, true));
471+
assertFalse(jsonArray.optBoolean(-1, false));
472+
}
473+
474+
@Test
475+
void testOptDoubleWithFallback() throws JSONException {
476+
jsonArray.put(1.0);
477+
jsonArray.put("not-a-number");
478+
assertEquals(1.0, jsonArray.optDouble(0, 99.0));
479+
assertEquals(99.0, jsonArray.optDouble(1, 99.0));
480+
assertEquals(99.0, jsonArray.optDouble(2, 99.0));
481+
assertEquals(99.0, jsonArray.optDouble(-1, 99.0));
482+
}
483+
484+
@Test
485+
void testOptIntWithFallback() throws JSONException {
486+
jsonArray.put(42);
487+
jsonArray.put("not-a-number");
488+
assertEquals(42, jsonArray.optInt(0, 99));
489+
assertEquals(99, jsonArray.optInt(1, 99));
490+
assertEquals(99, jsonArray.optInt(2, 99));
491+
assertEquals(99, jsonArray.optInt(-1, 99));
492+
}
493+
494+
@Test
495+
void testOptLongWithFallback() throws JSONException {
496+
jsonArray.put(42L);
497+
jsonArray.put("not-a-number");
498+
assertEquals(42L, jsonArray.optLong(0, 99L));
499+
assertEquals(99L, jsonArray.optLong(1, 99L));
500+
assertEquals(99L, jsonArray.optLong(2, 99L));
501+
assertEquals(99L, jsonArray.optLong(-1, 99L));
502+
}
503+
504+
@Test
505+
void testOptStringWithFallback() {
506+
jsonArray.put("Hello");
507+
assertEquals("Hello", jsonArray.optString(0, "default"));
508+
assertEquals("default", jsonArray.optString(1, "default"));
509+
assertEquals("default", jsonArray.optString(-1, "default"));
510+
}
449511
}

microsphere-java-core/src/test/java/io/microsphere/json/JSONObjectTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,57 @@ void testNULLToString() {
538538
assertEquals("null", NULL.toString());
539539
}
540540

541+
@Test
542+
void testPutDoubleOnInvalidValue() {
543+
assertThrows(JSONException.class, () -> jsonObject.put("value", NaN));
544+
assertThrows(JSONException.class, () -> jsonObject.put("value", POSITIVE_INFINITY));
545+
assertThrows(JSONException.class, () -> jsonObject.put("value", NEGATIVE_INFINITY));
546+
}
547+
548+
@Test
549+
void testOptBooleanWithFallback() throws JSONException {
550+
jsonObject.put("value", TRUE);
551+
assertEquals(true, jsonObject.optBoolean("value", false));
552+
assertEquals(false, jsonObject.optBoolean("missing", false));
553+
assertEquals(true, jsonObject.optBoolean("missing", true));
554+
}
555+
556+
@Test
557+
void testOptDoubleWithFallback() throws JSONException {
558+
jsonObject.put("value", Double.valueOf(1.0));
559+
assertEquals(1.0, jsonObject.optDouble("value", 99.0));
560+
assertEquals(99.0, jsonObject.optDouble("missing", 99.0));
561+
}
562+
563+
@Test
564+
void testOptIntWithFallback() throws JSONException {
565+
jsonObject.put("value", Integer.valueOf(42));
566+
assertEquals(42, jsonObject.optInt("value", 99));
567+
assertEquals(99, jsonObject.optInt("missing", 99));
568+
}
569+
570+
@Test
571+
void testOptLongWithFallback() throws JSONException {
572+
jsonObject.put("value", Long.valueOf(42L));
573+
assertEquals(42L, jsonObject.optLong("value", 99L));
574+
assertEquals(99L, jsonObject.optLong("missing", 99L));
575+
}
576+
577+
@Test
578+
void testOptStringWithFallback() throws JSONException {
579+
jsonObject.put("name", "Mercy");
580+
assertEquals("Mercy", jsonObject.optString("name", "default"));
581+
assertEquals("default", jsonObject.optString("missing", "default"));
582+
}
583+
584+
@Test
585+
void testWriteTo() throws JSONException {
586+
jsonObject.put("name", "Mercy").put("age", 18);
587+
JSONStringer stringer = new JSONStringer();
588+
jsonObject.writeTo(stringer);
589+
assertEquals("{\"name\":\"Mercy\",\"age\":18}", stringer.toString());
590+
}
591+
541592
void assertWrap(Object object) {
542593
assertEquals(object, wrap(object));
543594
}

0 commit comments

Comments
 (0)