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 org.apache.maven.plugin.testing.AbstractMojoTestCase;
31
32
33
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
45
46
47
48
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
55 mojo.getProject().setFile(testPom);
56
57 setVariableValueToObject(mojo, "classifier", classifier);
58
59 mojo.execute();
60 }
61
62
63
64
65
66
67
68
69
70
71
72
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
100
101
102
103
104
105
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
114
115
116
117
118
119
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 }