Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #47 Fail build on error. #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -198,6 +198,12 @@ public class AssertJAssertionsGeneratorMojo extends AbstractMojo {
@Parameter(property = "assertj.includePackagePrivateClasses")
public boolean includePackagePrivateClasses = false;

/**
* Fail the build if the generation of one or more assertion classes fails. Defaults to {@code false}.
*/
@Parameter(property = "assertj.failOnError")
public boolean failOnError = false;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
@@ -226,6 +232,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
if (cleanTargetDir) cleanPreviouslyGeneratedSources();
executeWithAssertionGenerator(assertionGenerator);
} catch (MojoFailureException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
@@ -249,7 +257,7 @@ private void cleanPreviouslyGeneratedSources() {
}

@VisibleForTesting
AssertionsGeneratorReport executeWithAssertionGenerator(AssertionsGenerator assertionGenerator) {
AssertionsGeneratorReport executeWithAssertionGenerator(AssertionsGenerator assertionGenerator) throws MojoFailureException {
if (classes == null) classes = new String[0];
AssertionsGeneratorReport generatorReport = assertionGenerator.generateAssertionsFor(packages, classes, targetDir,
entryPointClassPackage, hierarchical,
@@ -258,6 +266,17 @@ AssertionsGeneratorReport executeWithAssertionGenerator(AssertionsGenerator asse
if (isEmpty(generatedSourcesScope) || equalsIgnoreCase("test", generatedSourcesScope)) project.addTestCompileSourceRoot(targetDir);
else if (equalsIgnoreCase("compile", generatedSourcesScope)) project.addCompileSourceRoot(targetDir);
else getLog().warn(format("Unknown generated sources scope '%s' - no sources added to project", generatedSourcesScope));

if (this.failOnError) {
if (!generatorReport.getInputClassesNotFound().isEmpty()) {
throw new MojoFailureException(format("Could not generate all assertion classes. %s input class(es) could not be found.",
generatorReport.getInputClassesNotFound().size()));
}
if (generatorReport.getReportedException() != null) {
throw new MojoFailureException("Could not generate all assertion classes, an exception occured.",
generatorReport.getReportedException());
}
}
return generatorReport;
}

Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import static org.assertj.core.util.Files.newFile;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.maven.AssertJAssertionsGeneratorMojo.shouldHaveNonEmptyPackagesOrClasses;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -516,6 +517,79 @@ public void plugin_should_not_generate_assertions_for_included_package_private_c
assertThat(assertionsFileFor(PackagePrivate.class)).doesNotExist();
}

@Test
public void plugin_should_not_fail_build_if_input_class_not_found_and_fail_on_error_is_false() throws Exception {
// GIVEN
assertjAssertionsGeneratorMojo.classes = array("this.class.does.not.exist");
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(PackagePrivate.class.getName()));
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
// WHEN
AssertionsGeneratorReport report = assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
// THEN
assertThat(report.getInputClassesNotFound()).containsExactly("this.class.does.not.exist");
}

@Test
public void plugin_should_not_fail_build_if_exceptions_occurs_and_fail_on_error_is_false() throws Exception {
// GIVEN
assertjAssertionsGeneratorMojo.classes = array("org.assertj.maven.test.Employee");
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(Employee.class.getName()));
// let's throws an IOException when generating custom assertions
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
BaseAssertionGenerator baseGenerator = mock(BaseAssertionGenerator.class);
generator.setBaseGenerator(baseGenerator);
when(baseGenerator.generateCustomAssertionFor(any(ClassDescription.class))).thenThrow(IOException.class);
// WHEN
AssertionsGeneratorReport report = assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
// THEN
assertThat(report.getInputClassesNotFound()).isEmpty();
assertThat(report.getReportedException()).isInstanceOf(IOException.class);
}

@Test
public void plugin_must_fail_build_if_input_class_not_found_and_fail_on_error_is_true() throws Exception {
// GIVEN
assertjAssertionsGeneratorMojo.classes = array("this.class.does.not.exist");
assertjAssertionsGeneratorMojo.failOnError = true;
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(PackagePrivate.class.getName()));
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
// WHEN
try {
assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
}
// THEN
catch(final Exception e) {
assertThat(e).isInstanceOf(MojoFailureException.class)
.hasMessage("Could not generate all assertion classes. 1 input class(es) could not be found.");
return;
}
fail("Exception should have been thrown.");
}

@Test
public void plugin_must_fail_build_if_exceptions_occurs_and_fail_on_error_is_true() throws Exception {
// GIVEN
assertjAssertionsGeneratorMojo.classes = array("org.assertj.maven.test.Employee");
assertjAssertionsGeneratorMojo.failOnError = true;
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(Employee.class.getName()));
// let's throws an IOException when generating custom assertions
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
BaseAssertionGenerator baseGenerator = mock(BaseAssertionGenerator.class);
generator.setBaseGenerator(baseGenerator);
when(baseGenerator.generateCustomAssertionFor(any(ClassDescription.class))).thenThrow(IOException.class);
// WHEN
try {
assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
}
// THEN
catch(final Exception e) {
assertThat(e).isInstanceOf(MojoFailureException.class)
.hasMessage("Could not generate all assertion classes, an exception occured.");
return;
}
fail("Exception should have been thrown.");
}

private File assertionsFileFor(Class<?> clazz) {
return new File(temporaryFolder.getRoot(), basePathName(clazz) + "Assert.java");
}