|
| 1 | +package org.baeldung.java8; |
| 2 | + |
| 3 | +import java.io.PrintWriter; |
| 4 | +import java.io.StringWriter; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.Scanner; |
| 7 | + |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +public class JavaTryWithResourcesTest { |
| 12 | + |
| 13 | + private static final String TEST_STRING_HELLO_WORLD = "Hello World"; |
| 14 | + private Date resource1Date, resource2Date; |
| 15 | + |
| 16 | + /* |
| 17 | + * Example for using Try_with_resources |
| 18 | + */ |
| 19 | + @Test |
| 20 | + public void whenWritingToStringWriter_thenCorrectlyWritten() { |
| 21 | + |
| 22 | + StringWriter sw = new StringWriter(); |
| 23 | + try (PrintWriter pw = new PrintWriter(sw, true)) { |
| 24 | + pw.print(TEST_STRING_HELLO_WORLD); |
| 25 | + } |
| 26 | + |
| 27 | + Assert.assertEquals(sw.getBuffer().toString(), TEST_STRING_HELLO_WORLD); |
| 28 | + } |
| 29 | + |
| 30 | + /* |
| 31 | + * Example for using multiple resources |
| 32 | + */ |
| 33 | + @Test |
| 34 | + public void givenStringToScanner_whenWritingToStringWriter_thenCorrectlyWritten() { |
| 35 | + |
| 36 | + StringWriter sw = new StringWriter(); |
| 37 | + try (Scanner sc = new Scanner(TEST_STRING_HELLO_WORLD); PrintWriter pw = new PrintWriter(sw, true)) { |
| 38 | + while (sc.hasNext()) { |
| 39 | + pw.print(sc.nextLine()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + Assert.assertEquals(sw.getBuffer().toString(), TEST_STRING_HELLO_WORLD); |
| 44 | + } |
| 45 | + |
| 46 | + /* |
| 47 | + * Example to show order in which the resources are closed |
| 48 | + */ |
| 49 | + @Test |
| 50 | + public void whenFirstAutoClosableResourceIsinitializedFirst_thenFirstAutoClosableResourceIsReleasedFirst() throws Exception { |
| 51 | + |
| 52 | + try (AutoCloseableResourcesFirst af = new AutoCloseableResourcesFirst(); AutoCloseableResourcesSecond as = new AutoCloseableResourcesSecond()) { |
| 53 | + af.doSomething(); |
| 54 | + as.doSomething(); |
| 55 | + } |
| 56 | + Assert.assertTrue(resource1Date.after(resource2Date)); |
| 57 | + } |
| 58 | + |
| 59 | + class AutoCloseableResourcesFirst implements AutoCloseable { |
| 60 | + |
| 61 | + public AutoCloseableResourcesFirst() { |
| 62 | + System.out.println("Constructor -> AutoCloseableResources_First"); |
| 63 | + } |
| 64 | + |
| 65 | + public void doSomething() { |
| 66 | + System.out.println("Something -> AutoCloseableResources_First"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void close() throws Exception { |
| 71 | + System.out.println("Closed AutoCloseableResources_First"); |
| 72 | + resource1Date = new Date(); |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + class AutoCloseableResourcesSecond implements AutoCloseable { |
| 78 | + |
| 79 | + public AutoCloseableResourcesSecond() { |
| 80 | + System.out.println("Constructor -> AutoCloseableResources_Second"); |
| 81 | + } |
| 82 | + |
| 83 | + public void doSomething() { |
| 84 | + System.out.println("Something -> AutoCloseableResources_Second"); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void close() throws Exception { |
| 89 | + System.out.println("Closed AutoCloseableResources_Second"); |
| 90 | + resource2Date = new Date(); |
| 91 | + Thread.sleep(10000); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments