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