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/01/27 21:18:32 UTC

svn commit: r738222 - in /ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit: AntUnit.java AntUnitExecutionPlatform.java

Author: gscokart
Date: Tue Jan 27 20:18:31 2009
New Revision: 738222

URL: http://svn.apache.org/viewvc?rev=738222&view=rev
Log:
Decoupling AntUnitScriptRunner from the AntUnit task by introducing AntUnitExecutionPlatform interface

Added:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitExecutionPlatform.java   (with props)
Modified:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java?rev=738222&r1=738221&r2=738222&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java Tue Jan 27 20:18:31 2009
@@ -75,12 +75,36 @@
      */
     private Union buildFiles;
 
+    
+    private AntUnitExecutionPlatform myExecutionPlatform = new AntUnitExecutionPlatform() {
+
+        public Project createProjectForFile(File f) {
+            return AntUnit.this.createProjectForFile(f);
+        }
+
+        public void fireEndTest(String targetName) {
+            AntUnit.this.fireEndTest(targetName);
+        }
+
+        public void fireError(String targetName, Throwable t) {
+            AntUnit.this.fireError(targetName, t);
+        }
+
+        public void fireFail(String targetName, AssertionFailedException ae) {
+            AntUnit.this.fireFail(targetName, ae);
+        }
+
+        public void fireStartTest(String targetName) {
+            AntUnit.this.fireStartTest(targetName);
+        }
+    };
+    
     /**
      * The object responsible for the execution of the unit test.
      * scriptRunner is invoked to executes the targets and keep the
      * reference to the project.
      */
-    private AntUnitScriptRunner scriptRunner = new AntUnitScriptRunner();
+    private AntUnitScriptRunner scriptRunner = new AntUnitScriptRunner(myExecutionPlatform);
 
     /**
      * listeners.
@@ -203,7 +227,7 @@
     /** Manage the project reference in order to minimize the number of project creation
      * while allowing every test to run in isolation.  
      */ 
-    private class AntUnitScriptRunner {
+    private static class AntUnitScriptRunner {
         
         /** ant script currently under testing */
         private File scriptFile;
@@ -215,6 +239,19 @@
          * Value is undefined when project is null.
          */
         private boolean projectIsDirty;
+
+        /**
+         * The environment that creates the project and receive execution notification.
+         */
+        private final AntUnitExecutionPlatform env;
+        
+        
+        public AntUnitScriptRunner(AntUnitExecutionPlatform env) {
+            if (env==null) {
+                throw new AssertionError();
+            }
+            this.env = env;
+        }
         
         /** Set the ant script to use. */
         public void activate(File f) {
@@ -243,7 +280,7 @@
                 throw new AssertionError("scriptFile==null");
             }
             if (project == null) {
-                project = createProjectForFile(scriptFile);
+                project = env.createProjectForFile(scriptFile);
                 projectIsDirty = false;
             }
             return project;
@@ -258,7 +295,7 @@
                 throw new AssertionError("scriptFile==null");
             }
             if (project == null || projectIsDirty) {
-                project = createProjectForFile(scriptFile);
+                project = env.createProjectForFile(scriptFile);
             }
             //we already set isDirty to true in order to make sure we didn't reuse
             //this project next time getRenewed is called.  
@@ -321,11 +358,11 @@
                     Project newProject = getCleanProject();
                     newProject.executeTarget(SUITESETUP);
                 } catch (AssertionFailedException e) {
-                    fireStartTest(SUITESETUP);
-                    fireFail(SUITESETUP, e);
+                    env.fireStartTest(SUITESETUP);
+                    env.fireFail(SUITESETUP, e);
                     return false;
                 } catch (BuildException e) {
-                    fireStartTest(SUITESETUP);
+                    env.fireStartTest(SUITESETUP);
                     fireFailOrError(SUITESETUP, e);
                     return false;
                 }
@@ -343,10 +380,10 @@
             // create and register a logcapturer on the newProject
             LogCapturer lc = new LogCapturer(newProject);
             try {
-                fireStartTest(name);
+                env.fireStartTest(name);
                 newProject.executeTargets(v);
             } catch (AssertionFailedException e) {
-                fireFail(name, e);
+                env.fireFail(name, e);
             } catch (BuildException e) {
                 fireFailOrError(name, e);
             } finally {
@@ -355,13 +392,13 @@
                 // registered after the endTest event -
                 // endTarget is called before this method's catch block
                 // is reached.
-                fireEndTest(name);
+                env.fireEndTest(name);
                 // clean up
                 if (tearDown) {
                     try {
                         newProject.executeTarget(TEARDOWN);
                     } catch (final AssertionFailedException e) {
-                        fireFail(name, e);
+                        env.fireFail(name, e);
                     } catch (final BuildException e) {
                         fireFailOrError(name, e);
                     }
@@ -375,10 +412,10 @@
                     Project newProject = getCleanProject();
                     newProject.executeTarget(SUITETEARDOWN);
                 } catch (AssertionFailedException e) {
-                    fireStartTest(SUITETEARDOWN);
-                    fireFail(SUITETEARDOWN, e);
+                    env.fireStartTest(SUITETEARDOWN);
+                    env.fireFail(SUITETEARDOWN, e);
                 } catch (BuildException e) {
-                    fireStartTest(SUITETEARDOWN);
+                    env.fireStartTest(SUITETEARDOWN);
                     fireFailOrError(SUITETEARDOWN, e);
                 }
             }
@@ -395,14 +432,14 @@
             while (t != null && t instanceof BuildException) {
                 if (t instanceof AssertionFailedException) {
                     failed = true;
-                    fireFail(name, (AssertionFailedException) t);
+                    env.fireFail(name, (AssertionFailedException) t);
                     break;
                 }
                 t = ((BuildException) t).getCause();
             }
 
             if (!failed) {
-                fireError(name, e);
+                env.fireError(name, e);
             }
         }
 

Added: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitExecutionPlatform.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitExecutionPlatform.java?rev=738222&view=auto
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitExecutionPlatform.java (added)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitExecutionPlatform.java Tue Jan 27 20:18:31 2009
@@ -0,0 +1,65 @@
+/*
+ * 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;
+
+import java.io.File;
+
+import org.apache.tools.ant.Project;
+
+/** 
+ * Provides methods that allow the AntUnitScriptRunner to interact with the environment
+ * in which it executes.
+ */
+public interface AntUnitExecutionPlatform {
+
+	/**
+	 * Creates a new project instance and configures it.
+	 * @param f the File for which to create a Project.
+	 */
+	public Project createProjectForFile(File f);
+
+	/**
+	 * invokes start on all registered test listeners.
+	 * @param targetName the name of the target.
+	 */
+	public void fireStartTest(String targetName);
+
+	/**
+	 * invokes addFailure on all registered test listeners.
+	 * @param targetName the name of the failed target.
+	 * @param ae the associated AssertionFailedException.
+	 */
+	public void fireFail(String targetName, AssertionFailedException ae);
+
+	/**
+	 * invokes addError on all registered test listeners.
+	 * @param targetName the name of the failed target.
+	 * @param t the associated Throwable.
+	 */
+	public void fireError(String targetName, Throwable t);
+
+	/**
+	 * invokes endTest on all registered test listeners.
+	 * @param targetName the name of the current target.
+	 */
+	public void fireEndTest(String targetName);
+
+}

Propchange: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitExecutionPlatform.java
------------------------------------------------------------------------------
    svn:eol-style = native