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 static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
31  import static org.junit.jupiter.api.Assertions.assertFalse;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  /**
35   * @author Stephane Nicoll
36   */
37  public abstract class AbstractSourcePluginTestCase {
38  
39      protected static final String FINAL_NAME_PREFIX = "maven-source-plugin-test-";
40  
41      protected static final String FINAL_NAME_SUFFIX = "-99.0";
42  
43      protected void assertSourceArchive(final File testTargetDir, final String projectName) {
44          final File expectedFile = getSourceArchive(testTargetDir, projectName);
45          assertTrue(expectedFile.exists(), "Source archive does not exist[" + expectedFile.getAbsolutePath() + "]");
46      }
47  
48      protected void assertTestSourceArchive(final File testTargetDir, final String projectName) {
49          final File expectedFile = getTestSourceArchive(testTargetDir, projectName);
50          assertTrue(expectedFile.exists(), "Test source archive does not exist[" + expectedFile.getAbsolutePath() + "]");
51      }
52  
53      protected File getSourceArchive(final File testTargetDir, final String projectName) {
54          return new File(testTargetDir, buildFinalSourceName(projectName) + ".jar");
55      }
56  
57      protected File getTestSourceArchive(final File testTargetDir, final String projectName) {
58          return new File(testTargetDir, buildFinalTestSourceName(projectName) + ".jar");
59      }
60  
61      protected String buildFinalSourceName(final String projectName) {
62          return FINAL_NAME_PREFIX + projectName + FINAL_NAME_SUFFIX + "-sources";
63      }
64  
65      protected String buildFinalTestSourceName(final String projectName) {
66          return FINAL_NAME_PREFIX + projectName + FINAL_NAME_SUFFIX + "-test-sources";
67      }
68  
69      protected void assertJarContent(final File jarFile, final String[] expectedFiles) throws IOException {
70          ZipFile jar = new ZipFile(jarFile);
71          Enumeration<? extends ZipEntry> entries = jar.entries();
72  
73          if (expectedFiles.length == 0) {
74              assertFalse(entries.hasMoreElements(), "Jar file should not contain any entry");
75          } else {
76              assertTrue(entries.hasMoreElements());
77  
78              Set<String> expected = new TreeSet<>(Arrays.asList(expectedFiles));
79  
80              while (entries.hasMoreElements()) {
81                  ZipEntry entry = entries.nextElement();
82  
83                  assertTrue(expected.remove(entry.getName()), "Not expecting " + entry.getName() + " in " + jarFile);
84              }
85  
86              assertTrue(expected.isEmpty(), "Missing entries " + expected + " in " + jarFile);
87          }
88  
89          jar.close();
90      }
91  
92      protected File getTestTargetDir(String projectName) {
93          return new File(getBasedir(), "target/test-classes/unit/" + projectName + "/target");
94      }
95  }