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