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