1   package org.apache.maven.plugin.war;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:olamy@apache.org">olamy</a>
35   * @version $Id: WarZipTest.java 659218 2008-05-22 19:28:04Z olamy $
36   * @since 7 Oct 07
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  
46      protected File getTestDirectory()
47      {
48          return new File( getBasedir(), "target/test-classes/unit/warziptest" );
49      }
50  
51      public void setUp()
52          throws Exception
53      {
54          super.setUp();
55          mojo = (WarMojo) lookupMojo( "war", pomFile );
56      }
57  
58      private Artifact buildZipArtifact()
59          throws Exception
60      {
61          ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
62          File zipFile = new File( getTestDirectory(), "foobar.zip" );
63          return new ZipArtifactStub( "src/test/resources/unit/warziptest", artifactHandler, zipFile );
64      }
65  
66      private File configureMojo( String testId )
67          throws Exception
68      {
69          MavenZipProject project = new MavenZipProject();
70          String outputDir = getTestDirectory().getAbsolutePath() + File.separatorChar + testId + "-output";
71          // clean up
72          File outputDirFile = new File( outputDir );
73          if ( outputDirFile.exists() )
74          {
75              FileUtils.deleteDirectory( outputDirFile );
76          }
77          File webAppDirectory = new File( getTestDirectory(), testId );
78          WarArtifactStub warArtifact = new WarArtifactStub( getBasedir() );
79          String warName = "simple";
80          File webAppSource = createWebAppSource( testId );
81          File classesDir = createClassesDir( testId, true );
82          File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
83          project.setArtifact( warArtifact );
84  
85          this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
86          setVariableValueToObject( mojo, "outputDirectory", outputDir );
87          setVariableValueToObject( mojo, "warName", warName );
88          setVariableValueToObject( mojo, "workDirectory", new File( getTestDirectory(), "work" ) );
89          mojo.setWebXml( new File( xmlSource, "web.xml" ) );
90  
91          project.getArtifacts().add( buildZipArtifact() );
92  
93          return webAppDirectory;
94      }
95  
96      public void testOneZipWithNoSkip()
97          throws Exception
98      {
99          File webAppDirectory = configureMojo( "one-zip" );
100 
101         Overlay overlay = new DefaultOverlay( buildZipArtifact() );
102         //overlay.setSkip( false );
103         overlay.setType( "zip" );
104         mojo.addOverlay( overlay );
105         mojo.execute();
106 
107         File foo = new File( webAppDirectory, "foo.txt" );
108         assertTrue( "foo.txt not exists", foo.exists() );
109         assertTrue( "foo.txt not a file", foo.isFile() );
110 
111         File barDirectory = new File( webAppDirectory, "bar" );
112         assertTrue( "bar directory not exists", barDirectory.exists() );
113         assertTrue( "bar not a directory", barDirectory.isDirectory() );
114 
115         File bar = new File( barDirectory, "bar.txt" );
116         assertTrue( "bar/bar.txt not exists", bar.exists() );
117         assertTrue( "bar/bar.txt not a file", bar.isFile() );
118     }
119 
120     public void testOneZipWithTargetPathOverlay()
121         throws Exception
122     {
123         File webAppDirectory = configureMojo( "one-zip-overlay-targetPath" );
124 
125         Overlay overlay = new DefaultOverlay( buildZipArtifact() );
126         overlay.setSkip( false );
127         overlay.setType( "zip" );
128         overlay.setTargetPath( "overridePath" );
129         mojo.addOverlay( overlay );
130 
131         mojo.execute();
132 
133         File foo = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "foo.txt" );
134         assertTrue( "foo.txt not exists", foo.exists() );
135         assertTrue( "foo.txt not a file", foo.isFile() );
136 
137         File barDirectory = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "bar" );
138         assertTrue( "bar directory not exists", barDirectory.exists() );
139         assertTrue( "bar not a directory", barDirectory.isDirectory() );
140 
141         File bar = new File( barDirectory, "bar.txt" );
142         assertTrue( "bar/bar.txt not exists", bar.exists() );
143         assertTrue( "bar/bar.txt not a file", bar.isFile() );
144     }
145 
146     public void testOneZipDefaultSkip()
147         throws Exception
148     {
149         File webAppDirectory = configureMojo( "one-zip-overlay-skip" );
150 
151         mojo.execute();
152 
153         assertZipContentNotHere( webAppDirectory );
154     }
155 
156     public void testOneZipWithForceSkip()
157         throws Exception
158     {
159         File webAppDirectory = configureMojo( "one-zip-overlay-skip" );
160         Overlay overlay = new DefaultOverlay( buildZipArtifact() );
161         overlay.setSkip( true );
162         overlay.setType( "zip" );
163         mojo.addOverlay( overlay );
164 
165         mojo.execute();
166         assertZipContentNotHere( webAppDirectory );
167 
168     }
169 
170     protected void assertZipContentNotHere( File webAppDirectory )
171     {
172         File foo = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "foo.txt" );
173         assertFalse( "foo.txt exists", foo.exists() );
174         assertFalse( "foo.txt a file", foo.isFile() );
175 
176         File barDirectory = new File( webAppDirectory.getPath() + File.separatorChar + "overridePath", "bar" );
177         assertFalse( "bar directory exists", barDirectory.exists() );
178         assertFalse( "bar is a directory", barDirectory.isDirectory() );
179 
180         File bar = new File( barDirectory, "bar.txt" );
181         assertFalse( "bar/bar.txt exists", bar.exists() );
182         assertFalse( "bar/bar.txt is a file", bar.isFile() );
183     }
184 }