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