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