|
| 1 | +package java7; |
| 2 | + |
| 3 | +import com.sandwich.koan.Koan; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | + |
| 7 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 8 | +import static com.sandwich.util.Assert.assertEquals; |
| 9 | + |
| 10 | +public class AboutTryWithResources { |
| 11 | + |
| 12 | + @Koan |
| 13 | + public void lookMaNoClose() { |
| 14 | + String str = "first line" |
| 15 | + + System.lineSeparator() |
| 16 | + + "second line"; |
| 17 | + InputStream is = new ByteArrayInputStream(str.getBytes()); |
| 18 | + String line; |
| 19 | + /* BufferedReader implementing @see java.lang.AutoCloseable interface */ |
| 20 | + try (BufferedReader br = |
| 21 | + new BufferedReader( |
| 22 | + new InputStreamReader(is))) { |
| 23 | + line = br.readLine(); |
| 24 | + //br guaranteed to be closed |
| 25 | + } catch (IOException e) { |
| 26 | + line = "error"; |
| 27 | + } |
| 28 | + assertEquals(line, __); |
| 29 | + } |
| 30 | + |
| 31 | + @Koan |
| 32 | + public void lookMaNoCloseWithException() throws IOException { |
| 33 | + String line; |
| 34 | + try (BufferedReader br = |
| 35 | + new BufferedReader( |
| 36 | + new FileReader("I do not exist!"))) { |
| 37 | + line = br.readLine(); |
| 38 | + } |
| 39 | + assertEquals(line, __); |
| 40 | + } |
| 41 | + |
| 42 | + @Koan |
| 43 | + public void lookMaNoCloseWithMultipleResources() throws IOException { |
| 44 | + String str = "first line" |
| 45 | + + System.lineSeparator() |
| 46 | + + "second line"; |
| 47 | + InputStream is = new ByteArrayInputStream(str.getBytes()); |
| 48 | + String line; |
| 49 | + //multiple resources in the same try declaration |
| 50 | + try (BufferedReader br = |
| 51 | + new BufferedReader( |
| 52 | + new FileReader("I do not exist!")); |
| 53 | + BufferedReader brFromString = |
| 54 | + new BufferedReader( |
| 55 | + new InputStreamReader(is)) |
| 56 | + ) { |
| 57 | + line = br.readLine(); |
| 58 | + line += brFromString.readLine(); |
| 59 | + }catch (IOException e) { |
| 60 | + line = "error"; |
| 61 | + } |
| 62 | + assertEquals(line, __); |
| 63 | + } |
| 64 | + |
| 65 | + @Koan |
| 66 | + public void supressException(){ |
| 67 | + String message = ""; |
| 68 | + try { |
| 69 | + bar(); |
| 70 | + } catch (WorkException e) { |
| 71 | + message += e.getMessage() + " " + e.getSuppressed()[0].getMessage(); |
| 72 | + } catch (CloseException e) { |
| 73 | + message += e.getMessage(); |
| 74 | + } |
| 75 | + assertEquals(message, __); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public void bar() throws CloseException, WorkException { |
| 80 | + try (AutoClosableResource autoClosableResource = |
| 81 | + new AutoClosableResource()) { |
| 82 | + autoClosableResource.foo(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class AutoClosableResource implements AutoCloseable{ |
| 88 | + public void foo() throws WorkException{ |
| 89 | + throw new WorkException("Exception thrown while working"); |
| 90 | + } |
| 91 | + public void close() throws CloseException{ |
| 92 | + throw new CloseException("Exception thrown while closing"); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class WorkException extends Exception { |
| 97 | + public WorkException(String message) { |
| 98 | + super(message); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class CloseException extends Exception { |
| 103 | + public CloseException(String message) { |
| 104 | + super(message); |
| 105 | + } |
| 106 | +} |
0 commit comments