View Javadoc
1   package org.apache.maven.plugins.enforcer.utils;
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.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28  import org.apache.maven.enforcer.rule.api.EnforcerRule;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.Parent;
33  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
34  import org.apache.maven.plugins.enforcer.EnforcerTestUtils;
35  import org.apache.maven.plugins.enforcer.MockProject;
36  import org.codehaus.plexus.util.StringUtils;
37  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38  
39  /**
40   * The Class TestEnforcerRuleUtils.
41   *
42   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
43   */
44  public class TestEnforcerRuleUtils
45      extends AbstractMojoTestCase
46  {
47  
48      /**
49       * Test check if model matches.
50       */
51      public void testCheckIfModelMatches()
52      {
53  
54          EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper() );
55  
56          Model model = new Model();
57          model.setArtifactId( "" );
58          model.setGroupId( "" );
59          model.setVersion( "" );
60  
61          // should generate internal NPE on the parent, but
62          // will still
63          // compare the raw values
64          assertTrue( utils.checkIfModelMatches( "", "", "", model ) );
65          assertFalse( utils.checkIfModelMatches( "", "", "1.0", model ) );
66  
67          // now setup a parent
68          Parent parent = new Parent();
69          parent.setArtifactId( "foo" );
70          parent.setGroupId( "foo-group" );
71          parent.setVersion( "1.0" );
72          model.setParent( parent );
73  
74          // should NOT pickup the parent artifact
75          assertFalse( utils.checkIfModelMatches( "foo-group", "foo", "1.0", model ) );
76  
77          // check that the version and group are inherited
78          // from the parent.
79          assertTrue( utils.checkIfModelMatches( "foo-group", "", "1.0", model ) );
80  
81          // check handling of nulls
82          assertFalse( utils.checkIfModelMatches( "foo-group", null, "1.0", model ) );
83      }
84  
85      /**
86       * Test get models recursively bottom.
87       *
88       * @throws ArtifactResolutionException the artifact resolution exception
89       * @throws ArtifactNotFoundException the artifact not found exception
90       * @throws IOException Signals that an I/O exception has occurred.
91       * @throws XmlPullParserException the xml pull parser exception
92       */
93      public void testGetModelsRecursivelyBottom()
94          throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
95      {
96          String path = "target/test-classes/requirePluginVersions/getPomRecursively/b/c";
97  
98          StringUtils.replace( path, "/", File.separator );
99  
100         File pom = new File( getBasedir() + File.separator + path, "pom.xml" );
101 
102         EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper() );
103         List models = utils.getModelsRecursively( "group", "c", "1.0", pom );
104 
105         // there should be 3
106         assertEquals( 3, models.size() );
107 
108         // now make sure they are all there
109         Model m = new Model();
110         m.setGroupId( "group" );
111         m.setVersion( "1.0" );
112         m.setArtifactId( "c" );
113 
114         models.contains( m );
115 
116         m.setArtifactId( "b" );
117         models.contains( m );
118 
119         m.setArtifactId( "a" );
120         models.contains( m );
121     }
122 
123     /**
124      * Test get models recursively top.
125      *
126      * @throws ArtifactResolutionException the artifact resolution exception
127      * @throws ArtifactNotFoundException the artifact not found exception
128      * @throws IOException Signals that an I/O exception has occurred.
129      * @throws XmlPullParserException the xml pull parser exception
130      */
131     public void testGetModelsRecursivelyTop()
132         throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
133     {
134         String path = "target/test-classes/requirePluginVersions/getPomRecursively";
135 
136         StringUtils.replace( path, "/", File.separator );
137 
138         File pom = new File( getBasedir() + File.separator + path, "pom.xml" );
139 
140         EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper() );
141 
142         List models = utils.getModelsRecursively( "group", "a", "1.0", pom );
143 
144         // there should be 1
145         assertEquals( 1, models.size() );
146 
147         // now make sure they are all there
148         Model m = new Model();
149         m.setGroupId( "group" );
150         m.setVersion( "1.0" );
151         m.setArtifactId( "a" );
152 
153         models.contains( m );
154     }
155 
156     public void testGetModelsRecursivelyParentExpression()
157         throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
158     {
159         String path = "target/test-classes/requirePluginVersions/parentExpression/child";
160 
161         StringUtils.replace( path, "/", File.separator );
162 
163         File pom = new File( getBasedir() + File.separator + path, "pom.xml" );
164 
165         // bit backwards - the project here should really be the one read in the first stage of getModelsRecursively
166         MockProject parent = new MockProject();
167         parent.setGroupId( "org.apache.maven.plugins.enforcer.test" );
168         parent.setArtifactId( "parent" );
169         parent.setVersion( "1.0-SNAPSHOT" );
170 
171         MockProject project = new MockProject();
172         project.setParent( parent );
173 
174         EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper( project ) );
175 
176         List models =
177             utils.getModelsRecursively( "org.apache.maven.plugins.enforcer.test", "child", "1.0-SNAPSHOT", pom );
178 
179         // there should be 2
180         assertEquals( 2, models.size() );
181     }
182 
183     public void testGetModelsRecursivelyParentRelativePath()
184         throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
185     {
186         String path = "target/test-classes/requirePluginVersions/parentRelativePath";
187 
188         StringUtils.replace( path, "/", File.separator );
189 
190         File pom = new File( getBasedir() + File.separator + path, "pom.xml" );
191 
192         // bit backwards - the project here should really be the one read in the first stage of getModelsRecursively
193         MockProject parent = new MockProject();
194         parent.setGroupId( "org.apache.maven.plugins.enforcer.test" );
195         parent.setArtifactId( "parent" );
196         parent.setVersion( "1.0-SNAPSHOT" );
197 
198         MockProject project = new MockProject();
199         project.setParent( parent );
200 
201         EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper( project ) );
202 
203         List models =
204             utils.getModelsRecursively( "org.apache.maven.plugins.enforcer.test", "aggregate", "1.0-SNAPSHOT", pom );
205 
206         // there should be 2
207         assertEquals( 2, models.size() );
208     }
209 
210     public void testGetModelsRecursivelyParentRelativePathDirectory()
211         throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
212     {
213         String path = "target/test-classes/requirePluginVersions/parentRelativePathDirectory";
214 
215         StringUtils.replace( path, "/", File.separator );
216 
217         File pom = new File( getBasedir() + File.separator + path, "pom.xml" );
218 
219         // bit backwards - the project here should really be the one read in the first stage of getModelsRecursively
220         MockProject parent = new MockProject();
221         parent.setGroupId( "org.apache.maven.plugins.enforcer.test" );
222         parent.setArtifactId( "parent" );
223         parent.setVersion( "1.0-SNAPSHOT" );
224 
225         MockProject project = new MockProject();
226         project.setParent( parent );
227 
228         EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper( project ) );
229 
230         List models =
231             utils.getModelsRecursively( "org.apache.maven.plugins.enforcer.test", "aggregate", "1.0-SNAPSHOT", pom );
232 
233         // there should be 2
234         assertEquals( 2, models.size() );
235     }
236 
237     /**
238      * Simpler wrapper to execute and deal with the expected result.
239      *
240      * @param rule the rule
241      * @param helper the helper
242      * @param shouldFail the should fail
243      */
244     public static void execute( EnforcerRule rule, EnforcerRuleHelper helper, boolean shouldFail )
245     {
246         try
247         {
248             rule.execute( helper );
249             if ( shouldFail )
250             {
251                 fail( "Exception expected." );
252             }
253         }
254         catch ( EnforcerRuleException e )
255         {
256             if ( !shouldFail )
257             {
258                 fail( "No Exception expected:" + e.getLocalizedMessage() );
259             }
260             helper.getLog().debug( e.getMessage() );
261         }
262     }
263 }