View Javadoc

1   package org.apache.maven.model.converter;
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 junit.framework.Assert;
23  import org.apache.maven.model.Build;
24  import org.apache.maven.model.Dependency;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.model.Resource;
28  import org.codehaus.plexus.PlexusTestCase;
29  
30  import java.util.Arrays;
31  import java.util.Properties;
32  
33  public class PomV3ToV4TranslatorTest
34      extends PlexusTestCase
35  {
36  
37      private ModelConverter translator;
38  
39      private org.apache.maven.model.v3_0_0.Dependency v3Dep;
40  
41      private org.apache.maven.model.v3_0_0.Model v3Model;
42  
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47          translator = (ModelConverter) lookup( ModelConverter.ROLE );
48  
49          v3Dep = new org.apache.maven.model.v3_0_0.Dependency();
50          v3Dep.setGroupId( "testGroup" );
51          v3Dep.setArtifactId( "testArtifact" );
52          v3Dep.setVersion( "1.0" );
53  
54          v3Model = new org.apache.maven.model.v3_0_0.Model();
55          v3Model.setBuild( new org.apache.maven.model.v3_0_0.Build() );
56      }
57  
58      public void testShouldConvertPropertiesSection()
59          throws Exception
60      {
61          Properties properties = new Properties();
62          properties.setProperty( "Name", "Value" );
63  
64          v3Model.setProperties( properties );
65  
66          Model result = translator.translate( v3Model );
67  
68          assertNotNull( result.getProperties() );
69          assertEquals( "check properties if converted properly", properties, result.getProperties() );
70      }
71  
72      public void testConvertedEmptyResourceDirectory()
73          throws PomTranslationException
74      {
75          org.apache.maven.model.v3_0_0.Resource v3Resource = new org.apache.maven.model.v3_0_0.Resource();
76          v3Resource.setIncludes( Arrays.asList( new String[]{"**/*.properties"} ) );
77          v3Model.getBuild().addResource( v3Resource );
78  
79          Model result = translator.translate( v3Model );
80          Resource resource = (Resource) result.getBuild().getResources().get( 0 );
81          Assert.assertEquals( "check directory of v3Resource", ".", resource.getDirectory() );
82      }
83  
84      public void testShouldConvertScopePropertyToDependencyScope()
85          throws Exception
86      {
87          v3Dep.addProperty( "scope", "test" );
88  
89          v3Model.addDependency( v3Dep );
90  
91          Model result = translator.translate( v3Model );
92          Assert.assertEquals( "test", ( (Dependency) result.getDependencies().get( 0 ) ).getScope() );
93  
94      }
95  
96      public void testShouldConvertOptionalPropertyToDependencyOptional()
97          throws Exception
98      {
99          v3Dep.addProperty( "optional", "true" );
100 
101         v3Model.addDependency( v3Dep );
102 
103         Model result = translator.translate( v3Model );
104         Assert.assertTrue( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() );
105 
106         v3Dep.addProperty( "optional", "TRUE" );
107 
108         v3Model.addDependency( v3Dep );
109 
110         result = translator.translate( v3Model );
111         Assert.assertTrue( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() );
112     }
113 
114     public void testDontBreakWithMalformedOptionalProperty()
115         throws Exception
116     {
117         v3Dep.addProperty( "optional", "xxx" );
118 
119         v3Model.addDependency( v3Dep );
120 
121         Model result = translator.translate( v3Model );
122         Assert.assertFalse( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() );
123 
124         v3Dep.addProperty( "optional", "" );
125 
126         v3Model.addDependency( v3Dep );
127 
128         result = translator.translate( v3Model );
129         Assert.assertFalse( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() );
130     }
131 
132     public void testShouldConvertDependencyWithTypePluginToBuildPluginEntry()
133         throws Exception
134     {
135         v3Dep.setType( "plugin" );
136 
137         v3Model.addDependency( v3Dep );
138 
139         Model result = translator.translate( v3Model );
140 
141         Build build = result.getBuild();
142 
143         Plugin plugin = (Plugin) build.getPlugins().get( 0 );
144 
145         Assert.assertEquals( "testGroup", plugin.getGroupId() );
146         Assert.assertEquals( "testArtifact", plugin.getArtifactId() );
147         Assert.assertEquals( "1.0", plugin.getVersion() );
148 
149         Assert.assertEquals( "check one dependency", 1, result.getDependencies().size() );
150         Dependency dep = (Dependency) result.getDependencies().get( 0 );
151         Assert.assertEquals( "junit", dep.getGroupId() );
152         Assert.assertEquals( "junit", dep.getArtifactId() );
153         Assert.assertEquals( "3.8.2", dep.getVersion() );
154         Assert.assertEquals( "test", dep.getScope() );
155     }
156 
157     public void testShouldConvertDependencyWithTypePluginAndGroupMavenToBuildPluginEntryWithOAMPluginsGroup()
158         throws Exception
159     {
160         v3Dep.setGroupId( "maven" );
161         v3Dep.setType( "plugin" );
162 
163         v3Model.addDependency( v3Dep );
164 
165         Model result = translator.translate( v3Model );
166 
167         Build build = result.getBuild();
168 
169         Plugin plugin = (Plugin) build.getPlugins().get( 0 );
170 
171         Assert.assertEquals( "org.apache.maven.plugins", plugin.getGroupId() );
172         Assert.assertEquals( "testArtifact", plugin.getArtifactId() );
173         Assert.assertEquals( "1.0", plugin.getVersion() );
174 
175         Assert.assertEquals( "check one dependencies", 1, result.getDependencies().size() );
176         Dependency dep = (Dependency) result.getDependencies().get( 0 );
177         Assert.assertEquals( "junit", dep.getGroupId() );
178         Assert.assertEquals( "junit", dep.getArtifactId() );
179         Assert.assertEquals( "3.8.2", dep.getVersion() );
180         Assert.assertEquals( "test", dep.getScope() );
181     }
182 
183 }