1+ package pl .mperor .lab .java ;
2+
3+ import com .sun .net .httpserver .HttpServer ;
4+ import org .junit .jupiter .api .Assertions ;
5+ import org .junit .jupiter .api .Test ;
6+ import pl .mperor .lab .TestUtils ;
7+
8+ import java .io .BufferedReader ;
9+ import java .io .IOException ;
10+ import java .io .InputStreamReader ;
11+ import java .io .OutputStream ;
12+ import java .net .InetAddress ;
13+ import java .net .InetSocketAddress ;
14+ import java .net .UnknownHostException ;
15+ import java .nio .charset .Charset ;
16+
17+ /**
18+ * Java 18 (March 2022)
19+ */
20+ public class Java18 {
21+
22+ @ Test
23+ public void testDefaultCharsetIsUTF8 () throws IOException {
24+ System .out .printf ("Running Java Version: %d, file.encoding=%s, native.encoding=%s%n" ,
25+ Runtime .version ().feature (),
26+ System .getProperty ("file.encoding" ),
27+ System .getProperty ("native.encoding" )
28+ );
29+
30+ Charset defaultCharset = Charset .defaultCharset ();
31+ Assertions .assertEquals ("UTF-8" , defaultCharset .name ());
32+
33+ var utf8Reader = new BufferedReader (new InputStreamReader (ClassLoader .getSystemResourceAsStream ("encoding/fileUTF8.txt" )));
34+ var win1250Reader = new BufferedReader (new InputStreamReader (ClassLoader .getSystemResourceAsStream ("encoding/fileWin1250.txt" ), Charset .forName ("windows-1250" )));
35+
36+ try (utf8Reader ; win1250Reader ) {
37+ Assertions .assertEquals (utf8Reader .readLine (), win1250Reader .readLine ());
38+ }
39+ }
40+
41+ @ Test
42+ public void testSimpleWebServer () throws IOException , InterruptedException {
43+ HttpServer server = HttpServer .create (new InetSocketAddress (9000 ), 0 );
44+ server .createContext ("/hello" , exchange -> {
45+ String response = "Hello World!" ;
46+ exchange .sendResponseHeaders (200 , response .getBytes ().length );
47+ try (OutputStream os = exchange .getResponseBody ()) {
48+ os .write (response .getBytes ());
49+ }
50+ });
51+ server .start ();
52+ Assertions .assertNotNull (server );
53+ Assertions .assertEquals ("Hello World!" , curlResponse ("http://127.0.0.1:9000/hello" ));
54+
55+ server .stop (0 );
56+ }
57+
58+ private static String curlResponse (String url ) throws IOException , InterruptedException {
59+ var curlProcess = new ProcessBuilder ("curl" , url ).start ();
60+ String result ;
61+ try (var reader = curlProcess .inputReader ()) {
62+ result = reader .readLine ();
63+ }
64+ curlProcess .waitFor ();
65+ curlProcess .destroy ();
66+ return result ;
67+ }
68+
69+ @ SuppressWarnings ("removal" )
70+ @ Test
71+ public void testFinalizeDeprecated () throws Throwable {
72+ var out = TestUtils .setTempSystemOut ();
73+
74+ // warning: finalize() in Object has been deprecated and marked for removal
75+ var object = new Object () {
76+ @ Override
77+ protected void finalize () throws Throwable {
78+ super .finalize ();
79+ System .out .print ("finalizing ..." );
80+ }
81+ };
82+ object .finalize ();
83+ Assertions .assertEquals ("finalizing ..." , out .all ());
84+ TestUtils .resetSystemOut ();
85+ }
86+
87+ @ Test
88+ public void testInetAddressResolution () throws UnknownHostException {
89+ InetAddress address = InetAddress .getByName ("localhost" );
90+ Assertions .assertTrue (address .isLoopbackAddress ());
91+ }
92+
93+ @ Test
94+ public void testSnippet () {
95+ Assertions .assertNotNull (new JavadocSample ());
96+ }
97+
98+ /**
99+ * This is a doc for {@code JavadocSample}.
100+ * Code snippet:
101+ * {@snippet :
102+ * JavadocSample js = new JavadocSample();}
103+ */
104+ private class JavadocSample {
105+ }
106+
107+ }
0 commit comments