View Javadoc
1   package org.apache.maven.plugin.war.util;
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 junit.framework.TestCase;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.model.Dependency;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * @author Stephane Nicoll
31   */
32  public class WebappStructureTest
33      extends TestCase
34  {
35  
36  
37      public void testDependencyAnalysisNoChange()
38      {
39          final List<Dependency> dependencies = new ArrayList<Dependency>();
40          dependencies.add( createDependency( "groupTest", "artifactTest", "1.0" ) );
41          final WebappStructure cache = new WebappStructure( dependencies );
42  
43          final WebappStructure webappStructure = new WebappStructure( dependencies, cache );
44  
45          webappStructure.analyseDependencies( new WebappStructure.DependenciesAnalysisCallback()
46          {
47  
48              int count = 0;
49  
50              public void unchangedDependency( Dependency dependency )
51              {
52                  if ( count == 0 )
53                  {
54                      count++;
55                  }
56                  else
57                  {
58                      fail( "Should have called unchanged dependency only once" );
59                  }
60              }
61  
62              public void newDependency( Dependency dependency )
63              {
64                  fail( "Should have failed to trigger this callback" );
65              }
66  
67              public void removedDependency( Dependency dependency )
68              {
69                  fail( "Should have failed to trigger this callback" );
70              }
71  
72              public void updatedVersion( Dependency dependency, String previousVersion )
73              {
74                  fail( "Should have failed to trigger this callback" );
75              }
76  
77              public void updatedScope( Dependency dependency, String previousScope )
78              {
79                  fail( "Should have failed to trigger this callback" );
80              }
81  
82              public void updatedOptionalFlag( Dependency dependency, boolean previousOptional )
83              {
84                  fail( "Should have failed to trigger this callback" );
85              }
86  
87              public void updatedUnknown( Dependency dependency, Dependency previousDep )
88              {
89                  fail( "Should have failed to trigger this callback" );
90              }
91          } );
92  
93      }
94  
95  
96      public void testDependencyAnalysisWithNewDependency()
97      {
98          final List<Dependency> dependencies = new ArrayList<Dependency>();
99          dependencies.add( createDependency( "groupTest", "artifactTest", "1.0" ) );
100         final WebappStructure cache = new WebappStructure( dependencies );
101         final List<Dependency> newDependencies = new ArrayList<Dependency>( dependencies );
102         final Dependency newDependency = createDependency( "groupTest", "nexArtifact", "2.0" );
103         newDependencies.add( newDependency );
104 
105         final WebappStructure webappStructure = new WebappStructure( newDependencies, cache );
106 
107         webappStructure.analyseDependencies( new WebappStructure.DependenciesAnalysisCallback()
108         {
109 
110             int count = 0;
111 
112             public void unchangedDependency( Dependency dependency )
113             {
114                 if ( count == 0 )
115                 {
116                     count++;
117                 }
118                 else
119                 {
120                     fail( "Should have called unchanged dependency only once" );
121                 }
122             }
123 
124             public void newDependency( Dependency dependency )
125             {
126                 if ( !newDependency.equals( dependency ) )
127                 {
128                     fail( "Called new dependency with an unexpected dependency " + dependency );
129                 }
130             }
131 
132             public void removedDependency( Dependency dependency )
133             {
134                 fail( "Should have failed to trigger this callback" );
135             }
136 
137             public void updatedVersion( Dependency dependency, String previousVersion )
138             {
139                 fail( "Should have failed to trigger this callback" );
140             }
141 
142             public void updatedScope( Dependency dependency, String previousScope )
143             {
144                 fail( "Should have failed to trigger this callback" );
145             }
146 
147             public void updatedOptionalFlag( Dependency dependency, boolean previousOptional )
148             {
149                 fail( "Should have failed to trigger this callback" );
150             }
151 
152             public void updatedUnknown( Dependency dependency, Dependency previousDep )
153             {
154                 fail( "Should have failed to trigger this callback" );
155             }
156         } );
157 
158     }
159 
160     public void testDependencyAnalysisWithRemovedDependency()
161     {
162         final List<Dependency> dependencies = new ArrayList<Dependency>();
163         dependencies.add( createDependency( "groupTest", "artifactTest", "1.0" ) );
164         final Dependency removedDependency = createDependency( "groupTest", "removedDep", "5.2" );
165         dependencies.add( removedDependency );
166         final WebappStructure cache = new WebappStructure( dependencies );
167 
168         final List<Dependency> newDependencies = new ArrayList<Dependency>( dependencies );
169         newDependencies.remove( removedDependency );
170         final WebappStructure webappStructure = new WebappStructure( newDependencies, cache );
171 
172         webappStructure.analyseDependencies( new WebappStructure.DependenciesAnalysisCallback()
173         {
174 
175             int count = 0;
176 
177             public void unchangedDependency( Dependency dependency )
178             {
179                 if ( count == 0 )
180                 {
181                     count++;
182                 }
183                 else
184                 {
185                     fail( "Should have called unchanged dependency only once" );
186                 }
187             }
188 
189             public void newDependency( Dependency dependency )
190             {
191                 fail( "Should have failed to trigger this callback" );
192             }
193 
194             public void removedDependency( Dependency dependency )
195             {
196                 if ( !removedDependency.equals( dependency ) )
197                 {
198                     fail( "Called removed dependency with an unexpected dependency " + dependency );
199                 }
200             }
201 
202             public void updatedVersion( Dependency dependency, String previousVersion )
203             {
204                 fail( "Should have failed to trigger this callback" );
205             }
206 
207             public void updatedScope( Dependency dependency, String previousScope )
208             {
209                 fail( "Should have failed to trigger this callback" );
210             }
211 
212             public void updatedOptionalFlag( Dependency dependency, boolean previousOptional )
213             {
214                 fail( "Should have failed to trigger this callback" );
215             }
216 
217             public void updatedUnknown( Dependency dependency, Dependency previousDep )
218             {
219                 fail( "Should have failed to trigger this callback" );
220             }
221         } );
222 
223     }
224 
225     public void testUnknownFileNotAvailable()
226     {
227         final WebappStructure structure = new WebappStructure( new ArrayList<Dependency>() );
228         assertFalse( structure.isRegistered( "/foo/bar.txt" ) );
229     }
230 
231     public void testRegisterSamePathTwice()
232     {
233         final WebappStructure structure = new WebappStructure( new ArrayList<Dependency>() );
234         structure.registerFile( "overlay1", "WEB-INF/web.xml" );
235         assertFalse( structure.registerFile( "currentBuild", "WEB-INF/web.xml" ) );
236 
237     }
238 
239     public void testRegisterForced()
240     {
241         final String path = "WEB-INF/web.xml";
242         final WebappStructure structure = new WebappStructure( new ArrayList<Dependency>() );
243         assertFalse("New file should return false",
244                     structure.registerFileForced( "overlay1", path ));
245         assertEquals( "overlay1", structure.getOwner( path ) );         
246     }
247 
248     public void testRegisterSamePathTwiceForced()
249     {
250         final String path = "WEB-INF/web.xml";
251         final WebappStructure structure = new WebappStructure( new ArrayList<Dependency>() );
252         structure.registerFile( "overlay1", path );
253         assertEquals( "overlay1", structure.getOwner( path ) );
254         assertTrue("owner replacement should have returned true",
255                    structure.registerFileForced( "currentBuild", path ));
256         assertEquals("currentBuild", structure.getOwner( path ));
257     }
258 
259 
260     protected Dependency createDependency( String groupId, String artifactId, String version, String type, String scope,
261                                            String classifier )
262     {
263         final Dependency dep = new Dependency();
264         dep.setGroupId( groupId );
265         dep.setArtifactId( artifactId );
266         dep.setVersion( version );
267         if ( type == null )
268         {
269             dep.setType( "jar" );
270         }
271         else
272         {
273             dep.setType( type );
274         }
275         if ( scope != null )
276         {
277             dep.setScope( scope );
278         }
279         else
280         {
281             dep.setScope( Artifact.SCOPE_COMPILE );
282         }
283         if ( classifier != null )
284         {
285             dep.setClassifier( classifier );
286         }
287         return dep;
288     }
289 
290     protected Dependency createDependency( String groupId, String artifactId, String version, String type,
291                                            String scope )
292     {
293         return createDependency( groupId, artifactId, version, type, scope, null );
294     }
295 
296     protected Dependency createDependency( String groupId, String artifactId, String version, String type )
297     {
298         return createDependency( groupId, artifactId, version, type, null );
299     }
300 
301     protected Dependency createDependency( String groupId, String artifactId, String version )
302     {
303         return createDependency( groupId, artifactId, version, null );
304     }
305 }