[jboss-svn-commits] JBoss Common SVN: r4405 - in arquillian/trunk/junit/src: test/java/org/jboss/arquillian/junit and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri May 21 19:33:28 EDT 2010


Author: aslak
Date: 2010-05-21 19:33:27 -0400 (Fri, 21 May 2010)
New Revision: 4405

Modified:
   arquillian/trunk/junit/src/main/java/org/jboss/arquillian/junit/Arquillian.java
   arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/JUnitIntegrationTestCase.java
   arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/testspi/TestDeployableContainer.java
Log:
ARQ-106 Make sure AfterXX is called even when BeforeXX fails. Added testcase to ensure failures are handled correctly


Modified: arquillian/trunk/junit/src/main/java/org/jboss/arquillian/junit/Arquillian.java
===================================================================
--- arquillian/trunk/junit/src/main/java/org/jboss/arquillian/junit/Arquillian.java	2010-05-21 09:06:26 UTC (rev 4404)
+++ arquillian/trunk/junit/src/main/java/org/jboss/arquillian/junit/Arquillian.java	2010-05-21 23:33:27 UTC (rev 4405)
@@ -18,6 +18,7 @@
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -29,6 +30,7 @@
 import org.jboss.arquillian.spi.TestResult;
 import org.jboss.arquillian.spi.TestRunnerAdaptor;
 import org.jboss.arquillian.spi.util.TestEnrichers;
+import org.junit.internal.runners.model.MultipleFailureException;
 import org.junit.runner.Result;
 import org.junit.runner.notification.RunListener;
 import org.junit.runner.notification.RunNotifier;
@@ -161,8 +163,17 @@
          @Override
          public void evaluate() throws Throwable
          {
-            statementWithAfters.evaluate();
-            deployableTest.get().afterClass(Arquillian.this.getTestClass().getJavaClass());
+            new MultiStatementExecutor().execute
+            (
+                  new Statement() { public void evaluate() throws Throwable
+                  {
+                     statementWithAfters.evaluate();
+                  }},
+                  new Statement() { public void evaluate() throws Throwable 
+                  {
+                     deployableTest.get().afterClass(Arquillian.this.getTestClass().getJavaClass());
+                  }}
+            );
          }
       };
    }
@@ -191,8 +202,17 @@
          @Override
          public void evaluate() throws Throwable
          {
-            statementWithAfters.evaluate();
-            deployableTest.get().after(target, method.getMethod());
+            new MultiStatementExecutor().execute
+            (
+                  new Statement() { public void evaluate() throws Throwable 
+                  {
+                     statementWithAfters.evaluate();
+                  }},
+                  new Statement() { public void evaluate() throws Throwable 
+                  {
+                     deployableTest.get().after(target, method.getMethod());
+                  }}
+            );
          }
       };
    }
@@ -230,4 +250,41 @@
          }
       };
    }
+
+   /**
+    * A helper class to safely execute multiple statements in one.<br/>
+    * 
+    * Will execute all statements even if they fail, all exceptions will be kept. If multiple {@link Statement}s
+    * fail, a {@link MultipleFailureException} will be thrown.
+    *
+    * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+    * @version $Revision: $
+    */
+   private class MultiStatementExecutor 
+   {
+      public void execute(Statement... statements) throws Throwable 
+      {
+         List<Throwable> exceptions = new ArrayList<Throwable>();
+         for(Statement command : statements) 
+         {
+            try
+            {
+               command.evaluate();
+            } 
+            catch (Exception e) 
+            {
+               exceptions.add(e);
+            }
+         }
+         if(exceptions.isEmpty())
+         {
+            return;
+         }
+         if(exceptions.size() == 1)
+         {
+            throw exceptions.get(0);
+         }
+         throw new MultipleFailureException(exceptions);
+      }
+   }
 }
\ No newline at end of file

Modified: arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/JUnitIntegrationTestCase.java
===================================================================
--- arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/JUnitIntegrationTestCase.java	2010-05-21 09:06:26 UTC (rev 4404)
+++ arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/JUnitIntegrationTestCase.java	2010-05-21 23:33:27 UTC (rev 4405)
@@ -32,7 +32,7 @@
 
 
 /**
- * Verify the that JUnit integration adaptor fires the expected events.
+ * Verify the that JUnit integration adaptor fires the expected events even when Handlers are failing.
  *
  * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
  * @version $Revision: $
@@ -63,19 +63,27 @@
    }
    
    @Test
-   public void shouldHandleTheLifecycleCorrectlyOnMultipleTestRuns() throws Throwable 
+   public void shouldHandleTheLifecycleCorrectlyOnMultipleTestRunsWithExceptions() throws Throwable 
    {
       JUnitCore runner = new JUnitCore();
       Result result = runner.run(
             Request.classes(TestClass1.class, TestClass1.class));
 
-      if(result.getFailures().size() > 0)
-      {
-         throw result.getFailures().get(0).getException();
-      }
+      Assert.assertEquals(
+            "Verify that both exceptions thrown bubbled up",
+            2, result.getFailureCount());
       
-      Assert.assertTrue(result.wasSuccessful());
+      // Exceptions returned are wrapped in a FiredEventException, verify the cause 
+      Assert.assertEquals(
+            "Verify exception thrown",
+            "deploy", result.getFailures().get(0).getException().getCause().getMessage());
       
+      Assert.assertEquals(
+            "Verify exception thrown",
+            "undeploy", result.getFailures().get(1).getException().getCause().getMessage());
+      
+      Assert.assertFalse(result.wasSuccessful());
+      
       assertCallbacks();
    }
    
@@ -96,8 +104,8 @@
       Assert.assertEquals("Verify undeployed twice", 
             2, (int)containerCallbacks.get("undeploy"));
       
-      Assert.assertEquals("Verify test invoked twice", 
-            2, (int)containerCallbacks.get("shouldBeInvoked"));
+      Assert.assertEquals("Verify test invoked only once, first run should fail during deploy", 
+            1, (int)containerCallbacks.get("shouldBeInvoked"));
    }
    
    @RunWith(Arquillian.class)
@@ -110,7 +118,7 @@
       }
       
       @Test
-      public void shouldBeInvoked() 
+      public void shouldBeInvoked() throws Exception 
       {
          JUnitIntegrationTestCase.wasCalled("shouldBeInvoked");
       }

Modified: arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/testspi/TestDeployableContainer.java
===================================================================
--- arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/testspi/TestDeployableContainer.java	2010-05-21 09:06:26 UTC (rev 4404)
+++ arquillian/trunk/junit/src/test/java/org/jboss/arquillian/junit/testspi/TestDeployableContainer.java	2010-05-21 23:33:27 UTC (rev 4405)
@@ -36,6 +36,8 @@
  */
 public class TestDeployableContainer implements DeployableContainer
 {
+   private int numberOfTimesDeployed = 0;
+   
    /* (non-Javadoc)
     * @see org.jboss.arquillian.spi.DeployableContainer#setup(org.jboss.arquillian.spi.Context, org.jboss.arquillian.spi.Configuration)
     */
@@ -46,8 +48,7 @@
 
 
    /* (non-Javadoc)
-    * @see org.jboss.arquillian.spi.DeployableContainer#start(org.jboss.arquillian.spi.Context)
-    */
+    * @see org.jboss.arquillian.spi.DeployableContainer#start(org.jboss.arquillian.spi.Context)    */
    public void start(Context context) throws LifecycleException
    {
       JUnitIntegrationTestCase.wasCalled("start");
@@ -66,7 +67,13 @@
     */
    public ContainerMethodExecutor deploy(Context context, Archive<?> archive) throws DeploymentException
    {
+      numberOfTimesDeployed++;
       JUnitIntegrationTestCase.wasCalled("deploy");
+      if(numberOfTimesDeployed == 1) 
+      {
+         throw new RuntimeException("deploy");
+      }
+
       return new ContainerMethodExecutor()
       {
          public TestResult invoke(TestMethodExecutor testMethodExecutor)
@@ -93,5 +100,9 @@
    public void undeploy(Context context, Archive<?> archive) throws DeploymentException
    {
       JUnitIntegrationTestCase.wasCalled("undeploy");
+      if(numberOfTimesDeployed == 1)
+      {
+         throw new RuntimeException("undeploy");
+      }
    }
 }



More information about the jboss-svn-commits mailing list