Skip to content

Commit 8d8b23b

Browse files
authored
Merge pull request #493 from bradleylarrick/plugin-unit-tests
Added more unit tests for japicmp-maven-plugin
2 parents 4c9c5c7 + fd0ea30 commit 8d8b23b

18 files changed

Lines changed: 1256 additions & 656 deletions

File tree

japicmp-maven-plugin/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@
240240
<targetPath>${test.directory}</targetPath>
241241
<filtering>true</filtering>
242242
</testResource>
243+
<testResource>
244+
<directory>src/test/resources/binary</directory>
245+
<targetPath>${test.directory}</targetPath>
246+
<filtering>false</filtering>
247+
</testResource>
243248
</testResources>
244249
</build>
245250

@@ -276,6 +281,9 @@
276281
<plugin>
277282
<artifactId>maven-javadoc-plugin</artifactId>
278283
<configuration>
284+
<sourceFileExcludes>
285+
<sourceFileExclude>**/HelpMojo.java</sourceFileExclude>
286+
</sourceFileExcludes>
279287
<links>
280288
<link>https://maven.apache.org/ref/${maven-api.version}/maven-plugin-api/apidocs</link>
281289
<link>https://maven.apache.org/ref/${reporting.version}/apidocs</link>
@@ -333,6 +341,12 @@
333341
</plugin>
334342
<plugin>
335343
<artifactId>maven-checkstyle-plugin</artifactId>
344+
<configuration>
345+
<!-- Explicitly specify sourceDirectory so it doesn't check generated code -->
346+
<sourceDirectories>
347+
<sourceDirectory>${project.basedir}/src/main</sourceDirectory>
348+
</sourceDirectories>
349+
</configuration>
336350
</plugin>
337351
<plugin>
338352
<groupId>org.apache.maven.plugins</groupId>

japicmp-maven-plugin/src/main/java/japicmp/maven/ConfigParameters.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package japicmp.maven;
22

3-
import java.util.List;
3+
import java.util.*;
44

55
/** Class for storing the japicmp configuration parameters. */
66
public class ConfigParameters {
@@ -78,7 +78,6 @@ public class ConfigParameters {
7878

7979
private List<String> excludeModules;
8080

81-
8281
private boolean includeExclusively = false;
8382

8483
private boolean excludeExclusively = false;
@@ -138,7 +137,7 @@ public boolean getBreakBuildOnBinaryIncompatibleModifications() {
138137
}
139138

140139
void setBreakBuildOnBinaryIncompatibleModifications(
141-
boolean breakBuildOnBinaryIncompatibleModifications) {
140+
boolean breakBuildOnBinaryIncompatibleModifications) {
142141
this.breakBuildOnBinaryIncompatibleModifications = breakBuildOnBinaryIncompatibleModifications;
143142
}
144143

@@ -235,7 +234,7 @@ public boolean getBreakBuildOnSourceIncompatibleModifications() {
235234
}
236235

237236
void setBreakBuildOnSourceIncompatibleModifications(
238-
boolean breakBuildOnSourceIncompatibleModifications) {
237+
boolean breakBuildOnSourceIncompatibleModifications) {
239238
this.breakBuildOnSourceIncompatibleModifications = breakBuildOnSourceIncompatibleModifications;
240239
}
241240

@@ -316,7 +315,7 @@ public List<String> getIgnoreMissingClassesByRegularExpressions() {
316315
}
317316

318317
void setIgnoreMissingClassesByRegularExpressions(
319-
List<String> ignoreMissingClassesByRegularExpressions) {
318+
List<String> ignoreMissingClassesByRegularExpressions) {
320319
this.ignoreMissingClassesByRegularExpressions = ignoreMissingClassesByRegularExpressions;
321320
}
322321

@@ -365,9 +364,9 @@ public boolean isBreakBuildBasedOnSemanticVersioningForMajorVersionZero() {
365364
}
366365

367366
void setBreakBuildBasedOnSemanticVersioningForMajorVersionZero(
368-
boolean breakBuildBasedOnSemanticVersioningForMajorVersionZero) {
367+
boolean breakBuildBasedOnSemanticVersioningForMajorVersionZero) {
369368
this.breakBuildBasedOnSemanticVersioningForMajorVersionZero =
370-
breakBuildBasedOnSemanticVersioningForMajorVersionZero;
369+
breakBuildBasedOnSemanticVersioningForMajorVersionZero;
371370
}
372371

373372
public boolean isIncludeExlusively() {
@@ -395,7 +394,7 @@ public List<OverrideCompatibilityChangeParameter> getOverrideCompatibilityChange
395394
}
396395

397396
void setOverrideCompatibilityChangeParameters(
398-
List<OverrideCompatibilityChangeParameter> overrideCompatibilityChangeParameters) {
397+
List<OverrideCompatibilityChangeParameter> overrideCompatibilityChangeParameters) {
399398
this.overrideCompatibilityChangeParameters = overrideCompatibilityChangeParameters;
400399
}
401400

@@ -433,6 +432,10 @@ public String getCompatibilityChange() {
433432
return compatibilityChange;
434433
}
435434

435+
public void setCompatibilityChange(final String compatibilityChange) {
436+
this.compatibilityChange = compatibilityChange;
437+
}
438+
436439
public boolean isBinaryCompatible() {
437440
return binaryCompatible;
438441
}
@@ -444,5 +447,9 @@ public boolean isSourceCompatible() {
444447
public String getSemanticVersionLevel() {
445448
return semanticVersionLevel;
446449
}
450+
451+
public void setSemanticVersionLevel(final String semanticVersionLevel) {
452+
this.semanticVersionLevel = semanticVersionLevel;
453+
}
447454
}
448455
}

japicmp-maven-plugin/src/main/java/japicmp/maven/JApiCmpProcessor.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ private void setUpOverrideCompatibilityChanges(
140140
break;
141141
}
142142
}
143-
if (foundSemanticVersionLevel == null) {
144-
throw new MojoFailureException("Unknown semantic version level '"
145-
+ semanticVersionLevel
146-
+ "'. Supported values: "
147-
+ Joiner.on(',').join(
148-
JApiSemanticVersionLevel.values()));
149-
}
150143
comparatorOptions.addOverrideCompatibilityChange(
151144
new JarArchiveComparatorOptions.OverrideCompatibilityChange(foundChange,
152145
configChange.isBinaryCompatible(),
@@ -760,7 +753,7 @@ private Set<Artifact> getCompileArtifacts(final MavenProject mavenProject) throw
760753
Set<org.apache.maven.artifact.Artifact> projectDependencies =
761754
mavenProject.getArtifacts();
762755

763-
HashSet<Artifact> result = new HashSet<>(1+projectDependencies.size());
756+
HashSet<Artifact> result = new HashSet<>(1 + projectDependencies.size());
764757
// Include the project artifact; use the reactor to resolve the project artifact in case it's not being built
765758
Artifact project = RepositoryUtils.toArtifact(mavenProject.getArtifact());
766759
result.add(resolveArtifact(project, ConfigurationVersion.NEW));
@@ -899,22 +892,9 @@ List<JApiCmpArchive> resolveDependencyToFile(final String parameterName,
899892
}
900893
}
901894
} else {
902-
String systemPath = dependency.getSystemPath();
903-
Pattern pattern = Pattern.compile("\\$\\{([^}])");
904-
Matcher matcher = pattern.matcher(systemPath);
905-
if (matcher.matches()) {
906-
for (int i = 1; i <= matcher.groupCount(); i++) {
907-
String property = matcher.group(i);
908-
String propertyResolved = mavenParameters.mavenProject().getProperties().getProperty(
909-
property);
910-
if (propertyResolved != null) {
911-
systemPath = systemPath.replaceAll("${" + property + "}", propertyResolved);
912-
} else {
913-
throw new MojoFailureException("Could not resolve property '" + property + "'.");
914-
}
915-
}
916-
}
917-
File file = new File(systemPath);
895+
// Substitute any properties in the path with their values
896+
final String systemPath = expandProperties(dependency.getSystemPath());
897+
final File file = new File(systemPath);
918898
boolean addFile = true;
919899
if (!file.exists()) {
920900
if (ignoreMissingArtifact(configurationVersion)) {
@@ -942,6 +922,30 @@ List<JApiCmpArchive> resolveDependencyToFile(final String parameterName,
942922
return jApiCmpArchives;
943923
}
944924

925+
/**
926+
* Expands any properties found in the given {@code String}.
927+
*
928+
* @param source the source {@code String} to expand
929+
* @return the source {@code String} with all properties expanded
930+
* @throws MojoFailureException if a property can't be resolved
931+
*/
932+
private String expandProperties(final String source) throws MojoFailureException {
933+
final StringBuffer newString = new StringBuffer();
934+
final Pattern pattern = Pattern.compile("\\$\\{([^}]*)}");
935+
final Matcher matcher = pattern.matcher(source);
936+
while (matcher.find()) {
937+
final String property = matcher.group(1);
938+
final String propertyResolved = mavenParameters.mavenProject().getProperties().getProperty(property);
939+
if (propertyResolved != null) {
940+
matcher.appendReplacement(newString, Matcher.quoteReplacement(propertyResolved));
941+
} else {
942+
throw new MojoFailureException("Could not resolve property '" + property + "'.");
943+
}
944+
}
945+
matcher.appendTail(newString);
946+
return newString.toString();
947+
}
948+
945949
private boolean ignoreMissingArtifact(final ConfigurationVersion configurationVersion) {
946950
return ignoreNonResolvableArtifacts()
947951
|| ignoreMissingOldVersion(configurationVersion)
Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
package japicmp.maven;
22

3+
import static java.nio.charset.Charset.defaultCharset;
34
import static org.hamcrest.CoreMatchers.not;
45
import static org.hamcrest.MatcherAssert.assertThat;
56
import static org.hamcrest.Matchers.containsString;
67
import static org.hamcrest.Matchers.empty;
78
import static org.hamcrest.Matchers.is;
89
import static org.hamcrest.io.FileMatchers.anExistingFile;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
911
import static org.mockito.Mockito.mock;
1012

11-
import java.io.File;
12-
import java.io.IOException;
13-
import java.nio.charset.Charset;
14-
import java.nio.file.Files;
15-
import java.nio.file.Path;
16-
import java.nio.file.Paths;
17-
import java.util.ArrayList;
18-
import java.util.Collections;
19-
import java.util.Comparator;
20-
import java.util.List;
21-
import java.util.stream.Collectors;
2213
import org.apache.commons.io.FileUtils;
2314
import org.apache.maven.plugin.MojoExecution;
2415
import org.apache.maven.project.MavenProject;
2516
import org.eclipse.aether.RepositorySystem;
2617
import org.eclipse.aether.RepositorySystemSession;
2718
import org.eclipse.aether.repository.RemoteRepository;
2819

20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.*;
26+
import java.util.regex.Pattern;
27+
import java.util.stream.Collectors;
28+
2929
abstract class AbstractTest {
3030

31-
final Path testDefaultDir = Paths.get("target/test-run/default/target");
31+
final Path testAnnotationsDir = Paths.get("target/test-run/annotations/target");
3232
final Path testConfigDir = Paths.get("target/test-run/configured/target");
33+
final Path testDefaultDir = Paths.get("target/test-run/default/target");
34+
final Path testMultipleDir = Paths.get("target/test-run/multiple/target");
35+
final Path testOverrideDir = Paths.get("target/test-run/override/target");
3336
final Path testSkipPomDir = Paths.get("target/test-run/skippom/target");
3437

3538
/**
@@ -39,11 +42,11 @@ abstract class AbstractTest {
3942
*/
4043
MavenParameters createMavenParameters() {
4144
final RemoteRepository remoteRepository = new RemoteRepository.Builder("default", "releases",
42-
"(https://repo.maven.apache.org/maven2").build();
45+
"(https://repo.maven.apache.org/maven2").build();
4346
return new MavenParameters(new ArrayList<>(), new MavenProject(),
44-
mock(MojoExecution.class), "", mock(RepositorySystem.class),
45-
mock(RepositorySystemSession.class),
46-
Collections.singletonList(remoteRepository));
47+
mock(MojoExecution.class), "", mock(RepositorySystem.class),
48+
mock(RepositorySystemSession.class),
49+
Collections.singletonList(remoteRepository));
4750
}
4851

4952
/**
@@ -54,11 +57,21 @@ MavenParameters createMavenParameters() {
5457
PluginParameters createPluginParameters(final ConfigParameters configParameters) {
5558
final Version oldVersion = createVersion("groupId", "artifactId", "0.1.0");
5659
final Version newVersion = createVersion("groupId", "artifactId", "0.1.1");
60+
return createPluginParameters(configParameters, oldVersion, newVersion);
61+
}
62+
63+
/**
64+
* Creates the PluginParameters for tests.
65+
*
66+
* @return the mocked PluginParameters
67+
*/
68+
PluginParameters createPluginParameters(final ConfigParameters configParameters, final Version oldVersion,
69+
final Version newVersion) {
5770
return new PluginParameters(false, newVersion, oldVersion, configParameters, new ArrayList<>(),
58-
null,
59-
null, false, new ArrayList<>(), new ArrayList<>(),
60-
new ArrayList<>(), new ArrayList<>(), new SkipReport(),
61-
new BreakBuild());
71+
null,
72+
null, false, new ArrayList<>(), new ArrayList<>(),
73+
new ArrayList<>(), new ArrayList<>(), new SkipReport(),
74+
new BreakBuild());
6275
}
6376

6477
/**
@@ -67,7 +80,6 @@ PluginParameters createPluginParameters(final ConfigParameters configParameters)
6780
* @param groupId the group ID of the Version
6881
* @param artifactId the artifact ID of the Version
6982
* @param version the version of the Version
70-
*
7183
* @return a new Version instance
7284
*/
7385
Version createVersion(String groupId, String artifactId, String version) {
@@ -80,7 +92,6 @@ Version createVersion(String groupId, String artifactId, String version) {
8092
* @param groupId the group ID of the Dependency
8193
* @param artifactId the artifact ID of the Dependency
8294
* @param version the version of the Dependency
83-
*
8495
* @return a new Dependency instance
8596
*/
8697
Dependency createDependency(String groupId, String artifactId, String version) {
@@ -91,15 +102,14 @@ Dependency createDependency(String groupId, String artifactId, String version) {
91102
return dependency;
92103
}
93104

94-
95105
void deleteDirectory(final Path dir) throws IOException {
96106
if (Files.exists(dir)) {
97107
if (!Files.isDirectory(dir)) {
98108
throw new IOException(dir + " is not a directory");
99109
}
100110

101111
// noinspection ResultOfMethodCallIgnored
102-
Files.walk(dir).sorted(Comparator.reverseOrder()).map(Path :: toFile).forEach(File :: delete);
112+
Files.walk(dir).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
103113
}
104114
}
105115

@@ -112,15 +122,45 @@ void assertFileNotExists(final File file) {
112122
}
113123

114124
void assertFileContains(final File file, final String contents) throws IOException {
115-
final String fileContents = FileUtils.readFileToString(file, Charset.defaultCharset());
116-
assertThat(fileContents, containsString(contents));
125+
final String fileContents = FileUtils.readFileToString(file, defaultCharset());
126+
assertThat(clean(fileContents), containsString(contents));
127+
}
128+
129+
void assertFileContainsPattern(final File file, final String contents) throws IOException {
130+
Pattern pattern = Pattern.compile(contents);
131+
final List<String> fileContents = FileUtils.readLines(file, defaultCharset());
132+
boolean passed = false;
133+
for (final String line : fileContents) {
134+
if (pattern.matcher(line).matches()) {
135+
passed = true;
136+
break;
137+
}
138+
}
139+
assertTrue(passed);
140+
}
141+
142+
void assertFileNotContainsPattern(final File file, final String contents) throws IOException {
143+
Pattern pattern = Pattern.compile(contents);
144+
final List<String> fileContents = FileUtils.readLines(file, defaultCharset());
145+
boolean passed = true;
146+
for (final String line : fileContents) {
147+
if (pattern.matcher(line).matches()) {
148+
passed = false;
149+
break;
150+
}
151+
}
152+
assertTrue(passed);
117153
}
118154

119155
void assertDirectoryEmpty(final Path dir) throws IOException {
120156
// A non-existent directory is considered empty
121157
if (Files.exists(dir)) {
122158
final List<Path> contents = Files.list(dir).collect(Collectors.toList());
123-
assertThat(dir.getFileName() + "is not empty", contents, is(empty()));
159+
assertThat(dir.getFileName() + " is not empty", contents, is(empty()));
124160
}
125161
}
162+
163+
private String clean(final String contents) {
164+
return contents.replace('\n', ' ');
165+
}
126166
}

0 commit comments

Comments
 (0)