View Javadoc
1   package org.apache.maven.plugins.war;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
23  import org.apache.maven.plugins.war.overlay.DefaultOverlay;
24  import org.codehaus.plexus.util.FileUtils;
25  
26  import java.io.File;
27  import java.io.FileFilter;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.LinkedList;
31  import java.util.List;
32  
33  /**
34   * @author Stephane Nicoll
35   */
36  public class WarOverlaysTest
37      extends AbstractWarExplodedMojoTest
38  {
39  
40      private static File pomFile = new File( getBasedir(), "target/test-classes/unit/waroverlays/default.xml" );
41  
42      public void setUp()
43          throws Exception
44      {
45          super.setUp();
46          generateFullOverlayWar( "overlay-full-1" );
47          generateFullOverlayWar( "overlay-full-2" );
48          generateFullOverlayWar( "overlay-full-3" );
49      }
50  
51      protected File getPomFile()
52      {
53          return pomFile;
54      }
55  
56      protected File getTestDirectory()
57      {
58          return new File( getBasedir(), "target/test-classes/unit/waroverlays" );
59      }
60  
61      public void testEnvironment()
62          throws Exception
63      {
64          // see setup
65      }
66  
67      public void testNoOverlay()
68          throws Exception
69      {
70          // setup test data
71          final String testId = "no-overlay";
72          final File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
73  
74          final File webAppDirectory = setUpMojo( testId, null );
75          try
76          {
77              mojo.setWebXml( new File( xmlSource, "web.xml" ) );
78              mojo.execute();
79  
80              // Validate content of the webapp
81              assertDefaultContent( webAppDirectory );
82              assertWebXml( webAppDirectory );
83          }
84          finally
85          {
86              cleanDirectory( webAppDirectory );
87          }
88      }
89  
90      public void testDefaultOverlay()
91          throws Exception
92      {
93          // setup test data
94          final String testId = "default-overlay";
95  
96          // Add an overlay
97          final ArtifactStub overlay = buildWarOverlayStub( "overlay-one" );
98  
99          final File webAppDirectory = setUpMojo( testId, new ArtifactStub[] { overlay } );
100         final List<File> assertedFiles = new ArrayList<>();
101         try
102         {
103             mojo.execute();
104             assertedFiles.addAll( assertDefaultContent( webAppDirectory ) );
105             assertedFiles.addAll( assertWebXml( webAppDirectory ) );
106             assertedFiles.addAll( assertCustomContent( webAppDirectory, new String[] { "index.jsp", "login.jsp" },
107                                                        "overlay file not found" ) );
108 
109             // index and login come from overlay1
110             assertOverlayedFile( webAppDirectory, "overlay-one", "index.jsp" );
111             assertOverlayedFile( webAppDirectory, "overlay-one", "login.jsp" );
112 
113             // Ok now check that there is no more files/directories
114             final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
115             assertWebAppContent( webAppDirectory, assertedFiles, filter );
116         }
117         finally
118         {
119             cleanDirectory( webAppDirectory );
120         }
121     }
122 
123     public void testDefaultOverlays()
124         throws Exception
125     {
126         // setup test data
127         final String testId = "default-overlays";
128 
129         // Add an overlay
130         final ArtifactStub overlay = buildWarOverlayStub( "overlay-one" );
131         final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-two" );
132 
133         final File webAppDirectory = setUpMojo( testId, new ArtifactStub[] { overlay, overlay2 } );
134         final List<File> assertedFiles = new ArrayList<>();
135         try
136         {
137             mojo.execute();
138             assertedFiles.addAll( assertDefaultContent( webAppDirectory ) );
139             assertedFiles.addAll( assertWebXml( webAppDirectory ) );
140             assertedFiles.addAll( assertCustomContent( webAppDirectory, new String[] { "index.jsp", "login.jsp",
141                 "admin.jsp" }, "overlay file not found" ) );
142 
143             // index and login come from overlay1
144             assertOverlayedFile( webAppDirectory, "overlay-one", "index.jsp" );
145             assertOverlayedFile( webAppDirectory, "overlay-one", "login.jsp" );
146 
147             // admin comes from overlay2
148             // index and login comes from overlay1
149             assertOverlayedFile( webAppDirectory, "overlay-two", "admin.jsp" );
150 
151             // Ok now check that there is no more files/directories
152             final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
153             assertWebAppContent( webAppDirectory, assertedFiles, filter );
154         }
155         finally
156         {
157             cleanDirectory( webAppDirectory );
158         }
159     }
160 
161     /**
162      * Merge a dependent WAR when a file in the war source directory overrides one found in the WAR.
163      * 
164      * It also tests completeness of the resulting war as well as the proper order of dependencies.
165      *
166      * @throws Exception if any error occurs
167      */
168     public void testScenarioOneWithDefaulSettings()
169         throws Exception
170     {
171         // setup test data
172         final String testId = "scenario-one-default-settings";
173 
174         // Add an overlay
175         final ArtifactStub overlay1 = buildWarOverlayStub( "overlay-full-1" );
176         final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-full-2" );
177         final ArtifactStub overlay3 = buildWarOverlayStub( "overlay-full-3" );
178 
179         final File webAppDirectory =
180             setUpMojo( testId, new ArtifactStub[] { overlay1, overlay2, overlay3 }, new String[] {
181                 "org/sample/company/test.jsp", "jsp/b.jsp" } );
182 
183         assertScenariOne( testId, webAppDirectory );
184     }
185 
186     /**
187      * Tests that specifying the overlay explicitely has the same behavior as the default (i.e. order, etc).
188      * 
189      * The default project is not specified in this case so it is processed first by default
190      *
191      * @throws Exception if an error occurs
192      */
193     public void testScenarioOneWithOverlaySettings()
194         throws Exception
195     {
196         // setup test data
197         final String testId = "scenario-one-overlay-settings";
198 
199         // Add an overlay
200         final ArtifactStub overlay1 = buildWarOverlayStub( "overlay-full-1" );
201         final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-full-2" );
202         final ArtifactStub overlay3 = buildWarOverlayStub( "overlay-full-3" );
203 
204         final File webAppDirectory =
205             setUpMojo( testId, new ArtifactStub[] { overlay1, overlay2, overlay3 }, new String[] {
206                 "org/sample/company/test.jsp", "jsp/b.jsp" } );
207 
208         // Add the tags
209         final List<Overlay> overlays = new ArrayList<>();
210         overlays.add( new DefaultOverlay( overlay1 ) );
211         overlays.add( new DefaultOverlay( overlay2 ) );
212         overlays.add( new DefaultOverlay( overlay3 ) );
213         mojo.setOverlays( overlays );
214 
215         // current project ignored. Should be on top of the list
216         assertScenariOne( testId, webAppDirectory );
217     }
218 
219     /**
220      * Tests that specifying the overlay explicitely has the same behavior as the default (i.e. order, etc).
221      * 
222      * The default project is explicitely specified so this should match the default.
223      *
224      * @throws Exception if an error occurs
225      */
226     public void testScenarioOneWithFullSettings()
227         throws Exception
228     {
229         // setup test data
230         final String testId = "scenario-one-full-settings";
231 
232         // Add an overlay
233         final ArtifactStub overlay1 = buildWarOverlayStub( "overlay-full-1" );
234         final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-full-2" );
235         final ArtifactStub overlay3 = buildWarOverlayStub( "overlay-full-3" );
236 
237         final File webAppDirectory =
238             setUpMojo( testId, new ArtifactStub[] { overlay1, overlay2, overlay3 }, new String[] {
239                 "org/sample/company/test.jsp", "jsp/b.jsp" } );
240 
241         // Add the tags
242         final List<Overlay> overlays = new ArrayList<>();
243 
244         // Add the default project explicitely
245         overlays.add( mojo.getCurrentProjectOverlay() );
246 
247         // Other overlays
248         overlays.add( new DefaultOverlay( overlay1 ) );
249         overlays.add( new DefaultOverlay( overlay2 ) );
250         overlays.add( new DefaultOverlay( overlay3 ) );
251         mojo.setOverlays( overlays );
252 
253         // current project ignored. Should be on top of the list
254         assertScenariOne( testId, webAppDirectory );
255     }
256 
257     /**
258      * Runs the mojo and asserts a scenerio with 3 overlays and no includes/excludes settings.
259      *
260      * @param testId thie id of the test
261      * @param webAppDirectory the webapp directory
262      * @throws Exception if an exception occurs
263      */
264     private void assertScenariOne( String testId, File webAppDirectory )
265         throws Exception
266     {
267         final List<File> assertedFiles = new ArrayList<>();
268         try
269         {
270             mojo.execute();
271             assertedFiles.addAll( assertWebXml( webAppDirectory ) );
272             assertedFiles.addAll( assertCustomContent( webAppDirectory, new String[] { "jsp/a.jsp", "jsp/b.jsp",
273                 "jsp/c.jsp", "jsp/d/a.jsp", "jsp/d/b.jsp", "jsp/d/c.jsp", "org/sample/company/test.jsp",
274                 "WEB-INF/classes/a.class", "WEB-INF/classes/b.class", "WEB-INF/classes/c.class", "WEB-INF/lib/a.jar",
275                 "WEB-INF/lib/b.jar", "WEB-INF/lib/c.jar" }, "overlay file not found" ) );
276 
277             // Those files should come from the source webapp without any config
278             assertDefaultFileContent( testId, webAppDirectory, "jsp/b.jsp" );
279             assertDefaultFileContent( testId, webAppDirectory, "org/sample/company/test.jsp" );
280 
281             // Everything else comes from overlay1 (order of addition in the dependencies)
282             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/a.jsp" );
283             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/c.jsp" );
284             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/d/a.jsp" );
285             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/d/b.jsp" );
286             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/d/c.jsp" );
287             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/web.xml" );
288             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/classes/a.class" );
289             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/classes/b.class" );
290             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/classes/c.class" );
291             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/lib/a.jar" );
292             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/lib/b.jar" );
293             assertOverlayedFile( webAppDirectory, "overlay-full-1", "WEB-INF/lib/c.jar" );
294 
295             // Ok now check that there is no more files/directories
296             final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
297             assertWebAppContent( webAppDirectory, assertedFiles, filter );
298         }
299         finally
300         {
301             cleanDirectory( webAppDirectory );
302         }
303     }
304 
305     public void testOverlaysIncludesExcludesWithMultipleDefinitions()
306         throws Exception
307     {
308         // setup test data
309         final String testId = "overlays-includes-excludes-multiple-defs";
310 
311         // Add an overlay
312         final ArtifactStub overlay1 = buildWarOverlayStub( "overlay-full-1" );
313         final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-full-2" );
314         final ArtifactStub overlay3 = buildWarOverlayStub( "overlay-full-3" );
315 
316         final File webAppDirectory =
317             setUpMojo( testId, new ArtifactStub[] { overlay1, overlay2, overlay3 }, new String[] {
318                 "org/sample/company/test.jsp", "jsp/b.jsp" } );
319 
320         Overlay over1 = new DefaultOverlay( overlay3 );
321         over1.setExcludes( "**/a.*,**/c.*,**/*.xml" );
322 
323         Overlay over2 = new DefaultOverlay( overlay1 );
324         over2.setIncludes( "jsp/d/*" );
325         over2.setExcludes( "jsp/d/a.jsp" );
326 
327         Overlay over3 = new DefaultOverlay( overlay3 );
328         over3.setIncludes( "**/*.jsp" );
329 
330         Overlay over4 = new DefaultOverlay( overlay2 );
331 
332         mojo.setOverlays( new LinkedList<Overlay>() );
333         mojo.addOverlay( over1 );
334         mojo.addOverlay( over2 );
335         mojo.addOverlay( over3 );
336         mojo.addOverlay( mojo.getCurrentProjectOverlay() );
337         mojo.addOverlay( over4 );
338 
339         final List<File> assertedFiles = new ArrayList<>();
340         try
341         {
342             mojo.execute();
343             assertedFiles.addAll( assertWebXml( webAppDirectory ) );
344             assertedFiles.addAll( assertCustomContent( webAppDirectory, new String[] { "jsp/a.jsp", "jsp/b.jsp",
345                 "jsp/c.jsp", "jsp/d/a.jsp", "jsp/d/b.jsp", "jsp/d/c.jsp", "org/sample/company/test.jsp",
346                 "WEB-INF/classes/a.class", "WEB-INF/classes/b.class", "WEB-INF/classes/c.class", "WEB-INF/lib/a.jar",
347                 "WEB-INF/lib/b.jar", "WEB-INF/lib/c.jar" }, "overlay file not found" ) );
348 
349             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/a.jsp" );
350             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/b.jsp" );
351             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/c.jsp" );
352             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/d/a.jsp" );
353             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/d/b.jsp" );
354             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/d/c.jsp" );
355             assertDefaultFileContent( testId, webAppDirectory, "org/sample/company/test.jsp" );
356             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/web.xml" );
357             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/classes/a.class" );
358             assertOverlayedFile( webAppDirectory, "overlay-full-3", "WEB-INF/classes/b.class" );
359             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/classes/c.class" );
360             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/lib/a.jar" );
361             assertOverlayedFile( webAppDirectory, "overlay-full-3", "WEB-INF/lib/b.jar" );
362             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/lib/c.jar" );
363 
364             // Ok now check that there is no more files/directories
365             final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
366             assertWebAppContent( webAppDirectory, assertedFiles, filter );
367         }
368         finally
369         {
370             cleanDirectory( webAppDirectory );
371         }
372     }
373 
374     public void testOverlaysIncludesExcludesWithMultipleDefinitions2()
375         throws Exception
376     {
377         // setup test data
378         final String testId = "overlays-includes-excludes-multiple-defs2";
379 
380         // Add an overlay
381         final ArtifactStub overlay1 = buildWarOverlayStub( "overlay-full-1" );
382         final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-full-2" );
383         final ArtifactStub overlay3 = buildWarOverlayStub( "overlay-full-3" );
384 
385         final File webAppDirectory =
386             setUpMojo( testId, new ArtifactStub[] { overlay1, overlay2, overlay3 }, new String[] {
387                 "org/sample/company/test.jsp", "jsp/b.jsp" } );
388 
389         Overlay over1 = new DefaultOverlay( overlay3 );
390         over1.setExcludes( "**/a.*,**/c.*,**/*.xml,jsp/b.jsp" );
391 
392         Overlay over2 = new DefaultOverlay( overlay1 );
393         over2.setIncludes( "jsp/d/*" );
394         over2.setExcludes( "jsp/d/a.jsp" );
395 
396         Overlay over3 = new DefaultOverlay( overlay3 );
397         over3.setIncludes( "**/*.jsp" );
398         over3.setExcludes( "jsp/b.jsp" );
399 
400         Overlay over4 = new DefaultOverlay( overlay2 );
401 
402         mojo.setOverlays( new LinkedList<Overlay>() );
403         mojo.addOverlay( over1 );
404         mojo.addOverlay( over2 );
405         mojo.addOverlay( over3 );
406         mojo.addOverlay( mojo.getCurrentProjectOverlay() );
407         mojo.addOverlay( over4 );
408 
409         final List<File> assertedFiles = new ArrayList<>();
410         try
411         {
412             mojo.execute();
413             assertedFiles.addAll( assertWebXml( webAppDirectory ) );
414             assertedFiles.addAll( assertCustomContent( webAppDirectory, new String[] { "jsp/a.jsp", "jsp/b.jsp",
415                 "jsp/c.jsp", "jsp/d/a.jsp", "jsp/d/b.jsp", "jsp/d/c.jsp", "org/sample/company/test.jsp",
416                 "WEB-INF/classes/a.class", "WEB-INF/classes/b.class", "WEB-INF/classes/c.class", "WEB-INF/lib/a.jar",
417                 "WEB-INF/lib/b.jar", "WEB-INF/lib/c.jar" }, "overlay file not found" ) );
418 
419             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/a.jsp" );
420             assertDefaultFileContent( testId, webAppDirectory, "jsp/b.jsp" );
421             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/c.jsp" );
422             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/d/a.jsp" );
423             assertOverlayedFile( webAppDirectory, "overlay-full-3", "jsp/d/b.jsp" );
424             assertOverlayedFile( webAppDirectory, "overlay-full-1", "jsp/d/c.jsp" );
425             assertDefaultFileContent( testId, webAppDirectory, "org/sample/company/test.jsp" );
426             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/web.xml" );
427             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/classes/a.class" );
428             assertOverlayedFile( webAppDirectory, "overlay-full-3", "WEB-INF/classes/b.class" );
429             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/classes/c.class" );
430             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/lib/a.jar" );
431             assertOverlayedFile( webAppDirectory, "overlay-full-3", "WEB-INF/lib/b.jar" );
432             assertOverlayedFile( webAppDirectory, "overlay-full-2", "WEB-INF/lib/c.jar" );
433 
434             // Ok now check that there is no more files/directories
435             final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
436             assertWebAppContent( webAppDirectory, assertedFiles, filter );
437         }
438         finally
439         {
440             cleanDirectory( webAppDirectory );
441         }
442 
443     }
444 
445     // Helpers
446 
447     /**
448      * Asserts that the content of an overlayed file is correct.
449      * 
450      * Note that the <tt>filePath</tt> is relative to both the webapp directory and the overlayed directory, defined by
451      * the <tt>overlayId</tt>.
452      *
453      * @param webAppDirectory the webapp directory
454      * @param overlayId the id of the overlay
455      * @param filePath the relative path
456      * @throws IOException if an error occurred while reading the files
457      */
458     protected void assertOverlayedFile( File webAppDirectory, String overlayId, String filePath )
459         throws IOException
460     {
461         final File webAppFile = new File( webAppDirectory, filePath );
462         final File overlayFile = getOverlayFile( overlayId, filePath );
463         assertEquals( "Wrong content for overlayed file " + filePath, FileUtils.fileRead( overlayFile ),
464                       FileUtils.fileRead( webAppFile ) );
465 
466     }
467 
468     /**
469      * Asserts that the content of an overlayed file is correct.
470      * 
471      * Note that the <tt>filePath</tt> is relative to both the webapp directory and the overlayed directory, defined by
472      * the <tt>overlayId</tt>.
473      *
474      * @param testId te id of the test
475      * @param webAppDirectory the webapp directory
476      * @param filePath the relative path
477      * @throws IOException if an error occurred while reading the files
478      */
479     protected void assertDefaultFileContent( String testId, File webAppDirectory, String filePath )
480         throws Exception
481     {
482         final File webAppFile = new File( webAppDirectory, filePath );
483         final File sourceFile = new File( getWebAppSource( testId ), filePath );
484         final String expectedContent = sourceFile.toString();
485         assertEquals( "Wrong content for file " + filePath, expectedContent, FileUtils.fileRead( webAppFile ) );
486 
487     }
488 
489     protected ArtifactStub generateSimpleWarArtifactStub( String id )
490         throws Exception
491     {
492         return buildWarOverlayStub( id );
493     }
494 }