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