Skip to content

Commit b333322

Browse files
authored
Merge pull request #729 from dmatej/close-resources
Using try-with to reliable close resources in WsGenTaskTest and WsImportTaskTest and minor code cleanup
2 parents a44a1f8 + ebb7872 commit b333322

File tree

3 files changed

+58
-53
lines changed

3 files changed

+58
-53
lines changed

jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/WsImportMojo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.File;
2222
import java.io.FileFilter;
2323
import java.io.IOException;
24-
import java.io.UnsupportedEncodingException;
2524
import java.net.MalformedURLException;
2625
import java.net.URI;
2726
import java.net.URL;

jaxws-ri/tools/wscompile/src/test/java/com/sun/tools/ws/test/ant/WsGenTaskTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public void testEncoding() throws IOException, URISyntaxException {
4646
copy(pkg, "TestWs.java", WsGenTaskTest.class.getResourceAsStream("resources/TestWs.java_"), enc);
4747
assertEquals(0, AntExecutor.exec(script, "wsgen-server-utf16be"));
4848
File f = new File(srcDir, "test/jaxws/Hello.java");
49-
FileInputStream fis = new FileInputStream(f);
50-
byte[] in = new byte[22];
51-
fis.read(in);
52-
fis.close();
53-
String inStr = new String(in, enc);
54-
assertTrue("Got: '" + inStr + "'", inStr.contains("package t"));
49+
try (FileInputStream fis = new FileInputStream(f)) {
50+
byte[] in = new byte[22];
51+
fis.read(in);
52+
String inStr = new String(in, enc);
53+
assertTrue("Got: '" + inStr + "'", inStr.contains("package t"));
54+
}
5555
}
5656

5757
public void testInvalidEncoding() throws IOException, URISyntaxException {
@@ -86,16 +86,18 @@ public void testJavac() throws IOException, URISyntaxException {
8686
assertEquals(0, AntExecutor.exec(script, "wsgen-javac"));
8787
//wsgen compiled classes should be valid for java 5
8888
File f = new File(buildDir, "test/jaxws/Hello.class");
89-
DataInputStream in = new DataInputStream(new FileInputStream(f));
90-
assertEquals(0xcafebabe, in.readInt());
91-
assertEquals(0, in.readUnsignedShort());
92-
assertEquals(55, in.readUnsignedShort());
89+
try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
90+
assertEquals(0xcafebabe, in.readInt());
91+
assertEquals(0, in.readUnsignedShort());
92+
assertEquals(55, in.readUnsignedShort());
93+
}
9394

9495
//ws class is compiled by default javac (6+)
9596
f = new File(srcDir, "test/TestWs.class");
96-
in = new DataInputStream(new FileInputStream(f));
97-
assertEquals(0xcafebabe, in.readInt());
98-
in.readUnsignedShort();
99-
assertTrue(55 != in.readUnsignedShort());
97+
try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
98+
assertEquals(0xcafebabe, in.readInt());
99+
in.readUnsignedShort();
100+
assertTrue(55 != in.readUnsignedShort());
101+
}
100102
}
101103
}

jaxws-ri/tools/wscompile/src/test/java/com/sun/tools/ws/test/ant/WsImportTaskTest.java

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
public class WsImportTaskTest extends WsAntTaskTestBase {
2727

28-
private File wsdl;
2928
private File pkg;
3029
private File metainf;
3130

@@ -39,7 +38,7 @@ protected void setUp() throws Exception {
3938
super.setUp();
4039
pkg = new File(srcDir, "test");
4140
metainf = new File(buildDir, "META-INF");
42-
wsdl = copy(projectDir, "hello.wsdl", WsImportTaskTest.class.getResourceAsStream("resources/hello.wsdl"));
41+
copy(projectDir, "hello.wsdl", WsImportTaskTest.class.getResourceAsStream("resources/hello.wsdl"));
4342
assertTrue(pkg.mkdirs());
4443
assertTrue(metainf.mkdirs());
4544
}
@@ -49,21 +48,22 @@ public void testEncoding() throws IOException {
4948
assertEquals(1, AntExecutor.exec(script, "wsimport-client-encoding"));
5049
//UTF-8
5150
File f = new File(buildDir, "client/utf8/Hello.java");
52-
FileInputStream fis = new FileInputStream(f);
53-
byte[] in = new byte[11];
54-
fis.read(in);
55-
fis.close();
56-
String inStr = new String(in, "UTF-8");
57-
assertTrue("Got: '" + inStr + "'", inStr.contains("package c"));
51+
try (FileInputStream fis = new FileInputStream(f)) {
52+
byte[] in = new byte[11];
53+
fis.read(in);
54+
String inStr = new String(in, "UTF-8");
55+
assertTrue("Got: '" + inStr + "'", inStr.contains("package c"));
56+
}
5857

5958
//UTF-16LE
6059
f = new File(buildDir, "client/utf16LE/Hello.java");
61-
fis = new FileInputStream(f);
62-
in = new byte[22];
63-
fis.read(in);
64-
fis.close();
65-
inStr = new String(in, "UTF-16LE");
66-
assertTrue("Got: '" + inStr + "'", inStr.contains("package c"));
60+
byte[] in;
61+
try (FileInputStream fis = new FileInputStream(f)) {
62+
in = new byte[22];
63+
fis.read(in);
64+
String inStr = new String(in, "UTF-16LE");
65+
assertTrue("Got: '" + inStr + "'", inStr.contains("package c"));
66+
}
6767

6868
//UTF-74
6969
assertFalse(new File(buildDir, "client/invalid").exists());
@@ -72,27 +72,29 @@ public void testEncoding() throws IOException {
7272
public void testPlugin() throws IOException {
7373
assertEquals(0, AntExecutor.exec(script, "wsimport-plugin"));
7474
File f = new File(buildDir, "test/Hello_Service.java");
75-
BufferedReader br = new BufferedReader(new FileReader(f));
76-
String line;
77-
boolean found = false;
78-
while ((line = br.readLine()) != null) {
79-
if (line.contains("@Generated(value = \"com.sun.tools.ws.wscompile.WsimportTool\", ")) {
80-
found = true;
81-
break;
75+
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
76+
String line;
77+
boolean found = false;
78+
while ((line = br.readLine()) != null) {
79+
if (line.contains("@Generated(value = \"com.sun.tools.ws.wscompile.WsimportTool\", ")) {
80+
found = true;
81+
break;
82+
}
8283
}
84+
assertFalse("Plugin invoked", found);
8385
}
84-
br.close();
85-
assertFalse("Plugin invoked", found);
8686
f = new File(srcDir, "test/Hello_Service.java");
87-
br = new BufferedReader(new FileReader(f));
88-
while ((line = br.readLine()) != null) {
89-
if (line.contains("@Generated(value = \"com.sun.tools.ws.wscompile.WsimportTool\", ")) {
90-
found = true;
91-
break;
87+
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
88+
String line;
89+
boolean found = false;
90+
while ((line = br.readLine()) != null) {
91+
if (line.contains("@Generated(value = \"com.sun.tools.ws.wscompile.WsimportTool\", ")) {
92+
found = true;
93+
break;
94+
}
9295
}
96+
assertTrue("Plugin not invoked", found);
9397
}
94-
br.close();
95-
assertTrue("Plugin not invoked", found);
9698
}
9799

98100
public void testFork() throws FileNotFoundException, IOException {
@@ -105,16 +107,18 @@ public void testJavac() throws IOException {
105107
assertEquals(0, AntExecutor.exec(script, "wsimport-javac"));
106108
//wsimport compiled classes should be valid for java 5
107109
File f = new File(buildDir, "test/types/HelloType.class");
108-
DataInputStream in = new DataInputStream(new FileInputStream(f));
109-
assertEquals(0xcafebabe, in.readInt());
110-
assertEquals(0, in.readUnsignedShort());
111-
assertEquals(55, in.readUnsignedShort());
110+
try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
111+
assertEquals(0xcafebabe, in.readInt());
112+
assertEquals(0, in.readUnsignedShort());
113+
assertEquals(55, in.readUnsignedShort());
114+
}
112115

113116
f = new File(buildDir, "test/Hello.class");
114-
in = new DataInputStream(new FileInputStream(f));
115-
assertEquals(0xcafebabe, in.readInt());
116-
assertEquals(0, in.readUnsignedShort());
117-
assertEquals(55, in.readUnsignedShort());
117+
try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
118+
assertEquals(0xcafebabe, in.readInt());
119+
assertEquals(0, in.readUnsignedShort());
120+
assertEquals(55, in.readUnsignedShort());
121+
}
118122
}
119123

120124
}

0 commit comments

Comments
 (0)