Skip to content

Commit 89bc830

Browse files
committed
Update test classes for Java 4, 5, 12, and 15
- add new test cases for Collections API, formatted string handling, JavaBeans EventHandler, and NIO features - improve code documentation and readability
1 parent 11f3b11 commit 89bc830

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

JavaReleases/src/test/java/pl/mperor/lab/java/Java12.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private String generateFileHashMD5(Path file) throws NoSuchAlgorithmException, I
6262

6363
StringBuilder sb = new StringBuilder();
6464
for (byte b : hashBytes) {
65-
sb.append(String.format("%02x", b));
65+
sb.append(String.format("%02x", b)); // byte to 2‑digit hex
6666
}
6767
return sb.toString();
6868
}

JavaReleases/src/test/java/pl/mperor/lab/java/Java15.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ public void testTextBlock() {
6262

6363
@Test
6464
public void testStringFormatted() {
65-
Assertions.assertEquals("Hello World!", "Hello %s".formatted("World!"));
65+
var positionInFormatted = """
66+
user: %1$s
67+
last name: %2$s
68+
name: %1$s""".formatted("John", "Smith");
69+
Assertions.assertEquals("""
70+
user: John
71+
last name: Smith
72+
name: John""", positionInFormatted);
73+
6674
Assertions.assertEquals("Value: 0.00", String.format(Locale.US, "Value: %.2f", 0d));
6775
}
6876

JavaReleases/src/test/java/pl/mperor/lab/java/Java4.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
5+
import pl.mperor.lab.common.TestUtils;
56

67
import javax.imageio.ImageIO;
78
import java.awt.image.BufferedImage;
9+
import java.beans.EventHandler;
810
import java.io.*;
911
import java.net.InetSocketAddress;
1012
import java.net.ServerSocket;
1113
import java.nio.channels.ServerSocketChannel;
1214
import java.nio.channels.SocketChannel;
1315
import java.nio.file.Files;
1416
import java.nio.file.Path;
17+
import java.nio.file.attribute.BasicFileAttributes;
18+
import java.nio.file.attribute.FileTime;
19+
import java.time.Instant;
1520
import java.util.concurrent.CountDownLatch;
1621
import java.util.concurrent.Executors;
1722
import java.util.logging.Logger;
@@ -34,11 +39,17 @@ public void testAssertionKeyWord() {
3439

3540
@Test
3641
public void testNewInputOutputAkaNIO() throws IOException {
42+
Instant beforeAccessTime = Instant.now();
3743
Path path = Path.of("src", "test", "resources", "nio.txt");
44+
3845
byte[] fileBytes = Files.readAllBytes(path);
3946
String content = new String(fileBytes);
40-
4147
Assertions.assertEquals("Hello NIO!", content);
48+
49+
var fileTime = Files.readAttributes(path, BasicFileAttributes.class)
50+
.lastAccessTime();
51+
System.out.printf("📃 File '%s' - last access time: ⌚%s%n: ", path.getFileName(), fileTime);
52+
Assertions.assertTrue(FileTime.from(beforeAccessTime).compareTo(fileTime) <= 0);
4253
}
4354

4455
@Test
@@ -125,4 +136,17 @@ private void handleConnectedClient(ServerSocket serverSocket) throws IOException
125136
}
126137
}
127138

139+
@Test
140+
public void testJavaBeansEventHandler() {
141+
var out = TestUtils.setTempSystemOut();
142+
Runnable runnable = EventHandler.create(Runnable.class, this, "run"); // () -> run();
143+
runnable.run();
144+
Assertions.assertEquals("Run was called!", out.all());
145+
TestUtils.resetSystemOut();
146+
}
147+
148+
public void run() {
149+
System.out.print("Run was called!");
150+
}
151+
128152
}

JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.annotation.*;
99
import java.lang.reflect.Method;
1010
import java.util.Arrays;
11+
import java.util.Collections;
1112
import java.util.List;
1213
import java.util.concurrent.*;
1314
import java.util.concurrent.atomic.AtomicInteger;
@@ -146,4 +147,14 @@ public void testStringBuilderInsteadOfStringBuffer() {
146147
Assertions.assertEquals("Hello World", sb.reverse().toString());
147148
}
148149

150+
@Test
151+
public void testCollectionsAPI() {
152+
var collection = List.of(1,2,3,2,2);
153+
Assertions.assertEquals(3, Collections.frequency(collection, 2));
154+
155+
List<String> empty = Collections.emptyList();
156+
Assertions.assertNotNull(empty);
157+
Assertions.assertTrue(empty.isEmpty());
158+
}
159+
149160
}

0 commit comments

Comments
 (0)