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