View Javadoc

1   package org.apache.maven.plugin.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.MojoExecutionException;
23  import org.apache.maven.plugin.war.stub.MavenProject4CopyConstructor;
24  import org.apache.maven.plugin.war.stub.ProjectHelperStub;
25  import org.apache.maven.plugin.war.stub.WarArtifact4CCStub;
26  import org.codehaus.plexus.util.IOUtil;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.LinkedList;
33  import java.util.Map;
34  import java.util.jar.JarEntry;
35  import java.util.jar.JarFile;
36  import java.util.zip.ZipEntry;
37  
38  /**
39   * comprehensive test on buildExplodedWebApp is done on WarExplodedMojoTest
40   */
41  public class WarMojoTest
42      extends AbstractWarMojoTest
43  {
44      WarMojo mojo;
45  
46      private static File pomFile =
47          new File( getBasedir(), "target/test-classes/unit/warmojotest/plugin-config-primary-artifact.xml" );
48  
49      protected File getTestDirectory()
50      {
51          return new File( getBasedir(), "target/test-classes/unit/warmojotest" );
52      }
53  
54      public void setUp()
55          throws Exception
56      {
57          super.setUp();
58          mojo = (WarMojo) lookupMojo( "war", pomFile );
59      }
60  
61      public void testEnvironment()
62          throws Exception
63      {
64          // see setup
65      }
66  
67      public void testSimpleWar()
68          throws Exception
69      {
70          String testId = "SimpleWar";
71          MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
72          String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
73          File webAppDirectory = new File( getTestDirectory(), testId );
74          WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
75          String warName = "simple";
76          File webAppSource = createWebAppSource( testId );
77          File classesDir = createClassesDir( testId, true );
78          File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
79  
80          project.setArtifact( warArtifact );
81          this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
82          setVariableValueToObject( mojo, "outputDirectory", outputDir );
83          setVariableValueToObject( mojo, "warName", warName );
84          mojo.setWebXml( new File( xmlSource, "web.xml" ) );
85  
86          mojo.execute();
87  
88          //validate jar file
89          File expectedJarFile = new File( outputDir, "simple.war" );
90          assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
91              "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
92              "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
93                                             new String[]{null, mojo.getWebXml().toString(), null, null, null, null} );
94      }
95  
96      public void testSimpleWarPackagingExcludeWithIncludesRegEx()
97          throws Exception
98      {
99          String testId = "SimpleWarPackagingExcludeWithIncludesRegEx";
100         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
101         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
102         File webAppDirectory = new File( getTestDirectory(), testId );
103         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
104         String warName = "simple";
105         File webAppSource = createWebAppSource( testId );
106         File classesDir = createClassesDir( testId, true );
107         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
108 
109         project.setArtifact( warArtifact );
110         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
111         setVariableValueToObject( mojo, "outputDirectory", outputDir );
112         setVariableValueToObject( mojo, "warName", warName );
113         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
114         setVariableValueToObject( mojo,"packagingIncludes","%regex[(.(?!exile))+]" );
115        
116        
117         mojo.execute();
118 
119         //validate jar file
120         File expectedJarFile = new File( outputDir, "simple.war" );
121         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
122             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
123             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
124                                            new String[]{null, mojo.getWebXml().toString(), null, null, null, },
125                                            new String[]{"org/web/app/last-exile.jsp"} );
126     }
127 
128     public void testClassifier()
129         throws Exception
130     {
131         String testId = "Classifier";
132         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
133         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
134         File webAppDirectory = new File( getTestDirectory(), testId );
135         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
136         ProjectHelperStub projectHelper = new ProjectHelperStub();
137         String warName = "simple";
138         File webAppSource = createWebAppSource( testId );
139         File classesDir = createClassesDir( testId, true );
140         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
141 
142         project.setArtifact( warArtifact );
143         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
144         setVariableValueToObject( mojo, "projectHelper", projectHelper );
145         setVariableValueToObject( mojo, "classifier", "test-classifier" );
146         setVariableValueToObject( mojo, "outputDirectory", outputDir );
147         setVariableValueToObject( mojo, "warName", warName );
148         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
149 
150         mojo.execute();
151 
152         //validate jar file
153         File expectedJarFile = new File( outputDir, "simple-test-classifier.war" );
154         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
155             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
156             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
157                                            new String[]{null, mojo.getWebXml().toString(), null, null, null, null} );
158     }
159 
160     public void testPrimaryArtifact()
161         throws Exception
162     {
163         String testId = "PrimaryArtifact";
164         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
165         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
166         File webAppDirectory = new File( getTestDirectory(), testId );
167         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
168         ProjectHelperStub projectHelper = new ProjectHelperStub();
169         String warName = "simple";
170         File webAppSource = createWebAppSource( testId );
171         File classesDir = createClassesDir( testId, true );
172         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
173 
174         warArtifact.setFile( new File( "error.war" ) );
175         project.setArtifact( warArtifact );
176         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
177         setVariableValueToObject( mojo, "projectHelper", projectHelper );
178         setVariableValueToObject( mojo, "outputDirectory", outputDir );
179         setVariableValueToObject( mojo, "warName", warName );
180         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
181 
182         mojo.execute();
183 
184         //validate jar file
185         File expectedJarFile = new File( outputDir, "simple.war" );
186         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
187             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
188             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
189                                            new String[]{null, mojo.getWebXml().toString(), null, null, null, null} );
190     }
191 
192     public void testNotPrimaryArtifact()
193         throws Exception
194     {
195         // use a different pom
196         File pom = new File( getBasedir(), "target/test-classes/unit/warmojotest/not-primary-artifact.xml" );
197         mojo = (WarMojo) lookupMojo( "war", pom );
198 
199         String testId = "NotPrimaryArtifact";
200         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
201         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
202         File webAppDirectory = new File( getTestDirectory(), testId );
203         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
204         ProjectHelperStub projectHelper = new ProjectHelperStub();
205         String warName = "simple";
206         File webAppSource = createWebAppSource( testId );
207         File classesDir = createClassesDir( testId, true );
208         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
209 
210         warArtifact.setFile( new File( "error.war" ) );
211         project.setArtifact( warArtifact );
212         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
213         setVariableValueToObject( mojo, "projectHelper", projectHelper );
214         setVariableValueToObject( mojo, "outputDirectory", outputDir );
215         setVariableValueToObject( mojo, "warName", warName );
216         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
217 
218         mojo.execute();
219 
220         //validate jar file
221         File expectedJarFile = new File( outputDir, "simple.war" );
222         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
223             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
224             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
225                                            new String[]{null, mojo.getWebXml().toString(), null, null, null, null} );
226     }
227 
228     public void testMetaInfContent()
229         throws Exception
230     {
231         String testId = "SimpleWarWithMetaInfContent";
232         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
233         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
234         File webAppDirectory = new File( getTestDirectory(), testId );
235         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
236         String warName = "simple";
237         File webAppSource = createWebAppSource( testId );
238         File classesDir = createClassesDir( testId, true );
239         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
240 
241         // Create the sample config.xml
242         final File configFile = new File( webAppSource, "META-INF/config.xml" );
243         createFile( configFile, "<config></config>" );
244 
245         project.setArtifact( warArtifact );
246         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
247         setVariableValueToObject( mojo, "outputDirectory", outputDir );
248         setVariableValueToObject( mojo, "warName", warName );
249         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
250 
251         mojo.execute();
252 
253         //validate jar file
254         File expectedJarFile = new File( outputDir, "simple.war" );
255         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "META-INF/config.xml",
256             "WEB-INF/web.xml", "pansit.jsp", "org/web/app/last-exile.jsp",
257             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
258             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"}, new String[]{null, null,
259             mojo.getWebXml().toString(), null, null, null, null} );
260     }
261 
262     public void testMetaInfContentWithContainerConfig()
263         throws Exception
264     {
265         String testId = "SimpleWarWithContainerConfig";
266         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
267         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
268         File webAppDirectory = new File( getTestDirectory(), testId );
269         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
270         String warName = "simple";
271         File webAppSource = createWebAppSource( testId );
272         File classesDir = createClassesDir( testId, true );
273         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
274 
275         // Create the sample config.xml
276         final File configFile = new File( webAppSource, "META-INF/config.xml" );
277         createFile( configFile, "<config></config>" );
278 
279         project.setArtifact( warArtifact );
280         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
281         setVariableValueToObject( mojo, "outputDirectory", outputDir );
282         setVariableValueToObject( mojo, "warName", warName );
283         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
284         mojo.setContainerConfigXML( configFile );
285 
286         mojo.execute();
287 
288         //validate jar file
289         File expectedJarFile = new File( outputDir, "simple.war" );
290         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "META-INF/config.xml",
291             "WEB-INF/web.xml", "pansit.jsp", "org/web/app/last-exile.jsp",
292             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
293             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"}, new String[]{null, null,
294             mojo.getWebXml().toString(), null, null, null, null} );
295     }
296 
297 
298     public void testFailOnMissingWebXmlFalse()
299         throws Exception
300     {
301 
302         String testId = "SimpleWarMissingWebXmlFalse";
303         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
304         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
305         File webAppDirectory = new File( getTestDirectory(), testId );
306         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
307         String warName = "simple";
308         File webAppSource = createWebAppSource( testId );
309         File classesDir = createClassesDir( testId, true );
310 
311         project.setArtifact( warArtifact );
312         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
313         setVariableValueToObject( mojo, "outputDirectory", outputDir );
314         setVariableValueToObject( mojo, "warName", warName );
315         mojo.setFailOnMissingWebXml( false );
316         mojo.execute();
317 
318         //validate jar file
319         File expectedJarFile = new File( outputDir, "simple.war" );
320         final Map jarContent = assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "pansit.jsp",
321             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
322             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
323                                                                   new String[]{null, null, null, null, null} );
324 
325         assertFalse( "web.xml should be missing", jarContent.containsKey( "WEB-INF/web.xml" ) );
326     }
327 
328     public void testFailOnMissingWebXmlTrue()
329         throws Exception
330     {
331 
332         String testId = "SimpleWarMissingWebXmlTrue";
333         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
334         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
335         File webAppDirectory = new File( getTestDirectory(), testId );
336         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
337         String warName = "simple";
338         File webAppSource = createWebAppSource( testId );
339         File classesDir = createClassesDir( testId, true );
340 
341         project.setArtifact( warArtifact );
342         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
343         setVariableValueToObject( mojo, "outputDirectory", outputDir );
344         setVariableValueToObject( mojo, "warName", warName );
345         mojo.setFailOnMissingWebXml( true );
346 
347         try
348         {
349             mojo.execute();
350             fail( "Building of the war isn't possible because web.xml is missing" );
351         }
352         catch ( MojoExecutionException e )
353         {
354             //expected behaviour
355         }
356     }
357 
358     public void testAttachClasses()
359         throws Exception
360     {
361         String testId = "AttachClasses";
362         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
363         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
364         File webAppDirectory = new File( getTestDirectory(), testId );
365         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
366         String warName = "simple";
367         File webAppSource = createWebAppSource( testId );
368         File classesDir = createClassesDir( testId, false );
369         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
370 
371         project.setArtifact( warArtifact );
372         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
373         setVariableValueToObject( mojo, "outputDirectory", outputDir );
374         setVariableValueToObject( mojo, "warName", warName );
375         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
376         mojo.setAttachClasses( true );
377 
378         mojo.execute();
379 
380         //validate jar file
381         File expectedJarFile = new File( outputDir, "simple-classes.jar" );
382         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "sample-servlet.class"},
383                           new String[]{null, null} );
384     }
385 
386     public void testAttachClassesWithCustomClassifier()
387         throws Exception
388     {
389         String testId = "AttachClassesCustomClassifier";
390         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
391         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
392         File webAppDirectory = new File( getTestDirectory(), testId );
393         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
394         String warName = "simple";
395         File webAppSource = createWebAppSource( testId );
396         File classesDir = createClassesDir( testId, false );
397         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
398 
399         project.setArtifact( warArtifact );
400         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
401         setVariableValueToObject( mojo, "outputDirectory", outputDir );
402         setVariableValueToObject( mojo, "warName", warName );
403         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
404         mojo.setAttachClasses( true );
405         mojo.setClassesClassifier( "mystuff" );
406 
407         mojo.execute();
408 
409         //validate jar file
410         File expectedJarFile = new File( outputDir, "simple-mystuff.jar" );
411         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "sample-servlet.class"},
412                           new String[]{null, null} );
413     }
414 
415 
416     protected Map assertJarContent( final File expectedJarFile, final String[] files, final String[] filesContent )
417         throws IOException
418     {
419         return assertJarContent( expectedJarFile, files, filesContent, null );
420     }
421 
422     protected Map assertJarContent( final File expectedJarFile, final String[] files, final String[] filesContent, final String[] mustNotBeInJar )
423         throws IOException
424     {
425         // Sanity check
426         assertEquals( "Could not test, files and filesContent lenght does not match", files.length,
427                       filesContent.length );
428 
429         assertTrue( "war file not created: " + expectedJarFile.toString(), expectedJarFile.exists() );
430         final Map jarContent = new HashMap();
431         final JarFile jarFile = new JarFile( expectedJarFile );
432 
433         JarEntry entry;
434         Enumeration enumeration = jarFile.entries();
435         while ( enumeration.hasMoreElements() )
436         {
437             entry = (JarEntry) enumeration.nextElement();
438             Object previousValue = jarContent.put( entry.getName(), entry );
439             assertNull( "Duplicate Entry in Jar File: " + entry.getName(), previousValue );
440         }
441 
442         for ( int i = 0; i < files.length; i++ )
443         {
444             String file = files[i];
445 
446             assertTrue( "File[" + file + "] not found in archive", jarContent.containsKey( file ) );
447             if ( filesContent[i] != null )
448             {
449                 assertEquals( "Content of file[" + file + "] does not match", filesContent[i],
450                               IOUtil.toString( jarFile.getInputStream( (ZipEntry) jarContent.get( file ) ) ) );
451             }
452         }
453         if( mustNotBeInJar!=null )
454         {         
455             for ( int i = 0; i < mustNotBeInJar.length; i++ )
456             {
457                 String file = mustNotBeInJar[i];
458 
459                 assertFalse( "File[" + file + "]  found in archive", jarContent.containsKey( file ) );
460 
461             }
462         }
463         return jarContent;
464     }
465 
466 }
467