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