1 package org.apache.maven.shared.utils.introspection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
28
29 import junit.framework.TestCase;
30
31
32
33
34
35 public class ReflectionValueExtractorTest
36 extends TestCase
37 {
38 private Project project;
39
40 protected void setUp()
41 throws Exception
42 {
43 super.setUp();
44
45 Dependency dependency1 = new Dependency();
46 dependency1.setArtifactId( "dep1" );
47 Dependency dependency2 = new Dependency();
48 dependency2.setArtifactId( "dep2" );
49
50 project = new Project();
51 project.setModelVersion( "4.0.0" );
52 project.setGroupId( "org.apache.maven" );
53 project.setArtifactId( "maven-core" );
54 project.setName( "Maven" );
55 project.setVersion( "2.0-SNAPSHOT" );
56 project.setScm( new Scm() );
57 project.getScm().setConnection( "scm-connection" );
58 project.addDependency( dependency1 );
59 project.addDependency( dependency2 );
60 project.setBuild( new Build() );
61
62
63 project.addArtifact( new Artifact("g0","a0","v0","e0","c0") );
64 project.addArtifact( new Artifact("g1","a1","v1","e1","c1") );
65 project.addArtifact( new Artifact("g2","a2","v2","e2","c2") );
66 }
67
68 public void testValueExtraction()
69 throws Exception
70 {
71
72
73
74
75 assertEquals( "4.0.0", ReflectionValueExtractor.evaluate( "project.modelVersion", project ) );
76
77 assertEquals( "org.apache.maven", ReflectionValueExtractor.evaluate( "project.groupId", project ) );
78
79 assertEquals( "maven-core", ReflectionValueExtractor.evaluate( "project.artifactId", project ) );
80
81 assertEquals( "Maven", ReflectionValueExtractor.evaluate( "project.name", project ) );
82
83 assertEquals( "2.0-SNAPSHOT", ReflectionValueExtractor.evaluate( "project.version", project ) );
84
85
86
87
88
89 assertEquals( "scm-connection", ReflectionValueExtractor.evaluate( "project.scm.connection", project ) );
90
91
92
93
94
95 List<?> dependencies = (List<?>) ReflectionValueExtractor.evaluate( "project.dependencies", project );
96
97 assertNotNull( dependencies );
98
99 assertEquals( 2, dependencies.size() );
100
101
102
103
104
105
106 Dependency dependency = (Dependency) ReflectionValueExtractor.evaluate( "project.dependencies[0]", project );
107
108 assertNotNull( dependency );
109
110 assertTrue( "dep1".equals( dependency.getArtifactId() ) );
111
112 String artifactId = (String) ReflectionValueExtractor.evaluate( "project.dependencies[1].artifactId", project );
113
114 assertTrue( "dep2".equals( artifactId ) );
115
116
117
118 dependency = (Dependency) ReflectionValueExtractor.evaluate( "project.dependenciesAsArray[0]", project );
119
120 assertNotNull( dependency );
121
122 assertTrue( "dep1".equals( dependency.getArtifactId() ) );
123
124 artifactId = (String) ReflectionValueExtractor.evaluate( "project.dependenciesAsArray[1].artifactId", project );
125
126 assertTrue( "dep2".equals( artifactId ) );
127
128
129
130 dependency = (Dependency) ReflectionValueExtractor.evaluate( "project.dependenciesAsMap(dep1)", project );
131
132 assertNotNull( dependency );
133
134 assertTrue( "dep1".equals( dependency.getArtifactId() ) );
135
136 artifactId = (String) ReflectionValueExtractor.evaluate( "project.dependenciesAsMap(dep2).artifactId", project );
137
138 assertTrue( "dep2".equals( artifactId ) );
139
140
141
142
143
144 Build build = (Build) ReflectionValueExtractor.evaluate( "project.build", project );
145
146 assertNotNull( build );
147 }
148
149 public void testValueExtractorWithAInvalidExpression()
150 throws Exception
151 {
152 assertNull( ReflectionValueExtractor.evaluate( "project.foo", project ) );
153 assertNull( ReflectionValueExtractor.evaluate( "project.dependencies[10]", project ) );
154 assertNull( ReflectionValueExtractor.evaluate( "project.dependencies[0].foo", project ) );
155 }
156
157 public void testMappedDottedKey()
158 throws Exception
159 {
160 Map<String, String> map = new HashMap<String, String>();
161 map.put( "a.b", "a.b-value" );
162
163 assertEquals( "a.b-value", ReflectionValueExtractor.evaluate("h.value(a.b)", new ValueHolder(map)) );
164 }
165
166 public void testIndexedMapped()
167 throws Exception
168 {
169 Map<Object, Object> map = new HashMap<Object, Object>();
170 map.put( "a", "a-value" );
171 List<Object> list = new ArrayList<Object>();
172 list.add( map );
173
174 assertEquals( "a-value", ReflectionValueExtractor.evaluate("h.value[0](a)", new ValueHolder(list)) );
175 }
176
177 public void testMappedIndexed()
178 throws Exception
179 {
180 List<Object> list = new ArrayList<Object>();
181 list.add( "a-value" );
182 Map<Object, Object> map = new HashMap<Object, Object>();
183 map.put( "a", list );
184 assertEquals( "a-value", ReflectionValueExtractor.evaluate("h.value(a)[0]", new ValueHolder(map)) );
185 }
186
187 public void testMappedMissingDot()
188 throws Exception
189 {
190 Map<Object, Object> map = new HashMap<Object, Object>();
191 map.put( "a", new ValueHolder( "a-value" ) );
192 assertNull( ReflectionValueExtractor.evaluate("h.value(a)value", new ValueHolder(map)) );
193 }
194
195 public void testIndexedMissingDot()
196 throws Exception
197 {
198 List<Object> list = new ArrayList<Object>();
199 list.add( new ValueHolder( "a-value" ) );
200 assertNull( ReflectionValueExtractor.evaluate("h.value[0]value", new ValueHolder(list)) );
201 }
202
203 public void testDotDot()
204 throws Exception
205 {
206 assertNull( ReflectionValueExtractor.evaluate("h..value", new ValueHolder("value")) );
207 }
208
209 public void testBadIndexedSyntax()
210 throws Exception
211 {
212 List<Object> list = new ArrayList<Object>();
213 list.add( "a-value" );
214 Object value = new ValueHolder( list );
215
216 assertNull( ReflectionValueExtractor.evaluate("h.value[", value) );
217 assertNull( ReflectionValueExtractor.evaluate("h.value[]", value) );
218 assertNull( ReflectionValueExtractor.evaluate("h.value[a]", value) );
219 assertNull( ReflectionValueExtractor.evaluate("h.value[0", value) );
220 assertNull( ReflectionValueExtractor.evaluate("h.value[0)", value) );
221 assertNull( ReflectionValueExtractor.evaluate("h.value[-1]", value) );
222 }
223
224 public void testBadMappedSyntax()
225 throws Exception
226 {
227 Map<Object, Object> map = new HashMap<Object, Object>();
228 map.put( "a", "a-value" );
229 Object value = new ValueHolder( map );
230
231 assertNull( ReflectionValueExtractor.evaluate("h.value(", value) );
232 assertNull( ReflectionValueExtractor.evaluate("h.value()", value) );
233 assertNull( ReflectionValueExtractor.evaluate("h.value(a", value) );
234 assertNull( ReflectionValueExtractor.evaluate("h.value(a]", value) );
235 }
236
237 public void testIllegalIndexedType()
238 throws Exception
239 {
240 try
241 {
242 ReflectionValueExtractor.evaluate("h.value[1]", new ValueHolder("string"));
243 }
244 catch ( Exception e )
245 {
246
247 }
248 }
249
250 public void testIllegalMappedType()
251 throws Exception
252 {
253 try
254 {
255 ReflectionValueExtractor.evaluate("h.value(key)", new ValueHolder("string"));
256 }
257 catch ( Exception e )
258 {
259
260 }
261 }
262
263 public void testTrimRootToken()
264 throws Exception
265 {
266 assertNull( ReflectionValueExtractor.evaluate("project", project, true) );
267 }
268
269 public void testArtifactMap()
270 throws Exception
271 {
272 assertEquals( "g0", ((Artifact) ReflectionValueExtractor.evaluate("project.artifactMap(g0:a0:c0)", project)).getGroupId() );
273 assertEquals( "a1", ((Artifact) ReflectionValueExtractor.evaluate("project.artifactMap(g1:a1:c1)", project)).getArtifactId() );
274 assertEquals( "c2", ((Artifact) ReflectionValueExtractor.evaluate("project.artifactMap(g2:a2:c2)", project)).getClassifier() );
275 }
276
277 public static class Artifact
278 {
279 private String groupId;
280 private String artifactId;
281 private String version;
282 private String extension;
283 private String classifier;
284
285 public Artifact( String groupId, String artifactId, String version, String extension, String classifier )
286 {
287 this.groupId = groupId;
288 this.artifactId = artifactId;
289 this.version = version;
290 this.extension = extension;
291 this.classifier = classifier;
292 }
293
294 public String getGroupId()
295 {
296 return groupId;
297 }
298 public void setGroupId( String groupId )
299 {
300 this.groupId = groupId;
301 }
302 public String getArtifactId()
303 {
304 return artifactId;
305 }
306 public void setArtifactId( String artifactId )
307 {
308 this.artifactId = artifactId;
309 }
310 public String getVersion()
311 {
312 return version;
313 }
314 public void setVersion( String version )
315 {
316 this.version = version;
317 }
318 public String getExtension()
319 {
320 return extension;
321 }
322 public void setExtension( String extension )
323 {
324 this.extension = extension;
325 }
326 public String getClassifier()
327 {
328 return classifier;
329 }
330 public void setClassifier( String classifier )
331 {
332 this.classifier = classifier;
333 }
334 }
335
336 public static class Project
337 {
338 private String modelVersion;
339
340 private String groupId;
341
342 private Scm scm;
343
344 private final List<Dependency> dependencies = new ArrayList<Dependency>();
345
346 private Build build;
347
348 private String artifactId;
349
350 private String name;
351
352 private String version;
353
354 private Map<String,Artifact> artifactMap = new HashMap<String,Artifact>();
355
356 public void setModelVersion( String modelVersion )
357 {
358 this.modelVersion = modelVersion;
359 }
360
361 public void setGroupId( String groupId )
362 {
363 this.groupId = groupId;
364 }
365
366 public void setScm( Scm scm )
367 {
368 this.scm = scm;
369 }
370
371 public void addDependency( Dependency dependency )
372 {
373 this.dependencies.add( dependency );
374 }
375
376 public void setBuild( Build build )
377 {
378 this.build = build;
379 }
380
381 public void setArtifactId( String artifactId )
382 {
383 this.artifactId = artifactId;
384 }
385
386 public void setName( String name )
387 {
388 this.name = name;
389 }
390
391 public void setVersion( String version )
392 {
393 this.version = version;
394 }
395
396 public Scm getScm()
397 {
398 return scm;
399 }
400
401 public String getModelVersion()
402 {
403 return modelVersion;
404 }
405
406 public String getGroupId()
407 {
408 return groupId;
409 }
410
411 public List<Dependency> getDependencies()
412 {
413 return dependencies;
414 }
415
416 public Build getBuild()
417 {
418 return build;
419 }
420
421 public String getArtifactId()
422 {
423 return artifactId;
424 }
425
426 public String getName()
427 {
428 return name;
429 }
430
431 public String getVersion()
432 {
433 return version;
434 }
435
436 public Dependency[] getDependenciesAsArray()
437 {
438 List<Dependency> list = getDependencies();
439 return list.toArray( new Dependency[list.size()] );
440 }
441
442 public Map<String, Dependency> getDependenciesAsMap()
443 {
444 Map<String, Dependency> ret = new HashMap<String, Dependency>();
445 for ( Object o : getDependencies() )
446 {
447 Dependency dep = (Dependency) o;
448 ret.put( dep.getArtifactId(), dep );
449 }
450 return ret;
451 }
452
453
454
455 public void addArtifact(Artifact a)
456 {
457 artifactMap.put( a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getClassifier(), a );
458 }
459
460 public Map<String,Artifact> getArtifactMap()
461 {
462 return artifactMap;
463 }
464 }
465
466
467 public static class Build
468 {
469
470 }
471
472 public static class Dependency
473 {
474 private String artifactId;
475
476 public String getArtifactId()
477 {
478 return artifactId;
479 }
480
481 public void setArtifactId( String id )
482 {
483 artifactId = id;
484 }
485 }
486
487 public static class Scm
488 {
489 private String connection;
490
491 public void setConnection( String connection )
492 {
493 this.connection = connection;
494 }
495
496 public String getConnection()
497 {
498 return connection;
499 }
500 }
501
502 public static class ValueHolder
503 {
504 private final Object value;
505
506 public ValueHolder( Object value )
507 {
508 this.value = value;
509 }
510
511 public Object getValue()
512 {
513 return value;
514 }
515 }
516
517 public void testRootPropertyRegression()
518 throws Exception
519 {
520 MavenProjectStub project = new MavenProjectStub( );
521 project.setDescription( "c:\\\\org\\apache\\test" );
522 Object evalued = ReflectionValueExtractor.evaluate( "description", project );
523 assertNotNull( evalued );
524 }
525 }