Skip to content

Commit 655eb32

Browse files
authored
Merge pull request #257 from microsphere-projects/copilot/add-test-cases-for-coverage
Add tests for ThrowableFunction and FileExtensionFilter to approach 100% coverage
2 parents 3559210 + af5d740 commit 655eb32

File tree

15 files changed

+510
-1
lines changed

15 files changed

+510
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ abstract class AbstractURLClassPathHandleTest extends BaseURLClassPathHandleTest
4343
void testGetPriority() {
4444
assertEquals(DEFAULT_PRIORITY, handle.getPriority());
4545
}
46+
47+
@Test
48+
void testSetPriority() {
49+
int newPriority = 42;
50+
handle.setPriority(newPriority);
51+
assertEquals(newPriority, handle.getPriority());
52+
// Restore default priority
53+
handle.setPriority(DEFAULT_PRIORITY);
54+
assertEquals(DEFAULT_PRIORITY, handle.getPriority());
55+
}
4656
}

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/collection/AbstractDequeTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,44 @@ void testSize() {
197197
deque.poll();
198198
assertEquals(0, deque.size());
199199
}
200+
201+
@Test
202+
void testRemoveFirstOccurrenceWithMultipleOccurrences() {
203+
TestDeque<String> multiDeque = new TestDeque<>();
204+
multiDeque.add("A");
205+
multiDeque.add("B");
206+
multiDeque.add("A");
207+
// removeFirstOccurrence delegates to remove(o) in AbstractDeque which removes the first occurrence
208+
assertTrue(multiDeque.removeFirstOccurrence("A"));
209+
assertEquals(2, multiDeque.size());
210+
// "B" and the second "A" should remain, in order
211+
Iterator<String> it = multiDeque.iterator();
212+
assertEquals("B", it.next());
213+
assertEquals("A", it.next());
214+
assertFalse(it.hasNext());
215+
}
216+
217+
@Test
218+
void testOfferAndPollSequence() {
219+
TestDeque<String> multiDeque = new TestDeque<>();
220+
assertTrue(multiDeque.offer("X"));
221+
assertTrue(multiDeque.offer("Y"));
222+
assertTrue(multiDeque.offer("Z"));
223+
// offer delegates to offerLast; poll delegates to pollFirst (FIFO)
224+
assertEquals("X", multiDeque.poll());
225+
assertEquals("Y", multiDeque.poll());
226+
assertEquals("Z", multiDeque.poll());
227+
assertNull(multiDeque.poll());
228+
}
229+
230+
@Test
231+
void testPushAndPop() {
232+
TestDeque<String> multiDeque = new TestDeque<>();
233+
multiDeque.push("X");
234+
multiDeque.push("Y");
235+
// push delegates to addFirst (LIFO)
236+
assertEquals("Y", multiDeque.pop());
237+
assertEquals("X", multiDeque.pop());
238+
assertThrows(NoSuchElementException.class, () -> multiDeque.pop());
239+
}
200240
}

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/AbstractConverterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2626
import static org.junit.jupiter.api.Assertions.assertNotNull;
2727
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
2829

2930
/**
3031
* {@link AbstractConverter} Test
@@ -102,4 +103,25 @@ void testEquals() {
102103
assertNotEquals(this.converter, StringToBooleanConverter.INSTANCE);
103104
assertNotEquals(this.converter, ObjectToStringConverter.INSTANCE);
104105
}
106+
107+
@Test
108+
void testHashCode() {
109+
AbstractConverter<String, String> another = createConverter();
110+
assertEquals(this.converter.hashCode(), another.hashCode());
111+
assertNotEquals(this.converter.hashCode(), StringToBooleanConverter.INSTANCE.hashCode());
112+
assertNotEquals(this.converter.hashCode(), ObjectToStringConverter.INSTANCE.hashCode());
113+
}
114+
115+
@Test
116+
void testResolvePriority() {
117+
// When resolvePriority() returns a non-null value, getPriority() returns that value
118+
AbstractConverter<String, Integer> converter = new AbstractConverter<String, Integer>() {
119+
@Override
120+
protected Integer doConvert(String source) {
121+
return Integer.parseInt(source);
122+
}
123+
};
124+
// The priority is derived from the type hierarchy and must be negative
125+
assertTrue(converter.getPriority() < 0);
126+
}
105127
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.microsphere.convert.multiple;
18+
19+
import io.microsphere.convert.StringConverter;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.Optional;
27+
import java.util.Set;
28+
29+
import static java.lang.Integer.MAX_VALUE;
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertFalse;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertNull;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
36+
/**
37+
* {@link StringToIterableConverter} Test
38+
*
39+
* <p>Tests the abstract {@link StringToIterableConverter} behavior using
40+
* {@link StringToCollectionConverter} as the concrete implementation.</p>
41+
*
42+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
43+
* @see StringToIterableConverter
44+
* @see StringToCollectionConverter
45+
* @since 1.0.0
46+
*/
47+
class StringToIterableConverterTest {
48+
49+
private StringToCollectionConverter converter;
50+
51+
@BeforeEach
52+
void setUp() {
53+
converter = new StringToCollectionConverter();
54+
}
55+
56+
@Test
57+
void testAccept() {
58+
assertTrue(converter.accept(String.class, Collection.class));
59+
assertTrue(converter.accept(String.class, List.class));
60+
assertTrue(converter.accept(String.class, Set.class));
61+
assertFalse(converter.accept(String.class, String.class));
62+
assertFalse(converter.accept(null, null));
63+
}
64+
65+
@Test
66+
void testConvert() {
67+
Collection<Integer> result = (Collection<Integer>) converter.convert("1,2,3", Collection.class, Integer.class);
68+
assertNotNull(result);
69+
assertEquals(3, result.size());
70+
assertTrue(result.contains(1));
71+
assertTrue(result.contains(2));
72+
assertTrue(result.contains(3));
73+
74+
Collection<String> strResult = (Collection<String>) converter.convert("a,b", Collection.class, String.class);
75+
assertNotNull(strResult);
76+
assertEquals(2, strResult.size());
77+
assertTrue(strResult.contains("a"));
78+
assertTrue(strResult.contains("b"));
79+
}
80+
81+
@Test
82+
void testConvertOnNoStringConverter() {
83+
// No StringConverter registered for Object.class → returns null
84+
assertNull(converter.convert(new String[]{"a"}, 1, Collection.class, Object.class));
85+
}
86+
87+
@Test
88+
void testConvertOnNonCollectionIterable() {
89+
// When createMultiValue returns a non-Collection Iterable, the elements are not added;
90+
// the iterable is returned as-is once a StringConverter is found for the element type.
91+
StringToIterableConverter<Iterable> iterableConverter = new StringToIterableConverter<Iterable>() {
92+
@Override
93+
protected Iterable createMultiValue(int size, Class<?> multiValueType) {
94+
return Collections::emptyIterator;
95+
}
96+
};
97+
98+
Object result = iterableConverter.convert(new String[]{"1", "2"}, 2, Iterable.class, Integer.class);
99+
assertNotNull(result);
100+
}
101+
102+
@Test
103+
void testGetStringConverter() {
104+
// Integer has a registered StringConverter
105+
Optional<StringConverter> intConverter = converter.getStringConverter(Integer.class);
106+
assertTrue(intConverter.isPresent());
107+
108+
// Object.class has no registered StringConverter
109+
Optional<StringConverter> objectConverter = converter.getStringConverter(Object.class);
110+
assertFalse(objectConverter.isPresent());
111+
}
112+
113+
@Test
114+
void testGetSupportedType() {
115+
assertEquals(Collection.class, converter.getSupportedType());
116+
}
117+
118+
@Test
119+
void testGetPriority() {
120+
// Collection has 1 interface level above Iterable, so level=1 → priority = MAX_VALUE - 1
121+
assertEquals(MAX_VALUE - 1, converter.getPriority());
122+
}
123+
124+
@Test
125+
void testGetSourceType() {
126+
assertEquals(String.class, converter.getSourceType());
127+
}
128+
}

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
}

0 commit comments

Comments
 (0)