View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.source;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Enumeration;
25  import java.util.Set;
26  import java.util.TreeSet;
27  import java.util.zip.ZipEntry;
28  import java.util.zip.ZipFile;
29  
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  
32  /**
33   * @author Stephane Nicoll
34   */
35  public abstract class AbstractSourcePluginTestCase extends AbstractMojoTestCase {
36  
37      protected static final String FINAL_NAME_PREFIX = "maven-source-plugin-test-";
38  
39      protected static final String FINAL_NAME_SUFFIX = "-99.0";
40  
41      protected abstract String getGoal();
42  
43      /**
44       * Execute the source plugin for the specified project.
45       *
46       * @param projectName the name of the project
47       * @param classifier The classifier.
48       * @throws Exception if an error occurred
49       */
50      protected void executeMojo(final String projectName, String classifier) throws Exception {
51          File testPom = new File(getBasedir(), getTestDir(projectName) + "/pom.xml");
52          AbstractSourceJarMojo mojo = (AbstractSourceJarMojo) lookupMojo(getGoal(), testPom);
53  
54          // Without the following line the tests will fail, cause the project.getFile() will result with null.
55          mojo.getProject().setFile(testPom);
56  
57          setVariableValueToObject(mojo, "classifier", classifier);
58  
59          mojo.execute();
60      }
61  
62      /**
63       * Executes the specified projects and asserts the given artifacts.
64       *
65       * @param projectName             the project to test
66       * @param expectSourceArchive     if a source archive is expected
67       * @param expectTestSourceArchive if a test source archive is expected
68       * @param expectedSourceFiles     the expected files in the source archive, if any
69       * @param expectedTestSourceFiles the expected files in the test source archive, if any
70       * @param classifier              the classifier.
71       * @return the base directory of the project
72       * @throws Exception if any error occurs
73       */
74      protected File doTestProject(
75              final String projectName,
76              boolean expectSourceArchive,
77              boolean expectTestSourceArchive,
78              final String[] expectedSourceFiles,
79              final String[] expectedTestSourceFiles,
80              String classifier)
81              throws Exception {
82          executeMojo(projectName, classifier);
83          final File testTargetDir = getTestTargetDir(projectName);
84  
85          if (expectSourceArchive) {
86              assertSourceArchive(testTargetDir, projectName);
87              assertJarContent(getSourceArchive(testTargetDir, projectName), expectedSourceFiles);
88          }
89  
90          if (expectTestSourceArchive) {
91              assertTestSourceArchive(testTargetDir, projectName);
92              assertJarContent(getTestSourceArchive(testTargetDir, projectName), expectedTestSourceFiles);
93          }
94  
95          return testTargetDir;
96      }
97  
98      /**
99       * Executes the specified projects and asserts the given artifacts for a source archive.
100      *
101      * @param projectName         the project to test
102      * @param expectedSourceFiles the expected files in the source archive, if any
103      * @param classifier          the classifier.
104      * @return the base directory of the project
105      * @throws Exception if any error occurs
106      */
107     protected File doTestProjectWithSourceArchive(
108             final String projectName, final String[] expectedSourceFiles, String classifier) throws Exception {
109         return doTestProject(projectName, true, false, expectedSourceFiles, null, classifier);
110     }
111 
112     /**
113      * Executes the specified projects and asserts the given artifacts for a test source archive.
114      *
115      * @param projectName             the project to test
116      * @param expectedTestSourceFiles the expected files in the test source archive, if any
117      * @param classifier              the classifier.
118      * @return the base directory of the project
119      * @throws Exception if any error occurs
120      */
121     protected File doTestProjectWithTestSourceArchive(
122             final String projectName, final String[] expectedTestSourceFiles, String classifier) throws Exception {
123         return doTestProject(projectName, false, true, null, expectedTestSourceFiles, classifier);
124     }
125 
126     protected void assertSourceArchive(final File testTargetDir, final String projectName) {
127         final File expectedFile = getSourceArchive(testTargetDir, projectName);
128         assertTrue("Source archive does not exist[" + expectedFile.getAbsolutePath() + "]", expectedFile.exists());
129     }
130 
131     protected void assertTestSourceArchive(final File testTargetDir, final String projectName) {
132         final File expectedFile = getTestSourceArchive(testTargetDir, projectName);
133         assertTrue("Test source archive does not exist[" + expectedFile.getAbsolutePath() + "]", expectedFile.exists());
134     }
135 
136     protected File getSourceArchive(final File testTargetDir, final String projectName) {
137         return new File(testTargetDir, buildFinalSourceName(projectName) + ".jar");
138     }
139 
140     protected File getTestSourceArchive(final File testTargetDir, final String projectName) {
141         return new File(testTargetDir, buildFinalTestSourceName(projectName) + ".jar");
142     }
143 
144     protected String buildFinalSourceName(final String projectName) {
145         return FINAL_NAME_PREFIX + projectName + FINAL_NAME_SUFFIX + "-sources";
146     }
147 
148     protected String buildFinalTestSourceName(final String projectName) {
149         return FINAL_NAME_PREFIX + projectName + FINAL_NAME_SUFFIX + "-test-sources";
150     }
151 
152     protected File getTestDir(String projectName) throws IOException {
153         File f = new File("target/test-classes/unit/" + projectName);
154         if (!new File(f, "pom.xml").exists()) {
155             throw new IllegalStateException("No pom file found in " + f.getPath());
156         }
157         return f;
158     }
159 
160     protected void assertJarContent(final File jarFile, final String[] expectedFiles) throws IOException {
161         ZipFile jar = new ZipFile(jarFile);
162         Enumeration<? extends ZipEntry> entries = jar.entries();
163 
164         if (expectedFiles.length == 0) {
165             assertFalse("Jar file should not contain any entry", entries.hasMoreElements());
166         } else {
167             assertTrue(entries.hasMoreElements());
168 
169             Set<String> expected = new TreeSet<>(Arrays.asList(expectedFiles));
170 
171             while (entries.hasMoreElements()) {
172                 ZipEntry entry = entries.nextElement();
173 
174                 assertTrue("Not expecting " + entry.getName() + " in " + jarFile, expected.remove(entry.getName()));
175             }
176 
177             assertTrue("Missing entries " + expected.toString() + " in " + jarFile, expected.isEmpty());
178         }
179 
180         jar.close();
181     }
182 
183     protected File getTestTargetDir(String projectName) {
184         return new File(getBasedir(), "target/test/unit/" + projectName + "/target");
185     }
186 }