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.war;
20  
21  import java.io.File;
22  import java.text.SimpleDateFormat;
23  import java.util.Locale;
24  
25  import org.apache.maven.artifact.handler.ArtifactHandler;
26  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
27  import org.apache.maven.plugins.war.stub.AarArtifactStub;
28  import org.apache.maven.plugins.war.stub.EJBArtifactStub;
29  import org.apache.maven.plugins.war.stub.EJBArtifactStubWithClassifier;
30  import org.apache.maven.plugins.war.stub.EJBClientArtifactStub;
31  import org.apache.maven.plugins.war.stub.IncludeExcludeWarArtifactStub;
32  import org.apache.maven.plugins.war.stub.JarArtifactStub;
33  import org.apache.maven.plugins.war.stub.MarArtifactStub;
34  import org.apache.maven.plugins.war.stub.MavenProjectArtifactsStub;
35  import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
36  import org.apache.maven.plugins.war.stub.PARArtifactStub;
37  import org.apache.maven.plugins.war.stub.ResourceStub;
38  import org.apache.maven.plugins.war.stub.TLDArtifactStub;
39  import org.apache.maven.plugins.war.stub.WarArtifactStub;
40  import org.apache.maven.plugins.war.stub.XarArtifactStub;
41  import org.codehaus.plexus.util.FileUtils;
42  
43  import static org.junit.Assert.assertNotEquals;
44  
45  public class WarExplodedMojoTest extends AbstractWarExplodedMojoTest {
46  
47      @Override
48      protected File getPomFile() {
49          return new File(getBasedir(), "/target/test-classes/unit/warexplodedmojo/plugin-config.xml");
50      }
51  
52      @Override
53      protected File getTestDirectory() {
54          return new File(getBasedir(), "target/test-classes/unit/warexplodedmojo/test-dir");
55      }
56  
57      /**
58       * @throws Exception in case of an error.
59       */
60      public void testSimpleExplodedWar() throws Exception {
61          // setup test data
62          String testId = "SimpleExplodedWar";
63          MavenProjectBasicStub project = new MavenProjectBasicStub();
64          File webAppSource = createWebAppSource(testId);
65          File classesDir = createClassesDir(testId, false);
66          File webAppResource = new File(getTestDirectory(), testId + "-resources");
67          File webAppDirectory = new File(getTestDirectory(), testId);
68          File sampleResource = new File(webAppResource, "pix/panis_na.jpg");
69          ResourceStub[] resources = new ResourceStub[] {new ResourceStub()};
70  
71          createFile(sampleResource);
72  
73          assertTrue("sampeResource not found", sampleResource.exists());
74  
75          // configure mojo
76          resources[0].setDirectory(webAppResource.getAbsolutePath());
77          this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
78          setVariableValueToObject(mojo, "webResources", resources);
79          mojo.execute();
80  
81          // validate operation
82          File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
83          File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
84          File expectedWebResourceFile = new File(webAppDirectory, "pix/panis_na.jpg");
85          File expectedWEBINFDir = new File(webAppDirectory, "WEB-INF");
86          File expectedMETAINFDir = new File(webAppDirectory, "META-INF");
87  
88          assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
89          assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
90          assertTrue("resources doesn't exist: " + expectedWebResourceFile, expectedWebResourceFile.exists());
91          assertTrue("WEB-INF not found", expectedWEBINFDir.exists());
92          assertTrue("META-INF not found", expectedMETAINFDir.exists());
93  
94          // housekeeping
95          expectedWebSourceFile.delete();
96          expectedWebSource2File.delete();
97          expectedWebResourceFile.delete();
98      }
99  
100     /**
101      * @throws Exception in case of an error.
102      */
103     public void testSimpleExplodedWarWTargetPath() throws Exception {
104         // setup test data
105         String testId = "SimpleExplodedWar";
106         MavenProjectBasicStub project = new MavenProjectBasicStub();
107         File webAppSource = createWebAppSource(testId);
108         File classesDir = createClassesDir(testId, false);
109         File webAppResource = new File(getTestDirectory(), "resources");
110         File webAppDirectory = new File(getTestDirectory(), testId);
111         File sampleResource = new File(webAppResource, "pix/panis_na.jpg");
112         ResourceStub[] resources = new ResourceStub[] {new ResourceStub()};
113 
114         createFile(sampleResource);
115 
116         // configure mojo
117         resources[0].setDirectory(webAppResource.getAbsolutePath());
118         resources[0].setTargetPath("targetPath");
119         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
120         setVariableValueToObject(mojo, "webResources", resources);
121         mojo.execute();
122 
123         // validate operation
124         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
125         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
126         File expectedWebResourceFile = new File(webAppDirectory, "targetPath/pix/panis_na.jpg");
127         File expectedWEBINFDir = new File(webAppDirectory, "WEB-INF");
128         File expectedMETAINFDir = new File(webAppDirectory, "META-INF");
129 
130         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
131         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
132         assertTrue("resources doesn't exist: " + expectedWebResourceFile, expectedWebResourceFile.exists());
133         assertTrue("WEB-INF not found", expectedWEBINFDir.exists());
134         assertTrue("META-INF not found", expectedMETAINFDir.exists());
135 
136         // housekeeping
137         expectedWebSourceFile.delete();
138         expectedWebSource2File.delete();
139         expectedWebResourceFile.delete();
140     }
141 
142     /**
143      * @throws Exception in case of an error.
144      */
145     public void testExplodedWarWithCustomWebXML() throws Exception {
146         // setup test data
147         String testId = "ExplodedWarWithCustomWebXML";
148         MavenProjectBasicStub project = new MavenProjectBasicStub();
149         File webAppSource = createWebAppSource(testId);
150         File classesDir = createClassesDir(testId, true);
151         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
152         File webAppDirectory = new File(getTestDirectory(), testId);
153 
154         // configure mojo
155         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
156         mojo.setWebXml(new File(xmlSource, "web.xml"));
157         mojo.execute();
158 
159         // validate operation
160         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
161         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
162         File expectedWEBXMLFile = new File(webAppDirectory, "WEB-INF/web.xml");
163         File expectedMETAINFDir = new File(webAppDirectory, "META-INF");
164 
165         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
166         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
167         assertTrue("WEB XML not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists());
168         assertTrue("META-INF not found", expectedMETAINFDir.exists());
169         assertEquals("WEB XML not correct", mojo.getWebXml().toString(), FileUtils.fileRead(expectedWEBXMLFile));
170 
171         // housekeeping
172         expectedWebSourceFile.delete();
173         expectedWebSource2File.delete();
174         expectedWEBXMLFile.delete();
175         expectedMETAINFDir.delete();
176     }
177 
178     /**
179      * @throws Exception in case of an error.
180      */
181     public void testExplodedWarWithContainerConfigXML() throws Exception {
182         // setup test data
183         String testId = "ExplodedWarWithContainerConfigXML";
184         MavenProjectBasicStub project = new MavenProjectBasicStub();
185         File classesDir = createClassesDir(testId, true);
186         File webAppSource = createWebAppSource(testId);
187         File xmlSource = createXMLConfigDir(testId, new String[] {"config.xml"});
188         File webAppDirectory = new File(getTestDirectory(), testId);
189 
190         // configure mojo
191         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
192         mojo.setContainerConfigXML(new File(xmlSource, "config.xml"));
193         mojo.execute();
194 
195         // validate operation
196         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
197         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
198         File expectedContainerConfigXMLFile = new File(webAppDirectory, "META-INF/config.xml");
199         File expectedWEBINFDir = new File(webAppDirectory, "WEB-INF");
200 
201         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
202         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
203         assertTrue("WEB-INF not found", expectedWEBINFDir.exists());
204         assertTrue(
205                 "Container Config XML not found:" + expectedContainerConfigXMLFile.toString(),
206                 expectedContainerConfigXMLFile.exists());
207 
208         // housekeeping
209         expectedWebSourceFile.delete();
210         expectedWebSource2File.delete();
211         expectedContainerConfigXMLFile.delete();
212         expectedWEBINFDir.delete();
213     }
214 
215     /**
216      * @throws Exception in case of an error.
217      */
218     public void testExplodedWarWithSimpleExternalWARFile() throws Exception {
219         // setup test data
220         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
221         WarArtifactStub warArtifact = new WarArtifactStub(getBasedir());
222 
223         String testId = "ExplodedWarWithSimpleExternalWARFile";
224         File webAppDirectory = new File(getTestDirectory(), testId);
225         File webAppSource = createWebAppSource(testId);
226         File classesDir = createClassesDir(testId, true);
227         File workDirectory = new File(getTestDirectory(), "/war/work-" + testId);
228         File simpleWarFile = warArtifact.getFile();
229 
230         assertTrue("simple war not found: " + simpleWarFile.toString(), simpleWarFile.exists());
231 
232         createDir(workDirectory);
233 
234         // configure mojo
235         project.addArtifact(warArtifact);
236         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
237         setVariableValueToObject(mojo, "workDirectory", workDirectory);
238         mojo.execute();
239 
240         // validate operation - META-INF is automatically excluded so remove the file from the list
241         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
242         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
243         File expectedWEBXMLFile = new File(webAppDirectory, "WEB-INF/web.xml");
244         File expectedWARFile = new File(webAppDirectory, "/org/sample/company/test.jsp");
245 
246         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
247         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
248         // check simple.war in the unit test dir under resources to verify the list of files
249         assertTrue("web xml not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists());
250         assertTrue("war file not found: " + expectedWARFile.toString(), expectedWARFile.exists());
251 
252         // housekeeping
253         expectedWebSourceFile.delete();
254         expectedWebSource2File.delete();
255         expectedWEBXMLFile.delete();
256         expectedWARFile.delete();
257     }
258 
259     /**
260      * Merge a dependent WAR when a file in the war source directory overrides one found in the WAR.
261      * @throws Exception in case of an error.
262      */
263     public void testExplodedWarMergeWarLocalFileOverride() throws Exception {
264         // setup test data
265         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
266         WarArtifactStub warArtifact = new WarArtifactStub(getBasedir());
267 
268         String testId = "testExplodedWarMergeWarLocalFileOverride";
269         File webAppDirectory = new File(getTestDirectory(), testId);
270         File webAppSource = getWebAppSource(testId);
271         File simpleJSP = new File(webAppSource, "org/sample/company/test.jsp");
272         createFile(simpleJSP);
273 
274         File workDirectory = new File(getTestDirectory(), "/war/work-" + testId);
275         createDir(workDirectory);
276 
277         File classesDir = createClassesDir(testId, true);
278 
279         // configure mojo
280         project.addArtifact(warArtifact);
281         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
282         setVariableValueToObject(mojo, "workDirectory", workDirectory);
283         mojo.execute();
284 
285         // validate operation
286         File expectedFile = new File(webAppDirectory, "/org/sample/company/test.jsp");
287 
288         assertTrue("file not found: " + expectedFile.toString(), expectedFile.exists());
289         assertEquals("file incorrect", simpleJSP.toString(), FileUtils.fileRead(expectedFile));
290 
291         // check when the merged war file is newer - so set an old time on the local file
292         long time =
293                 new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse("2005-1-1").getTime();
294         simpleJSP.setLastModified(time);
295         expectedFile.setLastModified(time);
296 
297         project.addArtifact(warArtifact);
298         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
299         setVariableValueToObject(mojo, "workDirectory", workDirectory);
300         mojo.execute();
301 
302         assertTrue("file not found: " + expectedFile.toString(), expectedFile.exists());
303         assertEquals("file incorrect", simpleJSP.toString(), FileUtils.fileRead(expectedFile));
304 
305         // housekeeping
306         expectedFile.delete();
307     }
308 
309     /**
310      * @throws Exception in case of an error.
311      */
312     public void testExplodedWarWithEJB() throws Exception {
313         // setup test data
314         String testId = "ExplodedWarWithEJB";
315         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
316         File webAppDirectory = new File(getTestDirectory(), testId);
317         File webAppSource = createWebAppSource(testId);
318         File classesDir = createClassesDir(testId, true);
319         EJBArtifactStub ejbArtifact = new EJBArtifactStub(getBasedir());
320         File ejbFile = ejbArtifact.getFile();
321 
322         assertTrue("ejb jar not found: " + ejbFile.toString(), ejbFile.exists());
323 
324         // configure mojo
325         project.addArtifact(ejbArtifact);
326         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
327         mojo.execute();
328 
329         // validate operation
330         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
331         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
332         // final name form is <artifactId>-<version>.<type>
333         File expectedEJBArtifact = new File(webAppDirectory, "WEB-INF/lib/ejbartifact-0.0-Test.jar");
334 
335         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
336         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
337         assertTrue("ejb artifact not found: " + expectedEJBArtifact.toString(), expectedEJBArtifact.exists());
338 
339         // housekeeping
340         expectedWebSourceFile.delete();
341         expectedWebSource2File.delete();
342         expectedEJBArtifact.delete();
343     }
344 
345     public void testExplodedWarWithJar() throws Exception {
346         // setup test data
347         String testId = "ExplodedWarWithJar";
348         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
349         File webAppDirectory = new File(getTestDirectory(), testId);
350         File webAppSource = createWebAppSource(testId);
351         File classesDir = createClassesDir(testId, true);
352         ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
353         ArtifactStub jarArtifact = new JarArtifactStub(getBasedir(), artifactHandler);
354         File jarFile = jarArtifact.getFile();
355 
356         assertTrue("jar not found: " + jarFile.toString(), jarFile.exists());
357 
358         // configure mojo
359         project.addArtifact(jarArtifact);
360         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
361         mojo.execute();
362 
363         // validate operation
364         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
365         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
366         // final name form is <artifactId>-<version>.<type>
367         File expectedJarArtifact = new File(webAppDirectory, "WEB-INF/lib/jarartifact-0.0-Test.jar");
368 
369         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
370         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
371         assertTrue("jar artifact not found: " + expectedJarArtifact.toString(), expectedJarArtifact.exists());
372 
373         // housekeeping
374         expectedWebSourceFile.delete();
375         expectedWebSource2File.delete();
376         expectedJarArtifact.delete();
377     }
378 
379     /**
380      * @throws Exception in case of an error.
381      */
382     public void testExplodedWarWithEJBClient() throws Exception {
383         // setup test data
384         String testId = "ExplodedWarWithEJB";
385         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
386         File webAppDirectory = new File(getTestDirectory(), testId);
387         File webAppSource = createWebAppSource(testId);
388         File classesDir = createClassesDir(testId, true);
389         EJBClientArtifactStub ejbArtifact = new EJBClientArtifactStub(getBasedir());
390         File ejbFile = ejbArtifact.getFile();
391 
392         assertTrue("ejb jar not found: " + ejbFile.toString(), ejbFile.exists());
393 
394         // configure mojo
395         project.addArtifact(ejbArtifact);
396         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
397         mojo.execute();
398 
399         // validate operation
400         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
401         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
402         // final name form is <artifactId>-<version>.<type>
403         File expectedEJBArtifact = new File(webAppDirectory, "WEB-INF/lib/ejbclientartifact-0.0-Test-client.jar");
404 
405         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
406         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
407         assertTrue("ejb artifact not found: " + expectedEJBArtifact.toString(), expectedEJBArtifact.exists());
408 
409         // housekeeping
410         expectedWebSourceFile.delete();
411         expectedWebSource2File.delete();
412         expectedEJBArtifact.delete();
413     }
414 
415     /**
416      * @throws Exception in case of an error.
417      */
418     public void testExplodedWarWithTLD() throws Exception {
419         // setup test data
420         String testId = "ExplodedWarWithTLD";
421         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
422         File webAppDirectory = new File(getTestDirectory(), testId);
423         File webAppSource = createWebAppSource(testId);
424         File classesDir = createClassesDir(testId, true);
425         TLDArtifactStub tldArtifact = new TLDArtifactStub(getBasedir());
426         File tldFile = tldArtifact.getFile();
427 
428         assertTrue("tld jar not found: " + tldFile.getAbsolutePath(), tldFile.exists());
429 
430         // configure mojo
431         project.addArtifact(tldArtifact);
432         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
433         mojo.execute();
434 
435         // validate operation
436         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
437         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
438         // final name form is <artifactId>-<version>.<type>
439         File expectedTLDArtifact = new File(webAppDirectory, "WEB-INF/tld/tldartifact-0.0-Test.tld");
440 
441         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
442         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
443         assertTrue("tld artifact not found: " + expectedTLDArtifact.toString(), expectedTLDArtifact.exists());
444 
445         // housekeeping
446         expectedWebSourceFile.delete();
447         expectedWebSource2File.delete();
448         expectedTLDArtifact.delete();
449     }
450 
451     /**
452      * @throws Exception in case of an error.
453      */
454     public void testExplodedWarWithPAR() throws Exception {
455         // setup test data
456         String testId = "ExplodedWarWithPAR";
457         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
458         File webAppDirectory = new File(getTestDirectory(), testId);
459         File webAppSource = createWebAppSource(testId);
460         File classesDir = createClassesDir(testId, true);
461         PARArtifactStub parartifact = new PARArtifactStub(getBasedir());
462         File parFile = parartifact.getFile();
463 
464         assertTrue("par not found: " + parFile.getAbsolutePath(), parFile.exists());
465 
466         // configure mojo
467         project.addArtifact(parartifact);
468         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
469         mojo.execute();
470 
471         // validate operation
472         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
473         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
474         // final name form is <artifactId>-<version>.<type>
475         File expectedPARArtifact = new File(webAppDirectory, "WEB-INF/lib/parartifact-0.0-Test.jar");
476 
477         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
478         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
479         assertTrue("par artifact not found: " + expectedPARArtifact.toString(), expectedPARArtifact.exists());
480 
481         // housekeeping
482         expectedWebSourceFile.delete();
483         expectedWebSource2File.delete();
484         expectedPARArtifact.delete();
485     }
486 
487     /**
488      * @throws Exception in case of an error.
489      */
490     public void testExplodedWarWithAar() throws Exception {
491         // setup test data
492         String testId = "ExplodedWarWithAar";
493         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
494         File webAppDirectory = new File(getTestDirectory(), testId);
495         File webAppSource = createWebAppSource(testId);
496         File classesDir = createClassesDir(testId, true);
497         // Fake here since the aar artifact handler does not exist: no biggie
498         ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
499         ArtifactStub aarArtifact = new AarArtifactStub(getBasedir(), artifactHandler);
500         File aarFile = aarArtifact.getFile();
501 
502         assertTrue("jar not found: " + aarFile.toString(), aarFile.exists());
503 
504         // configure mojo
505         project.addArtifact(aarArtifact);
506         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
507         mojo.execute();
508 
509         // validate operation
510         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
511         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
512         // final name form is <artifactId>-<version>.<type>
513         File expectedJarArtifact = new File(webAppDirectory, "WEB-INF/services/aarartifact-0.0-Test.jar");
514 
515         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
516         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
517         assertTrue("jar artifact not found: " + expectedJarArtifact.toString(), expectedJarArtifact.exists());
518 
519         // housekeeping
520         expectedWebSourceFile.delete();
521         expectedWebSource2File.delete();
522         expectedJarArtifact.delete();
523     }
524 
525     /**
526      * @throws Exception in case of an error.
527      */
528     public void testExplodedWarWithMar() throws Exception {
529         // setup test data
530         String testId = "ExplodedWarWithMar";
531         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
532         File webAppDirectory = new File(getTestDirectory(), testId);
533         File webAppSource = createWebAppSource(testId);
534         File classesDir = createClassesDir(testId, true);
535         // Fake here since the mar artifact handler does not exist: no biggie
536         ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
537         ArtifactStub marArtifact = new MarArtifactStub(getBasedir(), artifactHandler);
538         File marFile = marArtifact.getFile();
539 
540         assertTrue("jar not found: " + marFile.toString(), marFile.exists());
541 
542         // configure mojo
543         project.addArtifact(marArtifact);
544         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
545         mojo.execute();
546 
547         // validate operation
548         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
549         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
550         // final name form is <artifactId>-<version>.<type>
551         File expectedJarArtifact = new File(webAppDirectory, "WEB-INF/modules/marartifact-0.0-Test.jar");
552 
553         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
554         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
555         assertTrue("jar artifact not found: " + expectedJarArtifact.toString(), expectedJarArtifact.exists());
556 
557         // housekeeping
558         expectedWebSourceFile.delete();
559         expectedWebSource2File.delete();
560         expectedJarArtifact.delete();
561     }
562 
563     /**
564      * @throws Exception in case of an error.
565      */
566     public void testExplodedWarWithXar() throws Exception {
567         // setup test data
568         String testId = "ExplodedWarWithXar";
569         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
570         File webAppDirectory = new File(getTestDirectory(), testId);
571         File webAppSource = createWebAppSource(testId);
572         File classesDir = createClassesDir(testId, true);
573         // Fake here since the xar artifact handler does not exist: no biggie
574         ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
575         ArtifactStub xarArtifact = new XarArtifactStub(getBasedir(), artifactHandler);
576         File xarFile = xarArtifact.getFile();
577 
578         assertTrue("jar not found: " + xarFile.toString(), xarFile.exists());
579 
580         // configure mojo
581         project.addArtifact(xarArtifact);
582         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
583         mojo.execute();
584 
585         // validate operation
586         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
587         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
588         // final name form is <artifactId>-<version>.<type>
589         File expectedJarArtifact = new File(webAppDirectory, "WEB-INF/extensions/xarartifact-0.0-Test.jar");
590 
591         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
592         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
593         assertTrue("jar artifact not found: " + expectedJarArtifact.toString(), expectedJarArtifact.exists());
594 
595         // housekeeping
596         expectedWebSourceFile.delete();
597         expectedWebSource2File.delete();
598         expectedJarArtifact.delete();
599     }
600 
601     /**
602      * @throws Exception in case of an error.
603      */
604     public void testExplodedWarWithDuplicateDependencies() throws Exception {
605         // setup test data
606         String testId = "ExplodedWarWithDuplicateDependencies";
607         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
608         File webAppDirectory = new File(getTestDirectory(), testId);
609         File webAppSource = createWebAppSource(testId);
610         File classesDir = createClassesDir(testId, true);
611         EJBArtifactStub ejbArtifact = new EJBArtifactStub(getBasedir());
612         EJBArtifactStub ejbArtifactDup = new EJBArtifactStub(getBasedir());
613         File ejbFile = ejbArtifact.getFile();
614 
615         // ejbArtifact has a hard coded file, only one assert is needed
616         assertTrue("ejb not found: " + ejbFile.getAbsolutePath(), ejbFile.exists());
617 
618         // configure mojo
619         ejbArtifact.setGroupId("org.sample.ejb");
620         ejbArtifactDup.setGroupId("org.dup.ejb");
621         project.addArtifact(ejbArtifact);
622         project.addArtifact(ejbArtifactDup);
623         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
624         mojo.execute();
625 
626         // validate operation
627         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
628         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
629         // final name form is <artifactId>-<version>.<type>
630         File expectedEJBArtifact = new File(webAppDirectory, "WEB-INF/lib/org.sample.ejb-ejbartifact-0.0-Test.jar");
631         File expectedEJBDupArtifact = new File(webAppDirectory, "WEB-INF/lib/org.dup.ejb-ejbartifact-0.0-Test.jar");
632 
633         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
634         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
635         assertTrue("ejb artifact not found: " + expectedEJBArtifact.toString(), expectedEJBArtifact.exists());
636         assertTrue("ejb dup artifact not found: " + expectedEJBDupArtifact.toString(), expectedEJBDupArtifact.exists());
637 
638         // housekeeping
639         expectedWebSourceFile.delete();
640         expectedWebSource2File.delete();
641         expectedEJBArtifact.delete();
642         expectedEJBDupArtifact.delete();
643     }
644 
645     /**
646      * @throws Exception in case of an error.
647      */
648     public void testExplodedWarDuplicateWithClassifier() throws Exception {
649         // setup test data
650         String testId = "ExplodedWarDuplicateWithClassifier";
651         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
652         File webAppDirectory = new File(getTestDirectory(), testId);
653         File webAppSource = createWebAppSource(testId);
654         File classesDir = createClassesDir(testId, true);
655         EJBArtifactStub ejbArtifact = new EJBArtifactStub(getBasedir());
656         EJBArtifactStubWithClassifier ejbArtifactDup = new EJBArtifactStubWithClassifier(getBasedir());
657 
658         File ejbFile = ejbArtifact.getFile();
659 
660         // ejbArtifact has a hard coded file, only one assert is needed
661         assertTrue("ejb not found: " + ejbFile.getAbsolutePath(), ejbFile.exists());
662 
663         // configure mojo
664 
665         ejbArtifact.setGroupId("org.sample.ejb");
666         ejbArtifactDup.setGroupId("org.sample.ejb");
667 
668         ejbArtifactDup.setClassifier("classifier");
669 
670         project.addArtifact(ejbArtifact);
671         project.addArtifact(ejbArtifactDup);
672 
673         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
674         mojo.execute();
675 
676         // validate operation
677         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
678         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
679         // final name form is <artifactId>-<version>.<type>
680         File expectedEJBArtifact = new File(webAppDirectory, "WEB-INF/lib/ejbartifact-0.0-Test.jar");
681         File expectedEJBDupArtifact = new File(webAppDirectory, "WEB-INF/lib/ejbartifact-0.0-Test-classifier.jar");
682 
683         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
684         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
685         assertTrue("ejb artifact not found: " + expectedEJBArtifact.toString(), expectedEJBArtifact.exists());
686         assertTrue("ejb dup artifact not found: " + expectedEJBDupArtifact.toString(), expectedEJBDupArtifact.exists());
687 
688         // housekeeping
689         expectedWebSourceFile.delete();
690         expectedWebSource2File.delete();
691         expectedEJBArtifact.delete();
692         expectedEJBDupArtifact.delete();
693     }
694 
695     /**
696      * @throws Exception in case of an error.
697      */
698     public void testExplodedWarWithClasses() throws Exception {
699         // setup test data
700         String testId = "ExplodedWarWithClasses";
701         MavenProjectBasicStub project = new MavenProjectBasicStub();
702         File webAppDirectory = new File(getTestDirectory(), testId);
703         File webAppSource = createWebAppSource(testId);
704         File classesDir = createClassesDir(testId, false);
705 
706         // configure mojo
707         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
708         mojo.execute();
709 
710         // validate operation
711         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
712         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
713         // final name form is <artifactId>-<version>.<type>
714         File expectedClass = new File(webAppDirectory, "WEB-INF/classes/sample-servlet.clazz");
715 
716         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
717         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
718         assertTrue("classes not found: " + expectedClass.toString(), expectedClass.exists());
719 
720         // housekeeping
721         expectedWebSourceFile.delete();
722         expectedWebSource2File.delete();
723         expectedClass.delete();
724     }
725 
726     /**
727      * @throws Exception in case of an error.
728      */
729     public void testExplodedWarWithSourceIncludeExclude() throws Exception {
730         // setup test data
731         String testId = "ExplodedWarWithSourceIncludeExclude";
732         MavenProjectBasicStub project = new MavenProjectBasicStub();
733         File webAppSource = createWebAppSource(testId);
734         File classesDir = createClassesDir(testId, true);
735         File webAppDirectory = new File(getTestDirectory(), testId);
736 
737         // configure mojo
738         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
739         setVariableValueToObject(mojo, "warSourceIncludes", "**/*sit.jsp");
740         setVariableValueToObject(mojo, "warSourceExcludes", "**/last*.*");
741         mojo.execute();
742 
743         // validate operation
744         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
745         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
746         File expectedWEBXMLDir = new File(webAppDirectory, "WEB-INF");
747         File expectedMETAINFDir = new File(webAppDirectory, "META-INF");
748 
749         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
750         assertFalse("source files found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
751         assertTrue("WEB XML not found: " + expectedWEBXMLDir.toString(), expectedWEBXMLDir.exists());
752         assertTrue("META-INF not found", expectedMETAINFDir.exists());
753 
754         // housekeeping
755         expectedWebSourceFile.delete();
756         expectedWebSource2File.delete();
757         expectedWEBXMLDir.delete();
758         expectedMETAINFDir.delete();
759     }
760 
761     /**
762      * @throws Exception in case of an error.
763      */
764     public void testExplodedWarWithWarDependencyIncludeExclude() throws Exception {
765         // setup test data
766         String testId = "ExplodedWarWithWarDependencyIncludeExclude";
767         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
768         IncludeExcludeWarArtifactStub includeexcludeWarArtifact = new IncludeExcludeWarArtifactStub(getBasedir());
769         File webAppDirectory = new File(getTestDirectory(), testId);
770         File webAppSource = createWebAppSource(testId);
771         File classesDir = createClassesDir(testId, true);
772         File workDirectory = new File(getTestDirectory(), "/war/work-" + testId);
773         File includeExcludeWarFile = includeexcludeWarArtifact.getFile();
774 
775         assertTrue("war not found: " + includeExcludeWarFile.toString(), includeExcludeWarFile.exists());
776 
777         createDir(workDirectory);
778 
779         // configure mojo
780         project.addArtifact(includeexcludeWarArtifact);
781         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
782         setVariableValueToObject(mojo, "dependentWarIncludes", "**/*Include.jsp,**/*.xml");
783         setVariableValueToObject(mojo, "dependentWarExcludes", "**/*Exclude*,**/MANIFEST.MF");
784         setVariableValueToObject(mojo, "workDirectory", workDirectory);
785         mojo.execute();
786 
787         // validate operation
788         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
789         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
790         File expectedManifestFile = new File(webAppDirectory, "META-INF/MANIFEST.MF");
791         File expectedWEBXMLFile = new File(webAppDirectory, "WEB-INF/web.xml");
792         File expectedIncludedWARFile = new File(webAppDirectory, "/org/sample/company/testInclude.jsp");
793         File expectedExcludedWarfile = new File(webAppDirectory, "/org/sample/companyExclude/test.jsp");
794 
795         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
796         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
797         // check include-exclude.war in the unit test dir under resources to verify the list of files
798         assertTrue("web xml not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists());
799         assertFalse("manifest file found: " + expectedManifestFile.toString(), expectedManifestFile.exists());
800         assertTrue("war file not found: " + expectedIncludedWARFile.toString(), expectedIncludedWARFile.exists());
801         assertFalse("war file not found: " + expectedExcludedWarfile.toString(), expectedExcludedWarfile.exists());
802 
803         // housekeeping
804         expectedWebSourceFile.delete();
805         expectedWebSource2File.delete();
806         expectedManifestFile.delete();
807         expectedWEBXMLFile.delete();
808         expectedIncludedWARFile.delete();
809         expectedExcludedWarfile.delete();
810     }
811 
812     /**
813      * @throws Exception in case of an error.
814      */
815     public void testExplodedWarWithSourceModificationCheck() throws Exception {
816         // setup test data
817         String testId = "ExplodedWarWithSourceModificationCheck";
818         MavenProjectBasicStub project = new MavenProjectBasicStub();
819         File webAppSource = createWebAppSource(testId);
820         File classesDir = createClassesDir(testId, false);
821         File webAppDirectory = new File(getTestDirectory(), testId);
822 
823         // configure mojo
824         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
825 
826         // destination file is already created manually containing an "error" string
827         // source is newer than the destination file
828         mojo.execute();
829 
830         // validate operation
831 
832         File expectedWEBINFDir = new File(webAppDirectory, "WEB-INF");
833         File expectedMETAINFDir = new File(webAppDirectory, "META-INF");
834         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
835         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
836 
837         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
838         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
839         assertTrue("WEB-INF not found", expectedWEBINFDir.exists());
840         assertTrue("META-INF not found", expectedMETAINFDir.exists());
841 
842         // 1st phase destination is older than source
843         // destination starts with a value of error replaced with a blank source
844         assertNotEquals(
845                 "source files not updated with new copy: " + expectedWebSourceFile.toString(),
846                 "error",
847                 FileUtils.fileRead(expectedWebSourceFile));
848 
849         // housekeeping
850         expectedWEBINFDir.delete();
851         expectedMETAINFDir.delete();
852         expectedWebSourceFile.delete();
853         expectedWebSource2File.delete();
854     }
855 
856     /**
857      * @throws Exception in case of an error.
858      */
859     public void testExplodedWarWithOutputFileNameMapping() throws Exception {
860         // setup test data
861         String testId = "ExplodedWarWithFileNameMapping";
862         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
863         File webAppDirectory = new File(getTestDirectory(), testId);
864         File webAppSource = createWebAppSource(testId);
865         File classesDir = createClassesDir(testId, true);
866         ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
867         ArtifactStub jarArtifact = new JarArtifactStub(getBasedir(), artifactHandler);
868         File jarFile = jarArtifact.getFile();
869 
870         assertTrue("jar not found: " + jarFile.toString(), jarFile.exists());
871 
872         // configure mojo
873         project.addArtifact(jarArtifact);
874         mojo.setOutputFileNameMapping("@{artifactId}@.@{extension}@");
875         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
876         mojo.execute();
877 
878         // validate operation
879         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
880         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
881         // final name form is <artifactId>-<version>.<type>
882         File expectedJarArtifact = new File(webAppDirectory, "WEB-INF/lib/jarartifact.jar");
883 
884         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
885         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
886         assertTrue("jar artifact not found: " + expectedJarArtifact.toString(), expectedJarArtifact.exists());
887 
888         // housekeeping
889         expectedWebSourceFile.delete();
890         expectedWebSource2File.delete();
891         expectedJarArtifact.delete();
892     }
893 
894     /**
895      * @throws Exception in case of an error.
896      */
897     public void testExplodedWarWithOutputFileNameMappingAndDuplicateDependencies() throws Exception {
898         // setup test data
899         String testId = "ExplodedWarWithFileNameMappingAndDuplicateDependencies";
900         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
901         File webAppDirectory = new File(getTestDirectory(), testId);
902         File webAppSource = createWebAppSource(testId);
903         File classesDir = createClassesDir(testId, true);
904         EJBArtifactStub ejbArtifact = new EJBArtifactStub(getBasedir());
905         EJBArtifactStub ejbArtifactDup = new EJBArtifactStub(getBasedir());
906         File ejbFile = ejbArtifact.getFile();
907 
908         // ejbArtifact has a hard coded file, only one assert is needed
909         assertTrue("ejb not found: " + ejbFile.getAbsolutePath(), ejbFile.exists());
910 
911         // configure mojo
912         ejbArtifact.setGroupId("org.sample.ejb");
913         ejbArtifactDup.setGroupId("org.dup.ejb");
914         project.addArtifact(ejbArtifact);
915         project.addArtifact(ejbArtifactDup);
916         mojo.setOutputFileNameMapping("@{artifactId}@.@{extension}@");
917         this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
918         mojo.execute();
919 
920         // validate operation
921         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
922         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
923         // final name form is <artifactId>-<version>.<type>
924         File expectedEJBArtifact = new File(webAppDirectory, "WEB-INF/lib/org.sample.ejb-ejbartifact.jar");
925         File expectedEJBDupArtifact = new File(webAppDirectory, "WEB-INF/lib/org.dup.ejb-ejbartifact.jar");
926 
927         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
928         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
929         assertTrue("ejb artifact not found: " + expectedEJBArtifact.toString(), expectedEJBArtifact.exists());
930         assertTrue("ejb dup artifact not found: " + expectedEJBDupArtifact.toString(), expectedEJBDupArtifact.exists());
931 
932         // housekeeping
933         expectedWebSourceFile.delete();
934         expectedWebSource2File.delete();
935         expectedEJBArtifact.delete();
936         expectedEJBDupArtifact.delete();
937     }
938 }