From a71e419f8745d21c7817552506eeb8bbaa5ecc60 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 10 Mar 2019 23:06:52 +0100 Subject: [PATCH] Fixes #47 Fail build on error. --- .../maven/AssertJAssertionsGeneratorMojo.java | 21 +++++- .../AssertJAssertionsGeneratorMojoTest.java | 74 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java b/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java index f066861..51a36e9 100644 --- a/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java +++ b/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java @@ -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; } diff --git a/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java b/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java index 41a7afc..b62db32 100644 --- a/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java +++ b/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java @@ -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"); }