Close resources properly for java tests and examples.

pull/3335/head
liujisi@google.com 14 years ago
parent e8e6eed0be
commit 3239fec94c
  1. 14
      examples/AddPerson.java
  2. 14
      java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
  3. 7
      java/src/test/java/com/google/protobuf/LiteTest.java

@ -70,8 +70,11 @@ class AddPerson {
// Read the existing address book.
try {
FileInputStream input = new FileInputStream(args[0]);
addressBook.mergeFrom(input);
input.close();
try {
addressBook.mergeFrom(input);
} finally {
try { input.close(); } catch (Throwable ignore) {}
}
} catch (FileNotFoundException e) {
System.out.println(args[0] + ": File not found. Creating a new file.");
}
@ -83,7 +86,10 @@ class AddPerson {
// Write the new address book back to disk.
FileOutputStream output = new FileOutputStream(args[0]);
addressBook.build().writeTo(output);
output.close();
try {
addressBook.build().writeTo(output);
} finally {
output.close();
}
}
}

@ -775,8 +775,11 @@ public class GeneratedMessageTest extends TestCase {
TestUtil.setAllFields(builder);
TestAllTypes expected = builder.build();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(expected);
out.close();
try {
out.writeObject(expected);
} finally {
out.close();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
TestAllTypes actual = (TestAllTypes) in.readObject();
@ -788,8 +791,11 @@ public class GeneratedMessageTest extends TestCase {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TestAllTypes expected = builder.buildPartial();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(expected);
out.close();
try {
out.writeObject(expected);
} finally {
out.close();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
TestAllTypes actual = (TestAllTypes) in.readObject();

@ -129,8 +129,11 @@ public class LiteTest extends TestCase {
TestAllTypesLite.NestedMessage.newBuilder().setBb(7))
.build();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(expected);
out.close();
try {
out.writeObject(expected);
} finally {
out.close();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
TestAllTypesLite actual = (TestAllTypesLite) in.readObject();

Loading…
Cancel
Save