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