Skip to content

Commit 97b72ea

Browse files
committed
Strengthen core utility test assertions
Replace several smoke-style tests with concrete assertions across collection, invoke, IO, logging, networking, and utility test suites. The updated tests now verify consumed values, retained state after close/flush, delegated URLConnection behavior, handler initialization/customization, callback execution, and other side effects so regressions are caught more reliably.
1 parent ffa038a commit 97b72ea

15 files changed

Lines changed: 230 additions & 37 deletions

‎microsphere-java-core/src/test/java/io/microsphere/collection/ReadOnlyIteratorTest.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
import io.microsphere.Loggable;
2020
import org.junit.jupiter.api.Test;
2121

22+
import java.util.ArrayList;
2223
import java.util.Iterator;
24+
import java.util.List;
2325
import java.util.NoSuchElementException;
26+
import java.util.Objects;
2427

2528
import static io.microsphere.AbstractTestCase.TEST_ELEMENT;
2629
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
2731
import static org.junit.jupiter.api.Assertions.assertThrows;
2832
import static org.junit.jupiter.api.Assertions.assertTrue;
2933

@@ -61,7 +65,12 @@ void testRemove() {
6165

6266
@Test
6367
void testForEachRemaining() {
64-
instance.forEachRemaining(this::log);
68+
List<Object> consumedValues = new ArrayList<>();
69+
instance.forEachRemaining(value -> {
70+
consumedValues.add(value);
71+
log("value : {}", value);
72+
});
73+
assertTrue(consumedValues.stream().allMatch(Objects::nonNull));
74+
assertFalse(instance.hasNext());
6575
}
66-
6776
}

‎microsphere-java-core/src/test/java/io/microsphere/invoke/MethodHandleUtilsTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static io.microsphere.invoke.MethodHandleUtils.handleInvokeExactFailure;
3232
import static io.microsphere.invoke.MethodHandleUtils.lookup;
3333
import static io.microsphere.invoke.MethodHandlesLookupUtils.NOT_FOUND_METHOD_HANDLE;
34+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3435
import static org.junit.jupiter.api.Assertions.assertEquals;
3536
import static org.junit.jupiter.api.Assertions.assertFalse;
3637
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -99,7 +100,7 @@ void testFindStaticOnPrivateStaticMethod() throws Throwable {
99100
@Test
100101
void testHandleInvokeExactFailure() {
101102
MethodHandle methodHandle = findVirtual(MethodHandleUtilsTest.class, "privateMethod");
102-
handleInvokeExactFailure(new Throwable("testing"), methodHandle);
103+
assertDoesNotThrow(() -> handleInvokeExactFailure(new Throwable("testing"), methodHandle));
103104
}
104105

105106
@Test

‎microsphere-java-core/src/test/java/io/microsphere/io/FastByteArrayOutputStreamTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ void testToString1() throws IOException {
109109

110110
@Test
111111
void testClose() {
112+
byte[] bytes = this.outputStream.toByteArray();
112113
tearDown();
114+
assertArrayEquals(bytes, this.outputStream.toByteArray());
113115
}
114116

115117
@Test

‎microsphere-java-core/src/test/java/io/microsphere/io/IOUtilsTest.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.net.URL;
1313
import java.nio.charset.Charset;
1414
import java.nio.charset.IllegalCharsetNameException;
15+
import java.util.concurrent.atomic.AtomicBoolean;
1516

1617
import static io.microsphere.AbstractTestCase.TEST_NULL_STRING;
1718
import static io.microsphere.io.IOUtils.BUFFER_SIZE;
@@ -27,9 +28,11 @@
2728
import static io.microsphere.util.SystemUtils.FILE_ENCODING;
2829
import static java.nio.charset.StandardCharsets.UTF_8;
2930
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3032
import static org.junit.jupiter.api.Assertions.assertEquals;
3133
import static org.junit.jupiter.api.Assertions.assertNull;
3234
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3336

3437
/**
3538
* {@link IOUtils} Test
@@ -200,18 +203,20 @@ void testCopy() throws IOException {
200203
@Test
201204
void testClose() {
202205
tearDown();
203-
close(new FastByteArrayOutputStream(0));
206+
AtomicBoolean closed = new AtomicBoolean(false);
207+
close(() -> closed.set(true));
208+
assertTrue(closed.get());
204209
}
205210

206211
@Test
207212
void testCloseOnNull() {
208-
close(null);
213+
assertDoesNotThrow(() -> close(null));
209214
}
210215

211216
@Test
212217
void testCloseOnIOException() {
213-
close(() -> {
218+
assertDoesNotThrow(() -> close(() -> {
214219
throw new IOException("For testing");
215-
});
220+
}));
216221
}
217222
}

‎microsphere-java-core/src/test/java/io/microsphere/io/StringBuilderWriterTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void testWriteWithChars() {
7070
void testWriteWithCharsOnEmpty() {
7171
writer.write((char[]) null);
7272
writer.write(new char[0]);
73+
assertEquals("", writer.toString());
7374
}
7475

7576
@Test
@@ -82,6 +83,7 @@ void testWriteWithCharsAndRange() {
8283
void testWriteWithCharsAndRangeOnNull() {
8384
writer.write((char[]) null, 0, 0);
8485
writer.write(new char[0], 0, 0);
86+
assertEquals("", writer.toString());
8587
}
8688

8789
@Test
@@ -94,6 +96,7 @@ void testWriteWithString() {
9496
void testWriteWithStringOnEmpty() {
9597
writer.write(TEST_NULL_STRING);
9698
writer.write(EMPTY_STRING);
99+
assertEquals("", writer.toString());
97100
}
98101

99102
@Test
@@ -106,6 +109,7 @@ void testWriteWithStringAndRange() {
106109
void testWriteWithStringAndRangeOnEmpty() {
107110
writer.write(TEST_NULL_STRING, 0, 0);
108111
writer.write(EMPTY_STRING, 0, 0);
112+
assertEquals("", writer.toString());
109113
}
110114

111115
@Test
@@ -140,11 +144,15 @@ void testAppendWithCharSequenceAndRangeOnEmpty() {
140144

141145
@Test
142146
void testClose() {
147+
writer.write("Hello");
143148
writer.close();
149+
assertEquals("Hello", writer.toString());
144150
}
145151

146152
@Test
147153
void testFlush() {
154+
writer.write("Hello");
148155
writer.flush();
156+
assertEquals("Hello", writer.toString());
149157
}
150158
}

‎microsphere-java-core/src/test/java/io/microsphere/logging/AbstractLoggerTest.java‎

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.jupiter.api.Test;
2121

2222
import static io.microsphere.util.ArrayUtils.EMPTY_OBJECT_ARRAY;
23+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2324
import static org.junit.jupiter.api.Assertions.assertNotNull;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
2526

@@ -53,11 +54,14 @@ void testIsTraceEnabled() {
5354

5455
@Test
5556
void testTrace() {
56-
logger.trace("test");
57-
logger.trace("test", EMPTY_OBJECT_ARRAY);
58-
logger.trace("test : {}", "a");
59-
logger.trace("test : {}", "a", new Throwable());
60-
logger.trace("test", new Throwable());
57+
assertTrue(logger.isTraceEnabled());
58+
assertDoesNotThrow(() -> {
59+
logger.trace("test");
60+
logger.trace("test", EMPTY_OBJECT_ARRAY);
61+
logger.trace("test : {}", "a");
62+
logger.trace("test : {}", "a", new Throwable());
63+
logger.trace("test", new Throwable());
64+
});
6165
}
6266

6367
@Test
@@ -67,11 +71,14 @@ void testIsDebugEnabled() {
6771

6872
@Test
6973
void testDebug() {
70-
logger.debug("test");
71-
logger.debug("test", EMPTY_OBJECT_ARRAY);
72-
logger.debug("test : {}", "a");
73-
logger.debug("test : {}", "a", new Throwable());
74-
logger.debug("test", new Throwable());
74+
assertTrue(logger.isDebugEnabled());
75+
assertDoesNotThrow(() -> {
76+
logger.debug("test");
77+
logger.debug("test", EMPTY_OBJECT_ARRAY);
78+
logger.debug("test : {}", "a");
79+
logger.debug("test : {}", "a", new Throwable());
80+
logger.debug("test", new Throwable());
81+
});
7582
}
7683

7784
@Test
@@ -81,11 +88,14 @@ void testIsInfoEnabled() {
8188

8289
@Test
8390
void testInfo() {
84-
logger.info("test");
85-
logger.info("test", EMPTY_OBJECT_ARRAY);
86-
logger.info("test : {}", "a");
87-
logger.info("test : {}", "a", new Throwable());
88-
logger.info("test", new Throwable());
91+
assertTrue(logger.isInfoEnabled());
92+
assertDoesNotThrow(() -> {
93+
logger.info("test");
94+
logger.info("test", EMPTY_OBJECT_ARRAY);
95+
logger.info("test : {}", "a");
96+
logger.info("test : {}", "a", new Throwable());
97+
logger.info("test", new Throwable());
98+
});
8999
}
90100

91101
@Test
@@ -95,11 +105,14 @@ void testIsWarnEnabled() {
95105

96106
@Test
97107
void testWarn() {
98-
logger.warn("test");
99-
logger.warn("test", EMPTY_OBJECT_ARRAY);
100-
logger.warn("test : {}", "a");
101-
logger.warn("test : {}", "a", new Throwable());
102-
logger.warn("test", new Throwable());
108+
assertTrue(logger.isWarnEnabled());
109+
assertDoesNotThrow(() -> {
110+
logger.warn("test");
111+
logger.warn("test", EMPTY_OBJECT_ARRAY);
112+
logger.warn("test : {}", "a");
113+
logger.warn("test : {}", "a", new Throwable());
114+
logger.warn("test", new Throwable());
115+
});
103116
}
104117

105118
@Test
@@ -109,10 +122,13 @@ void testIsErrorEnabled() {
109122

110123
@Test
111124
void testError() {
112-
logger.error("test");
113-
logger.error("test", EMPTY_OBJECT_ARRAY);
114-
logger.error("test : {}", "a");
115-
logger.error("test : {}", "a", new Throwable());
116-
logger.error("test", new Throwable());
125+
assertTrue(logger.isErrorEnabled());
126+
assertDoesNotThrow(() -> {
127+
logger.error("test");
128+
logger.error("test", EMPTY_OBJECT_ARRAY);
129+
logger.error("test : {}", "a");
130+
logger.error("test : {}", "a", new Throwable());
131+
logger.error("test", new Throwable());
132+
});
117133
}
118134
}

‎microsphere-java-core/src/test/java/io/microsphere/net/AbstractExtendableProtocolURLStreamHandlerTest.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.net.Proxy;
2525
import java.net.URL;
26+
import java.util.ArrayList;
2627
import java.util.LinkedList;
2728
import java.util.List;
2829

@@ -42,6 +43,7 @@
4243
import static org.junit.jupiter.api.Assertions.assertEquals;
4344
import static org.junit.jupiter.api.Assertions.assertNotNull;
4445
import static org.junit.jupiter.api.Assertions.assertNull;
46+
import static org.junit.jupiter.api.Assertions.assertTrue;
4547

4648
/**
4749
* Abstract {@link ExtendableProtocolURLStreamHandler} Test
@@ -82,6 +84,7 @@ final void testGetHandlePackagesPropertyValue() {
8284
@Test
8385
final void testInit() {
8486
handler.init();
87+
assertTrue(getHandlePackages().contains(resolveHandlePackage(handler)));
8588
}
8689

8790
@Test
@@ -94,9 +97,13 @@ final void testInitSubProtocolURLConnectionFactories() {
9497

9598
@Test
9699
final void testCustomizeSubProtocolURLConnectionFactories() {
100+
CompositeSubProtocolURLConnectionFactory factory = new CompositeSubProtocolURLConnectionFactory();
101+
List<SubProtocolURLConnectionFactory> customizedFactories = new ArrayList<>();
97102
handler.customizeSubProtocolURLConnectionFactories(factories -> {
98-
factories.add(new CompositeSubProtocolURLConnectionFactory());
103+
factories.add(factory);
104+
customizedFactories.addAll(factories);
99105
});
106+
assertTrue(customizedFactories.contains(factory));
100107
}
101108

102109
@Test

‎microsphere-java-core/src/test/java/io/microsphere/net/DelegatingURLConnectionTest.java‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static io.microsphere.net.console.HandlerTest.TEST_CONSOLE_URL;
3232
import static java.lang.System.currentTimeMillis;
3333
import static java.lang.System.out;
34+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3435
import static org.junit.jupiter.api.Assertions.assertEquals;
3536
import static org.junit.jupiter.api.Assertions.assertFalse;
3637
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -74,11 +75,13 @@ void tearDown() {
7475
@Test
7576
void testConnect() throws IOException {
7677
this.urlConnection.connect();
78+
assertTrue(copyToString(this.urlConnection.getInputStream()).length() > 0);
7779
}
7880

7981
@Test
8082
void testSetConnectTimeout() {
8183
this.urlConnection.setConnectTimeout(100);
84+
assertEquals(100, this.urlConnection.getConnectTimeout());
8285
}
8386

8487
@Test
@@ -90,6 +93,7 @@ void testGetConnectTimeout() {
9093
@Test
9194
void testSetReadTimeout() {
9295
this.urlConnection.setReadTimeout(200);
96+
assertEquals(200, this.urlConnection.getReadTimeout());
9397
}
9498

9599
@Test
@@ -224,6 +228,7 @@ void testToString() {
224228
@Test
225229
void testSetDoInput() {
226230
this.urlConnection.setDoInput(true);
231+
assertTrue(this.urlConnection.getDoInput());
227232
}
228233

229234
@Test
@@ -235,6 +240,7 @@ void testGetDoInput() {
235240
@Test
236241
void testSetDoOutput() {
237242
this.urlConnection.setDoOutput(true);
243+
assertTrue(this.urlConnection.getDoOutput());
238244
}
239245

240246
@Test
@@ -246,6 +252,7 @@ void testGetDoOutput() {
246252
@Test
247253
void testSetAllowUserInteraction() {
248254
this.urlConnection.setAllowUserInteraction(true);
255+
assertTrue(this.urlConnection.getAllowUserInteraction());
249256
}
250257

251258
@Test
@@ -257,6 +264,7 @@ void testGetAllowUserInteraction() {
257264
@Test
258265
void testSetUseCaches() {
259266
this.urlConnection.setUseCaches(true);
267+
assertTrue(this.urlConnection.getUseCaches());
260268
}
261269

262270
@Test
@@ -269,6 +277,7 @@ void testGetUseCaches() {
269277
void testSetIfModifiedSince() {
270278
long now = currentTimeMillis();
271279
this.urlConnection.setIfModifiedSince(now);
280+
assertEquals(now, this.urlConnection.getIfModifiedSince());
272281
}
273282

274283
@Test
@@ -280,6 +289,7 @@ void testGetIfModifiedSince() {
280289
@Test
281290
void testSetDefaultUseCaches() {
282291
this.urlConnection.setDefaultUseCaches(true);
292+
assertTrue(this.urlConnection.getDefaultUseCaches());
283293
}
284294

285295
@Test
@@ -290,13 +300,15 @@ void testGetDefaultUseCaches() {
290300

291301
@Test
292302
void testSetRequestProperty() {
293-
this.urlConnection.setRequestProperty("key-1", "value-1");
303+
// The "file:" URL handler does not retain request properties, so getRequestProperty()
304+
// stays null; the delegating call itself must be forwarded without failing.
305+
assertDoesNotThrow(() -> this.urlConnection.setRequestProperty("key-1", "value-1"));
294306
}
295307

296308
@Test
297309
void testAddRequestProperty() {
298-
this.urlConnection.addRequestProperty("key-1", "value-1-1");
299-
this.urlConnection.addRequestProperty("key-2", "value-2");
310+
assertDoesNotThrow(() -> this.urlConnection.addRequestProperty("key-1", "value-1-1"));
311+
assertDoesNotThrow(() -> this.urlConnection.addRequestProperty("key-2", "value-2"));
300312
}
301313

302314
@Test

0 commit comments

Comments
 (0)