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