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