1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.war;
20
21 import java.io.File;
22 import java.io.FileFilter;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
30 import org.apache.maven.plugins.war.stub.MavenProjectArtifactsStub;
31 import org.codehaus.plexus.util.FileUtils;
32
33
34
35
36 public abstract class AbstractWarExplodedMojoTest extends AbstractWarMojoTest {
37
38 protected WarExplodedMojo mojo;
39
40 public void setUp() throws Exception {
41 super.setUp();
42 mojo = (WarExplodedMojo) lookupMojo("exploded", getPomFile());
43 }
44
45
46
47
48
49
50 protected abstract File getPomFile();
51
52
53
54
55
56
57 protected abstract File getTestDirectory();
58
59
60
61
62
63
64
65
66
67
68
69
70 protected File setUpMojo(final String testId, ArtifactStub[] artifactStubs, String[] sourceFiles) throws Exception {
71 final MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
72 final File webAppDirectory = new File(getTestDirectory(), testId);
73
74
75 File webAppSource;
76 if (sourceFiles == null) {
77 webAppSource = createWebAppSource(testId);
78 } else {
79 webAppSource = createWebAppSource(testId, false);
80 for (String sourceFile : sourceFiles) {
81 File sample = new File(webAppSource, sourceFile);
82 createFile(sample);
83 }
84 }
85
86 final File classesDir = createClassesDir(testId, true);
87 final File workDirectory = new File(getTestDirectory(), "/war/work-" + testId);
88 createDir(workDirectory);
89
90 if (artifactStubs != null) {
91 for (ArtifactStub artifactStub : artifactStubs) {
92 project.addArtifact(artifactStub);
93 }
94 }
95
96 configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
97 setVariableValueToObject(mojo, "workDirectory", workDirectory);
98
99 return webAppDirectory;
100 }
101
102
103
104
105
106
107
108
109
110 protected File setUpMojo(final String testId, ArtifactStub[] artifactStubs) throws Exception {
111 return setUpMojo(testId, artifactStubs, null);
112 }
113
114
115
116
117
118
119
120 protected void cleanDirectory(File directory) throws IOException {
121 if (directory != null && directory.isDirectory() && directory.exists()) {
122 FileUtils.deleteDirectory(directory);
123 }
124 }
125
126
127
128
129
130
131
132 protected List<File> assertDefaultContent(File webAppDirectory) {
133
134 File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
135 File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
136
137 assertTrue("source file not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
138 assertTrue("source file not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
139
140 final List<File> content = new ArrayList<>();
141 content.add(expectedWebSourceFile);
142 content.add(expectedWebSource2File);
143
144 return content;
145 }
146
147
148
149
150
151
152
153 protected List<File> assertWebXml(File webAppDirectory) {
154 File expectedWEBXMLFile = new File(webAppDirectory, "WEB-INF/web.xml");
155 assertTrue("web xml not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists());
156
157 final List<File> content = new ArrayList<>();
158 content.add(expectedWEBXMLFile);
159
160 return content;
161 }
162
163
164
165
166
167
168
169
170
171 protected List<File> assertCustomContent(File webAppDirectory, String[] filePaths, String customMessage) {
172 final List<File> content = new ArrayList<>();
173 for (String filePath : filePaths) {
174 final File expectedFile = new File(webAppDirectory, filePath);
175 if (customMessage != null) {
176 assertTrue(customMessage + " - " + expectedFile.toString(), expectedFile.exists());
177 } else {
178 assertTrue("source file not found: " + expectedFile.toString(), expectedFile.exists());
179 }
180 content.add(expectedFile);
181 }
182 return content;
183 }
184
185
186
187
188
189
190
191
192 protected void assertWebAppContent(File webAppDirectory, List<File> expectedFiles, FileFilter filter) {
193 final List<File> webAppContent = new ArrayList<>();
194 if (filter != null) {
195 buildFilesList(webAppDirectory, filter, webAppContent);
196 } else {
197 buildFilesList(webAppDirectory, new FileFilterImpl(webAppDirectory, null), webAppContent);
198 }
199
200
201 Collections.sort(expectedFiles);
202 Collections.sort(webAppContent);
203 assertEquals(
204 "Invalid webapp content, expected " + expectedFiles.size() + "file(s) " + expectedFiles + " but got "
205 + webAppContent.size() + " file(s) " + webAppContent,
206 expectedFiles,
207 webAppContent);
208 }
209
210
211
212
213
214
215
216
217
218
219
220 private void buildFilesList(final File dir, FileFilter filter, final List<File> content) {
221 final File[] files = dir.listFiles();
222
223 for (File file : files) {
224
225 if (filter.accept(file)) {
226 content.add(file);
227 }
228
229
230 if (file.isDirectory()) {
231 buildFilesList(file, filter, content);
232 }
233 }
234 }
235
236 class FileFilterImpl implements FileFilter {
237
238 private final List<String> rejectedFilePaths;
239
240 private final int webAppDirIndex;
241
242 FileFilterImpl(File webAppDirectory, String[] rejectedFilePaths) {
243 if (rejectedFilePaths != null) {
244 this.rejectedFilePaths = Arrays.asList(rejectedFilePaths);
245 } else {
246 this.rejectedFilePaths = new ArrayList<>();
247 }
248 this.webAppDirIndex = webAppDirectory.getAbsolutePath().length() + 1;
249 }
250
251 public boolean accept(File file) {
252 String effectiveRelativePath = buildRelativePath(file);
253 return !(rejectedFilePaths.contains(effectiveRelativePath) || file.isDirectory());
254 }
255
256 private String buildRelativePath(File f) {
257 return f.getAbsolutePath().substring(webAppDirIndex);
258 }
259 }
260 }