You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by kk...@apache.org on 2014/03/14 22:15:36 UTC
svn commit: r1577714 -
/tomcat/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java
Author: kkolinko
Date: Fri Mar 14 21:15:36 2014
New Revision: 1577714
URL: http://svn.apache.org/r1577714
Log:
Properly delete %TEMP%/ser{number}.tmp temporary file that was used to test serialization of a Principal.
On Windows the test could not delete the file, because FileInputStream has not been properly closed after reading. Thus rerunning the testsuite left a bunch of such files in the %TEMP% directory.
The file is now created in output/tmp instead of the system temporary directory.
Modified:
tomcat/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java
Modified: tomcat/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java?rev=1577714&r1=1577713&r2=1577714&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java (original)
+++ tomcat/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java Fri Mar 14 21:15:36 2014
@@ -44,14 +44,10 @@ public class TestSerializablePrincipal
@SuppressWarnings("null")
@Test
public void testWriteReadPrincipal() {
- // Get a temporary file to use for the serialization test
- File file = null;
- try {
- file = File.createTempFile("ser", null);
- file.deleteOnExit();
- } catch (IOException e) {
- e.printStackTrace();
- fail("ioe creating temporary file");
+
+ File tempDir = new File(System.getProperty("tomcat.test.temp", "output/tmp"));
+ if (!tempDir.mkdirs() && !tempDir.isDirectory()) {
+ fail("Unable to create temporary directory for test");
}
// Create the Principal to serialize
@@ -62,36 +58,68 @@ public class TestSerializablePrincipal
GenericPrincipal gpOriginal =
new GenericPrincipal("usr", "pwd", roles, tpOriginal);
- // Do the serialization
+ // Get a temporary file to use for the serialization test
+ File file = null;
try {
- FileOutputStream fos = new FileOutputStream(file);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- SerializablePrincipal.writePrincipal(gpOriginal, oos);
- oos.close();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- fail("fnfe creating object output stream");
+ file = File.createTempFile("ser", null, tempDir);
} catch (IOException e) {
e.printStackTrace();
- fail("ioe serializing principal");
+ fail("ioe creating temporary file");
}
- // De-serialize the Principal
GenericPrincipal gpNew = null;
try {
- FileInputStream fis = new FileInputStream(file);
- ObjectInputStream ois = new ObjectInputStream(fis);
- gpNew = SerializablePrincipal.readPrincipal(ois);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- fail("fnfe reading object output stream");
- } catch (IOException e) {
- e.printStackTrace();
- fail("ioe de-serializing principal");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- fail("cnfe de-serializing principal");
+ // Do the serialization
+ FileOutputStream fos = null;
+ try {
+ fos = new FileOutputStream(file);
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+ SerializablePrincipal.writePrincipal(gpOriginal, oos);
+ oos.close();
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ fail("fnfe creating object output stream");
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("ioe serializing principal");
+ } finally {
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException ignored) {
+ // NO OP
+ }
+ }
+ }
+
+ // De-serialize the Principal
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(file);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+ gpNew = SerializablePrincipal.readPrincipal(ois);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ fail("fnfe reading object output stream");
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("ioe de-serializing principal");
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ fail("cnfe de-serializing principal");
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException ignored) {
+ // NO OP
+ }
+ }
+ }
+ } finally {
+ if (!file.delete()) {
+ System.out.println("Failed to delete " + file);
+ }
}
// Now test how similar original and de-serialized versions are
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org