1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }