You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by gs...@apache.org on 2009/03/26 14:33:02 UTC

svn commit: r758630 - in /ant/antlibs/antunit/trunk/src: main/org/apache/ant/antunit/junit3/ main/org/apache/ant/antunit/junit4/ tests/junit/org/apache/ant/antunit/junit3/ tests/junit/org/apache/ant/antunit/junit4/

Author: gscokart
Date: Thu Mar 26 13:32:52 2009
New Revision: 758630

URL: http://svn.apache.org/viewvc?rev=758630&view=rev
Log:
Report properly BuildException generated when parsing antunit script from Junit4

Added:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/ErrorTestCase.java
Modified:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/AntUnitSuite.java
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit4/AntUnitSuiteRunner.java
    ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit3/AntUnitSuiteTest.java
    ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit4/AntUnitSuiteRunnerTest.java

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/AntUnitSuite.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/AntUnitSuite.java?rev=758630&r1=758629&r2=758630&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/AntUnitSuite.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/AntUnitSuite.java Thu Mar 26 13:32:52 2009
@@ -27,7 +27,6 @@
 import java.util.List;
 
 import junit.framework.Test;
-import junit.framework.TestCase;
 import junit.framework.TestResult;
 import junit.framework.TestSuite;
 
@@ -49,7 +48,7 @@
     private final AntUnitScriptRunner antScriptRunner;
     private final MultiProjectDemuxOutputStream stderr;
     private final MultiProjectDemuxOutputStream stdout;
-    private final Test initializationReportingTest;
+    private final ErrorTestCase initializationReportingTest;
 
     /**
      * Create a JUnit TestSuite that when executed will run the given ant
@@ -66,6 +65,7 @@
      *            a name to the suite so that an IDE can reexecute this suite.
      */
     public AntUnitSuite(File scriptFile, Class rootClass) {
+        setName(rootClass.getName()); //This name allows eclipse to reexecute the test
         AntUnitScriptRunner createdScriptRunner = null;
         try {
             MyProjectFactory prjFactory = new MyProjectFactory(scriptFile);
@@ -74,7 +74,7 @@
             antScriptRunner = null;
             stdout = null;
             stderr = null;
-            initializationReportingTest = error(e);
+            initializationReportingTest = new ErrorTestCase(e);
             addTest(initializationReportingTest);
             return;
         }
@@ -82,8 +82,6 @@
         initializationReportingTest = null;
         stdout = new MultiProjectDemuxOutputStream(antScriptRunner, false);
         stderr = new MultiProjectDemuxOutputStream(antScriptRunner, true);
-        setName(antScriptRunner.getName() + "[" + scriptFile + "]");        
-        setName(rootClass.getName());// Allows eclipse to reexecute the test
         List testTargets = antScriptRunner.getTestTartgets();
         for (Iterator it = testTargets.iterator(); it.hasNext();) {
             String target = (String) it.next();
@@ -122,17 +120,6 @@
             runInContainer(testTartgets, notifier);
         }
     }
-
-    
-    private static Test error(final BuildException ex) {
-        return new TestCase("warning") {
-            protected void runTest() throws BuildException {
-                throw ex;
-            }
-        };
-    }
-
-
     
     /**
      * @Override Run a single test target of the AntUnit suite. suiteSetUp,
@@ -204,4 +191,13 @@
         }
     }
 
+    public boolean hasAntInitError() {
+        return this.initializationReportingTest!=null;
+    }
+
+    public BuildException getAntInitialisationException() {
+        return hasAntInitError() ? 
+                initializationReportingTest.getAntScriptError() :
+                null;
+    }
 }

Added: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/ErrorTestCase.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/ErrorTestCase.java?rev=758630&view=auto
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/ErrorTestCase.java (added)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit3/ErrorTestCase.java Thu Mar 26 13:32:52 2009
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.ant.antunit.junit3;
+
+import org.apache.tools.ant.BuildException;
+
+import junit.framework.TestCase;
+
+/**
+ * A TestCase that will just report an error when running. This is pretty useful
+ * when an error is detected during initialization.
+ */
+public class ErrorTestCase extends TestCase {
+
+    /** The name we use for the error test case ('warning') */
+    public static final String NAME = "warning";
+
+    private final BuildException ex;
+
+    /**
+     * Creates a TestCase that will report the Ant BuildException when running.
+     * @param antScriptError The Ant BuildException that triggered the initialization 
+     * failure
+     */
+    public ErrorTestCase(BuildException antScriptError) {
+        super(NAME);
+        this.ex = antScriptError;
+    }
+
+    /**
+     * @overwrite
+     */
+    protected void runTest() throws BuildException {
+        throw ex;
+    }
+
+    public BuildException getAntScriptError() {
+        return ex;
+    }
+
+}

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit4/AntUnitSuiteRunner.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit4/AntUnitSuiteRunner.java?rev=758630&r1=758629&r2=758630&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit4/AntUnitSuiteRunner.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/junit4/AntUnitSuiteRunner.java Thu Mar 26 13:32:52 2009
@@ -35,10 +35,13 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
+import junit.framework.TestCase;
+
 import org.apache.ant.antunit.AntUnitExecutionNotifier;
 import org.apache.ant.antunit.AssertionFailedException;
 import org.apache.ant.antunit.junit3.AntUnitSuite;
 import org.apache.ant.antunit.junit3.AntUnitTestCase;
+import org.apache.ant.antunit.junit3.ErrorTestCase;
 import org.junit.internal.runners.InitializationError;
 import org.junit.runner.Description;
 import org.junit.runner.Runner;
@@ -64,18 +67,22 @@
     private final Map/*<String, Description>*/ targetDescriptions = new HashMap();
     private final List/*<String>*/ targetsOrder = new LinkedList();
     
-    private AntUnitSuiteRunner(AntUnitSuite suite, Class junitTestClass) {
+    private AntUnitSuiteRunner(AntUnitSuite suite, Class junitTestClass) throws InitializationError {
         junit3Suite = suite;
-        Enumeration tests = suite.tests();
-        while (tests.hasMoreElements()) {
-            //TODO Handle the the case of FileNotFound. 
-            //In that case the suite contains an error Test and we have
-            //a ClassCastException instead of a nice & clear error
-            //TODO Handle the possibility for the user to define suite of AntUnit scripts
-            AntUnitTestCase tc = (AntUnitTestCase) tests.nextElement();
-            Description tc_desc = Description.createTestDescription(junitTestClass, tc.getName());
-            targetDescriptions.put(tc.getTarget(), tc_desc);
-            targetsOrder.add(tc.getTarget());
+        if (suite.hasAntInitError()) {
+            throw new InitializationError(
+                    new Throwable[] { suite.getAntInitialisationException() } 
+                  );
+        } else { 
+            Enumeration tests = suite.tests();
+            while (tests.hasMoreElements()) {
+                TestCase nextTc = (TestCase) tests.nextElement();
+                //TODO Handle the possibility for the user to define suite of AntUnit scripts            	
+            	AntUnitTestCase tc = (AntUnitTestCase) nextTc;
+            	Description tc_desc = Description.createTestDescription(junitTestClass, tc.getName());
+            	targetDescriptions.put(tc.getTarget(), tc_desc);
+            	targetsOrder.add(tc.getTarget());
+            }
         }
     }
 

Modified: ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit3/AntUnitSuiteTest.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit3/AntUnitSuiteTest.java?rev=758630&r1=758629&r2=758630&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit3/AntUnitSuiteTest.java (original)
+++ ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit3/AntUnitSuiteTest.java Thu Mar 26 13:32:52 2009
@@ -90,6 +90,7 @@
         TestResult testResult = new TestResult();
         suite.run(testResult);
         
+        assertNotNull(suite.getName()); 
         assertEquals(1 , testResult.errorCount());
         TestFailure error = (TestFailure) testResult.errors().nextElement();
         assertTrue("Unexpected error : " + error.exceptionMessage(),

Modified: ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit4/AntUnitSuiteRunnerTest.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit4/AntUnitSuiteRunnerTest.java?rev=758630&r1=758629&r2=758630&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit4/AntUnitSuiteRunnerTest.java (original)
+++ ant/antlibs/antunit/trunk/src/tests/junit/org/apache/ant/antunit/junit4/AntUnitSuiteRunnerTest.java Thu Mar 26 13:32:52 2009
@@ -29,6 +29,7 @@
 import junit.framework.TestSuite;
 
 import org.apache.ant.antunit.junit3.AntUnitSuite;
+import org.apache.ant.antunit.junit3.ErrorTestCase;
 import org.apache.tools.ant.util.FileUtils;
 import org.junit.internal.runners.InitializationError;
 import org.junit.runner.Description;
@@ -71,7 +72,6 @@
                 JUnit4AntUnitRunnable.class);
         final ArrayList tDescs = runner.getDescription().getChildren();
 
-        final int TEST_STARTED = 1, TEST_FINISHED = 2;
         RunNotifier notifierMock = new RunNotifier() {
             Description curTest = null;
 
@@ -87,7 +87,6 @@
                 curTest = description;
             }
 
-            @Override
             public void fireTestFinished(Description description) {
                 if (curTest == null) {
                     mockExcutionError += "Unexpected fireTestFinished("
@@ -155,7 +154,19 @@
         }
     }
 
-    
+
+    public void testInvalidSuiteReferencingMissingFile() {
+        try {
+            AntUnitSuiteRunner runner = new AntUnitSuiteRunner(
+                    JUnit4AntUnitRunnableRefferencingIncorrectFile.class);
+            fail("InitializationError expected");
+        } catch (InitializationError e) {
+            String msg = e.getCauses().get(0).getMessage();
+            assertTrue("Unexpected error : " + msg, msg.contains("FileNotFound"));
+            assertTrue("Unexpected error : " + msg, msg.contains("build_script_not_found.xml"));
+        }
+    }
+
     public static class JUnit4AntUnitRunnable {
         public static AntUnitSuite suite() {
             File f = new File("src/etc/testcases/antunit/junit.xml");
@@ -187,4 +198,13 @@
         }
     }
 
+    
+    public static class JUnit4AntUnitRunnableRefferencingIncorrectFile {
+        public static AntUnitSuite suite() {
+            File f = new File("build_script_not_found.xml");
+            return new AntUnitSuite(f,
+                    JUnit4AntUnitRunnableWithNonStaticSuite.class);
+        }
+    }
+
 }