1   package org.apache.maven.project;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  import org.apache.maven.MavenUtils;
28  
29  /**
30   * Test the semantics of ids inside project.xml.
31   *
32   * @author Brett Porter <a href="mailto:brett@apache.org">brett@apache.org</a>
33   * @version $Id: LegacyIdTest.java 517014 2007-03-11 21:15:50Z ltheussl $
34   */
35  public class LegacyIdTest
36      extends TestCase
37  {
38      /**
39       * Test for legacyToStandardId
40       */
41      public void testLegacyToStandardId()
42      {
43          // test 'ant' -> 'ant:ant'
44          String legacyId = "ant";
45          String standardId = "ant:ant";
46          assertEquals( "Single id conversion failed", standardId, Project.legacyToStandardId( legacyId ) );
47  
48          // test 'ant:ant' unchanged
49          legacyId = "ant:ant";
50          assertEquals( "Standard id conversion failed", standardId, Project.legacyToStandardId( legacyId ) );
51  
52          // test 'ant+optional' -> ant:ant-optional
53          legacyId = "ant+optional";
54          standardId = "ant:ant-optional";
55          assertEquals( "Plus format id conversion failed", standardId, Project.legacyToStandardId( legacyId ) );
56      }
57  
58      /**
59       * Test for legacyToDependencyKey
60       */
61      public void testLegacyToDependencyId()
62      {
63          Project project = new Project();
64          addDependency( project, "ant", "ant", "jar" );
65          addDependency( project, "ant", "ant-optional", "jar" );
66          addDependency( project, "ant", "ant-windows", "dll" );
67  
68          // test 'ant' -> 'ant:ant:jar'
69          String legacyId = "ant";
70          String standardId = "ant:ant:jar";
71          assertEquals( "Single id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
72  
73          // test 'ant:ant' -> 'ant:ant:jar'
74          legacyId = "ant:ant";
75          assertEquals( "Standard id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
76  
77          // test 'ant+optional' -> ant:ant-optional:jar
78          legacyId = "ant+optional";
79          standardId = "ant:ant-optional:jar";
80          assertEquals( "Plus format id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
81  
82          // test 'ant:ant:jar' unchanged
83          legacyId = "ant:ant:jar";
84          standardId = "ant:ant:jar";
85          assertEquals( "Standard id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
86  
87          // test 'ant:ant:tld' unchanged
88          legacyId = "ant:ant:tld";
89          standardId = "ant:ant:tld";
90          assertEquals( "Standard id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
91  
92          // test 'ant:ant-windows' gets correct one
93          legacyId = "ant:ant-windows";
94          standardId = "ant:ant-windows:dll";
95          assertEquals( "Standard id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
96  
97          // test 'ant:ant-windows:dll' unchanged
98          legacyId = "ant:ant-windows:dll";
99          standardId = "ant:ant-windows:dll";
100         assertEquals( "Standard id conversion failed", standardId, project.legacyToDependencyKey( legacyId ) );
101     }
102 
103     private static void addDependency( Project project, String groupId, String artifactId, String type )
104     {
105         Dependency dep = new Dependency();
106         dep.setId( groupId + ":" + artifactId );
107         dep.setGroupId( groupId );
108         dep.setArtifactId( artifactId );
109         dep.setType( type );
110         dep.setVersion( "1.0-SNAPSHOT" );
111         project.addDependency( dep );
112     }
113 
114     public void testIdOnly()
115         throws Exception
116     {
117         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
118                                                      "src/test/test-data/pom/test-id-only.xml" ) );
119         assertEquals( "check id", "artifactId:artifactId", p.getId() );
120         assertEquals( "check artifact id", "artifactId", p.getArtifactId() );
121         assertEquals( "check group id", "artifactId", p.getGroupId() );
122         assertEquals( "check id", "artifactId:artifactId", p.getId() );
123     }
124 
125     public void testGroupIdAndArtifactId()
126         throws Exception
127     {
128         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
129                                                      "src/test/test-data/pom/test-group-id-and-artifact-id.xml" ) );
130         assertEquals( "check id", "groupId:artifactId", p.getId() );
131         assertEquals( "check artifact id", "artifactId", p.getArtifactId() );
132         assertEquals( "check group id", "groupId", p.getGroupId() );
133         assertEquals( "check id", "groupId:artifactId", p.getId() );
134     }
135 
136     public void testGroupIdAndIdAsArtifactId()
137         throws Exception
138     {
139         Project p = MavenUtils
140             .getProject( new File( System.getProperty( "basedir" ),
141                                    "src/test/test-data/pom/test-group-id-and-id-as-artifact-id.xml" ) );
142         assertEquals( "check id", "groupId:artifactId", p.getId() );
143         assertEquals( "check artifact id", "artifactId", p.getArtifactId() );
144         assertEquals( "check group id", "groupId", p.getGroupId() );
145         assertEquals( "check id", "groupId:artifactId", p.getId() );
146     }
147 
148     public void testIdAndGroupIdAndArtifactId()
149         throws Exception
150     {
151         Project p = MavenUtils
152             .getProject( new File( System.getProperty( "basedir" ),
153                                    "src/test/test-data/pom/test-id-and-groupId-and-artifactId.xml" ) );
154         assertEquals( "check id", "groupId:artifactId", p.getId() );
155         assertEquals( "check artifact id", "artifactId", p.getArtifactId() );
156         assertEquals( "check group id", "groupId", p.getGroupId() );
157         assertEquals( "check id", "groupId:artifactId", p.getId() );
158     }
159 
160     public void testExtendNone()
161         throws Exception
162     {
163         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
164                                                      "src/test/test-data/pom/extend/test-none.xml" ) );
165         assertEquals( "check group id", "base-groupId", p.getGroupId() );
166     }
167 
168     public void testExtendIdOnly()
169         throws Exception
170     {
171         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
172                                                      "src/test/test-data/pom/extend/test-id-only.xml" ) );
173         assertEquals( "check id", "base-groupId:extend-artifactId", p.getId() );
174         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
175         assertEquals( "check group id", "base-groupId", p.getGroupId() );
176         assertEquals( "check id", "base-groupId:extend-artifactId", p.getId() );
177     }
178 
179     public void testExtendOnlyArtifactIds()
180         throws Exception
181     {
182         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
183                                                      "src/test/test-data/pom/extend/test-artifact-id-only-extend.xml" ) );
184         assertEquals( "check id", "childArtifactId:childArtifactId", p.getId() );
185         assertEquals( "check artifact id", "childArtifactId", p.getArtifactId() );
186         assertEquals( "check group id", "childArtifactId", p.getGroupId() );
187         assertEquals( "check id", "childArtifactId:childArtifactId", p.getId() );
188     }
189 
190     public void testExtendArtifactIdOnly()
191         throws Exception
192     {
193         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
194                                                      "src/test/test-data/pom/extend/test-artifact-id-only.xml" ) );
195         assertEquals( "check id", "base-groupId:extend-artifactId", p.getId() );
196         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
197         assertEquals( "check group id", "base-groupId", p.getGroupId() );
198         assertEquals( "check id", "base-groupId:extend-artifactId", p.getId() );
199     }
200 
201     public void testExtendArtifactIdOnlyOverMatching()
202         throws Exception
203     {
204         Project p = MavenUtils
205             .getProject( new File( System.getProperty( "basedir" ),
206                                    "src/test/test-data/pom/extend/test-artifact-id-only-over-matching.xml" ) );
207         assertEquals( "check id", "base-id:extend-artifactId", p.getId() );
208         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
209         assertEquals( "check group id", "base-id", p.getGroupId() );
210         assertEquals( "check id", "base-id:extend-artifactId", p.getId() );
211     }
212 
213     public void testExtendArtifactIdOnlyOverDifferent()
214         throws Exception
215     {
216         Project p = MavenUtils
217             .getProject( new File( System.getProperty( "basedir" ),
218                                    "src/test/test-data/pom/extend/test-artifact-id-only-over-different.xml" ) );
219         assertEquals( "check id", "base-groupId:extend-artifactId", p.getId() );
220         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
221         assertEquals( "check group id", "base-groupId", p.getGroupId() );
222         assertEquals( "check id", "base-groupId:extend-artifactId", p.getId() );
223     }
224 
225     public void testExtendIdOverrideGroupId()
226         throws Exception
227     {
228         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
229                                                      "src/test/test-data/pom/extend/test-id-override-groupid.xml" ) );
230         assertEquals( "check id", "extend-artifactId:extend-artifactId", p.getId() );
231         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
232         assertEquals( "check group id", "extend-artifactId", p.getGroupId() );
233         assertEquals( "check id", "extend-artifactId:extend-artifactId", p.getId() );
234     }
235 
236     public void testExtendGroupIdAndArtifactId()
237         throws Exception
238     {
239         Project p = MavenUtils
240             .getProject( new File( System.getProperty( "basedir" ),
241                                    "src/test/test-data/pom/extend/test-group-id-and-artifact-id.xml" ) );
242         assertEquals( "check id", "extend-groupId:extend-artifactId", p.getId() );
243         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
244         assertEquals( "check group id", "extend-groupId", p.getGroupId() );
245         assertEquals( "check id", "extend-groupId:extend-artifactId", p.getId() );
246     }
247 
248     public void testExtendGroupIdAndIdAsArtifactId()
249         throws Exception
250     {
251         Project p = MavenUtils
252             .getProject( new File( System.getProperty( "basedir" ),
253                                    "src/test/test-data/pom/extend/test-group-id-and-id-as-artifact-id.xml" ) );
254         assertEquals( "check id", "extend-groupId:extend-artifactId", p.getId() );
255         assertEquals( "check artifact id", "extend-artifactId", p.getArtifactId() );
256         assertEquals( "check group id", "extend-groupId", p.getGroupId() );
257         assertEquals( "check id", "extend-groupId:extend-artifactId", p.getId() );
258     }
259 
260     public void testDependencies()
261         throws Exception
262     {
263         Project p = MavenUtils.getProject( new File( System.getProperty( "basedir" ),
264                                                      "src/test/test-data/pom/dependencies.xml" ) );
265         assertEquals( "check id", "ant:ant", p.getDependency( "ant:ant" ).getId() );
266         assertEquals( "check id with +", "ant:ant-optional", p.getDependency( "ant:ant-optional" ).getId() );
267         assertEquals( "check id with -", "ant-optional:ant-optional", p.getDependency( "ant-optional:ant-optional" )
268             .getId() );
269         assertEquals( "check id with id and artifactId", "groupId:artifactId", p.getDependency( "groupId:artifactId" )
270             .getId() );
271     }
272 
273     /**
274      * Generate the test suite.
275      *
276      * @return the test suite
277      */
278     public static Test suite()
279     {
280         return new TestSuite( LegacyIdTest.class );
281     }
282 }