includes/excludes
* parameters. Each pattern you specify here will be used to create an
* include pattern formatted like **/${test}.java
, so you can just type "-Dtest=MyTest"
* to run a single test called "foo/MyTest.java". This parameter will override the TestNG suiteXmlFiles
* parameter.
*
* @parameter expression="${test}"
*/
protected String test;
/**
* List of patterns (separated by commas) used to specify the tests that should be included in testing. When not
* specified and when the test
parameter is not specified, the default includes will be
* **/Test*.java **/*Test.java **/*TestCase.java
. This parameter is ignored if
* TestNG suiteXmlFiles are specified.
*
* @parameter
*/
protected List includes;
/**
* List of patterns (separated by commas) used to specify the tests that should be excluded in testing. When not
* specified and when the test
parameter is not specified, the default excludes will be
* **/*$*
(which excludes all inner classes). This parameter is ignored if
* TestNG suiteXmlFiles are specified.
*
* @parameter
*/
protected List excludes;
/**
* ArtifactRepository of the localRepository. To obtain the directory of localRepository in unit tests use
* System.setProperty( "localRepository").
*
* @parameter expression="${localRepository}"
* @required
* @readonly
*/
protected ArtifactRepository localRepository;
/**
* List of System properties to pass to the JUnit tests.
*
* @parameter
*/
protected Properties systemProperties;
/**
* List of properties for configuring all TestNG related configurations. This is the new
* preferred method of configuring TestNG.
*
* @parameter
* @since 2.4
*/
protected Properties properties;
/**
* Map of of plugin artifacts.
*
* @parameter expression="${plugin.artifactMap}"
* @required
* @readonly
*/
protected Map pluginArtifactMap;
/**
* @parameter expression="${plugin.groupId}"
* @required
* @readonly
*/
protected String pluginGroupId;
/**
* @parameter expression="${plugin.artifactId}"
* @required
* @readonly
*/
protected String pluginArtifactId;
/**
* @parameter expression="${plugin.version}"
* @required
* @readonly
*/
protected String pluginVersion;
/**
* Map of of project artifacts.
*
* @parameter expression="${project.artifactMap}"
* @required
* @readonly
*/
protected Map projectArtifactMap;
/**
* Option to print summary of test suites or just print the test cases that has errors.
*
* @parameter expression="${surefire.printSummary}" default-value="true"
*/
protected boolean printSummary;
/**
* Selects the formatting for the test report to be generated. Can be set as brief or plain.
*
* @parameter expression="${surefire.reportFormat}" default-value="brief"
*/
protected String reportFormat;
/**
* Option to generate a file test report or just output the test report to the console.
*
* @parameter expression="${surefire.useFile}" default-value="true"
*/
protected boolean useFile;
/**
* When forking, set this to true to redirect the unit test standard output to a file (found in
* reportsDirectory/testName-output.txt).
*
* @parameter expression="${maven.test.redirectTestOutputToFile}" default-value="false"
* @since 2.3
*/
protected boolean redirectTestOutputToFile;
/**
* Set this to "true" to cause a failure if there are no tests to run. Defaults to false.
*
* @parameter expression="${failIfNoTests}"
* @since 2.4
*/
protected Boolean failIfNoTests;
/**
* Option to specify the forking mode. Can be "never", "once" or "always". "none" and "pertest" are also accepted
* for backwards compatibility.
*
* @parameter expression="${forkMode}" default-value="once"
* @since 2.1
*/
protected String forkMode;
/**
* Option to specify the jvm (or path to the java executable) to use with the forking options. For the default, the
* jvm will be the same as the one used to run Maven.
*
* @parameter expression="${jvm}"
* @since 2.1
*/
protected String jvm;
/**
* Arbitrary JVM options to set on the command line.
*
* @parameter expression="${argLine}"
* @since 2.1
*/
protected String argLine;
/**
* Attach a debugger to the forked JVM. If set to "true", the process will suspend and
* wait for a debugger to attach on port 5005. If set to some other string, that
* string will be appended to the argLine, allowing you to configure arbitrary
* debuggability options (without overwriting the other options specified in the argLine).
*
* @parameter expression="${maven.surefire.debug}"
* @since 2.4
*/
protected String debugForkedProcess;
/**
* Kill the forked test process after a certain number of seconds. If set to 0,
* wait forever for the process, never timing out.
*
* @parameter expression="${surefire.timeout}"
* @since 2.4
*/
protected int forkedProcessTimeoutInSeconds;
/**
* Additional environments to set on the command line.
*
* @parameter
* @since 2.1.3
*/
protected Map environmentVariables = new HashMap();
/**
* Command line working directory.
*
* @parameter expression="${basedir}"
* @since 2.1.3
*/
protected File workingDirectory;
/**
* When false it makes tests run using the standard classloader delegation instead of the default Maven isolated
* classloader. Only used when forking (forkMode is not "none").junit:junit
will be used.
*
* @parameter expression="${junitArtifactName}" default-value="junit:junit"
* @since 2.3.1
*/
protected String junitArtifactName;
/**
* Allows you to specify the name of the TestNG artifact. If not set, org.testng:testng
will be used.
*
* @parameter expression="${testNGArtifactName}" default-value="org.testng:testng"
* @since 2.3.1
*/
protected String testNGArtifactName;
/**
* (TestNG only) The attribute thread-count allows you to specify how many threads should be allocated for this execution. Only
* makes sense to use in conjunction with parallel.
*
* @parameter expression="${threadCount}"
* @since 2.2
*/
protected int threadCount;
/**
* (TestNG only) When you use the parallel attribute, TestNG will try to run all your test methods in separate threads, except for
* methods that depend on each other, which will be run in the same thread in order to respect their order of
* execution.
*
* @parameter expression="${parallel}"
* @todo test how this works with forking, and console/file output parallelism
* @since 2.2
*/
protected String parallel;
/**
* Whether to trim the stack trace in the reports to just the lines within the test, or show the full trace.
*
* @parameter expression="${trimStackTrace}" default-value="true"
* @since 2.2
*/
protected boolean trimStackTrace;
/**
* Resolves the artifacts needed.
*
* @component
*/
protected ArtifactResolver artifactResolver;
/**
* Creates the artifact
*
* @component
*/
protected ArtifactFactory artifactFactory;
/**
* The plugin remote repositories declared in the pom.
*
* @parameter expression="${project.pluginArtifactRepositories}"
* @since 2.2
*/
protected List remoteRepositories;
/**
* For retrieval of artifact's metadata.
*
* @component
*/
protected ArtifactMetadataSource metadataSource;
protected static final String BRIEF_REPORT_FORMAT = "brief";
protected static final String PLAIN_REPORT_FORMAT = "plain";
protected Properties originalSystemProperties;
/**
* Flag to disable the generation of report files in xml format.
*
* @parameter expression="${disableXmlReport}" default-value="false"
* @since 2.2
*/
protected boolean disableXmlReport;
/**
* Option to pass dependencies to the system's classloader instead of using an isolated class loader when forking.
* Prevents problems with JDKs which implement the service provider lookup mechanism by using the system's
* classloader. Default value is "true".
*
* @parameter expression="${surefire.useSystemClassLoader}"
* @since 2.3
*/
protected Boolean useSystemClassLoader;
/**
* By default, Surefire forks your tests using a manifest-only jar; set this parameter
* to "false" to force it to launch your tests with a plain old Java classpath.
* (See http://maven.apache.org/plugins/maven-surefire-plugin/examples/class-loading.html
* for a more detailed explanation of manifest-only jars and their benefits.)
*
* Default value is "true". Beware, setting this to "false" may cause your tests to
* fail on Windows if your classpath is too long.
*
* @parameter expression="${surefire.useManifestOnlyJar}" default-value="true"
* @since 2.4.3
*/
protected boolean useManifestOnlyJar;
/**
* By default, Surefire enables JVM assertions for the execution of your test cases. To disable the assertions, set
* this flag to false
.
*
* @parameter expression="${enableAssertions}" default-value="true"
* @since 2.3.1
*/
protected boolean enableAssertions;
/**
* The current build session instance.
*
* @parameter expression="${session}"
* @required
* @readonly
*/
protected MavenSession session;
public void execute() throws MojoExecutionException, MojoFailureException {
if (project.getPackaging().equals("pom")) {
return;
}
if (verifyParameters()) {
OSGiSurefireBooter surefireBooter = constructSurefireBooter();
Log log = getLog();
Set* Adds Reporters that will generate reports with different formatting. *
* The Reporter that will be added will be based on the value of the parameter useFile, reportFormat, and * printSummary. * * @param surefireBooter The surefire booter that will run tests. * @param forking */ private void addReporters(OSGiSurefireBooter surefireBooter, boolean forking) { Boolean trimStackTrace = Boolean.valueOf(this.trimStackTrace); if (useFile) { if (printSummary) { if (forking) { surefireBooter.addReport(ForkingConsoleReporter.class.getName(), new Object[] {trimStackTrace}); } else { surefireBooter.addReport(ConsoleReporter.class.getName(), new Object[] {trimStackTrace}); } } if (BRIEF_REPORT_FORMAT.equals(reportFormat)) { surefireBooter.addReport(BriefFileReporter.class.getName(), new Object[] {reportsDirectory, trimStackTrace}); } else if (PLAIN_REPORT_FORMAT.equals(reportFormat)) { surefireBooter.addReport(FileReporter.class.getName(), new Object[] {reportsDirectory, trimStackTrace}); } } else { if (BRIEF_REPORT_FORMAT.equals(reportFormat)) { surefireBooter.addReport(BriefConsoleReporter.class.getName(), new Object[] {trimStackTrace}); } else if (PLAIN_REPORT_FORMAT.equals(reportFormat)) { surefireBooter.addReport(DetailedConsoleReporter.class.getName(), new Object[] {trimStackTrace}); } } if (!disableXmlReport) { surefireBooter.addReport(XMLReporter.class.getName(), new Object[] {reportsDirectory, trimStackTrace}); } } /** * @return SurefirePlugin Returns the skipExec. */ public boolean isSkipExec() { return this.skipTests; } /** * @param skipExec the skipExec to set */ public void setSkipExec(boolean skipExec) { this.skipTests = skipExec; } //TODO remove the part with ToolchainManager lookup once we depend on //3.0.9 (have it as prerequisite). Define as regular component field then. private Toolchain getToolchain() { Toolchain tc = null; try { if (session != null) //session is null in tests.. { ToolchainManager toolchainManager = (ToolchainManager)session.getContainer().lookup(ToolchainManager.ROLE); if (toolchainManager != null) { tc = toolchainManager.getToolchainFromBuildContext("jdk", session); } } } catch (ComponentLookupException componentLookupException) { //just ignore, could happen in pre-3.0.9 builds.. } return tc; } protected Artifact getArtifact(String groupId, String artifactId) throws MojoExecutionException { Artifact artifact; VersionRange vr; try { vr = VersionRange.createFromVersionSpec(project.getVersion()); } catch (InvalidVersionSpecificationException e1) { vr = VersionRange.createFromVersion(project.getVersion()); } artifact = artifactFactory.createDependencyArtifact(groupId, artifactId, vr, "jar", null, Artifact.SCOPE_TEST); try { artifactResolver.resolve(artifact, remoteRepositories, localRepository); } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact.", e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException("Unable to find artifact.", e); } return artifact; } private void generateJar(File root, File jar, Manifest mf) throws IOException { getLog().info("Generating " + jar.toString()); FileOutputStream fos = new FileOutputStream(jar); JarOutputStream jos = mf != null ? new JarOutputStream(fos, mf) : new JarOutputStream(fos); addDir(jos, root, root); jos.close(); } /** * Convert the maven version into OSGi version * @param mavenVersion * @return */ static String osgiVersion(String mavenVersion) { ArtifactVersion ver = new DefaultArtifactVersion(mavenVersion); String qualifer = ver.getQualifier(); if (qualifer != null) { StringBuffer buf = new StringBuffer(qualifer); for (int i = 0; i < buf.length(); i++) { char c = buf.charAt(i); if (Character.isLetterOrDigit(c) || c == '-' || c == '_') { // Keep as-is } else { buf.setCharAt(i, '_'); } } qualifer = buf.toString(); } Version osgiVersion = new Version(ver.getMajorVersion(), ver.getMinorVersion(), ver.getIncrementalVersion(), qualifer); String version = osgiVersion.toString(); return version; } private Manifest createMainBundle() throws IOException { File mf = new File(project.getBasedir(), "META-INF/MANIFEST.MF"); Manifest manifest = null; if (mf.isFile()) { manifest = new Manifest(new FileInputStream(mf)); String bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); if (bundleName != null) { return manifest; } } if (manifest == null) { manifest = new Manifest(); } Attributes attributes = manifest.getMainAttributes(); attributes.putValue("Manifest-Version", "1.0"); attributes.putValue(BUNDLE_MANIFESTVERSION, "2"); attributes.putValue(BUNDLE_SYMBOLICNAME, project.getGroupId() + "." + project.getArtifactId()); attributes.putValue(BUNDLE_NAME, project.getName()); attributes.putValue(BUNDLE_VERSION, osgiVersion(project.getVersion())); attributes.putValue(Constants.DYNAMICIMPORT_PACKAGE, "*"); return manifest; } private Manifest createTestFragment(Manifest mf) { // Create a manifest Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue("Manifest-Version", "1.0"); attributes.putValue(BUNDLE_MANIFESTVERSION, "2"); String host = mf.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); int sc = host.indexOf(';'); if (sc != -1) { host = host.substring(0, sc); } attributes.putValue(BUNDLE_SYMBOLICNAME, host + ".tests"); attributes.putValue(BUNDLE_NAME, mf.getMainAttributes().getValue(BUNDLE_NAME) + " Tests"); attributes.putValue(BUNDLE_VERSION, mf.getMainAttributes().getValue(BUNDLE_VERSION)); attributes.putValue(Constants.FRAGMENT_HOST, host + ";bundle-version=\"" + mf.getMainAttributes().getValue(BUNDLE_VERSION) + "\""); // The main bundle may not have the dependency on JUNIT attributes.putValue(Constants.DYNAMICIMPORT_PACKAGE, "*"); return manifest; } private void addDir(JarOutputStream jos, File root, File dir) throws IOException, FileNotFoundException { for (File file : dir.listFiles()) { if (file.isDirectory()) { addDir(jos, root, file); } else if (file.isFile()) { // getLog().info(file.toString()); String uri = root.toURI().relativize(file.toURI()).toString(); if ("META-INF/MANIFEST.MF".equals(uri)) { continue; } ZipEntry entry = new ZipEntry(uri); jos.putNextEntry(entry); byte[] buf = new byte[4096]; FileInputStream in = new FileInputStream(file); for (;;) { int len = in.read(buf); if (len > 0) { jos.write(buf, 0, len); } else { break; } } in.close(); jos.closeEntry(); } } } }