11package japicmp .maven ;
22
3+ import static java .nio .charset .Charset .defaultCharset ;
34import static org .hamcrest .CoreMatchers .not ;
45import static org .hamcrest .MatcherAssert .assertThat ;
56import static org .hamcrest .Matchers .containsString ;
67import static org .hamcrest .Matchers .empty ;
78import static org .hamcrest .Matchers .is ;
89import static org .hamcrest .io .FileMatchers .anExistingFile ;
10+ import static org .junit .jupiter .api .Assertions .assertTrue ;
911import 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 ;
2213import org .apache .commons .io .FileUtils ;
2314import org .apache .maven .plugin .MojoExecution ;
2415import org .apache .maven .project .MavenProject ;
2516import org .eclipse .aether .RepositorySystem ;
2617import org .eclipse .aether .RepositorySystemSession ;
2718import 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+
2929abstract 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