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  import junit.framework.Assert;
28  import junit.framework.TestCase;
29  
30  /**
31   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
32   * @version $Id$
33   */
34  public class ReflectionValueExtractorTest
35      extends TestCase
36  {
37      private Project project;
38  
39      protected void setUp()
40          throws Exception
41      {
42          super.setUp();
43  
44          Dependency dependency1 = new Dependency();
45          dependency1.setArtifactId( "dep1" );
46          Dependency dependency2 = new Dependency();
47          dependency2.setArtifactId( "dep2" );
48  
49          project = new Project();
50          project.setModelVersion( "4.0.0" );
51          project.setGroupId( "org.apache.maven" );
52          project.setArtifactId( "maven-core" );
53          project.setName( "Maven" );
54          project.setVersion( "2.0-SNAPSHOT" );
55          project.setScm( new Scm() );
56          project.getScm().setConnection( "scm-connection" );
57          project.addDependency( dependency1 );
58          project.addDependency( dependency2 );
59          project.setBuild( new Build() );
60      }
61  
62      public void testValueExtraction()
63          throws Exception
64      {
65          // ----------------------------------------------------------------------
66          // Top level values
67          // ----------------------------------------------------------------------
68  
69          assertEquals( "4.0.0", ReflectionValueExtractor.evaluate( "project.modelVersion", project ) );
70  
71          assertEquals( "org.apache.maven", ReflectionValueExtractor.evaluate( "project.groupId", project ) );
72  
73          assertEquals( "maven-core", ReflectionValueExtractor.evaluate( "project.artifactId", project ) );
74  
75          assertEquals( "Maven", ReflectionValueExtractor.evaluate( "project.name", project ) );
76  
77          assertEquals( "2.0-SNAPSHOT", ReflectionValueExtractor.evaluate( "project.version", project ) );
78  
79          // ----------------------------------------------------------------------
80          // SCM
81          // ----------------------------------------------------------------------
82  
83          assertEquals( "scm-connection", ReflectionValueExtractor.evaluate( "project.scm.connection", project ) );
84  
85          // ----------------------------------------------------------------------
86          // Dependencies
87          // ----------------------------------------------------------------------
88  
89          List dependencies = (List) ReflectionValueExtractor.evaluate( "project.dependencies", project );
90  
91          Assert.assertNotNull( dependencies );
92  
93          Assert.assertEquals( 2, dependencies.size() );
94  
95          // ----------------------------------------------------------------------
96          // Dependencies - using index notation
97          // ----------------------------------------------------------------------
98  
99          // List
100         Dependency dependency = (Dependency) ReflectionValueExtractor.evaluate( "project.dependencies[0]", project );
101 
102         Assert.assertNotNull( dependency );
103 
104         Assert.assertTrue( "dep1".equals( dependency.getArtifactId() ) );
105 
106         String artifactId = (String) ReflectionValueExtractor.evaluate( "project.dependencies[1].artifactId", project );
107 
108         Assert.assertTrue( "dep2".equals( artifactId ) );
109 
110         // Array
111 
112         dependency = (Dependency) ReflectionValueExtractor.evaluate( "project.dependenciesAsArray[0]", project );
113 
114         Assert.assertNotNull( dependency );
115 
116         Assert.assertTrue( "dep1".equals( dependency.getArtifactId() ) );
117 
118         artifactId = (String) ReflectionValueExtractor.evaluate( "project.dependenciesAsArray[1].artifactId", project );
119 
120         Assert.assertTrue( "dep2".equals( artifactId ) );
121 
122         // Map
123 
124         dependency = (Dependency) ReflectionValueExtractor.evaluate( "project.dependenciesAsMap(dep1)", project );
125 
126         Assert.assertNotNull( dependency );
127 
128         Assert.assertTrue( "dep1".equals( dependency.getArtifactId() ) );
129 
130         artifactId = (String) ReflectionValueExtractor.evaluate( "project.dependenciesAsMap(dep2).artifactId", project );
131 
132         Assert.assertTrue( "dep2".equals( artifactId ) );
133 
134         // ----------------------------------------------------------------------
135         // Build
136         // ----------------------------------------------------------------------
137 
138         Build build = (Build) ReflectionValueExtractor.evaluate( "project.build", project );
139 
140         Assert.assertNotNull( build );
141     }
142 
143     public void testValueExtractorWithAInvalidExpression()
144         throws Exception
145     {
146         Assert.assertNull( ReflectionValueExtractor.evaluate( "project.foo", project ) );
147         Assert.assertNull( ReflectionValueExtractor.evaluate( "project.dependencies[10]", project ) );
148         Assert.assertNull( ReflectionValueExtractor.evaluate( "project.dependencies[0].foo", project ) );
149     }
150 
151     public static class Project
152     {
153         private String modelVersion;
154 
155         private String groupId;
156 
157         private Scm scm;
158 
159         private final List<Dependency> dependencies = new ArrayList<Dependency>();
160 
161         private Build build;
162 
163         private String artifactId;
164 
165         private String name;
166 
167         private String version;
168 
169         public void setModelVersion( String modelVersion )
170         {
171             this.modelVersion = modelVersion;
172         }
173 
174         public void setGroupId( String groupId )
175         {
176             this.groupId = groupId;
177         }
178 
179         public void setScm( Scm scm )
180         {
181             this.scm = scm;
182         }
183 
184         public void addDependency( Dependency dependency )
185         {
186             this.dependencies.add( dependency );
187         }
188 
189         public void setBuild( Build build )
190         {
191             this.build = build;
192         }
193 
194         public void setArtifactId( String artifactId )
195         {
196             this.artifactId = artifactId;
197         }
198 
199         public void setName( String name )
200         {
201             this.name = name;
202         }
203 
204         public void setVersion( String version )
205         {
206             this.version = version;
207         }
208 
209         public Scm getScm()
210         {
211             return scm;
212         }
213 
214         public String getModelVersion()
215         {
216             return modelVersion;
217         }
218 
219         public String getGroupId()
220         {
221             return groupId;
222         }
223 
224         public List<Dependency> getDependencies()
225         {
226             return dependencies;
227         }
228 
229         public Build getBuild()
230         {
231             return build;
232         }
233 
234         public String getArtifactId()
235         {
236             return artifactId;
237         }
238 
239         public String getName()
240         {
241             return name;
242         }
243 
244         public String getVersion()
245         {
246             return version;
247         }
248 
249         public Dependency[] getDependenciesAsArray()
250         {
251             List<Dependency> list = getDependencies();
252             return list.toArray( new Dependency[list.size()] );
253         }
254 
255         public Map<String, Dependency> getDependenciesAsMap()
256         {
257             Map<String, Dependency> ret = new HashMap<String, Dependency>();
258             for ( Object o : getDependencies() )
259             {
260                 Dependency dep = (Dependency) o;
261                 ret.put( dep.getArtifactId(), dep );
262             }
263             return ret;
264         }
265     }
266 
267     public static class Build
268     {
269 
270     }
271 
272     public static class Dependency
273     {
274         private String artifactId;
275 
276         public String getArtifactId()
277         {
278             return artifactId;
279         }
280 
281         public void setArtifactId( String id )
282         {
283             artifactId = id;
284         }
285     }
286 
287     public static class Scm
288     {
289         private String connection;
290 
291         public void setConnection( String connection )
292         {
293             this.connection = connection;
294         }
295 
296         public String getConnection()
297         {
298             return connection;
299         }
300     }
301 }