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.plugin.testing.stubs.ArtifactStub;
25  import org.apache.maven.plugins.war.stub.JarArtifactStub;
26  
27  import java.io.File;
28  import java.io.FileFilter;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * @author Stephane Nicoll
34   */
35  public class WarDependenciesAnalysisTest
36      extends AbstractWarExplodedMojoTest
37  {
38      protected File getPomFile()
39      {
40          return new File( getBasedir(), "/target/test-classes/unit/dependencies/default.xml" );
41      }
42  
43      protected File getTestDirectory()
44      {
45          return new File( getBasedir(), "target/test-classes/unit/dependenciesanalysis/test-dir" );
46      }
47  
48      public void testNoChange()
49          throws Exception
50      {
51          // setup test data
52          final String testId = "no-change";
53          final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
54          ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
55          jarArtifact.setArtifactId( "lib-test" );
56          jarArtifact.setVersion( "1.0" );
57  
58          doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact },
59                                            new ArtifactStub[] { jarArtifact },
60                                            new String[] { "WEB-INF/lib/lib-test-1.0.jar" },
61                                            new String[] { "WEB-INF/lib/lib-test-1.0.jar" } );
62  
63      }
64  
65      public void testRemovedDependency()
66          throws Exception
67      {
68          // setup test data
69          final String testId = "remove-dependency";
70          final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
71          ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
72          jarArtifact.setArtifactId( "lib-test" );
73          jarArtifact.setVersion( "1.0" );
74  
75          doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact }, null,
76                                            new String[] { "WEB-INF/lib/lib-test-1.0.jar" }, null );
77  
78      }
79  
80      public void testDependencyWithUpdatedVersion()
81          throws Exception
82      {
83          // setup test data
84          final String testId = "dependency-update-version";
85          final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
86          ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
87          jarArtifact.setArtifactId( "lib-test" );
88          jarArtifact.setVersion( "1.0" );
89  
90          ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
91          jarArtifact2.setArtifactId( "lib-test" );
92          jarArtifact2.setVersion( "2.0" );
93  
94          doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact },
95                                            new ArtifactStub[] { jarArtifact2 },
96                                            new String[] { "WEB-INF/lib/lib-test-1.0.jar" },
97                                            new String[] { "WEB-INF/lib/lib-test-2.0.jar" } );
98  
99      }
100 
101     public void testDependencyNowProvided()
102         throws Exception
103     {
104         // setup test data
105         final String testId = "dependency-now-provided";
106         final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
107         ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
108         jarArtifact.setArtifactId( "lib-test" );
109         jarArtifact.setVersion( "1.0" );
110 
111         ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
112         jarArtifact2.setArtifactId( "lib-test" );
113         jarArtifact2.setVersion( "1.0" );
114         jarArtifact2.setScope( Artifact.SCOPE_PROVIDED );
115 
116         doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact },
117                                           new ArtifactStub[] { jarArtifact2 },
118                                           new String[] { "WEB-INF/lib/lib-test-1.0.jar" }, null );
119 
120     }
121 
122     protected void doTestTwiceWithUpdatedDependency( String testId, ArtifactStub[] firstStubs,
123                                                      ArtifactStub[] secondStubs, String[] firstCustomContent,
124                                                      String[] secondCustomContent )
125         throws Exception
126     {
127         // setup test data
128         final File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
129         final File webAppDirectory = setUpMojoWithCache( testId, firstStubs );
130         try
131         {
132             mojo.setWebXml( new File( xmlSource, "web.xml" ) );
133             mojo.execute();
134 
135             final List<File> assertedFiles = new ArrayList<File>();
136             assertedFiles.addAll( assertDefaultContent( webAppDirectory ) );
137             assertedFiles.addAll( assertWebXml( webAppDirectory ) );
138             assertedFiles.addAll( assertCustomContent( webAppDirectory, firstCustomContent, "library not found" ) );
139 
140             // Ok now check that there is no more files/directories
141             final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
142             assertWebAppContent( webAppDirectory, assertedFiles, filter );
143 
144             // Run the thing again and check it's ok
145             setUpMojoWithCache( testId, secondStubs );
146 
147             mojo.execute();
148 
149             final List<File> assertedFiles2 = new ArrayList<File>();
150             assertedFiles2.addAll( assertDefaultContent( webAppDirectory ) );
151             assertedFiles2.addAll( assertWebXml( webAppDirectory ) );
152             if ( secondCustomContent != null )
153             {
154                 assertedFiles2.addAll( assertCustomContent( webAppDirectory, secondCustomContent, "library not found" ) );
155 
156             }
157             assertWebAppContent( webAppDirectory, assertedFiles2, filter );
158 
159         }
160         finally
161         {
162             cleanDirectory( webAppDirectory );
163             cleanDirectory( mojo.getWorkDirectory() );
164 
165         }
166     }
167 
168     /**
169      * Configures the exploded mojo for the specified test.
170      *
171      * @param testId the id of the test
172      * @param artifactStubs the dependencies (may be null)
173      * @return the webapp directory
174      * @throws Exception if an error occurs while configuring the mojo
175      */
176     protected File setUpMojoWithCache( final String testId, ArtifactStub[] artifactStubs )
177         throws Exception
178     {
179         final File webappDir = setUpMojo( testId, artifactStubs, null );
180         setVariableValueToObject( mojo, "useCache", Boolean.TRUE );
181         final File cacheFile = new File( mojo.getWorkDirectory(), "webapp-cache.xml" );
182         setVariableValueToObject( mojo, "cacheFile", cacheFile );
183 
184         return webappDir;
185     }
186 
187 }