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 testClassifier()
97          throws Exception
98      {
99          String testId = "Classifier";
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         ProjectHelperStub projectHelper = new ProjectHelperStub();
105         String warName = "simple";
106         File webAppSource = createWebAppSource( testId );
107         File classesDir = createClassesDir( testId, true );
108         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
109 
110         project.setArtifact( warArtifact );
111         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
112         setVariableValueToObject( mojo, "projectHelper", projectHelper );
113         setVariableValueToObject( mojo, "classifier", "test-classifier" );
114         setVariableValueToObject( mojo, "outputDirectory", outputDir );
115         setVariableValueToObject( mojo, "warName", warName );
116         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
117 
118         mojo.execute();
119 
120         //validate jar file
121         File expectedJarFile = new File( outputDir, "simple-test-classifier.war" );
122         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
123             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
124             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
125                                            new String[]{null, mojo.getWebXml().toString(), null, null, null, null} );
126     }
127 
128     public void testPrimaryArtifact()
129         throws Exception
130     {
131         String testId = "PrimaryArtifact";
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         warArtifact.setFile( new File( "error.war" ) );
143         project.setArtifact( warArtifact );
144         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
145         setVariableValueToObject( mojo, "projectHelper", projectHelper );
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.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 testNotPrimaryArtifact()
161         throws Exception
162     {
163         // use a different pom
164         File pom = new File( getBasedir(), "target/test-classes/unit/warmojotest/not-primary-artifact.xml" );
165         mojo = (WarMojo) lookupMojo( "war", pom );
166 
167         String testId = "NotPrimaryArtifact";
168         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
169         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
170         File webAppDirectory = new File( getTestDirectory(), testId );
171         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
172         ProjectHelperStub projectHelper = new ProjectHelperStub();
173         String warName = "simple";
174         File webAppSource = createWebAppSource( testId );
175         File classesDir = createClassesDir( testId, true );
176         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
177 
178         warArtifact.setFile( new File( "error.war" ) );
179         project.setArtifact( warArtifact );
180         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
181         setVariableValueToObject( mojo, "projectHelper", projectHelper );
182         setVariableValueToObject( mojo, "outputDirectory", outputDir );
183         setVariableValueToObject( mojo, "warName", warName );
184         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
185 
186         mojo.execute();
187 
188         //validate jar file
189         File expectedJarFile = new File( outputDir, "simple.war" );
190         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
191             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
192             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
193                                            new String[]{null, mojo.getWebXml().toString(), null, null, null, null} );
194     }
195 
196     public void testMetaInfContent()
197         throws Exception
198     {
199         String testId = "SimpleWarWithMetaInfContent";
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         String warName = "simple";
205         File webAppSource = createWebAppSource( testId );
206         File classesDir = createClassesDir( testId, true );
207         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
208 
209         // Create the sample config.xml
210         final File configFile = new File( webAppSource, "META-INF/config.xml" );
211         createFile( configFile, "<config></config>" );
212 
213         project.setArtifact( warArtifact );
214         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
215         setVariableValueToObject( mojo, "outputDirectory", outputDir );
216         setVariableValueToObject( mojo, "warName", warName );
217         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
218 
219         mojo.execute();
220 
221         //validate jar file
222         File expectedJarFile = new File( outputDir, "simple.war" );
223         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "META-INF/config.xml",
224             "WEB-INF/web.xml", "pansit.jsp", "org/web/app/last-exile.jsp",
225             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
226             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"}, new String[]{null, null,
227             mojo.getWebXml().toString(), null, null, null, null} );
228     }
229 
230     public void testMetaInfContentWithContainerConfig()
231         throws Exception
232     {
233         String testId = "SimpleWarWithContainerConfig";
234         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
235         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
236         File webAppDirectory = new File( getTestDirectory(), testId );
237         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
238         String warName = "simple";
239         File webAppSource = createWebAppSource( testId );
240         File classesDir = createClassesDir( testId, true );
241         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
242 
243         // Create the sample config.xml
244         final File configFile = new File( webAppSource, "META-INF/config.xml" );
245         createFile( configFile, "<config></config>" );
246 
247         project.setArtifact( warArtifact );
248         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
249         setVariableValueToObject( mojo, "outputDirectory", outputDir );
250         setVariableValueToObject( mojo, "warName", warName );
251         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
252         mojo.setContainerConfigXML( configFile );
253 
254         mojo.execute();
255 
256         //validate jar file
257         File expectedJarFile = new File( outputDir, "simple.war" );
258         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "META-INF/config.xml",
259             "WEB-INF/web.xml", "pansit.jsp", "org/web/app/last-exile.jsp",
260             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
261             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"}, new String[]{null, null,
262             mojo.getWebXml().toString(), null, null, null, null} );
263     }
264 
265 
266     public void testFailOnMissingWebXmlFalse()
267         throws Exception
268     {
269 
270         String testId = "SimpleWarMissingWebXmlFalse";
271         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
272         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
273         File webAppDirectory = new File( getTestDirectory(), testId );
274         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
275         String warName = "simple";
276         File webAppSource = createWebAppSource( testId );
277         File classesDir = createClassesDir( testId, true );
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.setFailOnMissingWebXml( false );
284         mojo.execute();
285 
286         //validate jar file
287         File expectedJarFile = new File( outputDir, "simple.war" );
288         final Map jarContent = assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "pansit.jsp",
289             "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
290             "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
291                                                                   new String[]{null, null, null, null, null} );
292 
293         assertFalse( "web.xml should be missing", jarContent.containsKey( "WEB-INF/web.xml" ) );
294     }
295 
296     public void testFailOnMissingWebXmlTrue()
297         throws Exception
298     {
299 
300         String testId = "SimpleWarMissingWebXmlTrue";
301         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
302         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
303         File webAppDirectory = new File( getTestDirectory(), testId );
304         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
305         String warName = "simple";
306         File webAppSource = createWebAppSource( testId );
307         File classesDir = createClassesDir( testId, true );
308 
309         project.setArtifact( warArtifact );
310         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
311         setVariableValueToObject( mojo, "outputDirectory", outputDir );
312         setVariableValueToObject( mojo, "warName", warName );
313         mojo.setFailOnMissingWebXml( true );
314 
315         try
316         {
317             mojo.execute();
318             fail( "Building of the war isn't possible because web.xml is missing" );
319         }
320         catch ( MojoExecutionException e )
321         {
322             //expected behaviour
323         }
324     }
325 
326     public void testAttachClasses()
327         throws Exception
328     {
329         String testId = "AttachClasses";
330         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
331         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
332         File webAppDirectory = new File( getTestDirectory(), testId );
333         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
334         String warName = "simple";
335         File webAppSource = createWebAppSource( testId );
336         File classesDir = createClassesDir( testId, false );
337         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
338 
339         project.setArtifact( warArtifact );
340         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
341         setVariableValueToObject( mojo, "outputDirectory", outputDir );
342         setVariableValueToObject( mojo, "warName", warName );
343         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
344         mojo.setAttachClasses( true );
345 
346         mojo.execute();
347 
348         //validate jar file
349         File expectedJarFile = new File( outputDir, "simple-classes.jar" );
350         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "sample-servlet.class"},
351                           new String[]{null, null} );
352     }
353 
354     public void testAttachClassesWithCustomClassifier()
355         throws Exception
356     {
357         String testId = "AttachClassesCustomClassifier";
358         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
359         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
360         File webAppDirectory = new File( getTestDirectory(), testId );
361         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
362         String warName = "simple";
363         File webAppSource = createWebAppSource( testId );
364         File classesDir = createClassesDir( testId, false );
365         File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
366 
367         project.setArtifact( warArtifact );
368         this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
369         setVariableValueToObject( mojo, "outputDirectory", outputDir );
370         setVariableValueToObject( mojo, "warName", warName );
371         mojo.setWebXml( new File( xmlSource, "web.xml" ) );
372         mojo.setAttachClasses( true );
373         mojo.setClassesClassifier( "mystuff" );
374 
375         mojo.execute();
376 
377         //validate jar file
378         File expectedJarFile = new File( outputDir, "simple-mystuff.jar" );
379         assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "sample-servlet.class"},
380                           new String[]{null, null} );
381     }
382 
383 
384     protected Map assertJarContent( final File expectedJarFile, final String[] files, final String[] filesContent )
385         throws IOException
386     {
387         // Sanity check
388         assertEquals( "Could not test, files and filesContent lenght does not match", files.length,
389                       filesContent.length );
390 
391         assertTrue( "war file not created: " + expectedJarFile.toString(), expectedJarFile.exists() );
392         final Map jarContent = new HashMap();
393         final JarFile jarFile = new JarFile( expectedJarFile );
394 
395         JarEntry entry;
396         Enumeration enumeration = jarFile.entries();
397         while ( enumeration.hasMoreElements() )
398         {
399             entry = (JarEntry) enumeration.nextElement();
400             Object previousValue = jarContent.put( entry.getName(), entry );
401             assertNull( "Duplicate Entry in Jar File: " + entry.getName(), previousValue );
402         }
403 
404         for ( int i = 0; i < files.length; i++ )
405         {
406             String file = files[i];
407             assertTrue( "File[" + file + "] not found in archive", jarContent.containsKey( file ) );
408             if ( filesContent[i] != null )
409             {
410                 assertEquals( "Content of file[" + file + "] does not match", filesContent[i],
411                               IOUtil.toString( jarFile.getInputStream( (ZipEntry) jarContent.get( file ) ) ) );
412             }
413         }
414         return jarContent;
415     }
416 
417 }
418