Skip to content

Commit 06d2c7c

Browse files
authored
Add Java 16 test class ... (#16)
- Day Period Support - Stream.toList Method - Local Records, Enums, Interfaces - Value-Based Classes - JPackage
1 parent 0317219 commit 06d2c7c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ This project includes unit tests for key functionalities introduced in each Java
2727
- [Java 13](src/test/java/pl/mperor/lab/java/Java13.java)
2828
- [Java 14](src/test/java/pl/mperor/lab/java/Java14.java)
2929
- [Java 15](src/test/java/pl/mperor/lab/java/Java15.java)
30+
- [Java 16](src/test/java/pl/mperor/lab/java/Java16.java)
3031

3132
For detailed examples and tests of each feature, please refer to the individual source files linked above.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package pl.mperor.lab.java;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.condition.DisabledIf;
7+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
8+
import pl.mperor.lab.TestUtils;
9+
10+
import java.io.IOException;
11+
import java.time.LocalTime;
12+
import java.time.format.DateTimeFormatter;
13+
import java.util.List;
14+
import java.util.Locale;
15+
import java.util.stream.Collectors;
16+
17+
/**
18+
* Java 16 (March 2021)
19+
*/
20+
public class Java16 {
21+
22+
@Test
23+
public void testPeriodSupport() {
24+
LocalTime date = LocalTime.parse("16:00:00");
25+
// symbol "B" alternative to the am/pm format
26+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B", Locale.US);
27+
Assertions.assertEquals("4 in the afternoon", date.format(formatter));
28+
}
29+
30+
@Test
31+
public void testStreamToList() {
32+
var source = List.of(1, 2, 3);
33+
var toList = source.stream().toList();
34+
var collectToList = source.stream().collect(Collectors.toList());
35+
Assertions.assertEquals(toList, collectToList);
36+
}
37+
38+
@Test
39+
public void testLocalElements() {
40+
var out = TestUtils.setTempSystemOut();
41+
42+
interface LocalInterface {
43+
default void call() {
44+
System.out.println("Local interface work!");
45+
}
46+
}
47+
48+
record LocalRecord() implements LocalInterface {
49+
@Override
50+
public void call() {
51+
System.out.println("Local records work!");
52+
}
53+
}
54+
55+
enum LocalEnum implements LocalInterface {
56+
SINGLE;
57+
58+
@Override
59+
public void call() {
60+
System.out.println("Local enums work!");
61+
}
62+
}
63+
64+
new LocalRecord().call();
65+
LocalEnum.SINGLE.call();
66+
new LocalInterface() {}.call();
67+
68+
var lines = out.lines();
69+
Assertions.assertEquals("Local records work!", lines.getFirst());
70+
Assertions.assertEquals("Local enums work!", lines.getSecond());
71+
Assertions.assertEquals("Local interface work!", lines.getThird());
72+
TestUtils.resetSystemOut();
73+
}
74+
75+
@SuppressWarnings({"removal", "synchronization"})
76+
@Test
77+
public void testValueBasedClasses() {
78+
// warning: new Long(...) has been deprecated and marked for removal
79+
Long longByConstructor = new Long(13);
80+
81+
// warning: attempt to synchronize on an instance of a value-based class
82+
synchronized (longByConstructor) {
83+
System.out.println("From the synchronized block!");
84+
}
85+
86+
Long longByFactoryMethod = Long.valueOf(13L);
87+
Assertions.assertFalse(longByConstructor == longByFactoryMethod);
88+
Assertions.assertTrue(longByConstructor.equals(longByFactoryMethod));
89+
Assertions.assertTrue(Long.valueOf(13L) == longByFactoryMethod);
90+
}
91+
92+
@Disabled("Dependent on the OS and additional libraries, besides having a long execution time.")
93+
@Test
94+
public void testJPackage() throws IOException, InterruptedException {
95+
Process process = new ProcessBuilder(
96+
"jpackage",
97+
"--name", "j16pack",
98+
"--input", "./build/libs",
99+
"--main-jar", "JavaLab-1.0-SNAPSHOT.jar",
100+
"--main-class", "pl.mperor.lab.java.Main"
101+
).start();
102+
103+
Assertions.assertEquals(0, process.waitFor());
104+
process.destroy();
105+
}
106+
107+
}

0 commit comments

Comments
 (0)