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