evenisse    2004/01/14 07:56:44

  Added:       surefire-runner .cvsignore project.xml
               surefire-runner/src/main/org/apache/maven/test
                        MavenJUnitTestSuite.java MavenTestSuiteLoader.java
                        ResultPrinter.java TestRunner.java
  Log:
  Move from surefire maven plugin
  
  Revision  Changes    Path
  1.1                  maven-components/surefire-runner/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  target
  velocity.log
  maven.log
  .classpath
  .project
  *.ipr
  *.iws
  
  
  1.1                  maven-components/surefire-runner/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <project>
    <pomVersion>3</pomVersion>
    <groupId>maven</groupId>
    <id>surefire-runner</id>
    <artifactId>surefire-runner</artifactId>
    <name>Surefire Test Runner</name>
    <currentVersion>1.0</currentVersion>
    <description/>
    <shortDescription>Run JUnit tests</shortDescription>
  
    <repository>
      <connection>scm:cvs:pserver:[EMAIL 
PROTECTED]:/home/cvspublic:maven-components/surefire-runner</connection>
      <url>http://cvs.apache.org/viewcvs/maven-components/surefire-runner</url>
    </repository>
    <developers>
    </developers>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
      </dependency>
    </dependencies>
    <build>
      <sourceDirectory>src/main</sourceDirectory>
    </build>
  </project>
  
  
  
  1.1                  
maven-components/surefire-runner/src/main/org/apache/maven/test/MavenJUnitTestSuite.java
  
  Index: MavenJUnitTestSuite.java
  ===================================================================
  package org.apache.maven.test;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestResult;
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import java.lang.reflect.Modifier;
  import java.util.Enumeration;
  import java.util.Vector;
  
  /**
   * A <code>TestSuite</code> is a <code>Composite</code> of Tests.
   * It runs a collection of test cases. Here is an example using
   * the dynamic test definition.
   * <pre>
   * TestSuite suite= new TestSuite();
   * suite.addTest(new MathTest("testAdd"));
   * suite.addTest(new MathTest("testDivideByZero"));
   * </pre>
   * Alternatively, a TestSuite can extract the tests to be run automatically.
   * To do so you pass the class of your TestCase class to the
   * TestSuite constructor.
   * <pre>
   * TestSuite suite= new TestSuite(MathTest.class);
   * </pre>
   * This constructor creates a suite with all the methods
   * starting with "test" that take no arguments.
   *
   * @see junit.framework.Test
   */
  public class MavenJUnitTestSuite
      implements Test
  {
      private Vector fTests = new Vector( 10 );
  
      private String fName;
  
      private Class junitTest = null;
  
      private Class testResultClass = null;
  
      /**
       * Constructs an empty TestSuite.
       */
      public MavenJUnitTestSuite()
      {
      }
  
      /**
       * Constructs a TestSuite from the given class with the given name.
       */
      public MavenJUnitTestSuite( Class theClass )
      {
          this( theClass, ClassLoader.getSystemClassLoader() );
      }
  
      /**
       * Constructs a TestSuite from the given class. Adds all the methods
       * starting with "test" as test cases to the suite.
       * Parts of this method was written at 2337 meters in the H�ffih�tte,
       * Kanton Uri
       */
      public MavenJUnitTestSuite( final Class theClass, ClassLoader classLoader )
      {
          try
          {
              junitTest = classLoader.loadClass( "junit.framework.Test" );
  
              testResultClass = classLoader.loadClass( "junit.framework.TestResult" );
          }
          catch ( ClassNotFoundException e )
          {
          }
  
          fName = theClass.getName();
          try
          {
              getTestConstructor( theClass ); // Avoid generating multiple error 
messages
          }
          catch ( NoSuchMethodException e )
          {
              addTest( warning( "Class " + theClass.getName() + " has no public 
constructor TestCase(String name) or TestCase()" ) );
              return;
          }
  
          if ( !Modifier.isPublic( theClass.getModifiers() ) )
          {
              addTest( warning( "Class " + theClass.getName() + " is not public" ) );
              return;
          }
  
          Class superClass = theClass;
          Vector names = new Vector();
  
          while ( junitTest.isAssignableFrom( superClass ) )
          {
              Method[] methods = superClass.getDeclaredMethods();
              for ( int i = 0; i < methods.length; i++ )
              {
                  addTestMethod( methods[i], names, theClass );
              }
              superClass = superClass.getSuperclass();
          }
  
          if ( fTests.size() == 0 )
          {
              addTest( warning( "No tests found in " + theClass.getName() ) );
          }
      }
  
      /**
       * Constructs an empty TestSuite.
       */
      public MavenJUnitTestSuite( String name )
      {
          setName( name );
      }
  
      /**
       * Adds a test to the suite.
       */
      public void addTest( Object test )
      {
          fTests.addElement( test );
      }
  
      /**
       * Adds the tests from the given class to the suite
       */
      public void addTestSuite( Class testClass )
      {
          addTest( new MavenJUnitTestSuite( testClass  ) );
      }
  
      private void addTestMethod( Method m, Vector names, Class theClass )
      {
          String name = m.getName();
  
          if ( names.contains( name ) )
          {
              return;
          }
  
          if ( !isPublicTestMethod( m ) )
          {
              if ( isTestMethod( m ) )
              {
                  addTest( warning( "Test method isn't public: " + m.getName() ) );
              }
              return;
          }
  
          names.addElement( name );
  
          addTest( createTest( theClass, name ) );
      }
  
      /**
       * ...as the moon sets over the early morning Merlin, Oregon
       * mountains, our intrepid adventurers type...
       */
      static public Object createTest( Class theClass, String name )
      {
          Constructor constructor;
  
          try
          {
              constructor = getTestConstructor( theClass );
          }
          catch ( NoSuchMethodException e )
          {
              return warning( "Class " + theClass.getName() + " has no public 
constructor TestCase(String name) or TestCase()" );
          }
          Object test;
          try
          {
              if ( constructor.getParameterTypes().length == 0 )
              {
                  test = constructor.newInstance( new Object[0] );
  
                  if ( test instanceof TestCase )
                  {
                      ( (TestCase) test ).setName( name );
                  }
              }
              else
              {
                  test = constructor.newInstance( new Object[]{name} );
              }
          }
          catch ( InstantiationException e )
          {
              return ( warning( "Cannot instantiate test case: " + name + " (" + 
exceptionToString( e ) + ")" ) );
          }
          catch ( InvocationTargetException e )
          {
              return ( warning( "Exception in constructor: " + name + " (" + 
exceptionToString( e.getTargetException() ) + ")" ) );
          }
          catch ( IllegalAccessException e )
          {
              return ( warning( "Cannot access test case: " + name + " (" + 
exceptionToString( e ) + ")" ) );
          }
  
          return test;
      }
  
      /**
       * Converts the stack trace into a string
       */
      private static String exceptionToString( Throwable t )
      {
          StringWriter stringWriter = new StringWriter();
          PrintWriter writer = new PrintWriter( stringWriter );
          t.printStackTrace( writer );
          return stringWriter.toString();
  
      }
  
      /**
       * Counts the number of test cases that will be run by this test.
       */
      public int countTestCases()
      {
          int count = 0;
          for ( Enumeration e = tests(); e.hasMoreElements(); )
          {
              Test test = (Test) e.nextElement();
              count = count + test.countTestCases();
          }
          return count;
      }
  
      /**
       * Gets a constructor which takes a single String as
       * its argument or a no arg constructor.
       */
      public static Constructor getTestConstructor( Class theClass )
          throws NoSuchMethodException
      {
          Class[] args = {String.class};
  
          try
          {
              return theClass.getConstructor( args );
          }
          catch ( NoSuchMethodException e )
          {
              // fall through
          }
          return theClass.getConstructor( new Class[0] );
      }
  
      private boolean isPublicTestMethod( Method m )
      {
          return isTestMethod( m ) && Modifier.isPublic( m.getModifiers() );
      }
  
      private boolean isTestMethod( Method m )
      {
          String name = m.getName();
          Class[] parameters = m.getParameterTypes();
          Class returnType = m.getReturnType();
          return parameters.length == 0 && name.startsWith( "test" ) && 
returnType.equals( Void.TYPE );
      }
  
      /**
       * Runs the tests and collects their result in a TestResult.
       */
      public void run( TestResult result )
      {
          for ( Enumeration e = tests(); e.hasMoreElements(); )
          {
              if ( result.shouldStop() )
              {
                  break;
              }
  
              Object test = e.nextElement();
  
              runTest( test, result );
          }
      }
  
      public void runTest( Object test, TestResult result )
      {
          try
          {
              Method m = test.getClass().getMethod( "run", new Class[] { 
testResultClass } );
  
              m.invoke( test, new Object[] { result } );
          }
          catch ( Exception e )
          {
              e.printStackTrace();
          }
      }
  
      /**
       * Returns the test at the given index
       */
      public Test testAt( int index )
      {
          return (Test) fTests.elementAt( index );
      }
  
      /**
       * Returns the number of tests in this suite
       */
      public int testCount()
      {
          return fTests.size();
      }
  
      /**
       * Returns the tests as an enumeration
       */
      public Enumeration tests()
      {
          return fTests.elements();
      }
  
      /**
       */
      public String toString()
      {
          if ( getName() != null )
              return getName();
          return super.toString();
      }
  
      /**
       * Sets the name of the suite.
       * @param name The name to set
       */
      public void setName( String name )
      {
          fName = name;
      }
  
      /**
       * Returns the name of the suite. Not all
       * test suites have a name and this method
       * can return null.
       */
      public String getName()
      {
          return fName;
      }
  
      /**
       * Returns a test which will fail and log a warning message.
       */
      private static Test warning( final String message )
      {
          return new TestCase( "warning" )
          {
              protected void runTest()
              {
                  fail( message );
              }
          };
      }
  }
  
  
  
  1.1                  
maven-components/surefire-runner/src/main/org/apache/maven/test/MavenTestSuiteLoader.java
  
  Index: MavenTestSuiteLoader.java
  ===================================================================
  package org.apache.maven.test;
  
  import junit.runner.TestSuiteLoader;
  
  /**
   *
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
   *
   * @version $Id: MavenTestSuiteLoader.java,v 1.1 2004/01/14 15:56:44 evenisse Exp $
   */
  public class MavenTestSuiteLoader
      implements TestSuiteLoader
  {
      private ClassLoader classLoader;
  
      public MavenTestSuiteLoader( ClassLoader classLoader )
      {
          this.classLoader = classLoader;
      }
  
      public Class load( String className )
          throws ClassNotFoundException
      {
          return classLoader.loadClass( className );
      }
  
      public Class reload( Class clazz )
          throws ClassNotFoundException
      {
          return clazz;
      }
  }
  
  
  
  1.1                  
maven-components/surefire-runner/src/main/org/apache/maven/test/ResultPrinter.java
  
  Index: ResultPrinter.java
  ===================================================================
  package org.apache.maven.test;
  
  import java.io.PrintStream;
  import java.text.NumberFormat;
  import java.util.Enumeration;
  
  import junit.framework.AssertionFailedError;
  import junit.framework.Test;
  import junit.framework.TestFailure;
  import junit.framework.TestListener;
  import junit.framework.TestResult;
  import junit.runner.BaseTestRunner;
  
  public class ResultPrinter implements TestListener
  {
      PrintStream fWriter;
      int fColumn = 0;
  
      public ResultPrinter( PrintStream writer )
      {
          fWriter = writer;
      }
  
      synchronized void print( TestResult result, long runTime, String testName )
      {
          //printErrors( result );
          //printFailures( result );
          printFooter( result, runTime, testName );
      }
  
      protected void printErrors( TestResult result )
      {
          printDefects( result.errors(), result.errorCount(), "error" );
      }
  
      protected void printFailures( TestResult result )
      {
          printDefects( result.failures(), result.failureCount(), "failure" );
      }
  
      protected void printDefects( Enumeration booBoos, int count, String type )
      {
          if ( count == 0 )
          {
              return;
          }
          if ( count == 1 )
          {
              getWriter().println( "There was " + count + " " + type + ":" );
          }
          else
          {
              getWriter().println( "There were " + count + " " + type + "s:" );
          }
  
          for ( int i = 1; booBoos.hasMoreElements(); i++ )
          {
              printDefect( (TestFailure) booBoos.nextElement(), i );
          }
      }
  
      public void printDefect( TestFailure booBoo, int count )
      { // only public for testing purposes
          printDefectHeader( booBoo, count );
          printDefectTrace( booBoo );
      }
  
      protected void printDefectHeader( TestFailure booBoo, int count )
      {
          // I feel like making this a println, then adding a line giving the 
throwable a chance to print something
          // before we get to the stack trace.
          getWriter().print( count + ") " + booBoo.failedTest() );
      }
  
      protected void printDefectTrace( TestFailure booBoo )
      {
          getWriter().print( BaseTestRunner.getFilteredTrace( booBoo.trace() ) );
      }
  
      protected void printFooter( TestResult result, long runtime, String testName )
      {
          getWriter().println( "    [funit] Running " + testName );
  
          getWriter().println( "    [funit] Tests run: " + result.runCount() +
                               ", Failures: " + result.failureCount() +
                               ", Errors: " + result.errorCount() +
                               ", Time elapsed: " + elapsedTimeAsString( runtime ) + " 
sec" );
  
          printErrors( result );
          printFailures( result );
      }
  
      /**
       * Returns the formatted string of the elapsed time.
       * Duplicated from BaseTestRunner. Fix it.
       */
      protected String elapsedTimeAsString( long runTime )
      {
          return NumberFormat.getInstance().format( (double) runTime / 1000 );
      }
  
      public PrintStream getWriter()
      {
          return fWriter;
      }
  
      /**
       * @see junit.framework.TestListener#addError(Test, Throwable)
       */
      public void addError( Test test, Throwable t )
      {
          getWriter().print( "E" );
      }
  
      /**
       * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
       */
      public void addFailure( Test test, AssertionFailedError t )
      {
          getWriter().print( "F" );
      }
  
      /**
       * @see junit.framework.TestListener#endTest(Test)
       */
      public void endTest( Test test )
      {
      }
  
      /**
       * @see junit.framework.TestListener#startTest(Test)
       */
      public void startTest( Test test )
      {
          getWriter().print( "." );
          if ( fColumn++ >= 40 )
          {
              getWriter().println();
              fColumn = 0;
          }
      }
  
  }
  
  
  
  1.1                  
maven-components/surefire-runner/src/main/org/apache/maven/test/TestRunner.java
  
  Index: TestRunner.java
  ===================================================================
  package org.apache.maven.test;
  
  import junit.framework.Test;
  import junit.framework.TestResult;
  import junit.framework.TestSuite;
  import junit.runner.BaseTestRunner;
  import junit.runner.TestSuiteLoader;
  
  import java.io.PrintStream;
  
  public class TestRunner
      extends BaseTestRunner
  {
      private ResultPrinter resultPrinter;
  
      public static final int SUCCESS_EXIT = 0;
  
      public static final int FAILURE_EXIT = 1;
  
      public static final int EXCEPTION_EXIT = 2;
  
      private static final String FS = System.getProperty( "file.separator" );
  
      public TestRunner()
      {
          this( System.out );
      }
  
      public TestRunner( PrintStream writer )
      {
          this( new ResultPrinter( writer ) );
      }
  
      public TestRunner( ResultPrinter printer )
      {
          resultPrinter = printer;
      }
  
      private ClassLoader classLoader;
  
      public void runTestClasses( ClassLoader classLoader, String[] tests )
          throws Exception
      {
          this.classLoader = classLoader;
  
          String s;
  
          for ( int i = 0; i < tests.length; i++ )
          {
              s = tests[i];
  
              s = s.substring( 0, s.indexOf( "." ) );
  
              s = s.replace( FS.charAt( 0 ), ".".charAt( 0 ) );
  
              Class clazz = null;
  
              try
              {
                  clazz = classLoader.loadClass( s );
              }
              catch ( ClassNotFoundException e )
              {
                  System.out.println( "Can't find className = " + s );
  
                  continue;
              }
  
              TestResult result = run( clazz, s );
  
              if ( result.errorCount() > 0 || result.failureCount() > 0 )
              {
                  failures = "true";
              }
          }
      }
  
      private String failures = "false";
  
      public String failures()
      {
          return failures;
      }
  
      public TestResult run( Class testClass, String testName )
      {
          return run( new TestSuite( testClass ), testName );
  
          //return run( new MavenJUnitTestSuite( testClass, classLoader ), testName );
      }
  
      public TestResult run( Test test, String testName )
      {
          TestRunner runner = new TestRunner();
  
          return runner.doRun( test, testName );
      }
  
      public TestSuiteLoader getLoader()
      {
          return new MavenTestSuiteLoader( classLoader );
      }
  
      public void testFailed( int status, Test test, Throwable t )
      {
      }
  
      public void testStarted( String testName )
      {
      }
  
      public void testEnded( String testName )
      {
      }
  
      protected TestResult createTestResult()
      {
          return new TestResult();
      }
  
      public TestResult doRun( Test suite, String testName )
      {
          TestResult result = createTestResult();
  
          long startTime = System.currentTimeMillis();
  
          suite.run( result );
  
          long endTime = System.currentTimeMillis();
  
          long runTime = endTime - startTime;
  
          resultPrinter.print( result, runTime, testName );
  
          return result;
      }
  
      protected void runFailed( String message )
      {
          System.err.println( message );
  
          System.exit( FAILURE_EXIT );
      }
  
      public void setPrinter( ResultPrinter printer )
      {
          resultPrinter = printer;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to