1+ package pl .mperor .lab .java ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .lang .reflect .Field ;
7+ import java .lang .reflect .InaccessibleObjectException ;
8+ import java .util .HexFormat ;
9+ import java .util .ServiceLoader ;
10+ import java .util .random .RandomGenerator ;
11+ import java .util .random .RandomGeneratorFactory ;
12+
13+ /**
14+ * Java 17 (September 2021)
15+ */
16+ public class Java17 {
17+
18+ @ Test
19+ public void testStronglyEncapsulatedInternals () {
20+ Assertions .assertThrows (InaccessibleObjectException .class , () -> deepLookIntoStringBytes ("Hello" ));
21+ }
22+
23+ private static byte [] deepLookIntoStringBytes (String string ) throws IllegalAccessException , NoSuchFieldException {
24+ Field valueField = String .class .getDeclaredField ("value" );
25+ valueField .setAccessible (true );
26+ return (byte []) valueField .get (string );
27+ }
28+
29+ @ Test
30+ public void testSwitchPatternMatching () {
31+ Assertions .assertEquals ("String: Hello" , switchOverClasses ("Hello" ));
32+ Assertions .assertEquals ("int: 1" , switchOverClasses (1 ));
33+ Assertions .assertEquals ("long: 13" , switchOverClasses (13L ));
34+ Assertions .assertEquals ("boolean: true" , switchOverClasses (true ));
35+ Assertions .assertEquals ("null" , switchOverClasses (null ));
36+ record Person (String name , String surname ) {
37+ @ Override
38+ public String toString () {
39+ return "%s %s" .formatted (name , surname );
40+ }
41+ }
42+ Assertions .assertEquals ("Object: John Doe" , switchOverClasses (new Person ("John" , "Doe" )));
43+ }
44+
45+ private static String switchOverClasses (Object obj ) {
46+ return switch (obj ) {
47+ case String s -> String .format ("String: %s" , s );
48+ case Integer i -> String .format ("int: %d" , i );
49+ case Long l -> String .format ("long: %d" , l );
50+ case Boolean b -> String .format ("boolean: %s" , b );
51+ case null -> "null" ;
52+ default -> "Object: " + obj ;
53+ };
54+ }
55+
56+ @ Test
57+ public void testRandomGeneratorFactory () {
58+ var generators = ServiceLoader .load (RandomGenerator .class ).stream ()
59+ .map (provider -> provider .type ().getCanonicalName ())
60+ .peek (System .out ::println )
61+ .toList ();
62+
63+ Assertions .assertTrue (generators .contains ("java.util.Random" ));
64+ var defaultRandomGenerator = RandomGeneratorFactory .getDefault ().create ();
65+ Assertions .assertEquals ("jdk.random.L32X64MixRandom" , defaultRandomGenerator .getClass ().getCanonicalName ());
66+ }
67+
68+ @ Test
69+ void testHexFormat () {
70+ HexFormat hexFormat = HexFormat .of ();
71+ String hex = hexFormat .formatHex (new byte []{0x1A , 0x2B , 0x3C });
72+ Assertions .assertEquals ("1a2b3c" , hex );
73+ Assertions .assertArrayEquals (new byte []{0x1A , 0x2B , 0x3C }, hexFormat .parseHex (hex ));
74+ Assertions .assertFalse (HexFormat .isHexDigit ('g' ));
75+ Assertions .assertEquals (16 + 10 , HexFormat .fromHexDigits ("1a" ));
76+ }
77+
78+ }
0 commit comments