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.List;
36  
37  public abstract class AbstractWarMojoTest
38      extends AbstractMojoTestCase
39  {
40  
41      protected static final File OVERLAYS_TEMP_DIR = new File( getBasedir(), "target/test-overlays/" );
42  
43      protected static final File OVERLAYS_ROOT_DIR = new File( getBasedir(), "target/test-classes/overlays/" );
44  
45      protected static final String MANIFEST_PATH = "META-INF" + File.separator + "MANIFEST.MF";
46  
47      protected abstract File getTestDirectory()
48          throws Exception;
49  
50      /**
51       * initialize required parameters
52       *
53       * @param mojo
54       * @param filters
55       * @param classesDir
56       * @param webAppSource
57       * @param webAppDir
58       * @param project
59       * @throws Exception
60       */
61      protected void configureMojo( AbstractWarMojo mojo, List<String> filters, File classesDir, File webAppSource,
62                                    File webAppDir, MavenProjectBasicStub project )
63          throws Exception
64      {
65          setVariableValueToObject( mojo, "filters", filters );
66          setVariableValueToObject( mojo, "useCache", Boolean.FALSE );
67          setVariableValueToObject( mojo, "mavenFileFilter", lookup( MavenFileFilter.class.getName() ) );
68          setVariableValueToObject( mojo, "useJvmChmod", Boolean.TRUE );
69          MavenSession mavenSession =
70              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              for ( String o : xmlFiles )
97              {
98                  XMLFile = new File( xmlConfigDir, o );
99                  createFile( XMLFile );
100             }
101         }
102 
103         return xmlConfigDir;
104     }
105 
106     /**
107      * Returns the webapp source directory for the specified id.
108      *
109      * @param id the id of the test
110      * @return the source directory for that test
111      * @throws Exception if an exception occurs
112      */
113     protected File getWebAppSource( String id )
114         throws Exception
115     {
116         return new File( getTestDirectory(), "/" + id + "-test-data/source" );
117     }
118 
119     /**
120      * create an isolated web source with a sample jsp file
121      *
122      * @param id
123      * @return
124      * @throws Exception
125      */
126     protected File createWebAppSource( String id, boolean createSamples )
127         throws Exception
128     {
129         File webAppSource = getWebAppSource( id );
130         if ( createSamples )
131         {
132             File simpleJSP = new File( webAppSource, "pansit.jsp" );
133             File jspFile = new File( webAppSource, "org/web/app/last-exile.jsp" );
134 
135             createFile( simpleJSP );
136             createFile( jspFile );
137         }
138         return webAppSource;
139     }
140 
141     protected File createWebAppSource( String id )
142         throws Exception
143     {
144         return createWebAppSource( id, true );
145     }
146 
147     /**
148      * create a class directory with or without a sample class
149      *
150      * @param id
151      * @param empty
152      * @return
153      * @throws Exception
154      */
155     protected File createClassesDir( String id, boolean empty )
156         throws Exception
157     {
158         File classesDir = new File( getTestDirectory() + "/" + id + "-test-data/classes/" );
159 
160         createDir( classesDir );
161 
162         if ( !empty )
163         {
164             createFile( new File( classesDir + "/sample-servlet.class" ) );
165         }
166 
167         return classesDir;
168     }
169 
170     protected void createDir( File dir )
171     {
172         if ( !dir.exists() )
173         {
174             assertTrue( "can not create test dir: " + dir.toString(), dir.mkdirs() );
175         }
176     }
177 
178     protected void createFile( File testFile, String body )
179         throws Exception
180     {
181         createDir( testFile.getParentFile() );
182         FileUtils.fileWrite( testFile.toString(), body );
183 
184         assertTrue( "could not create file: " + testFile, testFile.exists() );
185     }
186 
187     protected void createFile( File testFile )
188         throws Exception
189     {
190         createFile( testFile, testFile.toString() );
191     }
192 
193     /**
194      * Generates test war
195      * <p/>
196      * Generates war with such a structure:
197      * <ul>
198      * <li>jsp
199      * <ul>
200      * <li>d
201      * <ul>
202      * <li>a.jsp</li>
203      * <li>b.jsp</li>
204      * <li>c.jsp</li>
205      * </ul>
206      * </li>
207      * <li>a.jsp</li>
208      * <li>b.jsp</li>
209      * <li>c.jsp</li>
210      * </ul>
211      * </li>
212      * <li>WEB-INF
213      * <ul>
214      * <li>classes
215      * <ul>
216      * <li>a.class</li>
217      * <li>b.class</li>
218      * <li>c.class</li>
219      * </ul>
220      * </li>
221      * <li>lib
222      * <ul>
223      * <li>a.jar</li>
224      * <li>b.jar</li>
225      * <li>c.jar</li>
226      * </ul>
227      * </li>
228      * <li>web.xml</li>
229      * </ul>
230      * </li>
231      * </ul>
232      * <p/>
233      * Each of the files will contain: id+'-'+path
234      *
235      * @param id the id of the overlay containing the full structure
236      * @return the war file
237      * @throws Exception if an error occurs
238      */
239     protected File generateFullOverlayWar( String id )
240         throws Exception
241     {
242         final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
243         if ( destFile.exists() )
244         {
245             return destFile;
246         }
247 
248         // Archive was not yet created for that id so let's create it
249         final File rootDir = new File( OVERLAYS_ROOT_DIR, id );
250         rootDir.mkdirs();
251         String[] filePaths =
252             new String[] { "jsp/d/a.jsp", "jsp/d/b.jsp", "jsp/d/c.jsp", "jsp/a.jsp", "jsp/b.jsp", "jsp/c.jsp",
253                 "WEB-INF/classes/a.class", "WEB-INF/classes/b.class", "WEB-INF/classes/c.class", "WEB-INF/lib/a.jar",
254                 "WEB-INF/lib/b.jar", "WEB-INF/lib/c.jar", "WEB-INF/web.xml" };
255 
256         for ( String filePath : filePaths )
257         {
258             createFile( new File( rootDir, filePath ), id + "-" + filePath );
259         }
260 
261         createArchive( rootDir, destFile );
262         return destFile;
263     }
264 
265     // Overlay utilities
266 
267     /**
268      * Builds a test overlay.
269      *
270      * @param id the id of the overlay (see test/resources/overlays)
271      * @return a test war artifact with the content of the given test overlay
272      */
273     protected ArtifactStub buildWarOverlayStub( String id )
274     {
275         // Create war file
276         final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
277         if ( !destFile.exists() )
278         {
279             createArchive( new File( OVERLAYS_ROOT_DIR, id ), destFile );
280         }
281 
282         return new WarOverlayStub( getBasedir(), id, destFile );
283     }
284 
285     protected File getOverlayFile( String id, String filePath )
286     {
287         final File overlayDir = new File( OVERLAYS_ROOT_DIR, id );
288         final File file = new File( overlayDir, filePath );
289 
290         // Make sure the file exists
291         assertTrue( "Overlay file " + filePath + " does not exist for overlay " + id + " at " + file.getAbsolutePath(),
292                     file.exists() );
293         return file;
294 
295     }
296 
297     protected void createArchive( final File directory, final File destinationFile )
298     {
299         try
300         {
301             // WarArchiver archiver = new WarArchiver();
302 
303             Archiver archiver = new JarArchiver();
304 
305             archiver.setUseJvmChmod( true );
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 }