db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r679742 - in /db/derby/code/trunk/java: shared/org/apache/derby/shared/common/sanity/AssertFailure.java testing/org/apache/derbyTesting/unitTests/junit/AssertFailureTest.java
Date Fri, 25 Jul 2008 10:18:46 GMT
Author: kahatlen
Date: Fri Jul 25 03:18:46 2008
New Revision: 679742

URL: http://svn.apache.org/viewvc?rev=679742&view=rev
Log:
DERBY-3618: Addressed some review comments in AssertFailure and AssertFailureTest

Patch contributed by Erlend Birkenes <erlend@birkenes.net>.

Modified:
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/AssertFailureTest.java

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java?rev=679742&r1=679741&r2=679742&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java
Fri Jul 25 03:18:46 2008
@@ -1,6 +1,6 @@
 /*
 
-   Derby - Class org.apache.derby.iapi.services.sanity.AssertFailure
+   Derby - Class org.apache.derby.shared.common.sanity.AssertFailure
 
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -21,9 +21,9 @@
 
 package org.apache.derby.shared.common.sanity;
 
-import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.security.AccessControlException;
@@ -37,164 +37,159 @@
  * their failure is expected to be hard, they are under RuntimeException so that
  * no one needs to list them in their throws clauses. An AssertFailure at the
  * outermost system level will result in system shutdown.
- * 
- * An AssertFailure also contains a string representation of a full thread dump 
- * for all the live threads at the moment it was thrown if the JVM supports it 
- * and we have the right permissions. 
- * 
+ *
+ * An AssertFailure also contains a string representation of a full thread dump
+ * for all the live threads at the moment it was thrown if the JVM supports it
+ * and we have the right permissions.
+ *
  * If the JVM doesn't have the method Thread.getAllStackTraces i.e, we are on a
  * JVM < 1.5, or if we don't have the permissions java.lang.RuntimePermission
  * "getStackTrace" and "modifyThreadGroup", a message saying so is stored
  * instead.
- * 
- * The thread dump string is printed to System.err after the normal stack trace 
+ *
+ * The thread dump string is printed to System.err after the normal stack trace
  * when the error is thrown, and it is also directly available by getThreadDump().
  */
 public class AssertFailure extends RuntimeException {
-    
+
     private String threadDump;
-    
-	/**
-	 * This constructor takes the pieces of information
-	 * expected for each error.
-	 *
-	 * @param message the message associated with
-	 * the error.
-	 *
-	 * @param nestedError errors can be nested together;
-	 * if this error has another error associated with it,
-	 * it is specified here. The 'outermost' error should be
-	 * the most sever error; inner errors should be providing
-	 * additional information about what went wrong.
-	 **/
-	public AssertFailure(String message, Throwable nestedError) {   
-		super(message, nestedError);
-		threadDump = dumpThreads();
-	}
-	
-	/**
+
+    /**
+     * This constructor takes the pieces of information
+     * expected for each error.
+     *
+     * @param message the message associated with
+     * the error.
+     *
+     * @param nestedError errors can be nested together;
+     * if this error has another error associated with it,
+     * it is specified here. The 'outermost' error should be
+     * the most severe error; inner errors should be providing
+     * additional information about what went wrong.
+     **/
+    public AssertFailure(String message, Throwable nestedError) {
+        super(message, nestedError);
+        threadDump = dumpThreads();
+    }
+
+    /**
      * This constructor takes the just the message for this error.
      *
      * @param message the message associated with the error.
      **/
-	public AssertFailure(String message) {
+    public AssertFailure(String message) {
         super(message);
         threadDump = dumpThreads();
     }
-	
-	/**
-	 * This constructor expects no arguments or nested error.
-	 **/
-	public AssertFailure() {
-		super();
-		threadDump = dumpThreads();
-	}
-	
-	/**
-	 * Returns the thread dump stored in this AssertFailure as a string.
-	 * 
-	 * @return - thread dump string.
-	 */
-	public String getThreadDump() {
-	    return threadDump;
-	}
-	
-	/**
-	 * Overrides printStackTrace() in java.lang.Throwable to include
-	 * the thread dump after the normal stack trace. 
-	 */
-	
-	public void printStackTrace() {
-	    printStackTrace(System.err);
-	}
-	
-	/**
-     * Overrides printStackTrace(PrintStream s) in java.lang.Throwable 
-     * to include the thread dump after the normal stack trace. 
+
+    /**
+     * This constructor expects no arguments or nested error.
+     **/
+    public AssertFailure() {
+        super();
+        threadDump = dumpThreads();
+    }
+
+    /**
+     * Returns the thread dump stored in this AssertFailure as a string.
+     *
+     * @return - thread dump string.
      */
-	public void printStackTrace(PrintStream s) {
-	    super.printStackTrace(s);
-	    s.println(threadDump);
-    }
-	
-	/**
-     * Overrides printStackTrace(PrintWriter s) in java.lang.Throwable 
-     * to include the thread dump after the normal stack trace. 
+    public String getThreadDump() {
+        return threadDump;
+    }
+
+    /**
+     * Overrides printStackTrace() in java.lang.Throwable to include
+     * the thread dump after the normal stack trace.
      */
-	public void printStackTrace(PrintWriter s) { 
-	    super.printStackTrace(s);
+
+    public void printStackTrace() {
+        printStackTrace(System.err);
+    }
+
+    /**
+     * Overrides printStackTrace(PrintStream s) in java.lang.Throwable
+     * to include the thread dump after the normal stack trace.
+     */
+    public void printStackTrace(PrintStream s) {
+        super.printStackTrace(s);
         s.println(threadDump);
-	}
-	
-	/**
-	 * Dumps stack traces for all the threads if the JVM supports it.
-	 * The result is returned as a string, ready to print.
-	 * 
-	 * If the JVM doesn't have the method Thread.getAllStackTraces 
-	 * i.e, we are on a JVM < 1.5, or  if we don't have the permissions: 
-	 * java.lang.RuntimePermission "getStackTrace" and "modifyThreadGroup",
-	 * a message saying so is returned instead. 
-	 * 
-	 * @return stack traces for all live threads as a string or an error message.
-	 */
-	private String dumpThreads() {
-	    PrintWriter p;
-	    ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-	    p = new PrintWriter(out, true);
-	    
-	    //Try to get a thread dump and deal with various situations.
-	    try {
-	        try {
-	            //This checks that we are on a jvm >= 1.5 where we 
-	            //can actually do threaddumps.
-	            Class c = Class.forName("java.lang.Thread");
-	            c.getMethod("getAllStackTraces", new Class[] {});
-	            
-	            //Then get the thread dump.
-	            c = Class.forName("org.apache.derby.shared.common.sanity.ThreadDump");
-                final Method m = c.getMethod("getStackDumpString",new Class[] {});
-                
-                String dump;
-                try {
-                    dump = (String) AccessController.doPrivileged
-                        (new PrivilegedExceptionAction(){
-                                public Object run() throws 
-                                    IllegalArgumentException, 
-                                    IllegalAccessException, 
-                                    InvocationTargetException{
-                                    return m.invoke(null, null);
-                                }
-                            }
-                         );
-                } catch (PrivilegedActionException e) {
-                    throw  e.getException();
+    }
+
+    /**
+     * Overrides printStackTrace(PrintWriter s) in java.lang.Throwable
+     * to include the thread dump after the normal stack trace.
+     */
+    public void printStackTrace(PrintWriter s) {
+        super.printStackTrace(s);
+        s.println(threadDump);
+    }
+
+
+    /**
+     * Dumps stack traces for all the threads if the JVM supports it.
+     * The result is returned as a string, ready to print.
+     *
+     * If the JVM doesn't have the method Thread.getAllStackTraces
+     * i.e, we are on a JVM < 1.5, or  if we don't have the permissions:
+     * java.lang.RuntimePermission "getStackTrace" and "modifyThreadGroup",
+     * a message saying so is returned instead.
+     *
+     * @return stack traces for all live threads as a string or an error message.
+     */
+    private String dumpThreads() {
+
+        StringWriter out = new StringWriter();
+        PrintWriter p = new PrintWriter(out, true);
+
+        //Try to get a thread dump and deal with various situations.
+        try {
+            //This checks that we are on a jvm >= 1.5 where we
+            //can actually do threaddumps.
+            Thread.class.getMethod("getAllStackTraces", new Class[] {});
+
+            //Then get the thread dump.
+            Class c = Class.
+            forName("org.apache.derby.shared.common.sanity.ThreadDump");
+            final Method m = c.getMethod("getStackDumpString",new Class[] {});
+
+            String dump;
+
+            dump = (String) AccessController.doPrivileged
+            (new PrivilegedExceptionAction(){
+                public Object run() throws
+                IllegalArgumentException,
+                IllegalAccessException,
+                InvocationTargetException{
+                    return m.invoke(null, null);
                 }
-                //Print the dump to the message string. That went OK.
-                p.print("---------------\nStack traces for all " +
-                "live threads:");
-                p.println("\n" + dump);
-                p.println("---------------");
-	        
-	        } catch (NoSuchMethodException e) {
-	            p.println("(Skipping thread dump because it is not " +
-	            		"supported on JVM 1.4)");
-	        }
-	    } catch (Exception e){ 
-            if (e instanceof InvocationTargetException) {
-            	if (((InvocationTargetException) e).getTargetException() 
-            			instanceof AccessControlException) {
-            		p.println("(Skipping thread dump " +
-            				"because of insufficient permissions:\n" 
-            		        + e.getCause() + ")\n" );
-            	} else 
-            		((InvocationTargetException) e).getTargetException().
-            		printStackTrace(p);
+            }
+            );
+
+            //Print the dump to the message string. That went OK.
+            p.print("---------------\nStack traces for all " +
+            "live threads:");
+            p.println("\n" + dump);
+            p.println("---------------");
+        } catch (NoSuchMethodException e) {
+            p.println("(Skipping thread dump because it is not " +
+            "supported on JVM 1.4)");
+
+        } catch (Exception e) {
+            if (e instanceof PrivilegedActionException &&
+                e.getCause() instanceof InvocationTargetException &&
+                e.getCause().getCause() instanceof AccessControlException){
+
+                p.println("(Skipping thread dump "
+                        + "because of insufficient permissions:\n"
+                        + e.getCause().getCause() + ")\n");
             } else {
-            	p.println("Error trying to dump thread stack traces:");
-                e.printStackTrace(p);
+                p.println("\nAssertFailure tried to do a thread dump, but "
+                        + "there was an error:");
+                e.getCause().printStackTrace(p);
             }
         }
-	    return out.toString();
-	}
+        return out.toString();
+    }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/AssertFailureTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/AssertFailureTest.java?rev=679742&r1=679741&r2=679742&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/AssertFailureTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/AssertFailureTest.java
Fri Jul 25 03:18:46 2008
@@ -1,7 +1,5 @@
 /*
-
-   Derby - Class
-   org.apache.derbyTesting.unitTests.junit.AssertFailureTest
+   Derby - Class org.apache.derbyTesting.unitTests.junit.AssertFailureTest
 
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -19,6 +17,7 @@
    limitations under the License.
 
  */
+
 package org.apache.derbyTesting.unitTests.junit;
 
 import junit.framework.Test;
@@ -56,14 +55,22 @@
     public static Test suite() {
         TestSuite suite = new TestSuite("AssertFailureTest");
 
-        // Run with thread dump permissions
-        suite.addTest(new SecurityManagerSetup(new AssertFailureTest(
-            "testAssertFailureThreadDump"), POLICY_FILENAME));
-
-        // Run WITHOUT thread dump permissions
-        suite.addTest(new SecurityManagerSetup(new AssertFailureTest(
-            "testAssertFailureNoThreadDump"), NO_DUMP_POLICY_FILENAME));
+        try {
+            //Only add the tests if this is a sane build.
+            Class.forName("org.apache.derby.shared.common.sanity." +
+            		"AssertFailure");
+
+            // Run with thread dump permissions
+            suite.addTest(new SecurityManagerSetup(new AssertFailureTest(
+                "testAssertFailureThreadDump"), POLICY_FILENAME));
+
+            // Run WITHOUT thread dump permissions
+            suite.addTest(new SecurityManagerSetup(new AssertFailureTest(
+                "testAssertFailureNoThreadDump"), NO_DUMP_POLICY_FILENAME));
 
+        } catch (ClassNotFoundException e) {
+            //Ignore. Just return an empty suite.
+        }
         return suite;
     }
 
@@ -80,19 +87,19 @@
 
         // Assert that the string is correct, by checking that
         // it starts the right way.
-        if (JVMInfo.JDK_ID >= 6) {
-            String expected = "---------------\n" + 
+        if (JVMInfo.JDK_ID >= JVMInfo.J2SE_15) {
+            String expected = "---------------\n" +
             		"Stack traces for all live threads:\nThread name=";
-            
-            assertTrue("String not correct. Expected to start with:\n<" 
-                + expected + ">...\nWas:\n<" + s + ">.\n" , 
+
+            assertTrue("String not correct. Expected to start with:\n<"
+                + expected + ">...\nWas:\n<" + s + ">.\n" ,
                 s.startsWith(expected));
-            
+
         } else {
             String expected = "(Skipping thread dump because it is not " +
-            		"supported on JVM 1.4)";
-                        
-            assertTrue("String not correct.", s.startsWith(expected));
+            		"supported on JVM 1.4)\n";
+
+            assertEquals("String not correct.", expected, s);
         }
     }
 
@@ -106,21 +113,21 @@
 
         String s = new AssertFailure("AssertFailureTest").getThreadDump();
         //System.out.println(s);    //Debug failures.
-        
-        // Assert that the string is correct, by checking that is starts 
+
+        // Assert that the string is correct, by checking that is starts
         // the right way.
-        if (JVMInfo.JDK_ID >= 6) {
+        if (JVMInfo.JDK_ID >= JVMInfo.J2SE_15) {
             String expected = "(Skipping thread dump because of insufficient " +
-            		"permissions:";
-        
-            assertTrue("String not correct. Expected: <" + expected + 
+            		"permissions:\njava.security.AccessControlException:";
+
+            assertTrue("String not correct. Expected: <" + expected +
                 ">\nWas:\n<" + s + ">", s.startsWith(expected));
-            
+
         } else {
             String expected = "(Skipping thread dump because it is not " +
-                "supported on JVM 1.4)";
-            
-            assertTrue("String not correct.", s.startsWith(expected));
+                "supported on JVM 1.4)\n";
+
+            assertEquals("String not correct.", expected, s);
         }
     }
 }



Mime
View raw message