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