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 java.io.File;
23  import java.io.IOException;
24  import java.util.Date;
25  import java.util.List;
26  
27  import org.apache.maven.execution.DefaultMavenExecutionRequest;
28  import org.apache.maven.execution.MavenExecutionRequest;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
32  import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
33  import org.apache.maven.plugins.war.stub.WarOverlayStub;
34  import org.apache.maven.shared.filtering.MavenFileFilter;
35  import org.codehaus.plexus.PlexusContainer;
36  import org.codehaus.plexus.archiver.Archiver;
37  import org.codehaus.plexus.archiver.ArchiverException;
38  import org.codehaus.plexus.archiver.jar.JarArchiver;
39  import org.codehaus.plexus.util.FileUtils;
40  import org.sonatype.aether.RepositorySystemSession;
41  
42  public abstract class AbstractWarMojoTest
43      extends AbstractMojoTestCase
44  {
45  
46      protected static final File OVERLAYS_TEMP_DIR = new File( getBasedir(), "target/test-overlays/" );
47  
48      protected static final File OVERLAYS_ROOT_DIR = new File( getBasedir(), "target/test-classes/overlays/" );
49  
50      protected static final String MANIFEST_PATH = "META-INF" + File.separator + "MANIFEST.MF";
51  
52      protected abstract File getTestDirectory()
53          throws Exception;
54  
55      /**
56       * initialize required parameters
57       *
58       * @param mojo The mojo to be tested.
59       * @param filters The list of filters.
60       * @param classesDir The classes directory.
61       * @param webAppSource The webAppSource.
62       * @param webAppDir The webAppDir folder.
63       * @param project The Maven project.
64       * @throws Exception in case of errors
65       */
66      protected void configureMojo( AbstractWarMojo mojo, List<String> filters, File classesDir, File webAppSource,
67                                    File webAppDir, MavenProjectBasicStub project )
68                                        throws Exception
69      {
70          setVariableValueToObject( mojo, "filters", filters );
71          setVariableValueToObject( mojo, "mavenFileFilter", lookup( MavenFileFilter.class.getName() ) );
72          setVariableValueToObject( mojo, "useJvmChmod", Boolean.TRUE );
73  
74          MavenExecutionRequest request =
75              new DefaultMavenExecutionRequest().setSystemProperties( System.getProperties() ).setStartTime( new Date() );
76  
77          MavenSession mavenSession =
78              new MavenSession( (PlexusContainer) null, (RepositorySystemSession) null, request, null );
79          setVariableValueToObject( mojo, "session", mavenSession );
80          setVariableValueToObject( mojo, "outdatedCheckPath", "WEB-INF/lib/" );
81          mojo.setClassesDirectory( classesDir );
82          mojo.setWarSourceDirectory( webAppSource );
83          mojo.setWebappDirectory( webAppDir );
84          mojo.setProject( project );
85      }
86  
87      /**
88       * create an isolated xml dir
89       *
90       * @param id The id.
91       * @param xmlFiles array of xml files.
92       * @return The created file.
93       * @throws Exception in case of errors.
94       */
95      protected File createXMLConfigDir( String id, String[] xmlFiles )
96          throws Exception
97      {
98          File xmlConfigDir = new File( getTestDirectory(), "/" + id + "-test-data/xml-config" );
99          File XMLFile;
100 
101         createDir( xmlConfigDir );
102 
103         if ( xmlFiles != null )
104         {
105             for ( String o : xmlFiles )
106             {
107                 XMLFile = new File( xmlConfigDir, o );
108                 createFile( XMLFile );
109             }
110         }
111 
112         return xmlConfigDir;
113     }
114 
115     /**
116      * Returns the webapp source directory for the specified id.
117      *
118      * @param id the id of the test
119      * @return the source directory for that test
120      * @throws Exception if an exception occurs
121      */
122     protected File getWebAppSource( String id )
123         throws Exception
124     {
125         return new File( getTestDirectory(), "/" + id + "-test-data/source" );
126     }
127 
128     /**
129      * create an isolated web source with a sample jsp file
130      *
131      * @param id The id.
132      * @param createSamples Create example files yes or no.
133      * @return The created file.
134      * @throws Exception in case of errors.
135      */
136     protected File createWebAppSource( String id, boolean createSamples )
137         throws Exception
138     {
139         File webAppSource = getWebAppSource( id );
140         if ( createSamples )
141         {
142             File simpleJSP = new File( webAppSource, "pansit.jsp" );
143             File jspFile = new File( webAppSource, "org/web/app/last-exile.jsp" );
144 
145             createFile( simpleJSP );
146             createFile( jspFile );
147         }
148         return webAppSource;
149     }
150 
151     protected File createWebAppSource( String id )
152         throws Exception
153     {
154         return createWebAppSource( id, true );
155     }
156 
157     /**
158      * create a class directory with or without a sample class
159      *
160      * @param id The id.
161      * @param empty true to create a class files false otherwise.
162      * @return The created class file.
163      * @throws Exception in case of errors.
164      */
165     protected File createClassesDir( String id, boolean empty )
166         throws Exception
167     {
168         File classesDir = new File( getTestDirectory() + "/" + id + "-test-data/classes/" );
169 
170         createDir( classesDir );
171 
172         if ( !empty )
173         {
174             createFile( new File( classesDir + "/sample-servlet.class" ) );
175         }
176 
177         return classesDir;
178     }
179 
180     protected void createDir( File dir )
181     {
182         if ( !dir.exists() )
183         {
184             assertTrue( "can not create test dir: " + dir.toString(), dir.mkdirs() );
185         }
186     }
187 
188     protected void createFile( File testFile, String body )
189         throws Exception
190     {
191         createDir( testFile.getParentFile() );
192         FileUtils.fileWrite( testFile.toString(), body );
193 
194         assertTrue( "could not create file: " + testFile, testFile.exists() );
195     }
196 
197     protected void createFile( File testFile )
198         throws Exception
199     {
200         createFile( testFile, testFile.toString() );
201     }
202 
203     /**
204      * Generates test war.
205      * Generates war with such a structure:
206      * <ul>
207      * <li>jsp
208      * <ul>
209      * <li>d
210      * <ul>
211      * <li>a.jsp</li>
212      * <li>b.jsp</li>
213      * <li>c.jsp</li>
214      * </ul>
215      * </li>
216      * <li>a.jsp</li>
217      * <li>b.jsp</li>
218      * <li>c.jsp</li>
219      * </ul>
220      * </li>
221      * <li>WEB-INF
222      * <ul>
223      * <li>classes
224      * <ul>
225      * <li>a.class</li>
226      * <li>b.class</li>
227      * <li>c.class</li>
228      * </ul>
229      * </li>
230      * <li>lib
231      * <ul>
232      * <li>a.jar</li>
233      * <li>b.jar</li>
234      * <li>c.jar</li>
235      * </ul>
236      * </li>
237      * <li>web.xml</li>
238      * </ul>
239      * </li>
240      * </ul>
241      * Each of the files will contain: id+'-'+path
242      *
243      * @param id the id of the overlay containing the full structure
244      * @return the war file
245      * @throws Exception if an error occurs
246      */
247     protected File generateFullOverlayWar( String id )
248         throws Exception
249     {
250         final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
251         if ( destFile.exists() )
252         {
253             return destFile;
254         }
255 
256         // Archive was not yet created for that id so let's create it
257         final File rootDir = new File( OVERLAYS_ROOT_DIR, id );
258         rootDir.mkdirs();
259         String[] filePaths = new String[] { "jsp/d/a.jsp", "jsp/d/b.jsp", "jsp/d/c.jsp", "jsp/a.jsp", "jsp/b.jsp",
260             "jsp/c.jsp", "WEB-INF/classes/a.class", "WEB-INF/classes/b.class", "WEB-INF/classes/c.class",
261             "WEB-INF/lib/a.jar", "WEB-INF/lib/b.jar", "WEB-INF/lib/c.jar", "WEB-INF/web.xml" };
262 
263         for ( String filePath : filePaths )
264         {
265             createFile( new File( rootDir, filePath ), id + "-" + filePath );
266         }
267 
268         createArchive( rootDir, destFile );
269         return destFile;
270     }
271 
272     // Overlay utilities
273 
274     /**
275      * Builds a test overlay.
276      *
277      * @param id the id of the overlay (see test/resources/overlays)
278      * @return a test war artifact with the content of the given test overlay
279      */
280     protected ArtifactStub buildWarOverlayStub( String id )
281     {
282         // Create war file
283         final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
284         if ( !destFile.exists() )
285         {
286             createArchive( new File( OVERLAYS_ROOT_DIR, id ), destFile );
287         }
288 
289         return new WarOverlayStub( getBasedir(), id, destFile );
290     }
291 
292     protected File getOverlayFile( String id, String filePath )
293     {
294         final File overlayDir = new File( OVERLAYS_ROOT_DIR, id );
295         final File file = new File( overlayDir, filePath );
296 
297         // Make sure the file exists
298         assertTrue( "Overlay file " + filePath + " does not exist for overlay " + id + " at " + file.getAbsolutePath(),
299                     file.exists() );
300         return file;
301 
302     }
303 
304     protected void createArchive( final File directory, final File destinationFile )
305     {
306         try
307         {
308             // WarArchiver archiver = new WarArchiver();
309 
310             Archiver archiver = new JarArchiver();
311 
312             archiver.setDestFile( destinationFile );
313             archiver.addDirectory( directory );
314 
315             // archiver.setWebxml( new File(directory, "WEB-INF/web.xml"));
316 
317             // create archive
318             archiver.createArchive();
319 
320         }
321         catch ( ArchiverException e )
322         {
323             e.printStackTrace();
324             fail( "Failed to create overlay archive " + e.getMessage() );
325         }
326         catch ( IOException e )
327         {
328             e.printStackTrace();
329             fail( "Unexpected exception " + e.getMessage() );
330         }
331     }
332 
333 }