View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.war;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Date;
24  
25  import org.apache.maven.execution.DefaultMavenExecutionRequest;
26  import org.apache.maven.execution.MavenExecutionRequest;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
30  import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
31  import org.apache.maven.plugins.war.stub.WarOverlayStub;
32  import org.codehaus.plexus.PlexusContainer;
33  import org.codehaus.plexus.archiver.ArchiverException;
34  import org.codehaus.plexus.archiver.jar.JarArchiver;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.eclipse.aether.RepositorySystemSession;
37  
38  public abstract class AbstractWarMojoTest extends AbstractMojoTestCase {
39  
40      protected static final File OVERLAYS_TEMP_DIR = new File(getBasedir(), "target/test-overlays/");
41  
42      protected static final File OVERLAYS_ROOT_DIR = new File(getBasedir(), "target/test-classes/overlays/");
43  
44      protected static final String MANIFEST_PATH = "META-INF" + File.separator + "MANIFEST.MF";
45  
46      protected abstract File getTestDirectory() throws Exception;
47  
48      protected void setUp() throws Exception {
49          super.setUp();
50  
51          MavenExecutionRequest request = new DefaultMavenExecutionRequest()
52                  .setSystemProperties(System.getProperties())
53                  .setStartTime(new Date());
54  
55          MavenSession mavenSession =
56                  new MavenSession((PlexusContainer) null, (RepositorySystemSession) null, request, null);
57          getContainer().addComponent(mavenSession, MavenSession.class.getName());
58      }
59      /**
60       * initialize required parameters
61       *
62       * @param mojo The mojo to be tested.
63       * @param classesDir The classes' directory.
64       * @param webAppSource The webAppSource.
65       * @param webAppDir The webAppDir folder.
66       * @param project The Maven project.
67       * @throws Exception in case of errors
68       */
69      protected void configureMojo(
70              AbstractWarMojo mojo, File classesDir, File webAppSource, File webAppDir, MavenProjectBasicStub project)
71              throws Exception {
72          setVariableValueToObject(mojo, "outdatedCheckPath", "WEB-INF/lib/");
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 The id.
83       * @param xmlFiles array of xml files.
84       * @return The created file.
85       * @throws Exception in case of errors.
86       */
87      protected File createXMLConfigDir(String id, String[] xmlFiles) throws Exception {
88          File xmlConfigDir = new File(getTestDirectory(), "/" + id + "-test-data/xml-config");
89          File xmlFile;
90  
91          createDir(xmlConfigDir);
92  
93          if (xmlFiles != null) {
94              for (String o : xmlFiles) {
95                  xmlFile = new File(xmlConfigDir, o);
96                  createFile(xmlFile);
97              }
98          }
99  
100         return xmlConfigDir;
101     }
102 
103     /**
104      * Returns the webapp source directory for the specified id.
105      *
106      * @param id the id of the test
107      * @return the source directory for that test
108      * @throws Exception if an exception occurs
109      */
110     protected File getWebAppSource(String id) throws Exception {
111         return new File(getTestDirectory(), "/" + id + "-test-data/source");
112     }
113 
114     /**
115      * create an isolated web source with a sample jsp file
116      *
117      * @param id The id.
118      * @param createSamples Create example files yes or no.
119      * @return The created file.
120      * @throws Exception in case of errors.
121      */
122     protected File createWebAppSource(String id, boolean createSamples) throws Exception {
123         File webAppSource = getWebAppSource(id);
124         if (createSamples) {
125             File simpleJSP = new File(webAppSource, "pansit.jsp");
126             File jspFile = new File(webAppSource, "org/web/app/last-exile.jsp");
127 
128             createFile(simpleJSP);
129             createFile(jspFile);
130         }
131         return webAppSource;
132     }
133 
134     protected File createWebAppSource(String id) throws Exception {
135         return createWebAppSource(id, true);
136     }
137 
138     /**
139      * create a class directory with or without a sample class
140      *
141      * @param id The id.
142      * @param empty true to create a class files false otherwise.
143      * @return The created class file.
144      * @throws Exception in case of errors.
145      */
146     protected File createClassesDir(String id, boolean empty) throws Exception {
147         File classesDir = new File(getTestDirectory() + "/" + id + "-test-data/classes/");
148 
149         createDir(classesDir);
150 
151         if (!empty) {
152             createFile(new File(classesDir + "/sample-servlet.clazz"));
153         }
154 
155         return classesDir;
156     }
157 
158     protected void createDir(File dir) {
159         if (!dir.exists()) {
160             assertTrue("can not create test dir: " + dir.toString(), dir.mkdirs());
161         }
162     }
163 
164     protected void createFile(File testFile, String body) throws Exception {
165         createDir(testFile.getParentFile());
166         FileUtils.fileWrite(testFile.toString(), body);
167 
168         assertTrue("could not create file: " + testFile, testFile.exists());
169     }
170 
171     protected void createFile(File testFile) throws Exception {
172         createFile(testFile, testFile.toString());
173     }
174 
175     /**
176      * Generates test war.
177      * Generates war with such a structure:
178      * <ul>
179      * <li>jsp
180      * <ul>
181      * <li>d
182      * <ul>
183      * <li>a.jsp</li>
184      * <li>b.jsp</li>
185      * <li>c.jsp</li>
186      * </ul>
187      * </li>
188      * <li>a.jsp</li>
189      * <li>b.jsp</li>
190      * <li>c.jsp</li>
191      * </ul>
192      * </li>
193      * <li>WEB-INF
194      * <ul>
195      * <li>classes
196      * <ul>
197      * <li>a.clazz</li>
198      * <li>b.clazz</li>
199      * <li>c.clazz</li>
200      * </ul>
201      * </li>
202      * <li>lib
203      * <ul>
204      * <li>a.jar</li>
205      * <li>b.jar</li>
206      * <li>c.jar</li>
207      * </ul>
208      * </li>
209      * <li>web.xml</li>
210      * </ul>
211      * </li>
212      * </ul>
213      * Each of the files will contain: id+'-'+path
214      *
215      * @param id the id of the overlay containing the full structure
216      * @return the war file
217      * @throws Exception if an error occurs
218      */
219     protected File generateFullOverlayWar(String id) throws Exception {
220         final File destFile = new File(OVERLAYS_TEMP_DIR, id + ".war");
221         if (destFile.exists()) {
222             return destFile;
223         }
224 
225         // Archive was not yet created for that id so let's create it
226         final File rootDir = new File(OVERLAYS_ROOT_DIR, id);
227         rootDir.mkdirs();
228         String[] filePaths = new String[] {
229             "jsp/d/a.jsp",
230             "jsp/d/b.jsp",
231             "jsp/d/c.jsp",
232             "jsp/a.jsp",
233             "jsp/b.jsp",
234             "jsp/c.jsp",
235             "WEB-INF/classes/a.clazz",
236             "WEB-INF/classes/b.clazz",
237             "WEB-INF/classes/c.clazz",
238             "WEB-INF/lib/a.jar",
239             "WEB-INF/lib/b.jar",
240             "WEB-INF/lib/c.jar",
241             "WEB-INF/web.xml"
242         };
243 
244         for (String filePath : filePaths) {
245             createFile(new File(rootDir, filePath), id + "-" + filePath);
246         }
247 
248         createArchive(rootDir, destFile);
249         return destFile;
250     }
251 
252     // Overlay utilities
253 
254     /**
255      * Builds a test overlay.
256      *
257      * @param id the id of the overlay (see test/resources/overlays)
258      * @return a test war artifact with the content of the given test overlay
259      */
260     protected ArtifactStub buildWarOverlayStub(String id) {
261         // Create war file
262         final File destFile = new File(OVERLAYS_TEMP_DIR, id + ".war");
263         if (!destFile.exists()) {
264             createArchive(new File(OVERLAYS_ROOT_DIR, id), destFile);
265         }
266 
267         return new WarOverlayStub(getBasedir(), id, destFile);
268     }
269 
270     protected File getOverlayFile(String id, String filePath) {
271         final File overlayDir = new File(OVERLAYS_ROOT_DIR, id);
272         final File file = new File(overlayDir, filePath);
273 
274         // Make sure the file exists
275         assertTrue(
276                 "Overlay file " + filePath + " does not exist for overlay " + id + " at " + file.getAbsolutePath(),
277                 file.exists());
278         return file;
279     }
280 
281     protected void createArchive(final File directory, final File destinationFile) {
282         try {
283             JarArchiver archiver = new JarArchiver();
284 
285             archiver.setDestFile(destinationFile);
286             archiver.addDirectory(directory);
287 
288             archiver.createArchive();
289 
290         } catch (ArchiverException e) {
291             e.printStackTrace();
292             fail("Failed to create overlay archive " + e.getMessage());
293         } catch (IOException e) {
294             e.printStackTrace();
295             fail("Unexpected exception " + e.getMessage());
296         }
297     }
298 }