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