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.plugin.testing.stubs.ArtifactStub;
23  import org.apache.maven.plugin.war.stub.MavenProjectArtifactsStub;
24  import org.codehaus.plexus.util.FileUtils;
25  
26  import java.io.File;
27  import java.io.FileFilter;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Collections;
32  import java.util.LinkedList;
33  import java.util.List;
34  
35  /**
36   * @author Stephane Nicoll
37   */
38  public abstract class AbstractWarExplodedMojoTest
39      extends AbstractWarMojoTest
40  {
41  
42      protected WarExplodedMojo mojo;
43  
44      public void setUp()
45          throws Exception
46      {
47          super.setUp();
48          mojo = (WarExplodedMojo) lookupMojo( "exploded", getPomFile() );
49      }
50  
51  
52      /**
53       * Returns the pom configuration to use.
54       *
55       * @return the pom configuration
56       */
57      protected abstract File getPomFile();
58  
59      /**
60       * Returns the test directory to use.
61       *
62       * @return the test directory
63       */
64      protected abstract File getTestDirectory();
65  
66      /**
67       * Configures the exploded mojo for the specified test.
68       * <p/>
69       * If the <tt>sourceFiles</tt> parameter is <tt>null</tt>, sample
70       * JSPs are created by default.
71       *
72       * @param testId        the id of the test
73       * @param artifactStubs the dependencies (may be null)
74       * @param sourceFiles   the source files to create (may be null)
75       * @return the webapp directory
76       * @throws Exception if an error occurs while configuring the mojo
77       */
78      protected File setUpMojo( final String testId, ArtifactStub[] artifactStubs, String[] sourceFiles )
79          throws Exception
80      {
81          final MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
82          final File webAppDirectory = new File( getTestDirectory(), testId );
83  
84          // Create the webapp sources
85          File webAppSource;
86          if ( sourceFiles == null )
87          {
88              webAppSource = createWebAppSource( testId );
89          }
90          else
91          {
92              webAppSource = createWebAppSource( testId, false );
93              for ( int i = 0; i < sourceFiles.length; i++ )
94              {
95                  String sourceFile = sourceFiles[i];
96                  File sample = new File( webAppSource, sourceFile );
97                  createFile( sample );
98  
99              }
100 
101         }
102 
103         final File classesDir = createClassesDir( testId, true );
104         final File workDirectory = new File( getTestDirectory(), "/war/work-" + testId );
105         createDir( workDirectory );
106 
107         if ( artifactStubs != null )
108         {
109             for ( int i = 0; i < artifactStubs.length; i++ )
110             {
111                 ArtifactStub artifactStub = artifactStubs[i];
112                 project.addArtifact( artifactStub );
113             }
114         }
115 
116         configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
117         setVariableValueToObject( mojo, "workDirectory", workDirectory );
118 
119         return webAppDirectory;
120     }
121 
122 
123     /**
124      * Configures the exploded mojo for the specified test.
125      *
126      * @param testId        the id of the test
127      * @param artifactStubs the dependencies (may be null)
128      * @return the webapp directory
129      * @throws Exception if an error occurs while configuring the mojo
130      */
131     protected File setUpMojo( final String testId, ArtifactStub[] artifactStubs )
132         throws Exception
133     {
134         return setUpMojo( testId, artifactStubs, null );
135     }
136 
137     /**
138      * Cleans up a directory.
139      *
140      * @param directory the directory to remove
141      * @throws IOException if an error occurred while removing the directory
142      */
143     protected void cleanDirectory( File directory )
144         throws IOException
145     {
146         if ( directory != null && directory.isDirectory() && directory.exists() )
147         {
148             FileUtils.deleteDirectory( directory );
149         }
150     }
151 
152     /**
153      * Asserts the default content of the war based on the specified
154      * webapp directory.
155      *
156      * @param webAppDirectory the webapp directory
157      * @return a list of File objects that have been asserted
158      */
159     protected List assertDefaultContent( File webAppDirectory )
160     {
161         // Validate content of the webapp
162         File expectedWebSourceFile = new File( webAppDirectory, "pansit.jsp" );
163         File expectedWebSource2File = new File( webAppDirectory, "org/web/app/last-exile.jsp" );
164 
165         assertTrue( "source file not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists() );
166         assertTrue( "source file not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists() );
167 
168         final List content = new ArrayList();
169         content.add( expectedWebSourceFile );
170         content.add( expectedWebSource2File );
171 
172         return content;
173     }
174 
175 
176     /**
177      * Asserts the web.xml file of the war based on the specified
178      * webapp directory.
179      *
180      * @param webAppDirectory the webapp directory
181      * @return a list with the web.xml File object
182      */
183     protected List assertWebXml( File webAppDirectory )
184     {
185         File expectedWEBXMLFile = new File( webAppDirectory, "WEB-INF/web.xml" );
186         assertTrue( "web xml not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists() );
187 
188         final List content = new ArrayList();
189         content.add( expectedWEBXMLFile );
190 
191         return content;
192     }
193 
194     /**
195      * Asserts custom content of the war based on the specified webapp
196      * directory.
197      *
198      * @param webAppDirectory the webapp directory
199      * @param filePaths       an array of file paths relative to the webapp directory
200      * @param customMessage   a custom message if an assertion fails
201      * @return a list of File objects that have been inspected
202      */
203     protected List assertCustomContent( File webAppDirectory, String[] filePaths, String customMessage )
204     {
205         final List content = new ArrayList();
206         for ( int i = 0; i < filePaths.length; i++ )
207         {
208             String filePath = filePaths[i];
209             final File expectedFile = new File( webAppDirectory, filePath );
210             if ( customMessage != null )
211             {
212                 assertTrue( customMessage + " - " + expectedFile.toString(), expectedFile.exists() );
213             }
214             else
215             {
216                 assertTrue( "source file not found: " + expectedFile.toString(), expectedFile.exists() );
217             }
218             content.add( expectedFile );
219         }
220         return content;
221     }
222 
223     /**
224      * Asserts that the webapp contains only the specified files.
225      *
226      * @param webAppDirectory the webapp directory
227      * @param expectedFiles   the expected files
228      * @param filter          an optional filter to ignore some resources
229      */
230     protected void assertWebAppContent( File webAppDirectory, List expectedFiles, FileFilter filter )
231     {
232         final List webAppContent = new ArrayList();
233         if ( filter != null )
234         {
235             buildFilesList( webAppDirectory, filter, webAppContent );
236         }
237         else
238         {
239             buildFilesList( webAppDirectory, new FileFilterImpl( webAppDirectory, null ), webAppContent );
240         }
241 
242         // Now we have the files, sort them.
243         Collections.sort( expectedFiles );
244         Collections.sort( webAppContent );
245         assertEquals( "Invalid webapp content, expected " + expectedFiles.size() + "file(s) " + expectedFiles +
246             " but got " + webAppContent.size() + " file(s) " + webAppContent, expectedFiles, webAppContent );
247     }
248 
249     /**
250      * Builds the list of files and directories from the specified dir.
251      * <p/>
252      * Note that the filter is not used the usual way. If the filter does
253      * not accept the current file, it's not added but yet the subdirectories
254      * are added if any.
255      *
256      * @param dir     the base directory
257      * @param filter  the filter
258      * @param content the current content, updated recursivly
259      */
260     private void buildFilesList( final File dir, FileFilter filter, final List content )
261     {
262         final File[] files = dir.listFiles();
263 
264         for ( int i = 0; i < files.length; i++ )
265         {
266             File file = files[i];
267 
268             // Add the file if the filter is ok with it
269             if ( filter.accept( file ) )
270             {
271                 content.add( file );
272             }
273 
274             // Even if the file is not accepted and is a directory, add it
275             if ( file.isDirectory() )
276             {
277                 buildFilesList( file, filter, content );
278             }
279 
280         }
281     }
282 
283     class FileFilterImpl
284         implements FileFilter
285     {
286 
287         private final List rejectedFilePaths;
288 
289         private final int webAppDirIndex;
290 
291 
292         public FileFilterImpl( File webAppDirectory, String[] rejectedFilePaths )
293         {
294             if ( rejectedFilePaths != null )
295             {
296                 this.rejectedFilePaths = Arrays.asList( rejectedFilePaths );
297             }
298             else
299             {
300                 this.rejectedFilePaths = new ArrayList();
301             }
302             this.webAppDirIndex = webAppDirectory.getAbsolutePath().length() + 1;
303         }
304 
305         public boolean accept( File file )
306         {
307             String effectiveRelativePath = buildRelativePath( file );
308             return !( rejectedFilePaths.contains( effectiveRelativePath ) || file.isDirectory() );
309         }
310 
311         private String buildRelativePath( File f )
312         {
313             return f.getAbsolutePath().substring( webAppDirIndex );
314         }
315     }
316 
317 }