Skip to content

Commit 2d1195f

Browse files
Copilotmercyblitz
andcommitted
Add test cases to enhance coverage for AbstractConverter, AbstractDeque, StringToIterableConverter, and AbstractURLClassPathHandle
Co-authored-by: mercyblitz <533114+mercyblitz@users.noreply.github.com>
1 parent 0fcccbb commit 2d1195f

4 files changed

Lines changed: 196 additions & 0 deletions

File tree

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/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/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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
40+
* @see StringToIterableConverter
41+
* @since 1.0.0
42+
*/
43+
class StringToIterableConverterTest {
44+
45+
private StringToCollectionConverter converter;
46+
47+
@BeforeEach
48+
void setUp() {
49+
converter = new StringToCollectionConverter();
50+
}
51+
52+
@Test
53+
void testAccept() {
54+
assertTrue(converter.accept(String.class, Collection.class));
55+
assertTrue(converter.accept(String.class, List.class));
56+
assertTrue(converter.accept(String.class, Set.class));
57+
assertFalse(converter.accept(String.class, String.class));
58+
assertFalse(converter.accept(null, null));
59+
}
60+
61+
@Test
62+
void testConvert() {
63+
Collection<Integer> result = (Collection<Integer>) converter.convert("1,2,3", Collection.class, Integer.class);
64+
assertNotNull(result);
65+
assertEquals(3, result.size());
66+
assertTrue(result.contains(1));
67+
assertTrue(result.contains(2));
68+
assertTrue(result.contains(3));
69+
70+
Collection<String> strResult = (Collection<String>) converter.convert("a,b", Collection.class, String.class);
71+
assertNotNull(strResult);
72+
assertEquals(2, strResult.size());
73+
assertTrue(strResult.contains("a"));
74+
assertTrue(strResult.contains("b"));
75+
}
76+
77+
@Test
78+
void testConvertOnNoStringConverter() {
79+
// No StringConverter registered for Object.class → returns null
80+
assertNull(converter.convert(new String[]{"a"}, 1, Collection.class, Object.class));
81+
}
82+
83+
@Test
84+
void testConvertOnNonCollectionIterable() {
85+
// When createMultiValue returns a non-Collection Iterable, the elements are not added;
86+
// the iterable is returned as-is once a StringConverter is found for the element type.
87+
StringToIterableConverter<Iterable> iterableConverter = new StringToIterableConverter<Iterable>() {
88+
@Override
89+
protected Iterable createMultiValue(int size, Class<?> multiValueType) {
90+
return Collections::emptyIterator;
91+
}
92+
};
93+
94+
Object result = iterableConverter.convert(new String[]{"1", "2"}, 2, Iterable.class, Integer.class);
95+
assertNotNull(result);
96+
}
97+
98+
@Test
99+
void testGetStringConverter() {
100+
// Integer has a registered StringConverter
101+
Optional<StringConverter> intConverter = converter.getStringConverter(Integer.class);
102+
assertTrue(intConverter.isPresent());
103+
104+
// Object.class has no registered StringConverter
105+
Optional<StringConverter> objectConverter = converter.getStringConverter(Object.class);
106+
assertFalse(objectConverter.isPresent());
107+
}
108+
109+
@Test
110+
void testGetSupportedType() {
111+
assertEquals(Collection.class, converter.getSupportedType());
112+
}
113+
114+
@Test
115+
void testGetPriority() {
116+
// Collection has 1 interface level above Iterable, so level=1 → priority = MAX_VALUE - 1
117+
assertEquals(MAX_VALUE - 1, converter.getPriority());
118+
}
119+
120+
@Test
121+
void testGetSourceType() {
122+
assertEquals(String.class, converter.getSourceType());
123+
}
124+
}

0 commit comments

Comments
 (0)