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.testing.stubs.ArtifactStub;
25  import org.apache.maven.plugin.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   *
34   *
35   * @author Stephane Nicoll
36   */
37  public class WarDependenciesAnalysisTest
38      extends AbstractWarExplodedMojoTest
39  {
40      protected File getPomFile()
41      {
42          return new File( getBasedir(), "/target/test-classes/unit/dependencies/default.xml" );
43      }
44  
45      protected File getTestDirectory()
46      {
47          return new File( getBasedir(), "target/test-classes/unit/dependenciesanalysis/test-dir" );
48      }
49  
50  
51      public void testNoChange()
52          throws Exception
53      {
54          // setup test data
55          final String testId = "no-change";
56          final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
57          ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
58          jarArtifact.setArtifactId( "lib-test" );
59          jarArtifact.setVersion( "1.0" );
60  
61          doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, new ArtifactStub[]{jarArtifact},
62                                            new String[]{"WEB-INF/lib/lib-test-1.0.jar"},
63                                            new String[]{"WEB-INF/lib/lib-test-1.0.jar"} );
64  
65      }
66  
67      public void testRemovedDependency()
68          throws Exception
69      {
70          // setup test data
71          final String testId = "remove-dependency";
72          final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
73          ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
74          jarArtifact.setArtifactId( "lib-test" );
75          jarArtifact.setVersion( "1.0" );
76  
77          doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, null,
78                                            new String[]{"WEB-INF/lib/lib-test-1.0.jar"}, null );
79  
80      }
81  
82      public void testDependencyWithUpdatedVersion()
83          throws Exception
84      {
85          // setup test data
86          final String testId = "dependency-update-version";
87          final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
88          ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
89          jarArtifact.setArtifactId( "lib-test" );
90          jarArtifact.setVersion( "1.0" );
91  
92          ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
93          jarArtifact2.setArtifactId( "lib-test" );
94          jarArtifact2.setVersion( "2.0" );
95  
96          doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, new ArtifactStub[]{jarArtifact2},
97                                            new String[]{"WEB-INF/lib/lib-test-1.0.jar"},
98                                            new String[]{"WEB-INF/lib/lib-test-2.0.jar"} );
99  
100     }
101 
102     public void testDependencyNowProvided()
103         throws Exception
104     {
105         // setup test data
106         final String testId = "dependency-now-provided";
107         final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
108         ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
109         jarArtifact.setArtifactId( "lib-test" );
110         jarArtifact.setVersion( "1.0" );
111 
112         ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
113         jarArtifact2.setArtifactId( "lib-test" );
114         jarArtifact2.setVersion( "1.0" );
115         jarArtifact2.setScope( Artifact.SCOPE_PROVIDED );
116 
117         doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, 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 assertedFiles = new ArrayList();
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 assertedFiles2 = new ArrayList();
150             assertedFiles2.addAll( assertDefaultContent( webAppDirectory ) );
151             assertedFiles2.addAll( assertWebXml( webAppDirectory ) );
152             if ( secondCustomContent != null )
153             {
154                 assertedFiles2.addAll(
155                     assertCustomContent( webAppDirectory, secondCustomContent, "library not found" ) );
156 
157             }
158             assertWebAppContent( webAppDirectory, assertedFiles2, filter );
159 
160         }
161         finally
162         {
163             cleanDirectory( webAppDirectory );
164             cleanDirectory( mojo.getWorkDirectory() );
165 
166         }
167     }
168 
169     /**
170      * Configures the exploded mojo for the specified test.
171      *
172      * @param testId        the id of the test
173      * @param artifactStubs the dependencies (may be null)
174      * @return the webapp directory
175      * @throws Exception if an error occurs while configuring the mojo
176      */
177     protected File setUpMojoWithCache( final String testId, ArtifactStub[] artifactStubs )
178         throws Exception
179     {
180         final File webappDir = setUpMojo( testId, artifactStubs, null );
181         setVariableValueToObject( mojo, "useCache", Boolean.TRUE );
182         final File cacheFile = new File( mojo.getWorkDirectory(), "webapp-cache.xml" );
183         setVariableValueToObject( mojo, "cacheFile", cacheFile );
184 
185         return webappDir;
186     }
187 
188 
189 }