tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kkoli...@apache.org
Subject svn commit: r1713056 - in /tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session: TestPrincipal.java TestSerializablePrincipal.java TesterPrincipal.java
Date Sat, 07 Nov 2015 00:42:57 GMT
Author: kkolinko
Date: Sat Nov  7 00:42:56 2015
New Revision: 1713056

URL: http://svn.apache.org/viewvc?rev=1713056&view=rev
Log:
Convert test class to JUnit4 and sync with Tomcat 7

Added:
    tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TesterPrincipal.java
      - copied, changed from r1713051, tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestPrincipal.java
Removed:
    tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestPrincipal.java
Modified:
    tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java

Modified: tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java?rev=1713056&r1=1713055&r2=1713056&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java
(original)
+++ tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java
Sat Nov  7 00:42:56 2015
@@ -27,66 +27,101 @@ import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
 import org.apache.catalina.realm.GenericPrincipal;
 
-import junit.framework.TestCase;
 
-public class TestSerializablePrincipal extends TestCase {
+public class TestSerializablePrincipal  {
 
     /**
      * Simple serialization / de-serialization test for bug 43840.
      */
+    @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
         List<String> roles = new ArrayList<String>();
         roles.add("RoleA");
         roles.add("RoleB");
-        TestPrincipal tpOriginal = new TestPrincipal("inner");
+        TesterPrincipal tpOriginal = new TesterPrincipal("inner");
         GenericPrincipal gpOriginal =
             new GenericPrincipal(null, "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, null);
-        } 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, null);
+            } 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
         assertEquals("User names different", gpOriginal.getName(),
                 gpNew.getName());

Copied: tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TesterPrincipal.java (from
r1713051, tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestPrincipal.java)
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TesterPrincipal.java?p2=tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TesterPrincipal.java&p1=tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestPrincipal.java&r1=1713051&r2=1713056&rev=1713056&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TestPrincipal.java (original)
+++ tomcat/tc6.0.x/trunk/test/org/apache/catalina/ha/session/TesterPrincipal.java Sat Nov
 7 00:42:56 2015
@@ -20,16 +20,18 @@ package org.apache.catalina.ha.session;
 import java.io.Serializable;
 import java.security.Principal;
 
-public class TestPrincipal implements Principal, Serializable {
-    
+public class TesterPrincipal implements Principal, Serializable {
+
+    private static final long serialVersionUID = 1L;
+
     private String name;
 
-    public TestPrincipal(String theName) {
+    public TesterPrincipal(String theName) {
         name = theName;
     }
-    
+
     public String getName() {
         return name;
     }
-    
+
 }
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message