@@ -13,27 +13,24 @@ public class JavaTryWithResourcesTest {
1313 private static final String TEST_STRING_HELLO_WORLD = "Hello World" ;
1414 private Date resource1Date , resource2Date ;
1515
16- /*
17- * Example for using Try_with_resources
18- */
16+ // tests
17+
18+ /* Example for using Try_with_resources */
1919 @ Test
2020 public void whenWritingToStringWriter_thenCorrectlyWritten () {
21-
22- StringWriter sw = new StringWriter ();
21+ final StringWriter sw = new StringWriter ();
2322 try (PrintWriter pw = new PrintWriter (sw , true )) {
2423 pw .print (TEST_STRING_HELLO_WORLD );
2524 }
2625
2726 Assert .assertEquals (sw .getBuffer ().toString (), TEST_STRING_HELLO_WORLD );
2827 }
2928
30- /*
31- * Example for using multiple resources
32- */
29+ /* Example for using multiple resources */
3330 @ Test
3431 public void givenStringToScanner_whenWritingToStringWriter_thenCorrectlyWritten () {
3532
36- StringWriter sw = new StringWriter ();
33+ final StringWriter sw = new StringWriter ();
3734 try (Scanner sc = new Scanner (TEST_STRING_HELLO_WORLD ); PrintWriter pw = new PrintWriter (sw , true )) {
3835 while (sc .hasNext ()) {
3936 pw .print (sc .nextLine ());
@@ -43,12 +40,9 @@ public void givenStringToScanner_whenWritingToStringWriter_thenCorrectlyWritten(
4340 Assert .assertEquals (sw .getBuffer ().toString (), TEST_STRING_HELLO_WORLD );
4441 }
4542
46- /*
47- * Example to show order in which the resources are closed
48- */
43+ /* Example to show order in which the resources are closed */
4944 @ Test
5045 public void whenFirstAutoClosableResourceIsinitializedFirst_thenFirstAutoClosableResourceIsReleasedFirst () throws Exception {
51-
5246 try (AutoCloseableResourcesFirst af = new AutoCloseableResourcesFirst (); AutoCloseableResourcesSecond as = new AutoCloseableResourcesSecond ()) {
5347 af .doSomething ();
5448 as .doSomething ();
@@ -57,7 +51,6 @@ public void whenFirstAutoClosableResourceIsinitializedFirst_thenFirstAutoClosabl
5751 }
5852
5953 class AutoCloseableResourcesFirst implements AutoCloseable {
60-
6154 public AutoCloseableResourcesFirst () {
6255 System .out .println ("Constructor -> AutoCloseableResources_First" );
6356 }
@@ -70,12 +63,10 @@ public void doSomething() {
7063 public void close () throws Exception {
7164 System .out .println ("Closed AutoCloseableResources_First" );
7265 resource1Date = new Date ();
73-
7466 }
7567 }
7668
7769 class AutoCloseableResourcesSecond implements AutoCloseable {
78-
7970 public AutoCloseableResourcesSecond () {
8071 System .out .println ("Constructor -> AutoCloseableResources_Second" );
8172 }
@@ -91,4 +82,5 @@ public void close() throws Exception {
9182 Thread .sleep (10000 );
9283 }
9384 }
85+
9486}
0 commit comments