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 org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.handler.ArtifactHandler;
24 import org.apache.maven.plugin.war.overlay.DefaultOverlay;
25 import org.apache.maven.plugin.war.stub.MavenZipProject;
26 import org.apache.maven.plugin.war.stub.WarArtifactStub;
27 import org.apache.maven.plugin.war.stub.ZipArtifactStub;
28 import org.codehaus.plexus.util.FileUtils;
29
30 import java.io.File;
31 import java.util.LinkedList;
32
33
34
35
36
37
38 public class WarZipTest
39 extends AbstractWarMojoTest
40 {
41 WarMojo mojo;
42
43 private static File pomFile = new File( getBasedir(), "src/test/resources/unit/warziptest/war-with-zip.xml" );
44
45 protected File getTestDirectory()
46 {
47 return new File( getBasedir(), "target/test-classes/unit/warziptest" );
48 }
49
50 public void setUp()
51 throws Exception
52 {
53 super.setUp();
54 mojo = (WarMojo) lookupMojo( "war", pomFile );
55 }
56
57 private Artifact buildZipArtifact()
58 throws Exception
59 {
60 ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
61 File zipFile = new File( getTestDirectory(), "foobar.zip" );
62 return new ZipArtifactStub( "src/test/resources/unit/warziptest", artifactHandler, zipFile );
63 }
64
65 private File configureMojo( String testId )
66 throws Exception
67 {
68 MavenZipProject project = new MavenZipProject();
69 String outputDir = getTestDirectory().getAbsolutePath() + File.separatorChar + testId + "-output";
70
71 File outputDirFile = new File( outputDir );
72 if ( outputDirFile.exists() )
73 {
74 FileUtils.deleteDirectory( outputDirFile );
75 }
76 File webAppDirectory = new File( getTestDirectory(), testId );
77 WarArtifactStub warArtifact = new WarArtifactStub( getBasedir() );
78 String warName = "simple";
79 File webAppSource = createWebAppSource( testId );
80 File classesDir = createClassesDir( testId, true );
81 File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
82 project.setArtifact( warArtifact );
83
84 this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
85 setVariableValueToObject( mojo, "outputDirectory", outputDir );
86 setVariableValueToObject( mojo, "warName", warName );
87 setVariableValueToObject( mojo, "workDirectory", new File( getTestDirectory(), "work" ) );
88 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
89
90 project.getArtifacts().add( buildZipArtifact() );
91
92 return webAppDirectory;
93 }
94
95 public void testOneZipWithNoSkip()
96 throws Exception
97 {
98 File webAppDirectory = configureMojo( "one-zip" );
99
100 Overlay overlay = new DefaultOverlay( buildZipArtifact() );
101
102 overlay.setType( "zip" );
103 mojo.addOverlay( overlay );
104 mojo.execute();
105
106 File foo = new File( webAppDirectory, "foo.txt" );
107 assertTrue( "foo.txt not exists", foo.exists() );
108 assertTrue( "foo.txt not a file", foo.isFile() );
109
110 File barDirectory = new File( webAppDirectory, "bar" );
111 assertTrue( "bar directory not exists", barDirectory.exists() );
112 assertTrue( "bar not a directory", barDirectory.isDirectory() );
113
114 File bar = new File( barDirectory, "bar.txt" );
115 assertTrue( "bar/bar.txt not exists", bar.exists() );
116 assertTrue( "bar/bar.txt not a file", bar.isFile() );
117 }
118
119 public void testOneZipWithTargetPathOverlay()
120 throws Exception
121 {
122 File webAppDirectory = configureMojo( "one-zip-overlay-targetPath" );
123
124 Overlay overlay = new DefaultOverlay( buildZipArtifact() );
125 overlay.setSkip( false );
126 overlay.setType( "zip" );
127 overlay.setTargetPath( "overridePath" );
128 mojo.addOverlay( overlay );
129
130 mojo.execute();
131
132 File foo = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "foo.txt" );
133 assertTrue( "foo.txt not exists", foo.exists() );
134 assertTrue( "foo.txt not a file", foo.isFile() );
135
136 File barDirectory = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "bar" );
137 assertTrue( "bar directory not exists", barDirectory.exists() );
138 assertTrue( "bar not a directory", barDirectory.isDirectory() );
139
140 File bar = new File( barDirectory, "bar.txt" );
141 assertTrue( "bar/bar.txt not exists", bar.exists() );
142 assertTrue( "bar/bar.txt not a file", bar.isFile() );
143 }
144
145 public void testOneZipDefaultSkip()
146 throws Exception
147 {
148 File webAppDirectory = configureMojo( "one-zip-overlay-skip" );
149
150 mojo.execute();
151
152 assertZipContentNotHere( webAppDirectory );
153 }
154
155 public void testOneZipWithForceSkip()
156 throws Exception
157 {
158 File webAppDirectory = configureMojo( "one-zip-overlay-skip" );
159 Overlay overlay = new DefaultOverlay( buildZipArtifact() );
160 overlay.setSkip( true );
161 overlay.setType( "zip" );
162 mojo.addOverlay( overlay );
163
164 mojo.execute();
165 assertZipContentNotHere( webAppDirectory );
166
167 }
168
169 protected void assertZipContentNotHere( File webAppDirectory )
170 {
171 File foo = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "foo.txt" );
172 assertFalse( "foo.txt exists", foo.exists() );
173 assertFalse( "foo.txt a file", foo.isFile() );
174
175 File barDirectory = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "bar" );
176 assertFalse( "bar directory exists", barDirectory.exists() );
177 assertFalse( "bar is a directory", barDirectory.isDirectory() );
178
179 File bar = new File( barDirectory, "bar.txt" );
180 assertFalse( "bar/bar.txt exists", bar.exists() );
181 assertFalse( "bar/bar.txt is a file", bar.isFile() );
182 }
183 }