~ubuntu-branches/ubuntu/precise/surefire/precise

« back to all changes in this revision

Viewing changes to surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2011-10-10 20:42:16 UTC
  • mfrom: (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20111010204216-cemva69wkagf4fay
Tags: 2.10-1
* Team upload.
* New upstream release.
* Refresh and remove unneccesary patches.
* Add Build-Depends on libsurefire-java and
  libmaven-common-artifact-filters-java.
* Drop outdated Maven artifact surefire-junit.
* Provide new Maven artifacts: surefire-junit3, maven-surefire-common,
  common-junit3, common-junit4, surefire-junit47 and surefire-testng-utils.
* Fix clean target to allow "two in a row" builds.
* Update Vcs-Browser field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.apache.maven.surefire.junit;
 
2
 
 
3
/*
 
4
 * Licensed to the Apache Software Foundation (ASF) under one
 
5
 * or more contributor license agreements.  See the NOTICE file
 
6
 * distributed with this work for additional information
 
7
 * regarding copyright ownership.  The ASF licenses this file
 
8
 * to you under the Apache License, Version 2.0 (the
 
9
 * "License"); you may not use this file except in compliance
 
10
 * with the License.  You may obtain a copy of the License at
 
11
 *
 
12
 *     http://www.apache.org/licenses/LICENSE-2.0
 
13
 *
 
14
 * Unless required by applicable law or agreed to in writing,
 
15
 * software distributed under the License is distributed on an
 
16
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
17
 * KIND, either express or implied.  See the License for the
 
18
 * specific language governing permissions and limitations
 
19
 * under the License.
 
20
 */
 
21
 
 
22
import java.util.Iterator;
 
23
import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
 
24
import org.apache.maven.surefire.common.junit3.JUnit3TestChecker;
 
25
import org.apache.maven.surefire.providerapi.AbstractProvider;
 
26
import org.apache.maven.surefire.providerapi.ProviderParameters;
 
27
import org.apache.maven.surefire.report.ConsoleOutputCapture;
 
28
import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 
29
import org.apache.maven.surefire.report.ReportEntry;
 
30
import org.apache.maven.surefire.report.ReporterException;
 
31
import org.apache.maven.surefire.report.ReporterFactory;
 
32
import org.apache.maven.surefire.report.RunListener;
 
33
import org.apache.maven.surefire.report.SimpleReportEntry;
 
34
import org.apache.maven.surefire.suite.RunResult;
 
35
import org.apache.maven.surefire.testset.TestSetFailedException;
 
36
import org.apache.maven.surefire.util.DirectoryScanner;
 
37
import org.apache.maven.surefire.util.ReflectionUtils;
 
38
import org.apache.maven.surefire.util.TestsToRun;
 
39
 
 
40
/**
 
41
 * @author Kristian Rosenvold
 
42
 */
 
43
public class JUnit3Provider
 
44
    extends AbstractProvider
 
45
{
 
46
    private final ClassLoader testClassLoader;
 
47
 
 
48
    private final DirectoryScanner directoryScanner;
 
49
 
 
50
    private final PojoAndJUnit3Checker testChecker;
 
51
 
 
52
    private final JUnit3TestChecker jUnit3TestChecker;
 
53
 
 
54
    private final JUnit3Reflector reflector;
 
55
 
 
56
    private final ProviderParameters providerParameters;
 
57
 
 
58
    private TestsToRun testsToRun;
 
59
 
 
60
    public JUnit3Provider( ProviderParameters booterParameters )
 
61
    {
 
62
        this.providerParameters = booterParameters;
 
63
        this.testClassLoader = booterParameters.getTestClassLoader();
 
64
        this.directoryScanner = booterParameters.getDirectoryScanner();
 
65
        this.reflector = new JUnit3Reflector( testClassLoader );
 
66
        jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
 
67
        this.testChecker = new PojoAndJUnit3Checker( jUnit3TestChecker ); // Todo; use reflector
 
68
    }
 
69
 
 
70
    public RunResult invoke( Object forkTestSet )
 
71
        throws TestSetFailedException, ReporterException
 
72
    {
 
73
        if ( testsToRun == null )
 
74
        {
 
75
            testsToRun = forkTestSet == null ? scanClassPath() : TestsToRun.fromClass( (Class) forkTestSet );
 
76
        }
 
77
 
 
78
        ReporterFactory reporterFactory = providerParameters.getReporterFactory();
 
79
        final RunListener reporter = reporterFactory.createReporter();
 
80
        ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
 
81
 
 
82
        final String smClassName = System.getProperty( "surefire.security.manager" );
 
83
        if ( smClassName != null )
 
84
        {
 
85
            SecurityManager securityManager =
 
86
                (SecurityManager) ReflectionUtils.instantiate( this.getClass().getClassLoader(), smClassName );
 
87
            System.setSecurityManager( securityManager );
 
88
        }
 
89
 
 
90
        for ( Iterator iter = testsToRun.iterator(); iter.hasNext(); )
 
91
        {
 
92
            Class clazz = (Class) iter.next();
 
93
            SurefireTestSet surefireTestSet = createTestSet( clazz );
 
94
            executeTestSet( surefireTestSet, reporter, testClassLoader );
 
95
        }
 
96
 
 
97
        return reporterFactory.close();
 
98
    }
 
99
 
 
100
    private SurefireTestSet createTestSet( Class clazz )
 
101
        throws TestSetFailedException
 
102
    {
 
103
        return reflector.isJUnit3Available() && jUnit3TestChecker.accept( clazz )
 
104
            ? new JUnitTestSet( clazz, reflector )
 
105
            : (SurefireTestSet) new PojoTestSet( clazz );
 
106
 
 
107
    }
 
108
 
 
109
    private void executeTestSet( SurefireTestSet testSet, RunListener reporter, ClassLoader classLoader )
 
110
        throws ReporterException, TestSetFailedException
 
111
    {
 
112
 
 
113
        ReportEntry report = new SimpleReportEntry( this.getClass().getName(), testSet.getName() );
 
114
 
 
115
        reporter.testSetStarting( report );
 
116
 
 
117
        testSet.execute( reporter, classLoader );
 
118
 
 
119
        reporter.testSetCompleted( report );
 
120
    }
 
121
 
 
122
    private TestsToRun scanClassPath()
 
123
    {
 
124
        return directoryScanner.locateTestClasses( testClassLoader, testChecker );
 
125
    }
 
126
 
 
127
    public Iterator getSuites()
 
128
    {
 
129
        testsToRun = scanClassPath();
 
130
        return testsToRun.iterator();
 
131
    }
 
132
}