Skip to content

Commit 349e8c0

Browse files
rbantosjoel-costigliola
authored andcommitted
Fixes #46 (depends on assertj-assertions-generator #129)
Add a new option named includePackagePrivateClasses to collect package private classes when true, false by default.
1 parent 331e872 commit 349e8c0

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

pom.xml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<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">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
45
<packaging>maven-plugin</packaging>
@@ -42,23 +43,23 @@
4243
<goals>
4344
<goal>descriptor</goal>
4445
</goals>
45-
</execution>
46-
<execution>
46+
</execution>
47+
<execution>
4748
<id>help-goal</id>
4849
<goals>
4950
<goal>helpmojo</goal>
5051
</goals>
51-
</execution>
52-
</executions>
52+
</execution>
53+
</executions>
5354
</plugin>
5455
<!-- generate jacoco report -->
5556
<plugin>
5657
<groupId>org.jacoco</groupId>
5758
<artifactId>jacoco-maven-plugin</artifactId>
5859
<configuration>
5960
<excludes>
60-
<exclude>org/assertj/maven/HelpMojo.class</exclude>
61-
<exclude>org/assertj/maven/NoLog.class</exclude>
61+
<exclude>org/assertj/maven/HelpMojo.class</exclude>
62+
<exclude>org/assertj/maven/NoLog.class</exclude>
6263
</excludes>
6364
</configuration>
6465
</plugin>
@@ -72,7 +73,8 @@
7273
</plugins>
7374
<pluginManagement>
7475
<plugins>
75-
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
76+
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build
77+
itself. -->
7678
<plugin>
7779
<groupId>org.eclipse.m2e</groupId>
7880
<artifactId>lifecycle-mapping</artifactId>
@@ -108,7 +110,8 @@
108110
<dependency>
109111
<groupId>org.assertj</groupId>
110112
<artifactId>assertj-assertions-generator</artifactId>
111-
<version>2.1.0</version>
113+
<!-- try to have the same version but might not be possible if we release the plugin without generator changes -->
114+
<version>${project.version}</version>
112115
</dependency>
113116
<dependency>
114117
<groupId>org.assertj</groupId>

src/main/java/org/assertj/maven/AssertJAssertionsGeneratorMojo.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public class AssertJAssertionsGeneratorMojo extends AbstractMojo {
7070
public String targetDir;
7171

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

195+
/**
196+
* Generate assertions for package private classes if true
197+
*/
198+
@Parameter(property = "assertj.includePackagePrivateClasses")
199+
public boolean includePackagePrivateClasses = false;
200+
195201
@Override
196202
public void execute() throws MojoExecutionException, MojoFailureException {
197203
if (skip) {
@@ -215,7 +221,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
215221
}
216222
assertionGenerator.setLog(getLog());
217223
if (generateAssertionsInPackage != null) {
218-
// user has set generateAssertionsInPackage (not that maven converts empty string param to null)
224+
// user has set generateAssertionsInPackage (not that maven converts empty string param to null)
219225
assertionGenerator.setGeneratedAssertionsPackage(generateAssertionsInPackage);
220226
}
221227
if (cleanTargetDir) cleanPreviouslyGeneratedSources();
@@ -246,8 +252,8 @@ private void cleanPreviouslyGeneratedSources() {
246252
AssertionsGeneratorReport executeWithAssertionGenerator(AssertionsGenerator assertionGenerator) {
247253
if (classes == null) classes = new String[0];
248254
AssertionsGeneratorReport generatorReport = assertionGenerator.generateAssertionsFor(packages, classes, targetDir,
249-
entryPointClassPackage,
250-
hierarchical, templates);
255+
entryPointClassPackage, hierarchical,
256+
templates, includePackagePrivateClasses);
251257
printReport(generatorReport);
252258
if (isEmpty(generatedSourcesScope) || equalsIgnoreCase("test", generatedSourcesScope)) project.addTestCompileSourceRoot(targetDir);
253259
else if (equalsIgnoreCase("compile", generatedSourcesScope)) project.addCompileSourceRoot(targetDir);

src/main/java/org/assertj/maven/generator/AssertionsGenerator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,18 @@ public void setExcludePatterns(String[] excludeRegexs) {
8383
/**
8484
* Generates custom assertions for classes in given packages with the Assertions class entry point in given
8585
* destination dir.
86-
*
86+
*
8787
* @param inputPackages the packages containing the classes we want to generate Assert classes for.
8888
* @param inputClassNames the packages containing the classes we want to generate Assert classes for.
8989
* @param destDir the base directory where the classes are going to be generated.
9090
* @param entryPointFilePackage the package of the assertions entry point class, may be <code>null</code>.
91+
* @param includePackagePrivateClasses collect package private classes if true.
9192
* @throws IOException if the files can't be generated
9293
*/
9394
@SuppressWarnings("unchecked")
9495
public AssertionsGeneratorReport generateAssertionsFor(String[] inputPackages, String[] inputClassNames,
95-
String destDir, String entryPointFilePackage,
96-
boolean hierarchical, Templates userTemplates) {
96+
String destDir, String entryPointFilePackage, boolean hierarchical,
97+
Templates userTemplates, boolean includePackagePrivateClasses) {
9798
generator.setDirectoryWhereAssertionFilesAreGenerated(new File(destDir));
9899
AssertionsGeneratorReport report = new AssertionsGeneratorReport();
99100
report.setDirectoryPathWhereAssertionFilesAreGenerated(destDir);
@@ -102,7 +103,7 @@ public AssertionsGeneratorReport generateAssertionsFor(String[] inputPackages, S
102103
report.setInputPackages(inputPackages);
103104
report.setInputClasses(inputClassNames);
104105
try {
105-
Set<TypeToken<?>> classes = collectClasses(classLoader, addAll(inputPackages, inputClassNames));
106+
Set<TypeToken<?>> classes = collectClasses(classLoader, includePackagePrivateClasses, addAll(inputPackages, inputClassNames));
106107
report.reportInputClassesNotFound(classes, inputClassNames);
107108
Set<TypeToken<?>> filteredClasses = removeAssertClasses(classes);
108109
removeClassesAccordingToIncludeAndExcludePatterns(filteredClasses);

src/test/java/org/assertj/maven/AssertJAssertionsGeneratorMojoTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,31 @@ public void executing_plugin_with_invalid_target_scope_should_pass() throws Exce
491491
verify(mavenProject, never()).addTestCompileSourceRoot(assertjAssertionsGeneratorMojo.targetDir);
492492
}
493493

494+
@Test
495+
public void plugin_should_generate_assertions_for_included_package_private_classes_when_option_enabled() throws Exception {
496+
// GIVEN
497+
assertjAssertionsGeneratorMojo.classes = array("org.assertj.maven.PackagePrivate");
498+
assertjAssertionsGeneratorMojo.includePackagePrivateClasses = true;
499+
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(PackagePrivate.class.getName()));
500+
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
501+
// WHEN
502+
assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
503+
// THEN
504+
assertThat(assertionsFileFor(PackagePrivate.class)).exists();
505+
}
506+
507+
@Test
508+
public void plugin_should_not_generate_assertions_for_included_package_private_classes_by_default() throws Exception {
509+
// GIVEN
510+
assertjAssertionsGeneratorMojo.classes = array("org.assertj.maven.PackagePrivate");
511+
when(mavenProject.getCompileClasspathElements()).thenReturn(newArrayList(PackagePrivate.class.getName()));
512+
AssertionsGenerator generator = new AssertionsGenerator(Thread.currentThread().getContextClassLoader());
513+
// WHEN
514+
assertjAssertionsGeneratorMojo.executeWithAssertionGenerator(generator);
515+
// THEN
516+
assertThat(assertionsFileFor(PackagePrivate.class)).doesNotExist();
517+
}
518+
494519
private File assertionsFileFor(Class<?> clazz) {
495520
return new File(temporaryFolder.getRoot(), basePathName(clazz) + "Assert.java");
496521
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2017 the original author or authors.
12+
*/
13+
package org.assertj.maven;
14+
15+
class PackagePrivate {
16+
}

0 commit comments

Comments
 (0)