1 package org.apache.maven.plugin.war;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.Map;
28 import java.util.jar.JarEntry;
29 import java.util.jar.JarFile;
30 import java.util.zip.ZipEntry;
31
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.war.stub.MavenProject4CopyConstructor;
34 import org.apache.maven.plugin.war.stub.ProjectHelperStub;
35 import org.apache.maven.plugin.war.stub.WarArtifact4CCStub;
36 import org.codehaus.plexus.util.IOUtil;
37
38
39
40
41 public class WarMojoTest
42 extends AbstractWarMojoTest
43 {
44 WarMojo mojo;
45
46 private static File pomFile = new File( getBasedir(),
47 "target/test-classes/unit/warmojotest/plugin-config-primary-artifact.xml" );
48
49 protected File getTestDirectory()
50 {
51 return new File( getBasedir(), "target/test-classes/unit/warmojotest" );
52 }
53
54 public void setUp()
55 throws Exception
56 {
57 super.setUp();
58 mojo = (WarMojo) lookupMojo( "war", pomFile );
59 }
60
61 public void testEnvironment()
62 throws Exception
63 {
64
65 }
66
67 public void testSimpleWar()
68 throws Exception
69 {
70 String testId = "SimpleWar";
71 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
72 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
73 File webAppDirectory = new File( getTestDirectory(), testId );
74 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
75 String warName = "simple";
76 File webAppSource = createWebAppSource( testId );
77 File classesDir = createClassesDir( testId, true );
78 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
79
80 project.setArtifact( warArtifact );
81 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
82 setVariableValueToObject( mojo, "outputDirectory", outputDir );
83 setVariableValueToObject( mojo, "warName", warName );
84 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
85
86 mojo.execute();
87
88
89 File expectedJarFile = new File( outputDir, "simple.war" );
90 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
91 "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
92 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null,
93 mojo.getWebXml().toString(), null, null, null, null } );
94 }
95
96 public void testSimpleWarPackagingExcludeWithIncludesRegEx()
97 throws Exception
98 {
99 String testId = "SimpleWarPackagingExcludeWithIncludesRegEx";
100 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
101 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
102 File webAppDirectory = new File( getTestDirectory(), testId );
103 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
104 String warName = "simple";
105 File webAppSource = createWebAppSource( testId );
106 File classesDir = createClassesDir( testId, true );
107 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
108
109 project.setArtifact( warArtifact );
110 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
111 setVariableValueToObject( mojo, "outputDirectory", outputDir );
112 setVariableValueToObject( mojo, "warName", warName );
113 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
114 setVariableValueToObject( mojo, "packagingIncludes", "%regex[(.(?!exile))+]" );
115
116 mojo.execute();
117
118
119 File expectedJarFile = new File( outputDir, "simple.war" );
120 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
121 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
122 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null,
123 mojo.getWebXml().toString(), null, null, null, }, new String[] { "org/web/app/last-exile.jsp" } );
124 }
125
126 public void testClassifier()
127 throws Exception
128 {
129 String testId = "Classifier";
130 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
131 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
132 File webAppDirectory = new File( getTestDirectory(), testId );
133 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
134 ProjectHelperStub projectHelper = new ProjectHelperStub();
135 String warName = "simple";
136 File webAppSource = createWebAppSource( testId );
137 File classesDir = createClassesDir( testId, true );
138 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
139
140 project.setArtifact( warArtifact );
141 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
142 setVariableValueToObject( mojo, "projectHelper", projectHelper );
143 setVariableValueToObject( mojo, "classifier", "test-classifier" );
144 setVariableValueToObject( mojo, "outputDirectory", outputDir );
145 setVariableValueToObject( mojo, "warName", warName );
146 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
147
148 mojo.execute();
149
150
151 File expectedJarFile = new File( outputDir, "simple-test-classifier.war" );
152 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
153 "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
154 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null,
155 mojo.getWebXml().toString(), null, null, null, null } );
156 }
157
158 public void testPrimaryArtifact()
159 throws Exception
160 {
161 String testId = "PrimaryArtifact";
162 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
163 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
164 File webAppDirectory = new File( getTestDirectory(), testId );
165 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
166 ProjectHelperStub projectHelper = new ProjectHelperStub();
167 String warName = "simple";
168 File webAppSource = createWebAppSource( testId );
169 File classesDir = createClassesDir( testId, true );
170 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
171
172 warArtifact.setFile( new File( "error.war" ) );
173 project.setArtifact( warArtifact );
174 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
175 setVariableValueToObject( mojo, "projectHelper", projectHelper );
176 setVariableValueToObject( mojo, "outputDirectory", outputDir );
177 setVariableValueToObject( mojo, "warName", warName );
178 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
179
180 mojo.execute();
181
182
183 File expectedJarFile = new File( outputDir, "simple.war" );
184 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
185 "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
186 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null,
187 mojo.getWebXml().toString(), null, null, null, null } );
188 }
189
190 public void testNotPrimaryArtifact()
191 throws Exception
192 {
193
194 File pom = new File( getBasedir(), "target/test-classes/unit/warmojotest/not-primary-artifact.xml" );
195 mojo = (WarMojo) lookupMojo( "war", pom );
196
197 String testId = "NotPrimaryArtifact";
198 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
199 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
200 File webAppDirectory = new File( getTestDirectory(), testId );
201 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
202 ProjectHelperStub projectHelper = new ProjectHelperStub();
203 String warName = "simple";
204 File webAppSource = createWebAppSource( testId );
205 File classesDir = createClassesDir( testId, true );
206 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
207
208 warArtifact.setFile( new File( "error.war" ) );
209 project.setArtifact( warArtifact );
210 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
211 setVariableValueToObject( mojo, "projectHelper", projectHelper );
212 setVariableValueToObject( mojo, "outputDirectory", outputDir );
213 setVariableValueToObject( mojo, "warName", warName );
214 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
215
216 mojo.execute();
217
218
219 File expectedJarFile = new File( outputDir, "simple.war" );
220 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
221 "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
222 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null,
223 mojo.getWebXml().toString(), null, null, null, null } );
224 }
225
226 public void testMetaInfContent()
227 throws Exception
228 {
229 String testId = "SimpleWarWithMetaInfContent";
230 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
231 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
232 File webAppDirectory = new File( getTestDirectory(), testId );
233 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
234 String warName = "simple";
235 File webAppSource = createWebAppSource( testId );
236 File classesDir = createClassesDir( testId, true );
237 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
238
239
240 final File configFile = new File( webAppSource, "META-INF/config.xml" );
241 createFile( configFile, "<config></config>" );
242
243 project.setArtifact( warArtifact );
244 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
245 setVariableValueToObject( mojo, "outputDirectory", outputDir );
246 setVariableValueToObject( mojo, "warName", warName );
247 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
248
249 mojo.execute();
250
251
252 File expectedJarFile = new File( outputDir, "simple.war" );
253 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "META-INF/config.xml",
254 "WEB-INF/web.xml", "pansit.jsp", "org/web/app/last-exile.jsp",
255 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
256 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null, null,
257 mojo.getWebXml().toString(), null, null, null, null } );
258 }
259
260 public void testMetaInfContentWithContainerConfig()
261 throws Exception
262 {
263 String testId = "SimpleWarWithContainerConfig";
264 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
265 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
266 File webAppDirectory = new File( getTestDirectory(), testId );
267 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
268 String warName = "simple";
269 File webAppSource = createWebAppSource( testId );
270 File classesDir = createClassesDir( testId, true );
271 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
272
273
274 final File configFile = new File( webAppSource, "META-INF/config.xml" );
275 createFile( configFile, "<config></config>" );
276
277 project.setArtifact( warArtifact );
278 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
279 setVariableValueToObject( mojo, "outputDirectory", outputDir );
280 setVariableValueToObject( mojo, "warName", warName );
281 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
282 mojo.setContainerConfigXML( configFile );
283
284 mojo.execute();
285
286
287 File expectedJarFile = new File( outputDir, "simple.war" );
288 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "META-INF/config.xml",
289 "WEB-INF/web.xml", "pansit.jsp", "org/web/app/last-exile.jsp",
290 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
291 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null, null,
292 mojo.getWebXml().toString(), null, null, null, null } );
293 }
294
295 public void testFailOnMissingWebXmlFalse()
296 throws Exception
297 {
298
299 String testId = "SimpleWarMissingWebXmlFalse";
300 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
301 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
302 File webAppDirectory = new File( getTestDirectory(), testId );
303 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
304 String warName = "simple";
305 File webAppSource = createWebAppSource( testId );
306 File classesDir = createClassesDir( testId, true );
307
308 project.setArtifact( warArtifact );
309 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
310 setVariableValueToObject( mojo, "outputDirectory", outputDir );
311 setVariableValueToObject( mojo, "warName", warName );
312 mojo.setFailOnMissingWebXml( false );
313 mojo.execute();
314
315
316 File expectedJarFile = new File( outputDir, "simple.war" );
317 final Map<String, JarEntry> jarContent =
318 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "pansit.jsp",
319 "org/web/app/last-exile.jsp", "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.xml",
320 "META-INF/maven/org.apache.maven.plugin.test/maven-war-plugin-test/pom.properties" }, new String[] { null, null,
321 null, null, null } );
322
323 assertFalse( "web.xml should be missing", jarContent.containsKey( "WEB-INF/web.xml" ) );
324 }
325
326 public void testFailOnMissingWebXmlTrue()
327 throws Exception
328 {
329
330 String testId = "SimpleWarMissingWebXmlTrue";
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<String>(), classesDir, webAppSource, webAppDirectory, project );
341 setVariableValueToObject( mojo, "outputDirectory", outputDir );
342 setVariableValueToObject( mojo, "warName", warName );
343 mojo.setFailOnMissingWebXml( true );
344
345 try
346 {
347 mojo.execute();
348 fail( "Building of the war isn't possible because web.xml is missing" );
349 }
350 catch ( MojoExecutionException e )
351 {
352
353 }
354 }
355
356 public void testAttachClasses()
357 throws Exception
358 {
359 String testId = "AttachClasses";
360 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
361 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
362 File webAppDirectory = new File( getTestDirectory(), testId );
363 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
364 String warName = "simple";
365 File webAppSource = createWebAppSource( testId );
366 File classesDir = createClassesDir( testId, false );
367 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
368
369 project.setArtifact( warArtifact );
370 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
371 setVariableValueToObject( mojo, "outputDirectory", outputDir );
372 setVariableValueToObject( mojo, "warName", warName );
373 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
374 mojo.setAttachClasses( true );
375
376 mojo.execute();
377
378
379 File expectedJarFile = new File( outputDir, "simple-classes.jar" );
380 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "sample-servlet.class" },
381 new String[] { null, null } );
382 }
383
384 public void testAttachClassesWithCustomClassifier()
385 throws Exception
386 {
387 String testId = "AttachClassesCustomClassifier";
388 MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
389 String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
390 File webAppDirectory = new File( getTestDirectory(), testId );
391 WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
392 String warName = "simple";
393 File webAppSource = createWebAppSource( testId );
394 File classesDir = createClassesDir( testId, false );
395 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
396
397 project.setArtifact( warArtifact );
398 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
399 setVariableValueToObject( mojo, "outputDirectory", outputDir );
400 setVariableValueToObject( mojo, "warName", warName );
401 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
402 mojo.setAttachClasses( true );
403 mojo.setClassesClassifier( "mystuff" );
404
405 mojo.execute();
406
407
408 File expectedJarFile = new File( outputDir, "simple-mystuff.jar" );
409 assertJarContent( expectedJarFile, new String[] { "META-INF/MANIFEST.MF", "sample-servlet.class" },
410 new String[] { null, null } );
411 }
412
413 protected Map<String, JarEntry> assertJarContent( final File expectedJarFile, final String[] files,
414 final String[] filesContent )
415 throws IOException
416 {
417 return assertJarContent( expectedJarFile, files, filesContent, null );
418 }
419
420 protected Map<String, JarEntry> assertJarContent( final File expectedJarFile, final String[] files,
421 final String[] filesContent, final String[] mustNotBeInJar )
422 throws IOException
423 {
424
425 assertEquals( "Could not test, files and filesContent lenght does not match", files.length, filesContent.length );
426
427 assertTrue( "war file not created: " + expectedJarFile.toString(), expectedJarFile.exists() );
428 final Map<String, JarEntry> jarContent = new HashMap<String, JarEntry>();
429 final JarFile jarFile = new JarFile( expectedJarFile );
430
431 JarEntry entry;
432 Enumeration<JarEntry> enumeration = jarFile.entries();
433 while ( enumeration.hasMoreElements() )
434 {
435 entry = (JarEntry) enumeration.nextElement();
436 Object previousValue = jarContent.put( entry.getName(), entry );
437 assertNull( "Duplicate Entry in Jar File: " + entry.getName(), previousValue );
438 }
439
440 for ( int i = 0; i < files.length; i++ )
441 {
442 String file = files[i];
443
444 assertTrue( "File[" + file + "] not found in archive", jarContent.containsKey( file ) );
445 if ( filesContent[i] != null )
446 {
447 assertEquals( "Content of file[" + file + "] does not match", filesContent[i],
448 IOUtil.toString( jarFile.getInputStream( (ZipEntry) jarContent.get( file ) ) ) );
449 }
450 }
451 if ( mustNotBeInJar != null )
452 {
453 for ( String file : mustNotBeInJar )
454 {
455 assertFalse( "File[" + file + "] found in archive", jarContent.containsKey( file ) );
456
457 }
458 }
459 return jarContent;
460 }
461
462 }