Skip to content

Commit

Permalink
Fixes #46 (depends on assertj-assertions-generator #129)
Browse files Browse the repository at this point in the history
Add a new option named includePackagePrivateClasses to collect package private classes when true, false by default.
  • Loading branch information
rbantos authored and joel-costigliola committed Mar 10, 2019
1 parent 331e872 commit 349e8c0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
21 changes: 12 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
Expand Down Expand Up @@ -42,23 +43,23 @@
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
</execution>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</execution>
</executions>
</plugin>
<!-- generate jacoco report -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>org/assertj/maven/HelpMojo.class</exclude>
<exclude>org/assertj/maven/NoLog.class</exclude>
<exclude>org/assertj/maven/HelpMojo.class</exclude>
<exclude>org/assertj/maven/NoLog.class</exclude>
</excludes>
</configuration>
</plugin>
Expand All @@ -72,7 +73,8 @@
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build
itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
Expand Down Expand Up @@ -108,7 +110,8 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-assertions-generator</artifactId>
<version>2.1.0</version>
<!-- try to have the same version but might not be possible if we release the plugin without generator changes -->
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public class AssertJAssertionsGeneratorMojo extends AbstractMojo {
public String targetDir;

/**
* Package where generated assertion classes will reside.
* Package where generated assertion classes will reside.
* <p/>
* If not set (or set to empty), each assertion class is generated in the package of the corresponding class to assert.
* For example the generated assertion class for com.nba.Player will be com.nba.PlayerAssert (in the same package as Player).
* For example the generated assertion class for com.nba.Player will be com.nba.PlayerAssert (in the same package as Player).
* Defaults to ''.<br>
* <p/>
* Note that the Assertions entry point classes package is controlled by the entryPointClassPackage property.
Expand Down Expand Up @@ -192,6 +192,12 @@ public class AssertJAssertionsGeneratorMojo extends AbstractMojo {
@Parameter(property = "assertj.templates")
public Templates templates;

/**
* Generate assertions for package private classes if true
*/
@Parameter(property = "assertj.includePackagePrivateClasses")
public boolean includePackagePrivateClasses = false;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
Expand All @@ -215,7 +221,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
assertionGenerator.setLog(getLog());
if (generateAssertionsInPackage != null) {
// user has set generateAssertionsInPackage (not that maven converts empty string param to null)
// user has set generateAssertionsInPackage (not that maven converts empty string param to null)
assertionGenerator.setGeneratedAssertionsPackage(generateAssertionsInPackage);
}
if (cleanTargetDir) cleanPreviouslyGeneratedSources();
Expand Down Expand Up @@ -246,8 +252,8 @@ private void cleanPreviouslyGeneratedSources() {
AssertionsGeneratorReport executeWithAssertionGenerator(AssertionsGenerator assertionGenerator) {
if (classes == null) classes = new String[0];
AssertionsGeneratorReport generatorReport = assertionGenerator.generateAssertionsFor(packages, classes, targetDir,
entryPointClassPackage,
hierarchical, templates);
entryPointClassPackage, hierarchical,
templates, includePackagePrivateClasses);
printReport(generatorReport);
if (isEmpty(generatedSourcesScope) || equalsIgnoreCase("test", generatedSourcesScope)) project.addTestCompileSourceRoot(targetDir);
else if (equalsIgnoreCase("compile", generatedSourcesScope)) project.addCompileSourceRoot(targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ public void setExcludePatterns(String[] excludeRegexs) {
/**
* Generates custom assertions for classes in given packages with the Assertions class entry point in given
* destination dir.
*
*
* @param inputPackages the packages containing the classes we want to generate Assert classes for.
* @param inputClassNames the packages containing the classes we want to generate Assert classes for.
* @param destDir the base directory where the classes are going to be generated.
* @param entryPointFilePackage the package of the assertions entry point class, may be <code>null</code>.
* @param includePackagePrivateClasses collect package private classes if true.
* @throws IOException if the files can't be generated
*/
@SuppressWarnings("unchecked")
public AssertionsGeneratorReport generateAssertionsFor(String[] inputPackages, String[] inputClassNames,
String destDir, String entryPointFilePackage,
boolean hierarchical, Templates userTemplates) {
String destDir, String entryPointFilePackage, boolean hierarchical,
Templates userTemplates, boolean includePackagePrivateClasses) {
generator.setDirectoryWhereAssertionFilesAreGenerated(new File(destDir));
AssertionsGeneratorReport report = new AssertionsGeneratorReport();
report.setDirectoryPathWhereAssertionFilesAreGenerated(destDir);
Expand All @@ -102,7 +103,7 @@ public AssertionsGeneratorReport generateAssertionsFor(String[] inputPackages, S
report.setInputPackages(inputPackages);
report.setInputClasses(inputClassNames);
try {
Set<TypeToken<?>> classes = collectClasses(classLoader, addAll(inputPackages, inputClassNames));
Set<TypeToken<?>> classes = collectClasses(classLoader, includePackagePrivateClasses, addAll(inputPackages, inputClassNames));
report.reportInputClassesNotFound(classes, inputClassNames);
Set<TypeToken<?>> filteredClasses = removeAssertClasses(classes);
removeClassesAccordingToIncludeAndExcludePatterns(filteredClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,31 @@ public void executing_plugin_with_invalid_target_scope_should_pass() throws Exce
verify(mavenProject, never()).addTestCompileSourceRoot(assertjAssertionsGeneratorMojo.targetDir);
}

@Test
public void plugin_should_generate_assertions_for_included_package_private_classes_when_option_enabled() throws Exception {
// GIVEN
assertjAssertionsGeneratorMojo.classes = array("org.assertj.maven.PackagePrivate");
assertjAssertionsGeneratorMojo.includePackagePrivateClasses = true;
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(PackagePrivate.class.getName()));
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
// WHEN
assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
// THEN
assertThat(assertionsFileFor(PackagePrivate.class)).exists();
}

@Test
public void plugin_should_not_generate_assertions_for_included_package_private_classes_by_default() throws Exception {
// GIVEN
assertjAssertionsGeneratorMojo.classes = array("org.assertj.maven.PackagePrivate");
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(PackagePrivate.class.getName()));
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
// WHEN
assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
// THEN
assertThat(assertionsFileFor(PackagePrivate.class)).doesNotExist();
}

private File assertionsFileFor(Class<?> clazz) {
return new File(temporaryFolder.getRoot(), basePathName(clazz) + "Assert.java");
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/assertj/maven/PackagePrivate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2017 the original author or authors.
*/
package org.assertj.maven;

class PackagePrivate {
}

0 comments on commit 349e8c0

Please sign in to comment.