Author: brianf Date: Wed Aug 29 20:27:51 2007 New Revision: 571024 URL: http://svn.apache.org/viewvc?rev=571024&view=rev Log: MENFORCER-15: added tests and docs
Added: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requirePluginVersions.apt maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequirePluginVersions.java maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/c/ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/c/pom.xml maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/pom.xml maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/pom.xml Modified: maven/plugins/trunk/maven-enforcer-plugin/ (props changed) maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequirePluginVersions.java maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/bannedDependencies.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/noSnapshots.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireJavaVersion.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireMavenVersion.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireOS.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/usage.apt maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/EnforcerTestUtils.java maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java Propchange: maven/plugins/trunk/maven-enforcer-plugin/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Wed Aug 29 20:27:51 2007 @@ -1,3 +1,5 @@ target .classpath .project +.settings +cobertura.ser Modified: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequirePluginVersions.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequirePluginVersions.java?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequirePluginVersions.java (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequirePluginVersions.java Wed Aug 29 20:27:51 2007 @@ -122,7 +122,7 @@ List remoteRepositories; Log log; - + MavenSession session; public void execute ( EnforcerRuleHelper helper ) @@ -144,8 +144,7 @@ resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class ); local = (ArtifactRepository) helper.evaluate( "${localRepository}" ); remoteRepositories = project.getRemoteArtifactRepositories(); - - + // I couldn't find a direct way to get at the // lifecycles list. lifecycles = (List) ReflectionUtils.getValueIncludingSuperclasses( "lifecycles", life ); @@ -158,7 +157,7 @@ log.debug( "All Plugins: " + allPlugins ); List plugins = getAllPluginEntries( project ); - + // now look for the versions that aren't valid // and add to a list. ArrayList failures = new ArrayList(); @@ -166,7 +165,7 @@ while ( iter.hasNext() ) { Plugin plugin = (Plugin) iter.next(); - if ( !hasVersionSpecified( plugin, plugins )) + if ( !hasVersionSpecified( plugin, plugins ) ) { failures.add( plugin ); } @@ -184,7 +183,7 @@ Plugin plugin = (Plugin) iter.next(); newMsg.append( plugin.getGroupId() + ":" + plugin.getArtifactId() + "\n" ); } - if (StringUtils.isNotEmpty( message )) + if ( StringUtils.isNotEmpty( message ) ) { newMsg.append( message ); } @@ -218,19 +217,19 @@ } catch ( ArtifactResolutionException e ) { - throw new EnforcerRuleException( e.getLocalizedMessage()); + throw new EnforcerRuleException( e.getLocalizedMessage() ); } catch ( ArtifactNotFoundException e ) { - throw new EnforcerRuleException( e.getLocalizedMessage()); + throw new EnforcerRuleException( e.getLocalizedMessage() ); } catch ( IOException e ) { - throw new EnforcerRuleException( e.getLocalizedMessage()); + throw new EnforcerRuleException( e.getLocalizedMessage() ); } catch ( XmlPullParserException e ) { - throw new EnforcerRuleException( e.getLocalizedMessage()); + throw new EnforcerRuleException( e.getLocalizedMessage() ); } } @@ -240,8 +239,9 @@ * plugin. Can optionally ban "RELEASE" or "LATEST" even * if specified. */ - public boolean hasVersionSpecified ( Plugin source, List plugins ) + protected boolean hasVersionSpecified ( Plugin source, List plugins ) { + boolean status = false; Iterator iter = plugins.iterator(); while ( iter.hasNext() ) { @@ -265,11 +265,18 @@ } // the version was specified and not // banned. It's ok. - return true; + + status = true; + + if (!banRelease && !banLatest) + { + //no need to keep looking + break; + } } } } - return false; + return status; } /* @@ -556,7 +563,9 @@ } /** - * This method gets the model for the defined artifact. Looks first in the filesystem, then tries to get it from the repo. + * This method gets the model for the defined artifact. + * Looks first in the filesystem, then tries to get it + * from the repo. * * @param groupId * @param artifactId @@ -564,45 +573,50 @@ * @return * @throws ArtifactResolutionException * @throws ArtifactNotFoundException - * @throws XmlPullParserException - * @throws IOException + * @throws XmlPullParserException + * @throws IOException */ private Model getPomModel ( String groupId, String artifactId, String version, File pom ) throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException { - Model model= null; - - //do we want to look in the reactor like the project builder? Would require @aggregator goal - //which causes problems in maven core right now because we also need dependency resolution in other - //rules. (MNG-2277) - - //look in the location specified by pom first. + Model model = null; + + // do we want to look in the reactor like the + // project builder? Would require @aggregator goal + // which causes problems in maven core right now + // because we also need dependency resolution in + // other + // rules. (MNG-2277) + + // look in the location specified by pom first. boolean found = false; try { - model = readModel(pom); - - //i found a model, lets make sure it's the one I want + model = readModel( pom ); + + // i found a model, lets make sure it's the one + // I want found = checkIfModelMatches( groupId, artifactId, version, model ); } catch ( IOException e ) { - //nothing here, but lets look in the repo before giving up. + // nothing here, but lets look in the repo + // before giving up. } catch ( XmlPullParserException e ) { - //nothing here, but lets look in the repo before giving up. + // nothing here, but lets look in the repo + // before giving up. } - - //i didn't find it in the local file system, go look in the repo - if (!found) + + // i didn't find it in the local file system, go + // look in the repo + if ( !found ) { Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" ); resolver.resolve( pomArtifact, remoteRepositories, local ); model = readModel( pomArtifact.getFile() ); } - - return model; } @@ -620,7 +634,7 @@ * @throws IOException * @throws XmlPullParserException */ - private List getModelsRecursively ( String groupId, String artifactId, String version, File pom ) + protected List getModelsRecursively ( String groupId, String artifactId, String version, File pom ) throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException { List models = null; @@ -628,18 +642,18 @@ Parent parent = model.getParent(); - //recurse into the parent + // recurse into the parent if ( parent != null ) { - //get the relative path + // get the relative path String relativePath = parent.getRelativePath(); - if (StringUtils.isEmpty( relativePath )) + if ( StringUtils.isEmpty( relativePath ) ) { relativePath = "../pom.xml"; } - //calculate the recursive path - File parentPom = new File(pom.getParent(),relativePath); - + // calculate the recursive path + File parentPom = new File( pom.getParent(), relativePath ); + models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom ); } else @@ -664,12 +678,13 @@ * @throws IOException * @throws XmlPullParserException */ - private List getAllPluginEntries ( MavenProject project ) + protected List getAllPluginEntries ( MavenProject project ) throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException { List plugins = new ArrayList(); // get all the pom models - List models = getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(),new File(project.getBasedir(),"pom.xml")); + List models = getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(), + new File( project.getBasedir(), "pom.xml" ) ); // now find all the plugin entries, either in // build.plugins or build.pluginManagement.plugins @@ -698,32 +713,82 @@ return plugins; } - - private boolean checkIfModelMatches(String groupId, String artifactId, String version, Model model) + + protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model ) { - //try these first. + // try these first. String modelGroup = model.getGroupId(); String modelVersion = model.getVersion(); - + try { - if (StringUtils.isEmpty( modelGroup )) - { - modelGroup = model.getParent().getGroupId(); - } - - if (StringUtils.isEmpty( modelVersion )) - { - modelVersion = model.getParent().getVersion(); - } + if ( StringUtils.isEmpty( modelGroup ) ) + { + modelGroup = model.getParent().getGroupId(); + } + + if ( StringUtils.isEmpty( modelVersion ) ) + { + modelVersion = model.getParent().getVersion(); + } } - catch (NullPointerException e) + catch ( NullPointerException e ) { - //this is probably bad. I don't have a valid group or version and I can't find a parent???? - //lets see if it's what we're looking for anyway. + // this is probably bad. I don't have a valid + // group or version and I can't find a + // parent???? + // lets see if it's what we're looking for + // anyway. } - return (groupId.equals( modelGroup ) && - artifactId.equals( model.getArtifactId() ) && - version.equals( modelVersion )); + return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils + .equals( artifactId, model.getArtifactId() ) ); + } + + /** + * @return the banLatest + */ + protected boolean isBanLatest () + { + return this.banLatest; + } + + /** + * @param theBanLatest the banLatest to set + */ + protected void setBanLatest ( boolean theBanLatest ) + { + this.banLatest = theBanLatest; + } + + /** + * @return the banRelease + */ + protected boolean isBanRelease () + { + return this.banRelease; + } + + /** + * @param theBanRelease the banRelease to set + */ + protected void setBanRelease ( boolean theBanRelease ) + { + this.banRelease = theBanRelease; + } + + /** + * @return the message + */ + protected String getMessage () + { + return this.message; + } + + /** + * @param theMessage the message to set + */ + protected void setMessage ( String theMessage ) + { + this.message = theMessage; } } Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/bannedDependencies.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/bannedDependencies.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/bannedDependencies.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/bannedDependencies.apt Wed Aug 29 20:27:51 2007 @@ -63,7 +63,7 @@ <execution> <id>enforce-banned-dependencies</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt Wed Aug 29 20:27:51 2007 @@ -51,7 +51,7 @@ <execution> <id>enforce-beanshell</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt Wed Aug 29 20:27:51 2007 @@ -40,6 +40,8 @@ * {{{requireOS.html}requireOS}} - enforces the OS / CPU Archictecture. + * {{{requirePluginVersions.html}requirePluginVersions}} - enforces that all plugins have a specified version. + * {{{requireProperty.html}requireProperty}} - enforces the existence and values of properties. [] Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/noSnapshots.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/noSnapshots.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/noSnapshots.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/noSnapshots.apt Wed Aug 29 20:27:51 2007 @@ -51,7 +51,7 @@ <execution> <id>enforce-no-snapshots</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireJavaVersion.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireJavaVersion.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireJavaVersion.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireJavaVersion.apt Wed Aug 29 20:27:51 2007 @@ -73,7 +73,7 @@ <execution> <id>enforce-java</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireMavenVersion.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireMavenVersion.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireMavenVersion.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireMavenVersion.apt Wed Aug 29 20:27:51 2007 @@ -49,7 +49,7 @@ <execution> <id>enforce-java</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireOS.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireOS.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireOS.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireOS.apt Wed Aug 29 20:27:51 2007 @@ -118,7 +118,7 @@ <execution> <id>enforce-os</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Added: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requirePluginVersions.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requirePluginVersions.apt?rev=571024&view=auto ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requirePluginVersions.apt (added) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requirePluginVersions.apt Wed Aug 29 20:27:51 2007 @@ -0,0 +1,72 @@ +~~ Licensed to the Apache Software Foundation (ASF) under one +~~ or more contributor license agreements. See the NOTICE file +~~ distributed with this work for additional information +~~ regarding copyright ownership. The ASF licenses this file +~~ to you under the Apache License, Version 2.0 (the +~~ "License"); you may not use this file except in compliance +~~ with the License. You may obtain a copy of the License at +~~ +~~ http://www.apache.org/licenses/LICENSE-2.0 +~~ +~~ Unless required by applicable law or agreed to in writing, +~~ software distributed under the License is distributed on an +~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +~~ KIND, either express or implied. See the License for the +~~ specific language governing permissions and limitations +~~ under the License. + + ------ + Require Plugin Versions + ------ + Brian Fox + ------ + August 2007 + ------ + +Require Plugin Versions + + This rule enforces that all plugins have a version defined, either in the plugin or pluginManagement section of the pom or a parent pom. + + The following parameters are supported by this rule: + + * message - an optional message to the user if the rule fails. + + * banLatest - disallow any use of "LATEST" as a version for any plugin. Default = true + + * banRelease - disallow any use of "RELEASE" as a version for any plugin. Default = true + + [] + + Sample Plugin Configuration: + ++---+ +<project> + [...] + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-enforcer-plugin</artifactId> + <executions> + <execution> + <id>enforce-plugin-versions</id> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <requirePluginVersions> + <message>Best Practice is to always define plugin versions!</message> + <banLatest>true</banLatest> + <banRelease>true</banRelease> + </requirePluginVersions> + </rules> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + [...] +</project> ++---+ \ No newline at end of file Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt Wed Aug 29 20:27:51 2007 @@ -54,7 +54,7 @@ <execution> <id>enforce-property</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/usage.apt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/usage.apt?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/usage.apt (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/usage.apt Wed Aug 29 20:27:51 2007 @@ -73,7 +73,7 @@ <execution> <id>enforce-versions</id> <goals> - <goal>enforce-once</goal> + <goal>enforce</goal> </goals> <configuration> <rules> Modified: maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/EnforcerTestUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/EnforcerTestUtils.java?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/EnforcerTestUtils.java (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/EnforcerTestUtils.java Wed Aug 29 20:27:51 2007 @@ -23,6 +23,7 @@ import java.util.Properties; import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Plugin; import org.apache.maven.plugin.logging.SystemStreamLog; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper; @@ -53,5 +54,14 @@ ExpressionEvaluator eval = new EnforcerExpressionEvaluator( session, new MockPathTranslator(), project ); return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog() ); + } + + public static Plugin newPlugin(String groupId, String artifactId, String version) + { + Plugin plugin = new Plugin(); + plugin.setArtifactId( artifactId ); + plugin.setGroupId( groupId ); + plugin.setVersion( version ); + return plugin; } } Modified: maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java?rev=571024&r1=571023&r2=571024&view=diff ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java (original) +++ maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java Wed Aug 29 20:27:51 2007 @@ -155,6 +155,8 @@ private Set artifacts; private Properties properties = new Properties(); + + private File baseDir = null; public MockProject() { @@ -240,7 +242,16 @@ public File getBasedir() { - return new File( PlexusTestCase.getBasedir() ); + if (baseDir == null) + { + baseDir = new File( PlexusTestCase.getBasedir() ); + } + return baseDir; + } + + public void setBaseDir(File base) + { + baseDir = base; } public void setDependencies( List list ) Added: maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequirePluginVersions.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequirePluginVersions.java?rev=571024&view=auto ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequirePluginVersions.java (added) +++ maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequirePluginVersions.java Wed Aug 29 20:27:51 2007 @@ -0,0 +1,212 @@ +package org.apache.maven.plugin.enforcer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.model.Model; +import org.apache.maven.model.Parent; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +/** + * + * @author <a href="mailto:[EMAIL PROTECTED]">Brian Fox</a> + * + */ +public class TestRequirePluginVersions + extends AbstractMojoTestCase +{ + public void testCheckIfModelMatches () + { + + RequirePluginVersions rule = new RequirePluginVersions(); + + Model model = new Model(); + model.setArtifactId( "" ); + model.setGroupId( "" ); + model.setVersion( "" ); + + // should generate internal NPE on the parent, but + // will still + // compare the raw values + assertTrue( rule.checkIfModelMatches( "", "", "", model ) ); + assertFalse( rule.checkIfModelMatches( "", "", "1.0", model ) ); + + // now setup a parent + Parent parent = new Parent(); + parent.setArtifactId( "foo" ); + parent.setGroupId( "foo-group" ); + parent.setVersion( "1.0" ); + model.setParent( parent ); + + // should NOT pickup the parent artifact + assertFalse( rule.checkIfModelMatches( "foo-group", "foo", "1.0", model ) ); + + // check that the version and group are inherited + // from the parent. + assertTrue( rule.checkIfModelMatches( "foo-group", "", "1.0", model ) ); + + // check handling of nulls + assertFalse( rule.checkIfModelMatches( "foo-group", null, "1.0", model ) ); + } + + public void testHasVersionSpecified () + { + Plugin source = new Plugin(); + source.setArtifactId( "foo" ); + source.setGroupId( "group" ); + + // setup the plugins. I'm setting up the foo group + // with a few bogus entries and then a real one. + // this is to test that the list is exhaustively + // searched for versions before giving up. + // banLatest/Release will fail if it is found + // anywhere in the list + List plugins = new ArrayList(); + plugins.add( EnforcerTestUtils.newPlugin( "group", "a-artifact", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", null ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", "" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "b-artifact", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "c-artifact", "LATEST" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "c-artifact", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "d-artifact", "RELEASE" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "d-artifact", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "RELEASE" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "1.0" ) ); + plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "LATEST" ) ); + + RequirePluginVersions rule = new RequirePluginVersions(); + rule.setBanLatest( false ); + rule.setBanRelease( false ); + + assertTrue( rule.hasVersionSpecified( source, plugins ) ); + + // check that LATEST is allowed + source.setArtifactId( "c-artifact" ); + assertTrue( rule.hasVersionSpecified( source, plugins ) ); + + // check that LATEST is banned + rule.setBanLatest( true ); + assertFalse( rule.hasVersionSpecified( source, plugins ) ); + + // check that LATEST is exhausively checked + source.setArtifactId( "f-artifact" ); + assertFalse( rule.hasVersionSpecified( source, plugins ) ); + + // check that RELEASE is allowed + source.setArtifactId( "d-artifact" ); + assertTrue( rule.hasVersionSpecified( source, plugins ) ); + + // check that RELEASE is banned + rule.setBanRelease( true ); + assertFalse( rule.hasVersionSpecified( source, plugins ) ); + + // check that RELEASE is exhausively checked + source.setArtifactId( "e-artifact" ); + assertFalse( rule.hasVersionSpecified( source, plugins ) ); + } + + public void testGetModelsRecursivelyBottom () + throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException + { + RequirePluginVersions rule = new RequirePluginVersions(); + String path = "target/test-classes/requirePluginVersions/getPomRecursively/b/c"; + + StringUtils.replace( path, "/", File.separator ); + + File pom = new File( getBasedir() + File.separator + path, "pom.xml" ); + + List models = rule.getModelsRecursively( "group", "c", "1.0", pom ); + + // there should be 3 + assertEquals( 3, models.size() ); + + // now make sure they are all there + Model m = new Model(); + m.setGroupId( "group" ); + m.setVersion( "1.0" ); + m.setArtifactId( "c" ); + + models.contains( m ); + + m.setArtifactId( "b" ); + models.contains( m ); + + m.setArtifactId( "a" ); + models.contains( m ); + } + + public void testGetModelsRecursivelyTop () + throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException + { + RequirePluginVersions rule = new RequirePluginVersions(); + String path = "target/test-classes/requirePluginVersions/getPomRecursively"; + + StringUtils.replace( path, "/", File.separator ); + + File pom = new File( getBasedir() + File.separator + path, "pom.xml" ); + + List models = rule.getModelsRecursively( "group", "a", "1.0", pom ); + + // there should be 1 + assertEquals( 1, models.size() ); + + // now make sure they are all there + Model m = new Model(); + m.setGroupId( "group" ); + m.setVersion( "1.0" ); + m.setArtifactId( "a" ); + + models.contains( m ); + } + + public void testGetAllPlugins () + throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException + { + RequirePluginVersions rule = new RequirePluginVersions(); + String path = "target/test-classes/requirePluginVersions/getPomRecursively/b/c"; + + StringUtils.replace( path, "/", File.separator ); + + File projectDir = new File( getBasedir(), path ); + + MockProject project = new MockProject(); + project.setArtifactId( "c" ); + project.setGroupId( "group" ); + project.setVersion( "1.0" ); + project.setBaseDir( projectDir ); + + List plugins = rule.getAllPluginEntries( project ); + + // there should be 3 + assertEquals( 3, plugins.size() ); + } +} Added: maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/c/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/c/pom.xml?rev=571024&view=auto ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/c/pom.xml (added) +++ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/c/pom.xml Wed Aug 29 20:27:51 2007 @@ -0,0 +1,18 @@ +<project> + <parent> + <artifactId>b</artifactId> + <groupId>group</groupId> + <version>1.0</version> + </parent> + <artifactId>c</artifactId> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-deploy-plugin</artifactId> + </plugin> + </plugins> + </pluginManagement> + </build> +</project> \ No newline at end of file Added: maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/pom.xml?rev=571024&view=auto ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/pom.xml (added) +++ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/b/pom.xml Wed Aug 29 20:27:51 2007 @@ -0,0 +1,16 @@ +<project> + <parent> + <artifactId>a</artifactId> + <groupId>group</groupId> + <version>1.0</version> + </parent> + <artifactId>b</artifactId> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-clean-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file Added: maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/pom.xml?rev=571024&view=auto ============================================================================== --- maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/pom.xml (added) +++ maven/plugins/trunk/maven-enforcer-plugin/src/test/resources/requirePluginVersions/getPomRecursively/pom.xml Wed Aug 29 20:27:51 2007 @@ -0,0 +1,13 @@ +<project> + <artifactId>a</artifactId> + <groupId>group</groupId> + <version>1.0</version> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file