org.apache.maven.it.Verifier Java Examples

The following examples show how to use org.apache.maven.it.Verifier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XtendCompilerMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void readXtendPrefsUnused() throws Exception {
	Verifier verifier = MavenVerifierUtil.newVerifier(ROOT + "/xtend-prefs-unused");
	verifier.setDebug(true);
	verifier.executeGoal("test");
	verifier.verifyErrorFreeLog();
	String pomsOutputDir = "xtend-dir-from-pom";

	String xtendGenDir = verifier.getBasedir() + "/src/main/" + pomsOutputDir;
	verifier.assertFilePresent(xtendGenDir + "/test/XtendA.java");
	verifier.assertFilePresent(xtendGenDir + "/test/XtendC.java");

	String xtendTestGenDir = verifier.getBasedir() + "/src/test/" + pomsOutputDir;
	verifier.assertFilePresent(xtendTestGenDir + "/tests/XtendA.java");
	verifier.assertFilePresent(xtendTestGenDir + "/tests/XtendC.java");
}
 
Example #2
Source File: YangToSourcesPluginTestIT.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
static Verifier setUp(final String project, final boolean ignoreF)
        throws VerificationException, URISyntaxException, IOException {
    final URL path = YangToSourcesPluginTestIT.class.getResource("/" + project + "pom.xml");
    final Verifier verifier = new Verifier(new File(path.toURI()).getParent());
    if (ignoreF) {
        verifier.addCliOption("-fn");
    }

    final Optional<String> maybeSettings = getEffectiveSettingsXML();
    if (maybeSettings.isPresent()) {
        verifier.addCliOption("-gs");
        verifier.addCliOption(maybeSettings.get());
    }
    verifier.setMavenDebug(true);
    verifier.executeGoal("generate-sources");
    return verifier;
}
 
Example #3
Source File: MavenPluginMigrationTest.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@Before
public void setUpProject() throws IOException, VerificationException {
    assumeTrue("Set the M2_HOME environment variable", System.getenv("M2_HOME") != null);
    assumeTrue("Run from Maven or set the project.version system property", System.getProperty("project.version") != null);

    File projectDir = ResourceExtractor.extractResourcePath(getClass(), "/testing-project", tmp.getRoot(), true);

    verifier = new Verifier(projectDir.getAbsolutePath(), true);
    verifier.setForkJvm(true);

    String settingsXml = System.getProperty("org.apache.maven.user-settings");
    if (settingsXml != null && new File(settingsXml).isFile()) {
        verifier.addCliOption("-s");
        verifier.addCliOption(settingsXml);
    }

    logPath = Paths.get(verifier.getBasedir()).resolve(verifier.getLogFileName());
}
 
Example #4
Source File: PlatformJarArchetypeIT.java    From alfresco-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void whenGenerateProjectFromArchetypeThenAProperProjectIsCreated() throws Exception {
    generateProjectFromArchetype(LOGGER);

    LOGGER.info("---------------------------------------------------------------------");
    LOGGER.info("Building the generated project {}", archetypeProperties.getProjectArtifactId());
    LOGGER.info("---------------------------------------------------------------------");

    // Since creating the archetype was successful, we now want to actually build the generated project executing the integration tests
    // Execute a purge to ensure old data don't make the test fail
    ProcessBuilder purge = getProcessBuilder("purge");
    purge.start().waitFor();
    ProcessBuilder pb = getProcessBuilder("build_test");
    pb.start().waitFor();

    // Verify the execution of the integration tests of the project were successful
    Verifier verifier = new Verifier(projectPath);
    verifier.setAutoclean(false);
    verifier.setLogFileName(LOG_FILENAME);
    printVerifierLog("PROJECT BUILD", verifier, LOGGER);
    verifier.verifyErrorFreeLog();
    verifier.verifyTextInLog("Tests run: 5, Failures: 0, Errors: 0, Skipped: 0");
}
 
Example #5
Source File: XtendCompilerMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void readXtendPrefs() throws Exception {
	Verifier verifier = MavenVerifierUtil.newVerifier(ROOT + "/xtend-prefs");
	verifier.setDebug(true);
	verifier.executeGoal("test");
	verifier.verifyErrorFreeLog();
	String xtendOutputDirFromPrefs = "generated-sources/xtend-from-pref";

	String xtendGenDir = verifier.getBasedir() + "/src/main/" + xtendOutputDirFromPrefs;
	verifier.assertFilePresent(xtendGenDir + "/test/XtendA.java");
	verifier.assertFilePresent(xtendGenDir + "/test/XtendC.java");

	String xtendTestGenDir = verifier.getBasedir() + "/src/test/" + xtendOutputDirFromPrefs;
	verifier.assertFilePresent(xtendTestGenDir + "/tests/XtendA.java");
	verifier.assertFilePresent(xtendTestGenDir + "/tests/XtendC.java");
}
 
Example #6
Source File: ReleaseBranchIT.java    From gitflow-helper-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Attaching existing artifacts from the develop branch should pass.
 *
 * @throws Exception
 */
@Test
public void attachExistingArtifacts() throws Exception {
	Verifier verifier = createVerifier("/project-stub", "origin/release/1.0.0", "1.0.0");

	verifier.executeGoal("deploy");

	verifier.verifyErrorFreeLog();

	verifier.resetStreams();

	// Now re-attach in another verifier.
	verifier = createVerifier("/project-stub", "origin/release/1.0.0", "1.0.0");

	// Allow -SNAPSHOT builds of the plugin to succeed while still asserting the version match.
	verifier.getCliOptions().add("-DenforceNonSnapshots=false");
	verifier.executeGoal("gitflow-helper:attach-deployed");

	verifier.verifyErrorFreeLog();
}
 
Example #7
Source File: DeployAllMojoIntegrationTest.java    From app-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeployAllStandard()
    throws IOException, VerificationException, CloudSdkNotFoundException,
        ProcessHandlerException {

  Verifier verifier = new StandardVerifier("testDeployStandard");

  // execute with staging directory not present
  verifier.executeGoals(Arrays.asList("package", "appengine:deployAll"));

  // verify
  verifier.verifyErrorFreeLog();
  verifier.verifyTextInLog("Detected App Engine standard environment application");
  verifier.verifyTextInLog("GCLOUD: Deployed service");
  verifier.verifyTextInLog("GCLOUD: Custom routings have been updated.");
  verifier.verifyTextInLog("GCLOUD: DoS protection has been updated.");
  verifier.verifyTextInLog("GCLOUD: Indexes are being rebuilt. This may take a moment.");
  verifier.verifyTextInLog("GCLOUD: Cron jobs have been updated.");
  verifier.verifyTextInLog("GCLOUD: Task queues have been updated.");

  // cleanup
  deleteService("standard-project");
}
 
Example #8
Source File: RetargetDeployIT.java    From gitflow-helper-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void devTargetsSnapshot() throws Exception {
	Verifier verifier = createVerifier("/project-stub", "origin/develop", "3.0.0-SNAPSHOT");

	try {
		verifier.executeGoal("deploy");

		verifier.verifyErrorFreeLog();
	} finally {
		verifier.resetStreams();
	}

	// Ensure the file exists in the repo.
	File artifactDir = new File(System.getProperty("basedir"), "target/it-repositories/snapshots/com/e-gineering/gitflow-helper-maven-plugin-test-stub/3.0.0-SNAPSHOT");
	Assert.assertTrue(artifactDir.exists() && artifactDir.isDirectory() && artifactDir.list().length > 0);
}
 
Example #9
Source File: DevelopBranchIT.java    From gitflow-helper-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Attaching existing artifacts from the develop branch should pass.
 *
 * @throws Exception
 */
@Test
public void attachExistingArtifacts() throws Exception {
	Verifier verifier = createVerifier("/project-stub", "origin/develop", "1.0.0-SNAPSHOT");

	try {
		verifier.executeGoal("deploy");

		verifier.verifyErrorFreeLog();
	} finally {
		verifier.resetStreams();
	}


	// New verifier to attach existing artifacts
	verifier = createVerifier("/project-stub", "origin/develop", "1.0.0-SNAPSHOT");

	try {
		verifier.executeGoal("gitflow-helper:attach-deployed");

		verifier.verifyErrorFreeLog();
	} finally {
		verifier.resetStreams();
	}
}
 
Example #10
Source File: GitVersioningExtensionIT.java    From maven-git-versioning-extension with MIT License 6 votes vote down vote up
@Test
void outsideGitVersioning() throws Exception {
    // Given

    writeModel(projectDir.resolve("pom.xml").toFile(), pomModel);
    writeExtensionsFile(projectDir);
    writeExtensionConfigFile(projectDir, new Configuration());

    // When
    Verifier verifier = new Verifier(projectDir.toFile().getAbsolutePath());
    verifier.executeGoal("verify");

    // Then
    String log = getLog(verifier);
    assertThat(log).doesNotContain("[ERROR]", "[FATAL]");
    String expectedVersion = "0.0.0";
    assertThat(log).contains("Building " + pomModel.getArtifactId() + " " + expectedVersion);

    assertThat(projectDir.resolve("target/").resolve(GIT_VERSIONING_POM_NAME).toFile().exists()).isEqualTo(false);
    assertThat(log).contains("[WARNING] skip - project is not part of a git repository");
}
 
Example #11
Source File: XtendInstallDebugInfoMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void simpleXtendAsPrimaryWithSyntheticVars() throws Exception {
	Verifier verifier = verifyErrorFreeLog(ROOT + "/xtend_as_primary_with_synthetic_vars");
	String xtendGenDir = verifier.getBasedir() + "/target/classes/test";
	String debugInfo = new ClassFileDebugInfoExtractor().getDebugInfo(xtendGenDir);

	// please note that here we have source information only for Xtend
	String xpt = 
			"// test {\n" + 
			"  package test; // XtendA.class\n" + 
			"  class XtendA {\n" + 
			"    void <init>() { // XtendA.xtend:5\n" + 
			"      void this;\n" + 
			"      void _function;\n" + 
			"      void myvar;\n" + 
			"    }\n" + 
			"    void lambda$new$0() { // XtendA.xtend:6\n" + 
			"      void it;\n" + 
			"    }\n" + 
			"  }\n" + 
			"  \n" + 
			"  package test; // XtendA2.class\n" + 
			"  class XtendA2 {\n" + 
			"    void <init>() { // XtendA.xtend:13\n" + 
			"      void this;\n" + 
			"    }\n" + 
			"  }\n" + 
			"}";
	Assert.assertEquals(debugInfo, xpt.toString(), debugInfo);
}
 
Example #12
Source File: XtendCompilerMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void emptyDirWarningFalse() throws Exception {
	Verifier verifier = MavenVerifierUtil.newVerifier(ROOT + "/emptydir-warning-false");
	verifier.setDebug(true);
	verifier.executeGoal("test");
	verifier.verifyErrorFreeLog();
	String logFileName = verifier.getBasedir() + "/" + verifier.getLogFileName();
	assertFileDoesNotContain(verifier, logFileName, "is empty. Can't process.");
}
 
Example #13
Source File: XtendCompilerMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void assertFileDoesNotContain(Verifier verifier, String file, String contained) {
	verifier.assertFilePresent(file);
	try {
		String content = FileUtils.fileRead(new File(file), "UTF-8");
		if (content.contains(contained)) {
			Assert.fail("Content of " + file + " does contain " + contained + ", but it should not. Contents: " + content);
		}
	} catch (IOException e) {
		Assert.fail(e.getMessage());
	}
}
 
Example #14
Source File: GenRepoInfoFileMojoIntegrationTest.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerate() throws IOException, VerificationException {
  Verifier verifier = new StandardVerifier("testGenRepoInfoFile");

  verifier.executeGoal("appengine:genRepoInfoFile");
  verifier.assertFilePresent("target/appengine-staging/WEB-INF/classes/source-context.json");
}
 
Example #15
Source File: EnforceVersionsIT.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test(expected= VerificationException.class)
public void dependencySnapshotFail() throws Exception {
	// Stage the repository with version 1.0.0 of the stub.

	// Create a release version and get it deployed.
	Verifier verifier = createVerifier("/project-stub", "origin/release/1.0.0", "1.0.0");
	try {
		verifier.executeGoal("deploy");

		verifier.verifyErrorFreeLog();
	} finally {
		verifier.resetStreams();
	}

	// Promote (deploy) from /origin/master
	verifier = createVerifier("/project-stub", "origin/master", "1.0.0");
	try {
		verifier.executeGoal("deploy");

		verifier.verifyErrorFreeLog();
	} finally {
		verifier.resetStreams();
	}

	// Build a project that depends upon the upstream project.
	verifier = createVerifier("/project-alt1-stub", "origin/release/1.0.0", "1.0.0");
	verifier.getCliOptions().add("-Ddependency.stub.version=1.0.0-SNAPSHOT");
	verifier.getCliOptions().add("-Dplugin.stub.version=1.0.0");

	try {
		verifier.executeGoal("deploy");
	} finally {
		verifier.resetStreams();
	}
}
 
Example #16
Source File: Bug850Test.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
public void compile03() throws Exception {
	Verifier verifier = doCompile();

	Path path = FileSystems.getDefault().getPath(
			"src", "main", "generated-sources", "sarl",
			"io", "sarl", "maven", "bug850", "Agent3.java");
	assertNotNull(path);
	verifier.assertFilePresent(path.toString());
	assertEquals(EXPECTED_AGENT3, readFile(verifier, path));
}
 
Example #17
Source File: YangToSourcesPluginTestIT.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static String getMavenBuildDirectory(final Verifier verifier) throws IOException {
    final Properties sp = new Properties();
    final Path path = new File(verifier.getBasedir() + "/it-project.properties").toPath();
    try (InputStream is = Files.newInputStream(path)) {
        sp.load(is);
    }
    return sp.getProperty("target.dir");
}
 
Example #18
Source File: XtendCompilerMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void continueOnXtendWarnings() throws Exception {
	Verifier verifier = MavenVerifierUtil.newVerifier(ROOT + "/xtendwarnings");
	verifier.executeGoal("verify");
	verifier.verifyTextInLog("3: The import 'java.util.Collections' is never used.");
	verifier.verifyTextInLog("[INFO] BUILD SUCCESS");
}
 
Example #19
Source File: XtendCompilerMojoIT.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void assertFileContainsUTF16(Verifier verifier, String file, String contained) {
	verifier.assertFilePresent(file);
	try {
		String content = FileUtils.fileRead(new File(file), "UTF-16");
		if (!content.contains(contained)) {
			Assert.fail("Content of " + file + " does not contain " + contained + " but: " + content);
		}
	} catch (IOException e) {
		Assert.fail(e.getMessage());
	}
}
 
Example #20
Source File: DownstreamParentTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that a trivial project using brooklyn-downstream-parent can be
 * loaded into Brooklyn's catalogue and its entities deployed.
 */
@Test(groups = "Integration")
public void testDownstreamProjectsCanBeLoadedIntoBrooklynCatalogByDefault() throws Exception {
    int port = Networking.nextAvailablePort(57000);
    File dir = getBasedir("downstream-parent-test");
    Verifier verifier = new Verifier(dir.getAbsolutePath());
    verifier.setMavenDebug(true);
    verifier.executeGoal("post-integration-test", ImmutableMap.of(
            "bindPort", String.valueOf(port)));
    verifier.verifyErrorFreeLog();
    verifier.verifyTextInLog("Hello from the init method of the HelloEntity");
}
 
Example #21
Source File: AbstractSingleYamlDeployIntegrationTest.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeployFlexible() throws IOException, VerificationException {

  Verifier verifier = new FlexibleVerifier("testDeployFlexible");

  // execute with staging directory not present
  verifier.executeGoals(Arrays.asList("package", "appengine:" + getDeployGoal()));

  // verify
  verifier.verifyErrorFreeLog();
  verifier.verifyTextInLog("Detected App Engine flexible environment application");
  verifier.verifyTextInLog("GCLOUD: " + getExpectedLogMessage());
}
 
Example #22
Source File: AbstractSingleYamlDeployIntegrationTest.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeployStandard() throws IOException, VerificationException {

  Verifier verifier = new StandardVerifier("testDeployStandard");

  // execute with staging directory not present
  verifier.executeGoals(Arrays.asList("package", "appengine:" + getDeployGoal()));

  // verify
  verifier.verifyErrorFreeLog();
  verifier.verifyTextInLog("Detected App Engine standard environment application");
  verifier.verifyTextInLog("GCLOUD: " + getExpectedLogMessage());
}
 
Example #23
Source File: RetargetDeployIT.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void othersUnsetRepos() throws Exception {
	// Deploy a hotfix to the test-releases.
	Verifier verifier = createVerifier("/project-stub", "feature/undeployable", "3.5.0-SNAPSHOT");
	try {
		verifier.executeGoal("deploy");
		verifier.verifyTextInLog("[INFO] Skipping artifact deployment");
	} finally {
		verifier.resetStreams();
	}
}
 
Example #24
Source File: AbstractStageMojoIntegrationTest.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testStageStandard() throws IOException, VerificationException {

  String projectDir =
      ResourceExtractor.simpleExtractResources(getClass(), "/projects/standard-project")
          .getAbsolutePath();

  Verifier verifier = new StandardVerifier("testStageStandard");

  // execute with staging directory not present
  verifier.executeGoals(Arrays.asList("package", "appengine:stage"));

  // verify
  verifier.verifyErrorFreeLog();
  verifier.verifyTextInLog("Detected App Engine standard environment application");
  verifier.verifyTextInLog("GCLOUD: ");
  verifier.assertFilePresent("target/appengine-staging");
  verifier.assertFilePresent("target/appengine-staging/WEB-INF");
  verifier.assertFilePresent("target/appengine-staging/WEB-INF/web.xml");
  verifier.assertFilePresent("target/appengine-staging/app.yaml");
  verifier.assertFileMatches(
      projectDir + "/target/appengine-staging/app.yaml", "(?s).*service: 'standard-project'.*");

  // repeat with staging directory present
  verifier.setLogFileName("testStageStandard_repeat.txt");
  verifier.setAutoclean(false);
  verifier.executeGoal("appengine:stage");
  verifier.verifyTextInLog("Deleting the staging directory");
}
 
Example #25
Source File: RunAsyncMojoIntegrationTest.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunAsync() throws IOException, VerificationException, InterruptedException {
  String testName = "testRunAsync";
  try {
    Verifier verifier = createVerifier(testName);
    verifier.setSystemProperty("app.devserver.startSuccessTimeout", "60");
    verifier.executeGoals(Arrays.asList("package", "appengine:start"));

    String urlContent = UrlUtils.getUrlContentWithRetries(getServerUrl(), 60000, 1000);
    assertThat(urlContent, containsString("Hello from the App Engine Standard project."));
    assertThat(urlContent, containsString("TEST_VAR=testVariableValue"));

    Path expectedLog =
        Paths.get(
            "target",
            "test-classes",
            "projects",
            "standard-project",
            "target",
            "dev-appserver-out",
            "dev_appserver.out");
    assertTrue(Files.exists(expectedLog));
    String devAppServerOutput =
        new String(Files.readAllBytes(expectedLog), StandardCharsets.UTF_8);
    assertTrue(devAppServerOutput.contains(DEV_APP_SERVER_STARTED));

    verifier.verifyErrorFreeLog();
    verifier.verifyTextInLog(DEV_APP_SERVER_STARTED);
  } finally {
    Verifier stopVerifier = createVerifier(testName + "_stop");
    stopVerifier.executeGoal("appengine:stop");
    // wait up to 5 seconds for the server to stop
    assertTrue(UrlUtils.isUrlDownWithRetries(getServerUrl(), 5000, 100));
  }
}
 
Example #26
Source File: XtendCompilerMojoTraceIT.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void traceDisabled() throws Exception {
	Verifier verifier = verifyErrorFreeLog(ROOT + "/trace_disabled");
	String xtendGenDir = verifier.getBasedir() + "/src/main/generated-sources/xtend/";
	verifier.assertFileNotPresent(xtendGenDir + "test/.XtendA.java._trace");
	verifier.assertFileNotPresent(xtendGenDir + "test/.XtendA2.java._trace");
}
 
Example #27
Source File: CompileMojoIT.java    From jspc-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected void testJspc(File testDir) throws VerificationException {
        Verifier verifier  = new Verifier(testDir.getAbsolutePath() );
        verifier.setLogFileName("verifier.log");
        
//        verifier.setDebug(true);
//        verifier.setMavenDebug(true);
        
        verifier.executeGoal("clean");
        verifier.executeGoal("package");
        
        verifier.verifyErrorFreeLog();
        
        verifier.assertFilePresent("target/jspweb.xml");
        verifier.assertFilePresent("target/classes/jsp/index_jsp.class");
    }
 
Example #28
Source File: DeployAllMojoIntegrationTest.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeployAllFlexible()
    throws IOException, VerificationException, CloudSdkNotFoundException,
        ProcessHandlerException {

  Verifier verifier = new FlexibleVerifier("testDeployFlexible");

  // execute with staging directory not present
  verifier.executeGoals(Arrays.asList("package", "appengine:deployAll"));

  // verify
  verifier.verifyErrorFreeLog();
  verifier.verifyTextInLog("Detected App Engine flexible environment application");
  verifier.verifyTextInLog("GCLOUD: Deployed service");
  verifier.verifyTextInLog("GCLOUD: Custom routings have been updated.");
  verifier.verifyTextInLog("GCLOUD: DoS protection has been updated.");
  verifier.verifyTextInLog("GCLOUD: Indexes are being rebuilt. This may take a moment.");
  verifier.verifyTextInLog("GCLOUD: Cron jobs have been updated.");
  verifier.verifyTextInLog("GCLOUD: Task queues have been updated.");

  // verify debugger required file generation
  verifier.assertFilePresent(
      "target/flexible-project-1.0-SNAPSHOT/WEB-INF/classes/source-context.json");

  // cleanup
  deleteService("flexible-project");
}
 
Example #29
Source File: EnforceVersionsIT.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void featureAllowsSnapshot() throws Exception {
	Verifier verifier = createVerifier("/project-stub", "origin/feature/aFeatureBranch", "1.0.0-SNAPSHOT");

	try {
		verifier.executeGoal("gitflow-helper:enforce-versions");
		verifier.verifyErrorFreeLog();
	} finally {
		verifier.resetStreams();
	}
}
 
Example #30
Source File: AbstractMojoTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Execute a Mojo.
 *
 * @param projectName the name of the project to test for the unit test.
 * @param goalName the goal to run.
 * @return the verifier.
 * @throws Exception any exception.
 */
protected Verifier executeMojo(String projectName, String goalName) throws Exception {
	String tempDirPath = System.getProperty("maven.test.tmpdir", //$NON-NLS-1$
			System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
	File tempDir = new File(tempDirPath);
	File baseDir = new File(tempDir, projectName);
	assertNotNull(baseDir);

	FileUtils.deleteDirectory(baseDir);

	URL url = getClass().getResource("/projects/" + projectName); //$NON-NLS-1$
	if (url == null) {
		throw new IllegalArgumentException("Resource not found: " + projectName); //$NON-NLS-1$
	}
	File resourceFile = new File(new URI(url.toExternalForm()));
	assertTrue(resourceFile.isDirectory());
	FileUtils.copyDirectoryStructure(resourceFile, baseDir);

	assertTrue(baseDir.exists());
	assertTrue(baseDir.isDirectory());

	recursiveDeleteOnShutdownHook(baseDir);

	Verifier verifier = new Verifier(baseDir.getAbsolutePath());
	verifier.setAutoclean(false);
	verifier.setDebug(false);
	final String m2home = findDefaultMavenHome();
	if (m2home != null && !m2home.isEmpty()) {
		verifier.setForkJvm(false);
		verifier.getSystemProperties().put("maven.multiModuleProjectDirectory", m2home); //$NON-NLS-1$
		verifier.getVerifierProperties().put("use.mavenRepoLocal", Boolean.FALSE.toString()); //$NON-NLS-1$
	}
	verifier.executeGoals(Arrays.asList("clean", goalName)); //$NON-NLS-1$
	return verifier;
}