Skip to content

Commit fd30f1f

Browse files
Copilotmercyblitz
andcommitted
Add test cases to enhance coverage for Sets, Lists, Maps, and ReflectionUtils
Co-authored-by: mercyblitz <533114+mercyblitz@users.noreply.github.com>
1 parent 2d1195f commit fd30f1f

5 files changed

Lines changed: 91 additions & 0 deletions

File tree

microsphere-java-core/src/test/java/io/microsphere/collection/ListsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static io.microsphere.collection.Lists.ofList;
88
import static java.util.Collections.emptyList;
99
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
1011

1112
/**
1213
* {@link Lists} Test
@@ -77,4 +78,12 @@ void testOfList() {
7778
assertEquals(emptyList(), ofList(TEST_NULL_OBJECT_ARRAY));
7879
assertEquals(of(1, 2, 3), ofList(1, 2, 3));
7980
}
81+
82+
@Test
83+
void testOfListImmutability() {
84+
assertThrows(UnsupportedOperationException.class, () -> ofList().add("x"));
85+
assertThrows(UnsupportedOperationException.class, () -> ofList(1).add(2));
86+
assertThrows(UnsupportedOperationException.class, () -> ofList(1, 2).remove(0));
87+
assertThrows(UnsupportedOperationException.class, () -> ofList(1, 2, 3).clear());
88+
}
8089
}

microsphere-java-core/src/test/java/io/microsphere/collection/MapsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static org.junit.jupiter.api.Assertions.assertEquals;
1212
import static org.junit.jupiter.api.Assertions.assertNull;
1313
import static org.junit.jupiter.api.Assertions.assertSame;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
1415

1516
/**
1617
* {@link Maps} Test
@@ -186,4 +187,22 @@ void testMapOfOnEmptyEntries() {
186187
Map map = Maps.ofMap(new Map.Entry[0]);
187188
assertSame(emptyMap(), map);
188189
}
190+
191+
@Test
192+
void testOfMapOnSingleEntry() {
193+
Map.Entry<String, Integer> entry = ofEntry("A", 1);
194+
Map<String, Integer> map = Maps.ofMap(entry);
195+
assertEquals(1, map.size());
196+
assertEquals(1, map.get("A"));
197+
assertNull(map.get("B"));
198+
assertOfMap(map);
199+
}
200+
201+
@Test
202+
void testOfMapImmutability() {
203+
assertThrows(UnsupportedOperationException.class, () -> ofMap().put("k", "v"));
204+
assertThrows(UnsupportedOperationException.class, () -> ofMap("A", 1).put("B", 2));
205+
assertThrows(UnsupportedOperationException.class, () -> ofMap("A", 1, "B", 2).remove("A"));
206+
assertThrows(UnsupportedOperationException.class, () -> ofMap("A", 1, "B", 2, "C", 3).clear());
207+
}
189208
}

microsphere-java-core/src/test/java/io/microsphere/collection/SetsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static io.microsphere.collection.Sets.ofSet;
88
import static java.util.Collections.emptySet;
99
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
1011

1112
/**
1213
* {@link Sets} Test
@@ -77,4 +78,12 @@ void testOfSet() {
7778
assertEquals(emptySet(), ofSet(TEST_NULL_OBJECT_ARRAY));
7879
assertEquals(of(1, 2, 3), ofSet(1, 2, 3));
7980
}
81+
82+
@Test
83+
void testOfSetImmutability() {
84+
assertThrows(UnsupportedOperationException.class, () -> ofSet().add("x"));
85+
assertThrows(UnsupportedOperationException.class, () -> ofSet(1).add(2));
86+
assertThrows(UnsupportedOperationException.class, () -> ofSet(1, 2).remove(1));
87+
assertThrows(UnsupportedOperationException.class, () -> ofSet(1, 2, 3).clear());
88+
}
8089
}

microsphere-java-core/src/test/java/io/microsphere/convert/multiple/StringToIterableConverterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
/**
3737
* {@link StringToIterableConverter} Test
3838
*
39+
* <p>Tests the abstract {@link StringToIterableConverter} behavior using
40+
* {@link StringToCollectionConverter} as the concrete implementation.</p>
41+
*
3942
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
4043
* @see StringToIterableConverter
44+
* @see StringToCollectionConverter
4145
* @since 1.0.0
4246
*/
4347
class StringToIterableConverterTest {

microsphere-java-core/src/test/java/io/microsphere/reflect/ReflectionUtilsTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
import static io.microsphere.collection.Lists.ofList;
1111
import static io.microsphere.reflect.ReflectionUtils.INACCESSIBLE_OBJECT_EXCEPTION_CLASS;
1212
import static io.microsphere.reflect.ReflectionUtils.INACCESSIBLE_OBJECT_EXCEPTION_CLASS_NAME;
13+
import static io.microsphere.reflect.ReflectionUtils.STACK_WALKER_CLASS;
14+
import static io.microsphere.reflect.ReflectionUtils.STACK_WALKER_CLASS_NAME;
15+
import static io.microsphere.reflect.ReflectionUtils.STACK_WALKER_STACK_FRAME_CLASS;
16+
import static io.microsphere.reflect.ReflectionUtils.STACK_WALKER_STACK_FRAME_CLASS_NAME;
17+
import static io.microsphere.reflect.ReflectionUtils.SUN_REFLECT_REFLECTION_CLASS;
18+
import static io.microsphere.reflect.ReflectionUtils.SUN_REFLECT_REFLECTION_CLASS_NAME;
1319
import static io.microsphere.reflect.ReflectionUtils.getCallerClass;
1420
import static io.microsphere.reflect.ReflectionUtils.getCallerClassInSunReflectReflection;
1521
import static io.microsphere.reflect.ReflectionUtils.getCallerClassName;
@@ -28,6 +34,7 @@
2834
import static java.util.Arrays.asList;
2935
import static org.junit.jupiter.api.Assertions.assertEquals;
3036
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3138
import static org.junit.jupiter.api.Assertions.assertNull;
3239
import static org.junit.jupiter.api.Assertions.assertThrows;
3340
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -144,6 +151,49 @@ void testIsInaccessibleObjectException() {
144151
assertTrue(isInaccessibleObjectException(INACCESSIBLE_OBJECT_EXCEPTION_CLASS_NAME));
145152
}
146153

154+
@Test
155+
void testReadFieldsAsMapOnNull() {
156+
assertTrue(readFieldsAsMap(null).isEmpty());
157+
}
158+
159+
@Test
160+
void testConstants() {
161+
assertEquals("sun.reflect.Reflection", SUN_REFLECT_REFLECTION_CLASS_NAME);
162+
assertEquals("java.lang.StackWalker", STACK_WALKER_CLASS_NAME);
163+
assertEquals("java.lang.StackWalker$StackFrame", STACK_WALKER_STACK_FRAME_CLASS_NAME);
164+
assertEquals("java.lang.reflect.InaccessibleObjectException", INACCESSIBLE_OBJECT_EXCEPTION_CLASS_NAME);
165+
}
166+
167+
@Test
168+
void testStackWalkerClassAvailability() {
169+
if (testCurrentJavaVersion("<", JAVA_VERSION_9)) {
170+
assertNull(STACK_WALKER_CLASS);
171+
assertNull(STACK_WALKER_STACK_FRAME_CLASS);
172+
} else {
173+
assertNotNull(STACK_WALKER_CLASS);
174+
assertNotNull(STACK_WALKER_STACK_FRAME_CLASS);
175+
}
176+
}
177+
178+
@Test
179+
void testSunReflectReflectionClassAvailability() {
180+
boolean supported = isSupportedSunReflectReflection();
181+
if (supported) {
182+
assertNotNull(SUN_REFLECT_REFLECTION_CLASS);
183+
}
184+
// On JDK 9+, sun.reflect.Reflection#getCallerClass(int) was removed,
185+
// so isSupportedSunReflectReflection() returns false even though the class may exist
186+
}
187+
188+
@Test
189+
void testGetCallerClassInSunReflectReflectionWithOffset() {
190+
if (isSupportedSunReflectReflection()) {
191+
assertNotNull(getCallerClassInSunReflectReflection(0));
192+
} else {
193+
assertNull(getCallerClassInSunReflectReflection(0));
194+
}
195+
}
196+
147197
static class T extends Data {
148198

149199
private T self;

0 commit comments

Comments
 (0)