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