[jboss-cvs] JBossAS SVN: r62643 - in projects/test/trunk/src: main/java/org/jboss/test/thread and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Apr 30 12:24:33 EDT 2007


Author: adrian at jboss.org
Date: 2007-04-30 12:24:33 -0400 (Mon, 30 Apr 2007)
New Revision: 62643

Added:
   projects/test/trunk/src/main/java/org/jboss/test/thread/
   projects/test/trunk/src/main/java/org/jboss/test/thread/TestThread.java
   projects/test/trunk/src/test/java/org/jboss/test/thread/
   projects/test/trunk/src/test/java/org/jboss/test/thread/test/
   projects/test/trunk/src/test/java/org/jboss/test/thread/test/TestThreadUnitTestCase.java
Modified:
   projects/test/trunk/src/main/java/org/jboss/test/AbstractTestCase.java
Log:
Add a simple TestThread where you doJoin() and it will either
throw an AssertionFailedError if an error occurred executing the thread
or you can expect an error and assert it was thrown.

Modified: projects/test/trunk/src/main/java/org/jboss/test/AbstractTestCase.java
===================================================================
--- projects/test/trunk/src/main/java/org/jboss/test/AbstractTestCase.java	2007-04-30 16:01:53 UTC (rev 62642)
+++ projects/test/trunk/src/main/java/org/jboss/test/AbstractTestCase.java	2007-04-30 16:24:33 UTC (rev 62643)
@@ -45,6 +45,9 @@
  */
 public abstract class AbstractTestCase extends TestCase
 {
+   /** The static log */
+   private static final Logger staticLog = Logger.getLogger(AbstractTestCase.class); 
+   
    /** The start time */
    long startTime;
    
@@ -186,7 +189,7 @@
     * @param expected the excepted class of the exception
     * @param throwable the real exception
     */
-   protected void checkThrowable(Class expected, Throwable throwable)
+   public static void checkThrowable(Class<? extends Throwable> expected, Throwable throwable)
    {
       if (expected == null)
          fail("Must provide an expected class");
@@ -196,12 +199,12 @@
          throw (Error) throwable;
       if (expected.equals(throwable.getClass()) == false)
       {
-         getLog().error("Unexpected throwable", throwable);
+         staticLog.error("Unexpected throwable", throwable);
          fail("Unexpected throwable: " + throwable);
       }
       else
       {
-         getLog().debug("Got expected " + expected.getName() + "(" + throwable + ")");
+         staticLog.debug("Got expected " + expected.getName() + "(" + throwable + ")");
       }
    }
 

Added: projects/test/trunk/src/main/java/org/jboss/test/thread/TestThread.java
===================================================================
--- projects/test/trunk/src/main/java/org/jboss/test/thread/TestThread.java	                        (rev 0)
+++ projects/test/trunk/src/main/java/org/jboss/test/thread/TestThread.java	2007-04-30 16:24:33 UTC (rev 62643)
@@ -0,0 +1,105 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.thread;
+
+import java.lang.Thread.UncaughtExceptionHandler;
+
+import junit.framework.AssertionFailedError;
+
+import org.jboss.logging.Logger;
+import org.jboss.test.AbstractTestCase;
+
+/**
+ * TestThread.<p>
+ * 
+ * A simple thread that you can {@link #doJoin()} on
+ * and it will rethrow any error in the thread's execution as an
+ * {@link AssertionFailedError} 
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestThread extends Thread implements UncaughtExceptionHandler
+{
+   private Logger log = Logger.getLogger(TestThread.class);
+   
+   private Throwable t;
+
+   public TestThread()
+   {
+      setUncaughtExceptionHandler(this);
+   }
+
+   public TestThread(String name)
+   {
+      super(name);
+      setUncaughtExceptionHandler(this);
+   }
+   
+   public void uncaughtException(Thread t, Throwable e)
+   {
+      log.error("Error in thread " + t, e);
+      this.t = e;
+   }
+   
+   public void doJoin() throws InterruptedException
+   {
+      super.join();
+      if (t != null)
+      {
+         if (t instanceof AssertionFailedError)
+            throw (AssertionFailedError) t;
+         AssertionFailedError e = new AssertionFailedError("Error in thread" + t);
+         e.initCause(t);
+         throw e;
+      }
+   }
+   
+   public void doJoin(long millis) throws InterruptedException
+   {
+      super.join(millis);
+      if (t != null)
+      {
+         if (t instanceof AssertionFailedError)
+            throw (AssertionFailedError) t;
+         AssertionFailedError e = new AssertionFailedError("Error in thread" + t);
+         e.initCause(t);
+         throw e;
+      }
+   }
+   
+   public void doJoinExpectError(Class<? extends Throwable> expected) throws InterruptedException
+   {
+      super.join();
+      if (t == null)
+         throw new AssertionFailedError("No error from thread " + this);
+      AbstractTestCase.checkThrowable(expected, t);
+   }
+   
+   public void doJoinExpectError(long millis, Class<? extends Throwable> expected) throws InterruptedException
+   {
+      super.join(millis);
+      if (t == null)
+         throw new AssertionFailedError("No error from thread " + this);
+      AbstractTestCase.checkThrowable(expected, t);
+  }
+}
\ No newline at end of file

Added: projects/test/trunk/src/test/java/org/jboss/test/thread/test/TestThreadUnitTestCase.java
===================================================================
--- projects/test/trunk/src/test/java/org/jboss/test/thread/test/TestThreadUnitTestCase.java	                        (rev 0)
+++ projects/test/trunk/src/test/java/org/jboss/test/thread/test/TestThreadUnitTestCase.java	2007-04-30 16:24:33 UTC (rev 62643)
@@ -0,0 +1,74 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.thread.test;
+
+import junit.framework.AssertionFailedError;
+
+import org.jboss.test.BaseTestCase;
+import org.jboss.test.thread.TestThread;
+
+public class TestThreadUnitTestCase extends BaseTestCase
+{
+   public TestThreadUnitTestCase(String name)
+   {
+      super(name);
+   }
+   
+   public void testThread()
+   {
+      TestThread thread = new TestThread("TestThread")
+      {
+         public void run()
+         {
+            throw new Error();
+         }
+      };
+      thread.start();
+      
+      try
+      {
+         thread.doJoin();
+         fail("Should not be here!");
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof AssertionFailedError == false)
+            failure("Did not get expected error", t);
+         Throwable e = t.getCause();
+         assertNotNull("Should have a cause", e);
+         checkThrowable(Error.class, e);
+      }
+   }
+   
+   public void testThreadWithException() throws Exception
+   {
+      TestThread thread = new TestThread("TestThreadWithException")
+      {
+         public void run()
+         {
+            throw new Error();
+         }
+      };
+      thread.start();
+      thread.doJoinExpectError(Error.class);
+   }
+}




More information about the jboss-cvs-commits mailing list