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.Enumeration;
24  import java.util.HashMap;
25  import java.util.LinkedList;
26  import java.util.Map;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarFile;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.handler.ArtifactHandler;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.war.stub.JarArtifactStub;
34  import org.apache.maven.plugins.war.stub.MavenProject4CopyConstructor;
35  import org.apache.maven.plugins.war.stub.MavenProjectArtifactsStub;
36  import org.apache.maven.plugins.war.stub.ProjectHelperStub;
37  import org.apache.maven.plugins.war.stub.WarArtifact4CCStub;
38  import org.codehaus.plexus.util.IOUtil;
39  
40  /**
41   * comprehensive test on buildExplodedWebApp is done on WarExplodedMojoTest
42   */
43  public class WarMojoTest extends AbstractWarMojoTest {
44      WarMojo mojo;
45  
46      private static File pomFile =
47              new File(getBasedir(), "target/test-classes/unit/warmojotest/plugin-config-primary-artifact.xml");
48  
49      protected File getTestDirectory() {
50          return new File(getBasedir(), "target/test-classes/unit/warmojotest");
51      }
52  
53      public void setUp() throws Exception {
54          super.setUp();
55          mojo = (WarMojo) lookupMojo("war", pomFile);
56      }
57  
58      public void testEnvironment() throws Exception {
59          // see setup
60      }
61  
62      public void testSimpleWar() throws Exception {
63          String testId = "SimpleWar";
64          MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
65          String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
66          File webAppDirectory = new File(getTestDirectory(), testId);
67          WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
68          String warName = "simple";
69          File webAppSource = createWebAppSource(testId);
70          File classesDir = createClassesDir(testId, true);
71          File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
72  
73          project.setArtifact(warArtifact);
74          this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
75          setVariableValueToObject(mojo, "outputDirectory", outputDir);
76          setVariableValueToObject(mojo, "warName", warName);
77          mojo.setWebXml(new File(xmlSource, "web.xml"));
78  
79          mojo.execute();
80  
81          // validate jar file
82          File expectedJarFile = new File(outputDir, "simple.war");
83          assertJarContent(
84                  expectedJarFile,
85                  new String[] {
86                      "META-INF/MANIFEST.MF",
87                      "WEB-INF/web.xml",
88                      "pansit.jsp",
89                      "org/web/app/last-exile.jsp",
90                      "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
91                      "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
92                  },
93                  new String[] {null, mojo.getWebXml().toString(), null, null, null, null});
94      }
95  
96      public void testSimpleWarPackagingExcludeWithIncludesRegEx() throws Exception {
97          String testId = "SimpleWarPackagingExcludeWithIncludesRegEx";
98          MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
99          String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
100         File webAppDirectory = new File(getTestDirectory(), testId);
101         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
102         String warName = "simple";
103         File webAppSource = createWebAppSource(testId);
104         File classesDir = createClassesDir(testId, true);
105         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
106 
107         project.setArtifact(warArtifact);
108         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
109         setVariableValueToObject(mojo, "outputDirectory", outputDir);
110         setVariableValueToObject(mojo, "warName", warName);
111         mojo.setWebXml(new File(xmlSource, "web.xml"));
112         setVariableValueToObject(mojo, "packagingIncludes", "%regex[(.(?!exile))+]");
113         //        setVariableValueToObject( mojo, "packagingIncludes", "%regex" );
114 
115         mojo.execute();
116 
117         // validate jar file
118         File expectedJarFile = new File(outputDir, "simple.war");
119         assertJarContent(
120                 expectedJarFile,
121                 new String[] {
122                     "META-INF/MANIFEST.MF",
123                     "WEB-INF/web.xml",
124                     "pansit.jsp",
125                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
126                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
127                 },
128                 new String[] {
129                     null, mojo.getWebXml().toString(), null, null, null,
130                 },
131                 new String[] {"org/web/app/last-exile.jsp"});
132     }
133 
134     public void testClassifier() throws Exception {
135         String testId = "Classifier";
136         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
137         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
138         File webAppDirectory = new File(getTestDirectory(), testId);
139         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
140         ProjectHelperStub projectHelper = new ProjectHelperStub();
141         String warName = "simple";
142         File webAppSource = createWebAppSource(testId);
143         File classesDir = createClassesDir(testId, true);
144         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
145 
146         project.setArtifact(warArtifact);
147         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
148         setVariableValueToObject(mojo, "projectHelper", projectHelper);
149         setVariableValueToObject(mojo, "classifier", "test-classifier");
150         setVariableValueToObject(mojo, "outputDirectory", outputDir);
151         setVariableValueToObject(mojo, "warName", warName);
152         mojo.setWebXml(new File(xmlSource, "web.xml"));
153 
154         mojo.execute();
155 
156         // validate jar file
157         File expectedJarFile = new File(outputDir, "simple-test-classifier.war");
158         assertJarContent(
159                 expectedJarFile,
160                 new String[] {
161                     "META-INF/MANIFEST.MF",
162                     "WEB-INF/web.xml",
163                     "pansit.jsp",
164                     "org/web/app/last-exile.jsp",
165                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
166                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
167                 },
168                 new String[] {null, mojo.getWebXml().toString(), null, null, null, null});
169     }
170 
171     public void testPrimaryArtifact() throws Exception {
172         String testId = "PrimaryArtifact";
173         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
174         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
175         File webAppDirectory = new File(getTestDirectory(), testId);
176         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
177         ProjectHelperStub projectHelper = new ProjectHelperStub();
178         String warName = "simple";
179         File webAppSource = createWebAppSource(testId);
180         File classesDir = createClassesDir(testId, true);
181         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
182 
183         warArtifact.setFile(new File("error.war"));
184         project.setArtifact(warArtifact);
185         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
186         setVariableValueToObject(mojo, "projectHelper", projectHelper);
187         setVariableValueToObject(mojo, "outputDirectory", outputDir);
188         setVariableValueToObject(mojo, "warName", warName);
189         mojo.setWebXml(new File(xmlSource, "web.xml"));
190 
191         mojo.execute();
192 
193         // validate jar file
194         File expectedJarFile = new File(outputDir, "simple.war");
195         assertJarContent(
196                 expectedJarFile,
197                 new String[] {
198                     "META-INF/MANIFEST.MF",
199                     "WEB-INF/web.xml",
200                     "pansit.jsp",
201                     "org/web/app/last-exile.jsp",
202                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
203                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
204                 },
205                 new String[] {null, mojo.getWebXml().toString(), null, null, null, null});
206     }
207 
208     public void testNotPrimaryArtifact() throws Exception {
209         // use a different pom
210         File pom = new File(getBasedir(), "target/test-classes/unit/warmojotest/not-primary-artifact.xml");
211         mojo = (WarMojo) lookupMojo("war", pom);
212 
213         String testId = "NotPrimaryArtifact";
214         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
215         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
216         File webAppDirectory = new File(getTestDirectory(), testId);
217         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
218         ProjectHelperStub projectHelper = new ProjectHelperStub();
219         String warName = "simple";
220         File webAppSource = createWebAppSource(testId);
221         File classesDir = createClassesDir(testId, true);
222         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
223 
224         warArtifact.setFile(new File("error.war"));
225         project.setArtifact(warArtifact);
226         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
227         setVariableValueToObject(mojo, "projectHelper", projectHelper);
228         setVariableValueToObject(mojo, "outputDirectory", outputDir);
229         setVariableValueToObject(mojo, "warName", warName);
230         mojo.setWebXml(new File(xmlSource, "web.xml"));
231 
232         mojo.execute();
233 
234         // validate jar file
235         File expectedJarFile = new File(outputDir, "simple.war");
236         assertJarContent(
237                 expectedJarFile,
238                 new String[] {
239                     "META-INF/MANIFEST.MF",
240                     "WEB-INF/web.xml",
241                     "pansit.jsp",
242                     "org/web/app/last-exile.jsp",
243                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
244                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
245                 },
246                 new String[] {null, mojo.getWebXml().toString(), null, null, null, null});
247     }
248 
249     public void testMetaInfContent() throws Exception {
250         String testId = "SimpleWarWithMetaInfContent";
251         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
252         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
253         File webAppDirectory = new File(getTestDirectory(), testId);
254         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
255         String warName = "simple";
256         File webAppSource = createWebAppSource(testId);
257         File classesDir = createClassesDir(testId, true);
258         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
259 
260         // Create the sample config.xml
261         final File configFile = new File(webAppSource, "META-INF/config.xml");
262         createFile(configFile, "<config></config>");
263 
264         project.setArtifact(warArtifact);
265         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
266         setVariableValueToObject(mojo, "outputDirectory", outputDir);
267         setVariableValueToObject(mojo, "warName", warName);
268         mojo.setWebXml(new File(xmlSource, "web.xml"));
269 
270         mojo.execute();
271 
272         // validate jar file
273         File expectedJarFile = new File(outputDir, "simple.war");
274         assertJarContent(
275                 expectedJarFile,
276                 new String[] {
277                     "META-INF/MANIFEST.MF",
278                     "META-INF/config.xml",
279                     "WEB-INF/web.xml",
280                     "pansit.jsp",
281                     "org/web/app/last-exile.jsp",
282                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
283                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
284                 },
285                 new String[] {null, null, mojo.getWebXml().toString(), null, null, null, null});
286     }
287 
288     public void testMetaInfContentWithContainerConfig() throws Exception {
289         String testId = "SimpleWarWithContainerConfig";
290         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
291         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
292         File webAppDirectory = new File(getTestDirectory(), testId);
293         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
294         String warName = "simple";
295         File webAppSource = createWebAppSource(testId);
296         File classesDir = createClassesDir(testId, true);
297         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
298 
299         // Create the sample config.xml
300         final File configFile = new File(webAppSource, "META-INF/config.xml");
301         createFile(configFile, "<config></config>");
302 
303         project.setArtifact(warArtifact);
304         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
305         setVariableValueToObject(mojo, "outputDirectory", outputDir);
306         setVariableValueToObject(mojo, "warName", warName);
307         mojo.setWebXml(new File(xmlSource, "web.xml"));
308         mojo.setContainerConfigXML(configFile);
309 
310         mojo.execute();
311 
312         // validate jar file
313         File expectedJarFile = new File(outputDir, "simple.war");
314         assertJarContent(
315                 expectedJarFile,
316                 new String[] {
317                     "META-INF/MANIFEST.MF",
318                     "META-INF/config.xml",
319                     "WEB-INF/web.xml",
320                     "pansit.jsp",
321                     "org/web/app/last-exile.jsp",
322                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
323                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
324                 },
325                 new String[] {null, null, mojo.getWebXml().toString(), null, null, null, null});
326     }
327 
328     public void testFailOnMissingWebXmlFalse() throws Exception {
329 
330         String testId = "SimpleWarMissingWebXmlFalse";
331         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
332         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
333         File webAppDirectory = new File(getTestDirectory(), testId);
334         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
335         String warName = "simple";
336         File webAppSource = createWebAppSource(testId);
337         File classesDir = createClassesDir(testId, true);
338 
339         project.setArtifact(warArtifact);
340         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
341         setVariableValueToObject(mojo, "outputDirectory", outputDir);
342         setVariableValueToObject(mojo, "warName", warName);
343         mojo.setFailOnMissingWebXml(false);
344         mojo.execute();
345 
346         // validate jar file
347         File expectedJarFile = new File(outputDir, "simple.war");
348         final Map<String, JarEntry> jarContent = assertJarContent(
349                 expectedJarFile,
350                 new String[] {
351                     "META-INF/MANIFEST.MF",
352                     "pansit.jsp",
353                     "org/web/app/last-exile.jsp",
354                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
355                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
356                 },
357                 new String[] {null, null, null, null, null});
358 
359         assertFalse("web.xml should be missing", jarContent.containsKey("WEB-INF/web.xml"));
360     }
361 
362     public void testFailOnMissingWebXmlTrue() throws Exception {
363 
364         String testId = "SimpleWarMissingWebXmlTrue";
365         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
366         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
367         File webAppDirectory = new File(getTestDirectory(), testId);
368         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
369         String warName = "simple";
370         File webAppSource = createWebAppSource(testId);
371         File classesDir = createClassesDir(testId, true);
372 
373         project.setArtifact(warArtifact);
374         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
375         setVariableValueToObject(mojo, "outputDirectory", outputDir);
376         setVariableValueToObject(mojo, "warName", warName);
377         mojo.setFailOnMissingWebXml(true);
378 
379         try {
380             mojo.execute();
381             fail("Building of the war isn't possible because web.xml is missing");
382         } catch (MojoExecutionException e) {
383             // expected behaviour
384         }
385     }
386 
387     public void testFailOnMissingWebXmlNotSpecifiedAndServlet30Used() throws Exception {
388         String testId = "SimpleWarUnderServlet30";
389         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
390         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
391         File webAppDirectory = new File(getTestDirectory(), testId);
392         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
393         String warName = "simple";
394         File webAppSource = createWebAppSource(testId);
395         File classesDir = createClassesDir(testId, true);
396 
397         final ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
398         JarArtifactStub jarArtifactStub = new JarArtifactStub(getBasedir(), artifactHandler);
399         jarArtifactStub.setFile(
400                 new File(getBasedir(), "/target/test-classes/unit/sample_wars/javax.servlet-api-3.0.1.jar"));
401         jarArtifactStub.setScope(Artifact.SCOPE_PROVIDED);
402         project.addArtifact(jarArtifactStub);
403 
404         project.setArtifact(warArtifact);
405         project.setFile(warArtifact.getFile());
406         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
407         setVariableValueToObject(mojo, "outputDirectory", outputDir);
408         setVariableValueToObject(mojo, "warName", warName);
409 
410         mojo.execute();
411 
412         // validate war file
413         File expectedWarFile = new File(outputDir, "simple.war");
414         final Map<String, JarEntry> jarContent = assertJarContent(
415                 expectedWarFile,
416                 new String[] {
417                     "META-INF/MANIFEST.MF",
418                     "pansit.jsp",
419                     "org/web/app/last-exile.jsp",
420                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
421                     "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties"
422                 },
423                 new String[] {null, null, null, null, null});
424 
425         assertFalse("web.xml should be missing", jarContent.containsKey("WEB-INF/web.xml"));
426     }
427 
428     public void testFailOnMissingWebXmlNotSpecifiedAndServlet30NotUsed() throws Exception {
429         String testId = "SimpleWarNotUnderServlet30";
430         MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
431         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
432         File webAppDirectory = new File(getTestDirectory(), testId);
433         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
434         String warName = "simple";
435         File webAppSource = createWebAppSource(testId);
436         File classesDir = createClassesDir(testId, true);
437 
438         project.setArtifact(warArtifact);
439         project.setFile(warArtifact.getFile());
440         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
441         setVariableValueToObject(mojo, "outputDirectory", outputDir);
442         setVariableValueToObject(mojo, "warName", warName);
443 
444         try {
445             mojo.execute();
446             fail("Building of the war isn't possible because no 'failOnMissingWebXml' policy was set and the project "
447                     + "does not depend on Servlet 3.0");
448         } catch (MojoExecutionException e) {
449             // expected behaviour
450         }
451     }
452 
453     public void testAttachClasses() throws Exception {
454         String testId = "AttachClasses";
455         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
456         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
457         File webAppDirectory = new File(getTestDirectory(), testId);
458         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
459         String warName = "simple";
460         File webAppSource = createWebAppSource(testId);
461         File classesDir = createClassesDir(testId, false);
462         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
463 
464         project.setArtifact(warArtifact);
465         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
466         setVariableValueToObject(mojo, "outputDirectory", outputDir);
467         setVariableValueToObject(mojo, "warName", warName);
468         mojo.setWebXml(new File(xmlSource, "web.xml"));
469         mojo.setAttachClasses(true);
470         mojo.setClassesClassifier("classes");
471 
472         mojo.execute();
473 
474         // validate jar file
475         File expectedJarFile = new File(outputDir, "simple-classes.jar");
476         assertJarContent(
477                 expectedJarFile, new String[] {"META-INF/MANIFEST.MF", "sample-servlet.clazz"}, new String[] {null, null
478                 });
479     }
480 
481     public void testAttachClassesWithCustomClassifier() throws Exception {
482         String testId = "AttachClassesCustomClassifier";
483         MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
484         String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
485         File webAppDirectory = new File(getTestDirectory(), testId);
486         WarArtifact4CCStub warArtifact = new WarArtifact4CCStub(getBasedir());
487         String warName = "simple";
488         File webAppSource = createWebAppSource(testId);
489         File classesDir = createClassesDir(testId, false);
490         File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
491 
492         project.setArtifact(warArtifact);
493         this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
494         setVariableValueToObject(mojo, "outputDirectory", outputDir);
495         setVariableValueToObject(mojo, "warName", warName);
496         mojo.setWebXml(new File(xmlSource, "web.xml"));
497         mojo.setAttachClasses(true);
498         mojo.setClassesClassifier("mystuff");
499 
500         mojo.execute();
501 
502         // validate jar file
503         File expectedJarFile = new File(outputDir, "simple-mystuff.jar");
504         assertJarContent(
505                 expectedJarFile, new String[] {"META-INF/MANIFEST.MF", "sample-servlet.clazz"}, new String[] {null, null
506                 });
507     }
508 
509     protected Map<String, JarEntry> assertJarContent(
510             final File expectedJarFile, final String[] files, final String[] filesContent) throws IOException {
511         return assertJarContent(expectedJarFile, files, filesContent, null);
512     }
513 
514     protected Map<String, JarEntry> assertJarContent(
515             final File expectedJarFile,
516             final String[] files,
517             final String[] filesContent,
518             final String[] mustNotBeInJar)
519             throws IOException {
520         // Sanity check
521         assertEquals("Could not test, files and filesContent length does not match", files.length, filesContent.length);
522 
523         assertTrue("war file not created: " + expectedJarFile.toString(), expectedJarFile.exists());
524         final Map<String, JarEntry> jarContent = new HashMap<>();
525         try (JarFile jarFile = new JarFile(expectedJarFile)) {
526             Enumeration<JarEntry> enumeration = jarFile.entries();
527             while (enumeration.hasMoreElements()) {
528                 JarEntry entry = enumeration.nextElement();
529                 Object previousValue = jarContent.put(entry.getName(), entry);
530                 assertNull("Duplicate Entry in Jar File: " + entry.getName(), previousValue);
531             }
532 
533             for (int i = 0; i < files.length; i++) {
534                 String file = files[i];
535 
536                 assertTrue("File[" + file + "] not found in archive", jarContent.containsKey(file));
537                 if (filesContent[i] != null) {
538                     assertEquals(
539                             "Content of file[" + file + "] does not match",
540                             filesContent[i],
541                             IOUtil.toString(jarFile.getInputStream(jarContent.get(file))));
542                 }
543             }
544             if (mustNotBeInJar != null) {
545                 for (String file : mustNotBeInJar) {
546                     assertFalse("File[" + file + "]  found in archive", jarContent.containsKey(file));
547                 }
548             }
549             return jarContent;
550         }
551     }
552 }