Skip to content

Commit 2cb832b

Browse files
authored
Fix #139: convert JUnit4 to JUnit5 (#140)
1 parent fb2b197 commit 2cb832b

16 files changed

+618
-721
lines changed

pom.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,25 @@ JUG supports 3 original official UUID generation methods as well as later additi
7474
<scope>runtime</scope>
7575
<optional>true</optional>
7676
</dependency>
77-
<!-- For testing, JUnit is needed -->
77+
<!-- For testing, JUnit 5 is needed -->
7878
<dependency>
79-
<groupId>junit</groupId>
80-
<artifactId>junit</artifactId>
81-
<version>${version.junit}</version>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter</artifactId>
81+
<version>${version.junit5}</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<!-- For JUnit 4 parameterized tests migration -->
85+
<dependency>
86+
<groupId>org.junit.jupiter</groupId>
87+
<artifactId>junit-jupiter-params</artifactId>
88+
<version>${version.junit5}</version>
89+
<scope>test</scope>
90+
</dependency>
91+
<!-- For Hamcrest matchers used in tests -->
92+
<dependency>
93+
<groupId>org.hamcrest</groupId>
94+
<artifactId>hamcrest</artifactId>
95+
<version>2.2</version>
8296
<scope>test</scope>
8397
</dependency>
8498
</dependencies>
@@ -184,6 +198,12 @@ https://stackoverflow.com/questions/37958104/maven-javadoc-no-source-files-for-p
184198
<plugin>
185199
<groupId>org.jacoco</groupId>
186200
<artifactId>jacoco-maven-plugin</artifactId>
201+
<configuration>
202+
<excludes>
203+
<exclude>perf/**</exclude>
204+
<exclude>test/**</exclude>
205+
</excludes>
206+
</configuration>
187207
<executions>
188208
<execution>
189209
<goals>

release-notes/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Releases
88

99
#124: TimeBasedEpochGenerator should prevent overflow
1010
(Chad P)
11+
#139: Upgrade tests from JUnit 4 to JUnit 5
1112
- Update to `oss-parent` v69
1213

1314
5.1.1 (26-Sep-2025)

src/main/java/com/fasterxml/uuid/ext/LockedFile.java

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
*/
4040
class LockedFile
4141
{
42+
private static final Logger LOGGER = LoggerFactory.getLogger(LockedFile.class);
4243

43-
private static final Logger logger = LoggerFactory.getLogger(LockedFile.class);
44+
// Wrapper just to allow test(s) to disable/re-route
45+
static LoggerWrapper logger = new LoggerWrapper(LOGGER);
4446

4547
/**
4648
* Expected file length comes from hex-timestamp (16 digits),
@@ -115,6 +117,11 @@ class LockedFile
115117
mLock = lock;
116118
}
117119

120+
// @since 5.2 for testing (for `LockedFileTest`)
121+
static void logging(boolean enabled) {
122+
logger = new LoggerWrapper(enabled ? LOGGER : null);
123+
}
124+
118125
public void deactivate()
119126
{
120127
RandomAccessFile raf = mRAFile;
@@ -131,7 +138,7 @@ public long readStamp()
131138
try {
132139
size = (int) mChannel.size();
133140
} catch (IOException ioe) {
134-
logger.error("Failed to read file size", ioe);
141+
logger.error(ioe, "Failed to read file size");
135142
return READ_ERROR;
136143
}
137144

@@ -151,7 +158,7 @@ public long readStamp()
151158
try {
152159
mRAFile.readFully(data);
153160
} catch (IOException ie) {
154-
logger.error("(file '{}') Failed to read {} bytes", mFile, size, ie);
161+
logger.error(ie, "(file '"+mFile+"') Failed to read "+size+" bytes");
155162
return READ_ERROR;
156163
}
157164

@@ -168,25 +175,25 @@ public long readStamp()
168175
dataStr = dataStr.trim();
169176

170177
long result = -1;
171-
String err = null;
178+
String errMsg = null;
172179

173180
if (!dataStr.startsWith("[0")
174181
|| dataStr.length() < 3
175182
|| Character.toLowerCase(dataStr.charAt(2)) != 'x') {
176-
err = "does not start with '[0x' prefix";
183+
errMsg = "does not start with '[0x' prefix";
177184
} else {
178185
int ix = dataStr.indexOf(']', 3);
179186
if (ix <= 0) {
180-
err = "does not end with ']' marker";
187+
errMsg = "does not end with ']' marker";
181188
} else {
182189
String hex = dataStr.substring(3, ix);
183190
if (hex.length() > 16) {
184-
err = "length of the (hex) timestamp too long; expected 16, had "+hex.length()+" ('"+hex+"')";
191+
errMsg = "length of the (hex) timestamp too long; expected 16, had "+hex.length()+" ('"+hex+"')";
185192
} else {
186193
try {
187194
result = Long.parseLong(hex, 16);
188195
} catch (NumberFormatException nex) {
189-
err = "does not contain a valid hex timestamp; got '"
196+
errMsg = "does not contain a valid hex timestamp; got '"
190197
+hex+"' (parse error: "+nex+")";
191198
}
192199
}
@@ -195,7 +202,7 @@ public long readStamp()
195202

196203
// Unsuccesful?
197204
if (result < 0L) {
198-
logger.error("(file '{}') Malformed timestamp file contents: {}", mFile, err);
205+
logger.error("(file '{}') Malformed timestamp file contents: {}", mFile, errMsg);
199206
return READ_ERROR;
200207
}
201208

@@ -210,12 +217,11 @@ public void writeStamp(long stamp)
210217
{
211218
// Let's do sanity check first:
212219
if (stamp <= mLastTimestamp) {
213-
/* same stamp is not dangerous, but pointless... so warning,
214-
* not an error:
215-
*/
220+
// same stamp is not dangerous, but pointless... so warning,
221+
// not an error:
216222
if (stamp == mLastTimestamp) {
217223
logger.warn("(file '{}') Trying to re-write existing timestamp ({})", mFile, stamp);
218-
return;
224+
return;
219225
}
220226
throw new IOException(""+mFile+" trying to overwrite existing value ("+mLastTimestamp+") with an earlier timestamp ("+stamp+")");
221227
}
@@ -260,20 +266,58 @@ public void writeStamp(long stamp)
260266
*/
261267

262268
protected static void doDeactivate(File f, RandomAccessFile raf,
263-
FileLock lock)
269+
FileLock lock)
264270
{
265271
if (lock != null) {
266272
try {
267273
lock.release();
268274
} catch (Throwable t) {
269-
logger.error("Failed to release lock (for file '{}')", f, t);
275+
logger.error(t, "Failed to release lock (for file '"+f+"')");
270276
}
271277
}
272278
if (raf != null) {
273279
try {
274280
raf.close();
275281
} catch (Throwable t) {
276-
logger.error("Failed to close file '{}'", f, t);
282+
logger.error(t, "Failed to close file '{"+f+"}'");
283+
}
284+
}
285+
}
286+
287+
/*
288+
//////////////////////////////////////////////////////////////
289+
// Helper class(es)
290+
//////////////////////////////////////////////////////////////
291+
*/
292+
293+
private static class LoggerWrapper {
294+
private final Logger logger;
295+
296+
public LoggerWrapper(Logger l) {
297+
logger = l;
298+
}
299+
300+
public void error(Throwable t, String msg) {
301+
if (logger != null) {
302+
logger.error(msg, t);
303+
}
304+
}
305+
306+
public void error(String msg, Object arg1, Object arg2) {
307+
if (logger != null) {
308+
logger.error(msg, arg1, arg2);
309+
}
310+
}
311+
312+
public void warn(String msg) {
313+
if (logger != null) {
314+
logger.warn(msg);
315+
}
316+
}
317+
318+
public void warn(String msg, Object arg1, Object arg2) {
319+
if (logger != null) {
320+
logger.warn(msg, arg1, arg2);
277321
}
278322
}
279323
}

src/test/java/com/fasterxml/uuid/EgressInterfaceFinderTest.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,61 @@
22

33
import com.fasterxml.uuid.EgressInterfaceFinder.EgressResolutionException;
44
import com.fasterxml.uuid.EgressInterfaceFinder.Finder;
5-
import junit.framework.TestCase;
5+
import org.junit.jupiter.api.Test;
66

77
import java.net.InetAddress;
88
import java.net.InetSocketAddress;
99
import java.net.NetworkInterface;
1010
import java.net.UnknownHostException;
1111

1212
import static com.fasterxml.uuid.EgressInterfaceFinder.DEFAULT_TIMEOUT_MILLIS;
13+
import static org.junit.jupiter.api.Assertions.*;
1314

14-
public class EgressInterfaceFinderTest extends TestCase {
15+
public class EgressInterfaceFinderTest {
1516

1617
private final EgressInterfaceFinder finder = new EgressInterfaceFinder();
1718

19+
@Test
1820
public void testUnspecifiedIPv4LocalAddress() throws UnknownHostException {
1921
EgressResolutionException ex = null;
2022
try {
2123
finder.fromLocalAddress(InetAddress.getByName("0.0.0.0"));
2224
} catch (EgressResolutionException e) {
2325
ex = e;
2426
}
25-
assertNotNull("EgressResolutionException was not thrown", ex);
27+
assertNotNull(ex, "EgressResolutionException was not thrown");
2628
String message = ex.getMessage();
27-
assertTrue(String.format(
29+
assertTrue(message.startsWith("local address"), String.format(
2830
"message [%s] does not begin with \"local address\"",
29-
message),
30-
message.startsWith("local address"));
31+
message));
3132
assertEquals(1, ex.getMessages().size());
3233
}
3334

35+
@Test
3436
public void testUnspecifiedIPv6LocalAddress() throws Exception {
3537
EgressResolutionException ex = null;
3638
try {
3739
finder.fromLocalAddress(InetAddress.getByName("::"));
3840
} catch (EgressResolutionException e) {
3941
ex = e;
4042
}
41-
assertNotNull("EgressResolutionException was not thrown", ex);
43+
assertNotNull(ex, "EgressResolutionException was not thrown");
4244
String message = ex.getMessage();
43-
assertTrue(String.format(
45+
assertTrue(message.startsWith("local address"), String.format(
4446
"message [%s] does not begin with \"local address\"",
45-
message),
46-
message.startsWith("local address"));
47+
message));
4748
assertEquals(1, ex.getMessages().size());
4849
}
4950

51+
@Test
5052
public void testFromLocalAddress() throws Exception {
5153
NetworkInterface anInterface =
5254
NetworkInterface.getNetworkInterfaces().nextElement();
5355
InetAddress anAddress = anInterface.getInetAddresses().nextElement();
5456
assertEquals(anInterface, finder.fromLocalAddress(anAddress));
5557
}
5658

59+
@Test
5760
public void testFromIncorrectLocalAddress() throws Exception {
5861
EgressResolutionException ex = null;
5962
try {
@@ -62,15 +65,15 @@ public void testFromIncorrectLocalAddress() throws Exception {
6265
} catch (EgressResolutionException e) {
6366
ex = e;
6467
}
65-
assertNotNull("EgressResolutionException was not thrown", ex);
68+
assertNotNull(ex, "EgressResolutionException was not thrown");
6669
String message = ex.getMessage();
67-
assertTrue(String.format(
70+
assertTrue(message.startsWith("no interface found"), String.format(
6871
"message [%s] does not begin with \"no interface found\"",
69-
message),
70-
message.startsWith("no interface found"));
72+
message));
7173
assertEquals(1, ex.getMessages().size());
7274
}
7375

76+
@Test
7477
public void testFromRemoteDatagramSocketConnection() throws Exception {
7578
if (!System.getProperty("os.name").startsWith("Mac")) {
7679
String name = EgressInterfaceFinder.randomRootServerName();
@@ -79,22 +82,26 @@ public void testFromRemoteDatagramSocketConnection() throws Exception {
7982
}
8083
}
8184

85+
@Test
8286
public void testFromRemoteSocketConnection() throws Exception {
8387
String name = EgressInterfaceFinder.randomRootServerName();
8488
InetSocketAddress address = new InetSocketAddress(name, 53);
8589
finder.fromRemoteSocketConnection(DEFAULT_TIMEOUT_MILLIS, address);
8690
}
8791

92+
@Test
8893
public void testFromRemoteConnection() throws Exception {
8994
String name = EgressInterfaceFinder.randomRootServerName();
9095
InetSocketAddress address = new InetSocketAddress(name, 53);
9196
finder.fromRemoteConnection(DEFAULT_TIMEOUT_MILLIS, address);
9297
}
9398

99+
@Test
94100
public void testFromRootNameServerConnection() throws Exception {
95101
finder.fromRootNameserverConnection(DEFAULT_TIMEOUT_MILLIS);
96102
}
97103

104+
@Test
98105
public void testAggregateExceptions() {
99106
EgressResolutionException ex = null;
100107
final int[] counter = {0};
@@ -112,10 +119,11 @@ public NetworkInterface egressInterface()
112119
} catch (EgressResolutionException e) {
113120
ex = e;
114121
}
115-
assertNotNull("EgressResolutionException was not thrown", ex);
122+
assertNotNull(ex, "EgressResolutionException was not thrown");
116123
assertEquals(9, ex.getMessages().size());
117124
}
118125

126+
@Test
119127
public void testDefaultMechanisms() throws Exception {
120128
try {
121129
finder.egressInterface();

0 commit comments

Comments
 (0)