You are viewing a plain text version of this content. The canonical link for it is here.
Posted to surefire-commits@maven.apache.org by br...@apache.org on 2006/03/03 09:55:14 UTC

svn commit: r382729 - in /maven/surefire/branches/surefire-testng: surefire-api/src/main/java/org/apache/maven/surefire/ surefire-api/src/main/java/org/apache/maven/surefire/assertion/ surefire-api/src/main/java/org/apache/maven/surefire/suite/ surefir...

Author: brett
Date: Fri Mar  3 00:55:12 2006
New Revision: 382729

URL: http://svn.apache.org/viewcvs?rev=382729&view=rev
Log:
[MSUREFIRE-23] bring back assert helper and pojo testing

Added:
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/
      - copied from r382711, maven/surefire/trunk/surefire/src/main/java/org/apache/maven/surefire/battery/assertion/
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java   (with props)
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java   (with props)
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java   (with props)
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/PojoTestSet.java
      - copied, changed from r382711, maven/surefire/trunk/surefire/src/main/java/org/apache/maven/surefire/battery/AbstractBattery.java
Removed:
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/BatteryAssert.java
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/BatteryAssertionFailedError.java
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/BatteryComparisonFailure.java
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/BatteryTestFailedException.java
Modified:
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/Surefire.java
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/suite/AbstractDirectoryTestSuite.java
    maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/AbstractTestSet.java
    maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java
    maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
    maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
    maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java

Modified: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/Surefire.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/Surefire.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/Surefire.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/Surefire.java Fri Mar  3 00:55:12 2006
@@ -35,7 +35,9 @@
  */
 public class Surefire
 {
-    private ResourceBundle bundle = ResourceBundle.getBundle( "org.apache.maven.surefire.surefire" );
+    private ResourceBundle bundle = ResourceBundle.getBundle( SUREFIRE_BUNDLE_NAME );
+
+    public static final String SUREFIRE_BUNDLE_NAME = "org.apache.maven.surefire.surefire";
 
     public boolean run( List reportDefinitions, Object[] testSuiteDefinition, String testSetName,
                         ClassLoader surefireClassLoader, ClassLoader testsClassLoader )

Added: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java?rev=382729&view=auto
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java (added)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java Fri Mar  3 00:55:12 2006
@@ -0,0 +1,318 @@
+package org.apache.maven.surefire.assertion;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class SurefireAssert
+{
+    private SurefireAssert()
+    {
+        // No touchy.
+    }
+
+    public static void fail()
+    {
+        throw new SurefireAssertionFailedException();
+    }
+
+    public static void fail( String message )
+    {
+        if ( message == null )
+        {
+            throw new NullPointerException( "message is null" );
+        }
+        throw new SurefireAssertionFailedException( message );
+    }
+
+    public static void fail( String message, Throwable cause )
+    {
+
+        if ( message == null )
+        {
+            throw new NullPointerException( "message is null" );
+        }
+
+        if ( cause == null )
+        {
+            throw new NullPointerException( "cause is null" );
+        }
+
+        throw new SurefireAssertionFailedException( message, cause );
+    }
+
+    public static void fail( Throwable cause )
+    {
+        if ( cause == null )
+        {
+            throw new NullPointerException( "cause is null" );
+        }
+
+        throw new SurefireAssertionFailedException( cause );
+    }
+
+    // ----------------------------------------------------------------------
+    // JUnit type assertions
+    // ----------------------------------------------------------------------
+
+    public static void assertTrue( String message, boolean condition )
+    {
+        if ( !condition )
+        {
+            fail( message );
+        }
+    }
+
+    public static void assertTrue( boolean condition )
+    {
+        assertTrue( null, condition );
+    }
+
+    public static void assertFalse( String message, boolean condition )
+    {
+        assertTrue( message, !condition );
+    }
+
+    public static void assertFalse( boolean condition )
+    {
+        assertFalse( null, condition );
+    }
+
+    public static void assertEquals( String message, Object expected, Object actual )
+    {
+        if ( expected != null || actual != null )
+        {
+            if ( expected == null || !expected.equals( actual ) )
+            {
+                failNotEquals( message, expected, actual );
+            }
+        }
+    }
+
+    public static void assertEquals( Object expected, Object actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, String expected, String actual )
+    {
+        if ( expected != null || actual != null )
+        {
+            if ( expected == null || !expected.equals( actual ) )
+            {
+                throw new SurefireComparisonFailureException( message, expected, actual );
+            }
+        }
+    }
+
+    public static void assertEquals( String expected, String actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, double expected, double actual, double delta )
+    {
+        // handle infinity specially since subtracting to infinite values gives NaN and the
+        // the following test fails
+        if ( Double.isInfinite( expected ) )
+        {
+            if ( !Double.isInfinite( actual ) )
+            {
+                failNotEquals( message, new Double( expected ), new Double( actual ) );
+            }
+        }
+        else if ( !( Math.abs( expected - actual ) <= delta ) ) // Because comparison with NaN always returns false
+        {
+            failNotEquals( message, new Double( expected ), new Double( actual ) );
+        }
+    }
+
+    public static void assertEquals( double expected, double actual, double delta )
+    {
+        assertEquals( null, expected, actual, delta );
+    }
+
+    public static void assertEquals( String message, float expected, float actual, float delta )
+    {
+        // handle infinity specially since subtracting to infinite values gives NaN and the
+        // the following test fails
+        if ( Float.isInfinite( expected ) )
+        {
+            if ( !Float.isInfinite( actual ) )
+            {
+                failNotEquals( message, new Float( expected ), new Float( actual ) );
+            }
+
+        }
+        else if ( !( Math.abs( expected - actual ) <= delta ) )
+        {
+            failNotEquals( message, new Float( expected ), new Float( actual ) );
+        }
+    }
+
+    public static void assertEquals( float expected, float actual, float delta )
+    {
+        assertEquals( null, expected, actual, delta );
+    }
+
+    public static void assertEquals( String message, long expected, long actual )
+    {
+        assertEquals( message, new Long( expected ), new Long( actual ) );
+    }
+
+    public static void assertEquals( long expected, long actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, boolean expected, boolean actual )
+    {
+        assertEquals( message, expected ? Boolean.TRUE : Boolean.FALSE, actual ? Boolean.TRUE : Boolean.FALSE );
+    }
+
+    public static void assertEquals( boolean expected, boolean actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, byte expected, byte actual )
+    {
+        assertEquals( message, new Byte( expected ), new Byte( actual ) );
+    }
+
+    public static void assertEquals( byte expected, byte actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, char expected, char actual )
+    {
+        assertEquals( message, new Character( expected ), new Character( actual ) );
+    }
+
+    public static void assertEquals( char expected, char actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, short expected, short actual )
+    {
+        assertEquals( message, new Short( expected ), new Short( actual ) );
+    }
+
+    public static void assertEquals( short expected, short actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertEquals( String message, int expected, int actual )
+    {
+        assertEquals( message, new Integer( expected ), new Integer( actual ) );
+    }
+
+    public static void assertEquals( int expected, int actual )
+    {
+        assertEquals( null, expected, actual );
+    }
+
+    public static void assertNotNull( Object object )
+    {
+        assertNotNull( null, object );
+    }
+
+    public static void assertNotNull( String message, Object object )
+    {
+        assertTrue( message, object != null );
+    }
+
+    public static void assertNull( Object object )
+    {
+        assertNull( null, object );
+    }
+
+    public static void assertNull( String message, Object object )
+    {
+        assertTrue( message, object == null );
+    }
+
+    public static void assertSame( String message, Object expected, Object actual )
+    {
+        //noinspection ObjectEquality
+        if ( expected != actual )
+        {
+            failNotSame( message, expected, actual );
+        }
+    }
+
+    public static void assertSame( Object expected, Object actual )
+    {
+        assertSame( null, expected, actual );
+    }
+
+    public static void assertNotSame( String message, Object expected, Object actual )
+    {
+        //noinspection ObjectEquality
+        if ( expected == actual )
+        {
+            failSame( message );
+        }
+    }
+
+    public static void assertNotSame( Object expected, Object actual )
+    {
+        assertNotSame( null, expected, actual );
+    }
+
+    private static void failSame( String message )
+    {
+        String formatted = "";
+
+        if ( message != null )
+        {
+            formatted = message + " ";
+        }
+
+        fail( formatted + "expected not same" );
+    }
+
+    private static void failNotSame( String message, Object expected, Object actual )
+    {
+        String formatted = "";
+
+        if ( message != null )
+        {
+            formatted = message + " ";
+        }
+
+        fail( formatted + "expected same:<" + expected + "> was not:<" + actual + ">" );
+    }
+
+    private static void failNotEquals( String message, Object expected, Object actual )
+    {
+        fail( formatMismatch( message, expected, actual ) );
+    }
+
+    static String formatMismatch( String message, Object expected, Object actual )
+    {
+        String formatted = "";
+
+        if ( message != null )
+        {
+            formatted = message + " ";
+        }
+
+        return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
+    }
+}

Propchange: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssert.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java?rev=382729&view=auto
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java (added)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java Fri Mar  3 00:55:12 2006
@@ -0,0 +1,43 @@
+package org.apache.maven.surefire.assertion;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * @noinspection UncheckedExceptionClass
+ */
+public class SurefireAssertionFailedException
+    extends RuntimeException
+{
+    public SurefireAssertionFailedException()
+    {
+    }
+
+    public SurefireAssertionFailedException( String message )
+    {
+        super( message );
+    }
+
+    public SurefireAssertionFailedException( Throwable cause )
+    {
+        super( cause );
+    }
+
+    public SurefireAssertionFailedException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+}
\ No newline at end of file

Propchange: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireAssertionFailedException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java?rev=382729&view=auto
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java (added)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java Fri Mar  3 00:55:12 2006
@@ -0,0 +1,114 @@
+package org.apache.maven.surefire.assertion;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * Thrown when an assertion equals for Strings failed.
+ * <p/>
+ * Inspired by a patch from Alex Chaffee mailto:alex@purpletech.com
+ *
+ * @noinspection UncheckedExceptionClass
+ */
+public class SurefireComparisonFailureException
+    extends SurefireAssertionFailedException
+{
+    private final String expected;
+
+    private final String actual;
+
+    /**
+     * Constructs a comparison failure.
+     *
+     * @param message  the identifying message or null
+     * @param expected the expected string value
+     * @param actual   the actual string value
+     */
+    public SurefireComparisonFailureException( String message, String expected, String actual )
+    {
+        super( message );
+        this.expected = expected;
+        this.actual = actual;
+    }
+
+    /**
+     * Returns "..." in place of common prefix and "..." in
+     * place of common suffix between expected and actual.
+     *
+     * @see java.lang.Throwable#getMessage()
+     */
+    public String getMessage()
+    {
+        String path;
+        if ( expected == null || actual == null )
+        {
+            path = SurefireAssert.formatMismatch( super.getMessage(), expected, actual );
+        }
+        else
+        {
+            int end = Math.min( expected.length(), actual.length() );
+
+            int i = 0;
+            for ( ; i < end; i++ )
+            {
+                if ( expected.charAt( i ) != actual.charAt( i ) )
+                {
+                    break;
+                }
+            }
+            int j = expected.length() - 1;
+            int k = actual.length() - 1;
+            for ( ; k >= i && j >= i; k--, j-- )
+            {
+                if ( expected.charAt( j ) != actual.charAt( k ) )
+                {
+                    break;
+                }
+            }
+
+            String actual;
+            String expected;
+
+            // equal strings
+            if ( j < i && k < i )
+            {
+                expected = this.expected;
+                actual = this.actual;
+            }
+            else
+            {
+                expected = this.expected.substring( i, j + 1 );
+                actual = this.actual.substring( i, k + 1 );
+                if ( i <= end && i > 0 )
+                {
+                    expected = "..." + expected;
+                    actual = "..." + actual;
+                }
+
+                if ( j < this.expected.length() - 1 )
+                {
+                    expected = expected + "...";
+                }
+                if ( k < this.actual.length() - 1 )
+                {
+                    actual = actual + "...";
+                }
+            }
+            path = SurefireAssert.formatMismatch( super.getMessage(), expected, actual );
+        }
+        return path;
+    }
+}

Propchange: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/assertion/SurefireComparisonFailureException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/suite/AbstractDirectoryTestSuite.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/suite/AbstractDirectoryTestSuite.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/suite/AbstractDirectoryTestSuite.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/suite/AbstractDirectoryTestSuite.java Fri Mar  3 00:55:12 2006
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+import org.apache.maven.surefire.Surefire;
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.ReporterException;
 import org.apache.maven.surefire.report.ReporterManager;
@@ -37,8 +38,7 @@
 public abstract class AbstractDirectoryTestSuite
     implements SurefireTestSuite
 {
-    // TODO: fix this
-    private ResourceBundle bundle = ResourceBundle.getBundle( "org.apache.maven.surefire.surefire" );
+    private ResourceBundle bundle = ResourceBundle.getBundle( Surefire.SUREFIRE_BUNDLE_NAME );
 
     private static final String FS = System.getProperty( "file.separator" );
 
@@ -90,7 +90,7 @@
 
             if ( !Modifier.isAbstract( testClass.getModifiers() ) )
             {
-                SurefireTestSet testSet = createTestSet( testClass );
+                SurefireTestSet testSet = createTestSet( testClass, classLoader );
 
                 if ( testSets.containsKey( testSet.getName() ) )
                 {
@@ -105,7 +105,7 @@
         return Collections.unmodifiableMap( testSets );
     }
 
-    protected abstract SurefireTestSet createTestSet( Class testClass )
+    protected abstract SurefireTestSet createTestSet( Class testClass, ClassLoader classLoader )
         throws TestSetFailedException;
 
     public void execute( ReporterManager reporterManager, ClassLoader classLoader )

Modified: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/AbstractTestSet.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/AbstractTestSet.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/AbstractTestSet.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/AbstractTestSet.java Fri Mar  3 00:55:12 2006
@@ -22,7 +22,7 @@
 import java.util.List;
 
 /**
- * @todo bring back other helpers and put in a separate package
+ * Base test class.
  */
 public abstract class AbstractTestSet
     implements SurefireTestSet
@@ -31,9 +31,21 @@
 
     protected List testMethods;
 
+    private Class testClass;
+
+    protected AbstractTestSet( Class testClass )
+    {
+        if ( testClass == null )
+        {
+            throw new NullPointerException( "testClass is null" );
+        }
+
+        this.testClass = testClass;
+    }
+
     public String getName()
     {
-        return getTestClass().getName();
+        return testClass.getName();
     }
 
     public int getTestCount()
@@ -46,7 +58,7 @@
 
     protected Class getTestClass()
     {
-        return getClass();
+        return testClass;
     }
 
     protected void discoverTestMethods()
@@ -55,7 +67,7 @@
         {
             testMethods = new ArrayList();
 
-            Method[] methods = getTestClass().getMethods();
+            Method[] methods = testClass.getMethods();
 
             for ( int i = 0; i < methods.length; ++i )
             {

Copied: maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/PojoTestSet.java (from r382711, maven/surefire/trunk/surefire/src/main/java/org/apache/maven/surefire/battery/AbstractBattery.java)
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/PojoTestSet.java?p2=maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/PojoTestSet.java&p1=maven/surefire/trunk/surefire/src/main/java/org/apache/maven/surefire/battery/AbstractBattery.java&r1=382711&r2=382729&rev=382729&view=diff
==============================================================================
--- maven/surefire/trunk/surefire/src/main/java/org/apache/maven/surefire/battery/AbstractBattery.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-api/src/main/java/org/apache/maven/surefire/testset/PojoTestSet.java Fri Mar  3 00:55:12 2006
@@ -1,7 +1,7 @@
-package org.apache.maven.surefire.battery;
+package org.apache.maven.surefire.testset;
 
 /*
- * Copyright 2001-2005 The Codehaus.
+ * Copyright 2001-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,29 +17,44 @@
  */
 
 import org.apache.maven.surefire.Surefire;
-import org.apache.maven.surefire.battery.assertion.BatteryAssert;
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.ReporterManager;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.ResourceBundle;
 
-public abstract class AbstractBattery
-    extends BatteryAssert
-    implements Battery
+public class PojoTestSet
+    extends AbstractTestSet
 {
-    private static final String TEST_METHOD_PREFIX = "test";
+    private ResourceBundle bundle = ResourceBundle.getBundle( Surefire.SUREFIRE_BUNDLE_NAME );
 
-    private List testMethods;
+    private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
 
-    private List subBatteryClassNames;
+    private Object testObject;
 
-    public void execute( ReporterManager reportManager )
-        throws Exception
+    public PojoTestSet( Class testClass )
+        throws TestSetFailedException
+    {
+        super( testClass );
+
+        try
+        {
+            testObject = testClass.newInstance();
+        }
+        catch ( InstantiationException e )
+        {
+            throw new TestSetFailedException( "Unable to instantiate POJO '" + testClass + "'", e );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new TestSetFailedException( "Unable to instantiate POJO '" + testClass + "'", e );
+        }
+    }
+
+    public void execute( ReporterManager reportManager, ClassLoader loader )
+        throws TestSetFailedException
     {
         if ( reportManager == null )
         {
@@ -61,16 +76,17 @@
             discoverTestMethods();
         }
 
-        Object[] args = new Object[0];
-
         boolean abort = false;
 
-        for ( int i = 0; ( i < testMethods.size() ) && !abort; ++i )
+        for ( int i = 0; i < testMethods.size() && !abort; ++i )
         {
-            abort = executeTestMethod( (Method) testMethods.get( i ), args, reportManager );
+            abort = executeTestMethod( (Method) testMethods.get( i ), EMPTY_OBJECT_ARRAY, reportManager );
         }
     }
 
+    /**
+     * @noinspection CatchGenericClass,OverlyBroadCatchBlock,MethodWithMultipleReturnPoints
+     */
     protected boolean executeTestMethod( Method method, Object[] args, ReporterManager reportManager )
     {
         if ( method == null || args == null || reportManager == null )
@@ -87,7 +103,7 @@
 
         userFriendlyMethodName += ')';
 
-        ReportEntry report = new ReportEntry( this, getTestName( userFriendlyMethodName ), getClass().getName() );
+        ReportEntry report = new ReportEntry( testObject, getTestName( userFriendlyMethodName ), getName() );
 
         reportManager.testStarting( report );
 
@@ -98,7 +114,7 @@
         catch ( Exception e )
         {
             // Treat any exception from setUpFixture as a failure of the test.
-            String rawString = Surefire.getResources().getString( "setupFixtureFailed" );
+            String rawString = bundle.getString( "setupFixtureFailed" );
 
             MessageFormat msgFmt = new MessageFormat( rawString );
 
@@ -106,7 +122,7 @@
 
             String stringToPrint = msgFmt.format( stringArgs );
 
-            report = new ReportEntry( this, getTestName( userFriendlyMethodName ), stringToPrint, e );
+            report = new ReportEntry( testObject, getTestName( userFriendlyMethodName ), stringToPrint, e );
 
             reportManager.testFailed( report );
 
@@ -121,9 +137,9 @@
         // Make sure that tearDownFixture
         try
         {
-            method.invoke( getTestClassInstance(), args );
+            method.invoke( testObject, args );
 
-            report = new ReportEntry( this, getTestName( userFriendlyMethodName ), this.getClass().getName() );
+            report = new ReportEntry( testObject, getTestName( userFriendlyMethodName ), getName() );
 
             reportManager.testSucceeded( report );
         }
@@ -138,22 +154,22 @@
                 msg = t.toString();
             }
 
-            report = new ReportEntry( this, getTestName( userFriendlyMethodName ), msg, t );
+            report = new ReportEntry( testObject, getTestName( userFriendlyMethodName ), msg, t );
 
             reportManager.testFailed( report );
             // Don't return  here, because tearDownFixture should be called even
             // if the test method throws an exception.
         }
-        catch ( Exception e )
+        catch ( Throwable t )
         {
-            String msg = e.getMessage();
+            String msg = t.getMessage();
 
             if ( msg == null )
             {
-                msg = e.toString();
+                msg = t.toString();
             }
 
-            report = new ReportEntry( this, getTestName( userFriendlyMethodName ), msg, e );
+            report = new ReportEntry( testObject, getTestName( userFriendlyMethodName ), msg, t );
 
             reportManager.testFailed( report );
             // Don't return  here, because tearDownFixture should be called even
@@ -164,11 +180,10 @@
         {
             tearDownFixture();
         }
-        catch ( Exception e )
+        catch ( Throwable t )
         {
-
             // Treat any exception from tearDownFixture as a failure of the test.
-            String rawString = Surefire.getResources().getString( "cleanupFixtureFailed" );
+            String rawString = bundle.getString( "cleanupFixtureFailed" );
 
             MessageFormat msgFmt = new MessageFormat( rawString );
 
@@ -176,13 +191,7 @@
 
             String stringToPrint = msgFmt.format( stringArgs );
 
-            String msg = e.getMessage();
-            if ( msg == null )
-            {
-                msg = e.toString();
-            }
-
-            report = new ReportEntry( this, getTestName( userFriendlyMethodName ), stringToPrint, e );
+            report = new ReportEntry( testObject, getTestName( userFriendlyMethodName ), stringToPrint, t );
 
             reportManager.testFailed( report );
 
@@ -202,11 +211,6 @@
         return false;
     }
 
-    public String getBatteryName()
-    {
-        return getClass().getName();
-    }
-
     public String getTestName( String testMethodName )
     {
         if ( testMethodName == null )
@@ -214,103 +218,14 @@
             throw new NullPointerException( "testMethodName is null" );
         }
 
-        return getClass() + "." + testMethodName;
-    }
-
-    public int getTestCount()
-    {
-        discoverTestMethods();
-
-        return testMethods.size();
+        return getTestClass() + "." + testMethodName;
     }
 
     public void setUpFixture()
-        throws Exception
     {
     }
 
     public void tearDownFixture()
-        throws Exception
-    {
-    }
-
-    protected Class getTestClass()
-    {
-        return getClass();
-    }
-
-    protected Object getTestClassInstance()
     {
-        return this;
-    }
-
-    protected void discoverTestMethods()
-    {
-        if ( testMethods != null )
-        {
-            return;
-        }
-
-        testMethods = new ArrayList();
-
-        Method[] methods = getTestClass().getMethods();
-
-        for ( int i = 0; i < methods.length; ++i )
-        {
-            Method m = methods[i];
-
-            Class[] paramTypes = m.getParameterTypes();
-
-            boolean isInstanceMethod = !Modifier.isStatic( m.getModifiers() );
-
-            boolean returnsVoid = m.getReturnType() == void.class;
-
-            boolean hasNoParams = paramTypes.length == 0;
-
-            if ( isInstanceMethod && returnsVoid && hasNoParams )
-            {
-                String simpleName = m.getName();
-
-                if ( simpleName.length() <= 4 )
-                {
-                    // name must have 5 or more chars
-                    continue;
-                }
-
-                String firstFour = simpleName.substring( 0, 4 );
-
-                if ( !firstFour.equals( TEST_METHOD_PREFIX ) )
-                {
-                    // name must start with "test"
-                    continue;
-                }
-
-                testMethods.add( m );
-            }
-        }
-    }
-
-    // ----------------------------------------------------------------------
-    // Batteries
-    // ----------------------------------------------------------------------
-
-    public void discoverBatteryClassNames()
-        throws Exception
-    {
-    }
-
-    public void addSubBatteryClassName( String batteryClassName )
-    {
-        getSubBatteryClassNames().add( batteryClassName );
-    }
-
-    public List getSubBatteryClassNames()
-    {
-        if ( subBatteryClassNames == null )
-        {
-            subBatteryClassNames = new ArrayList();
-        }
-
-        return subBatteryClassNames;
     }
 }

Modified: maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java Fri Mar  3 00:55:12 2006
@@ -16,7 +16,9 @@
  * limitations under the License.
  */
 
+import junit.framework.TestCase;
 import org.apache.maven.surefire.suite.AbstractDirectoryTestSuite;
+import org.apache.maven.surefire.testset.PojoTestSet;
 import org.apache.maven.surefire.testset.SurefireTestSet;
 import org.apache.maven.surefire.testset.TestSetFailedException;
 
@@ -36,9 +38,28 @@
         super( basedir, includes, excludes );
     }
 
-    protected SurefireTestSet createTestSet( Class testClass )
+    protected SurefireTestSet createTestSet( Class testClass, ClassLoader classLoader )
         throws TestSetFailedException
     {
-        return new JUnitTestSet( testClass );
+        Class junitClass = null;
+        try
+        {
+            junitClass = classLoader.loadClass( TestCase.class.getName() );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            // ignore this
+        }
+
+        SurefireTestSet testSet;
+        if ( junitClass != null && junitClass.isAssignableFrom( testClass ) )
+        {
+            testSet = new JUnitTestSet( testClass );
+        }
+        else
+        {
+            testSet = new PojoTestSet( testClass );
+        }
+        return testSet;
     }
 }

Modified: maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java Fri Mar  3 00:55:12 2006
@@ -56,8 +56,6 @@
 
     private Class testResultClass;
 
-    private Class testClass;
-
     private Method addListenerMethod;
 
     private Method countTestCasesMethod;
@@ -71,21 +69,11 @@
     public JUnitTestSet( Class testClass )
         throws TestSetFailedException
     {
-        if ( testClass == null )
-        {
-            throw new NullPointerException( "testClass is null" );
-        }
-
-        this.testClass = testClass;
+        super( testClass );
 
         processTestClass( testClass.getClassLoader() );
     }
 
-    public Class getTestClass()
-    {
-        return testClass;
-    }
-
     private void processTestClass( ClassLoader loader )
         throws TestSetFailedException
     {
@@ -114,6 +102,7 @@
 
             Object testObject = createInstanceFromSuiteMethod();
 
+            Class testClass = getTestClass();
             if ( testObject == null && testCaseClass.isAssignableFrom( testClass ) )
             {
                 Class[] constructorParamTypes = {Class.class};
@@ -192,7 +181,7 @@
         Object testObject = null;
         try
         {
-            Method suiteMethod = testClass.getMethod( "suite", EMPTY_CLASS_ARRAY );
+            Method suiteMethod = getTestClass().getMethod( "suite", EMPTY_CLASS_ARRAY );
 
             if ( Modifier.isPublic( suiteMethod.getModifiers() ) && Modifier.isStatic( suiteMethod.getModifiers() ) )
             {

Modified: maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java Fri Mar  3 00:55:12 2006
@@ -38,7 +38,7 @@
         // TODO
     }
 
-    protected SurefireTestSet createTestSet( Class testClass )
+    protected SurefireTestSet createTestSet( Class testClass, ClassLoader classLoader )
     {
         return new TestNGTestSet( testClass );
     }

Modified: maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java
URL: http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java?rev=382729&r1=382728&r2=382729&view=diff
==============================================================================
--- maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java (original)
+++ maven/surefire/branches/surefire-testng/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java Fri Mar  3 00:55:12 2006
@@ -42,8 +42,6 @@
 {
     private String testSourceDirectory;
 
-    private Class testClass;
-
     private static IAnnotationFinder annotationFinder;
 
     /**
@@ -52,24 +50,20 @@
      */
     public TestNGTestSet( Class testClass )
     {
-        this.testClass = testClass;
+        super( testClass );
 
 /*
         this.testSourceDirectory = testSourceDirectory;
 */
     }
 
-    public Class getTestClass()
-    {
-        return testClass;
-    }
-
     protected void discoverTestMethods()
     {
         if ( testMethods == null )
         {
             testMethods = new ArrayList();
 
+            Class testClass = getTestClass();
             Method[] methods = testClass.getMethods();
 
             for ( int i = 0; i < methods.length; ++i )
@@ -97,11 +91,11 @@
 
         TestNG testNG = new TestNG();
         List classes = new ArrayList();
-        classes.add( testClass );
+        classes.add( getTestClass() );
 
         String groups = null;
 
-        if ( !TestNGClassFinder.isTestNGClass( testClass, getAnnotationFinder() ) )
+        if ( !TestNGClassFinder.isTestNGClass( getTestClass(), getAnnotationFinder() ) )
         {
 //            testNG.setJUnit( Boolean.TRUE );
         }