From c29afc23104e1df507ec748f5e531a715c1023f7 Mon Sep 17 00:00:00 2001 From: Fabien PERIE Date: Fri, 11 Oct 2019 18:18:27 +0200 Subject: [PATCH 1/3] Add silent option to mojo This commit add a new silent option to ignore the classloading errors during the phase of classes selection. --- .../maven/AssertJAssertionsGeneratorMojo.java | 64 +++++++++++++++---- .../AssertJAssertionsGeneratorMojoTest.java | 42 +++++++++++- 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java b/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java index f066861..baa38fe 100644 --- a/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java +++ b/src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java @@ -20,7 +20,10 @@ import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_TEST_SOURCES; import static org.apache.maven.plugins.annotations.ResolutionScope.TEST; -import static org.assertj.assertions.generator.AssertionsEntryPointType.*; +import static org.assertj.assertions.generator.AssertionsEntryPointType.BDD; +import static org.assertj.assertions.generator.AssertionsEntryPointType.JUNIT_SOFT; +import static org.assertj.assertions.generator.AssertionsEntryPointType.SOFT; +import static org.assertj.assertions.generator.AssertionsEntryPointType.STANDARD; import java.io.File; import java.io.IOException; @@ -33,6 +36,7 @@ import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -198,6 +202,12 @@ public class AssertJAssertionsGeneratorMojo extends AbstractMojo { @Parameter(property = "assertj.includePackagePrivateClasses") public boolean includePackagePrivateClasses = false; + /** + * Don't crash the mojo if a class cannot be loaded owing to linkage error. + */ + @Parameter(defaultValue = "false", property = "assertj.silent") + public boolean silent; + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (skip) { @@ -236,6 +246,47 @@ public Log getLog() { return quiet ? NO_LOG : super.getLog(); } + @VisibleForTesting + protected ClassLoader getProjectClassLoader() throws DependencyResolutionRequiredException, MalformedURLException { + List classpathElements = new ArrayList(project.getCompileClasspathElements()); + classpathElements.addAll(project.getTestClasspathElements()); + List classpathElementUrls = new ArrayList<>(classpathElements.size()); + for (String classpathElement : classpathElements) { + classpathElementUrls.add(new File(classpathElement).toURI().toURL()); + } + return new URLClassLoader(classpathElementUrls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader()) { + @Override + public Class loadClass(String name) throws ClassNotFoundException { + try { + return loadClass(name, false); + } catch (ClassNotFoundException | NoClassDefFoundError cnfe) { + if (silent && isClass(name)) { + return Object.class; + } + + throw cnfe; + } + } + + @Override + protected Class findClass(final String name) throws ClassNotFoundException { + try { + return super.findClass(name); + } catch (ClassNotFoundException | NoClassDefFoundError cnfe) { + if (silent && isClass(name)) { + return Object.class; + } + + throw cnfe; + } + } + + protected boolean isClass(String name) { + return StringUtils.containsAny(name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + } + }; + } + private void cleanPreviouslyGeneratedSources() { try { Path targetDirPath = Paths.get(targetDir); @@ -290,17 +341,6 @@ private void failIfMojoParametersAreMissing() throws MojoFailureException { } } - @SuppressWarnings("unchecked") - private ClassLoader getProjectClassLoader() throws DependencyResolutionRequiredException, MalformedURLException { - List classpathElements = new ArrayList(project.getCompileClasspathElements()); - classpathElements.addAll(project.getTestClasspathElements()); - List classpathElementUrls = new ArrayList<>(classpathElements.size()); - for (String classpathElement : classpathElements) { - classpathElementUrls.add(new File(classpathElement).toURI().toURL()); - } - return new URLClassLoader(classpathElementUrls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader()); - } - @VisibleForTesting static String shouldHaveNonEmptyPackagesOrClasses() { return format( diff --git a/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java b/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java index 41a7afc..9ccea02 100644 --- a/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java +++ b/src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java @@ -19,7 +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.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -516,6 +516,46 @@ public void plugin_should_not_generate_assertions_for_included_package_private_c assertThat(assertionsFileFor(PackagePrivate.class)).doesNotExist(); } + @Test + public void should_dont_crash_if_silent_option_enabled_and_errors_detected_to_load_classes() throws Exception { + assertjAssertionsGeneratorMojo.silent = true; + List classes = newArrayList(Employee.class.getName(), Address.class.getName()); + when(mavenProject.getCompileClasspathElements()).thenReturn(classes); + ClassLoader classloader = assertjAssertionsGeneratorMojo.getProjectClassLoader(); + assertThat(classloader).isNotNull(); + assertThat(classloader.loadClass("fr.mycompany.InexistingClass")).isInstanceOf(Object.class); + } + + @Test(expected = ClassNotFoundException.class) + public void should_crash_if_silent_option_disabled_and_errors_detected_to_load_classes() throws Exception { + assertjAssertionsGeneratorMojo.silent = false; + List classes = newArrayList(Employee.class.getName(), Address.class.getName()); + when(mavenProject.getCompileClasspathElements()).thenReturn(classes); + ClassLoader classloader = assertjAssertionsGeneratorMojo.getProjectClassLoader(); + assertThat(classloader).isNotNull(); + classloader.loadClass("fr.mycompany.InexistingClass"); + } + + @Test(expected = ClassNotFoundException.class) + public void should_throws_class_not_found_exception_if_silent_option_enabled_and_errors_detected_to_load_packages() throws Exception { + assertjAssertionsGeneratorMojo.silent = true; + List classes = newArrayList(Employee.class.getName(), Address.class.getName()); + when(mavenProject.getCompileClasspathElements()).thenReturn(classes); + ClassLoader classloader = assertjAssertionsGeneratorMojo.getProjectClassLoader(); + assertThat(classloader).isNotNull(); + classloader.loadClass("fr.mycompany"); + } + + @Test(expected = ClassNotFoundException.class) + public void should_throws_class_not_found_exception_if_silent_option_disabled_and_errors_detected_to_load_packages() throws Exception { + assertjAssertionsGeneratorMojo.silent = false; + List classes = newArrayList(Employee.class.getName(), Address.class.getName()); + when(mavenProject.getCompileClasspathElements()).thenReturn(classes); + ClassLoader classloader = assertjAssertionsGeneratorMojo.getProjectClassLoader(); + assertThat(classloader).isNotNull(); + classloader.loadClass("fr.mycompany"); + } + private File assertionsFileFor(Class clazz) { return new File(temporaryFolder.getRoot(), basePathName(clazz) + "Assert.java"); } From b74b284a61502eb9d39a14925dc0f724a26b7575 Mon Sep 17 00:00:00 2001 From: Fabien PERIE Date: Fri, 11 Oct 2019 18:44:59 +0200 Subject: [PATCH 2/3] [maven-release-plugin] prepare release assertj-assertions-generator-maven-plugin-2.2.0-mipih-1 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 466c03b..74b7a00 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 assertj-assertions-generator-maven-plugin maven-plugin - 2.2.1-SNAPSHOT + 2.2.0-mipih-1 Maven plugin for AssertJ assertions generator @@ -15,7 +15,7 @@ scm:git:git@github.com:joel-costigliola/assertj-assertions-generator-maven-plugin.git scm:git:git@github.com:joel-costigliola/assertj-assertions-generator-maven-plugin.git git@github.com:joel-costigliola/assertj-assertions-generator-maven-plugin - HEAD + assertj-assertions-generator-maven-plugin-2.2.0-mipih-1 From 3859288872d8490261f20cb24e95726d3648e645 Mon Sep 17 00:00:00 2001 From: Fabien PERIE Date: Fri, 11 Oct 2019 18:48:19 +0200 Subject: [PATCH 3/3] [maven-release-plugin] prepare for next development iteration --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 74b7a00..466c03b 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 assertj-assertions-generator-maven-plugin maven-plugin - 2.2.0-mipih-1 + 2.2.1-SNAPSHOT Maven plugin for AssertJ assertions generator @@ -15,7 +15,7 @@ scm:git:git@github.com:joel-costigliola/assertj-assertions-generator-maven-plugin.git scm:git:git@github.com:joel-costigliola/assertj-assertions-generator-maven-plugin.git git@github.com:joel-costigliola/assertj-assertions-generator-maven-plugin - assertj-assertions-generator-maven-plugin-2.2.0-mipih-1 + HEAD