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

Add silent option to mojo #62

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
64 changes: 52 additions & 12 deletions src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -236,6 +246,47 @@ public Log getLog() {
return quiet ? NO_LOG : super.getLog();
}

@VisibleForTesting
protected ClassLoader getProjectClassLoader() throws DependencyResolutionRequiredException, MalformedURLException {
List<String> classpathElements = new ArrayList<String>(project.getCompileClasspathElements());
classpathElements.addAll(project.getTestClasspathElements());
List<URL> 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);
Expand Down Expand Up @@ -290,17 +341,6 @@ private void failIfMojoParametersAreMissing() throws MojoFailureException {
}
}

@SuppressWarnings("unchecked")
private ClassLoader getProjectClassLoader() throws DependencyResolutionRequiredException, MalformedURLException {
List<String> classpathElements = new ArrayList<String>(project.getCompileClasspathElements());
classpathElements.addAll(project.getTestClasspathElements());
List<URL> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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<String> 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<String> 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");
}
Expand Down