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