Author: brianf Date: Thu Sep 27 19:26:19 2007 New Revision: 580196 URL: http://svn.apache.org/viewvc?rev=580196&view=rev Log: new rule to enforce no repositories in the poms.
Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireNoRepositories.java maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtils.java maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/TestEnforcerRuleUtils.java maven/enforcer/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/EnforcerTestUtils.java Modified: maven/enforcer/trunk/ (props changed) maven/enforcer/trunk/enforcer-api/ (props changed) maven/enforcer/trunk/enforcer-rules/ (props changed) maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java maven/enforcer/trunk/maven-enforcer-plugin/ (props changed) maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/util/EnforcerUtils.java Propchange: maven/enforcer/trunk/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Thu Sep 27 19:26:19 2007 @@ -0,0 +1,2 @@ +target +archetype.properties Propchange: maven/enforcer/trunk/enforcer-api/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Thu Sep 27 19:26:19 2007 @@ -7,3 +7,4 @@ *.iws *.iml .settings +archetype.properties Propchange: maven/enforcer/trunk/enforcer-rules/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Thu Sep 27 19:26:19 2007 @@ -0,0 +1,5 @@ +target +.classpath +.project +.settings +archetype.properties Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireNoRepositories.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireNoRepositories.java?rev=580196&view=auto ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireNoRepositories.java (added) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireNoRepositories.java Thu Sep 27 19:26:19 2007 @@ -0,0 +1,113 @@ +package org.apache.maven.plugins.enforcer; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.enforcer.rule.api.EnforcerRule; +import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +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 RequireNoRepositories + implements EnforcerRule +{ + /** + * The message to be printed in case the condition + * returns <b>true</b> + * + * @required + * @parameter + */ + public String message; + + public void execute ( EnforcerRuleHelper helper ) + throws EnforcerRuleException + { + EnforcerRuleUtils utils = new EnforcerRuleUtils( helper ); + + MavenProject project; + try + { + project = (MavenProject) helper.evaluate( "${project}" ); + + List models = utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project + .getVersion(), new File( project.getBasedir(), "pom.xml" ) ); + + List badModels = checkModels( models ); + + // if anything was found, log it then append the + // optional message. + if ( !badModels.isEmpty() ) + { + StringBuffer newMsg = new StringBuffer(); + newMsg.append( "Some poms have repositories defined:\n" ); + Iterator iter = badModels.iterator(); + while ( iter.hasNext() ) + { + Model model = (Model) iter.next(); + newMsg.append( model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()+ "\n" ); + } + if ( StringUtils.isNotEmpty( message ) ) + { + newMsg.append( message ); + } + + throw new EnforcerRuleException( newMsg.toString() ); + } + + } + catch ( ExpressionEvaluationException e ) + { + throw new EnforcerRuleException( e.getLocalizedMessage() ); + } + catch ( ArtifactResolutionException e ) + { + throw new EnforcerRuleException( e.getLocalizedMessage() ); + } + catch ( ArtifactNotFoundException e ) + { + throw new EnforcerRuleException( e.getLocalizedMessage() ); + } + catch ( IOException e ) + { + throw new EnforcerRuleException( e.getLocalizedMessage() ); + } + catch ( XmlPullParserException e ) + { + throw new EnforcerRuleException( e.getLocalizedMessage() ); + } + } + + private List checkModels ( List models ) + { + List badModels = new ArrayList(); + + Iterator iter = models.iterator(); + while ( iter.hasNext() ) + { + Model model = (Model) iter.next(); + List repos = model.getRepositories(); + if (repos != null && !repos.isEmpty()) + { + badModels.add( model ); + } + } + return badModels; + } +} Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java?rev=580196&r1=580195&r2=580196&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java Thu Sep 27 19:26:19 2007 @@ -20,9 +20,7 @@ */ import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -34,7 +32,6 @@ import java.util.Map.Entry; import org.apache.maven.BuildFailureException; -import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; @@ -50,9 +47,7 @@ import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.lifecycle.mapping.LifecycleMapping; import org.apache.maven.model.Model; -import org.apache.maven.model.Parent; import org.apache.maven.model.Plugin; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.InvalidPluginException; import org.apache.maven.plugin.PluginManager; import org.apache.maven.plugin.PluginManagerException; @@ -61,6 +56,7 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.version.PluginVersionNotFoundException; import org.apache.maven.plugin.version.PluginVersionResolutionException; +import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; @@ -125,6 +121,8 @@ MavenSession session; + EnforcerRuleUtils utils; + public void execute ( EnforcerRuleHelper helper ) throws EnforcerRuleException { @@ -145,6 +143,8 @@ local = (ArtifactRepository) helper.evaluate( "${localRepository}" ); remoteRepositories = project.getRemoteArtifactRepositories(); + utils = new EnforcerRuleUtils( helper ); + // I couldn't find a direct way to get at the // lifecycles list. lifecycles = (List) ReflectionUtils.getValueIncludingSuperclasses( "lifecycles", life ); @@ -536,137 +536,7 @@ return pluginDescriptor; } - /** - * Gets the pom model for this file. - * - * @param pom - * @return - * @throws IOException - * @throws XmlPullParserException - */ - private Model readModel ( File pom ) - throws IOException, XmlPullParserException - { - Reader reader = new FileReader( pom ); - MavenXpp3Reader xpp3 = new MavenXpp3Reader(); - Model model = null; - try - { - model = xpp3.read( reader ); - } - finally - { - reader.close(); - reader = null; - } - return model; - } - - /** - * 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 - * @param version - * @return - * @throws ArtifactResolutionException - * @throws ArtifactNotFoundException - * @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. - boolean found = false; - try - { - 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. - } - catch ( XmlPullParserException e ) - { - // 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 ) - { - Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" ); - resolver.resolve( pomArtifact, remoteRepositories, local ); - model = readModel( pomArtifact.getFile() ); - } - - return model; - } - - /** - * This method loops through all the parents, getting - * each pom model and then its parent. - * - * @param groupId - * @param artifactId - * @param version - * @return - * @throws ArtifactResolutionException - * @throws ArtifactNotFoundException - * @throws IOException - * @throws XmlPullParserException - */ - protected List getModelsRecursively ( String groupId, String artifactId, String version, File pom ) - throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException - { - List models = null; - Model model = getPomModel( groupId, artifactId, version, pom ); - - Parent parent = model.getParent(); - - // recurse into the parent - if ( parent != null ) - { - // get the relative path - String relativePath = parent.getRelativePath(); - if ( StringUtils.isEmpty( relativePath ) ) - { - relativePath = "../pom.xml"; - } - // calculate the recursive path - File parentPom = new File( pom.getParent(), relativePath ); - - models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom ); - } - else - { - // only create it here since I'm not at the top - models = new ArrayList(); - } - models.add( model ); - - return models; - } - - /** + /** * Gets all plugin entries in build.plugins or * build.pluginManagement.plugins in this project and * all parents @@ -683,7 +553,7 @@ { List plugins = new ArrayList(); // get all the pom models - List models = getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(), + List models = utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(), new File( project.getBasedir(), "pom.xml" ) ); // now find all the plugin entries, either in @@ -714,35 +584,6 @@ return plugins; } - protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model ) - { - // 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(); - } - } - 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. - } - return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils - .equals( artifactId, model.getArtifactId() ) ); - } /** * @return the banLatest @@ -790,5 +631,21 @@ protected void setMessage ( String theMessage ) { this.message = theMessage; + } + + /** + * @return the utils + */ + protected EnforcerRuleUtils getUtils () + { + return this.utils; + } + + /** + * @param theUtils the utils to set + */ + protected void setUtils ( EnforcerRuleUtils theUtils ) + { + this.utils = theUtils; } } Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtils.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtils.java?rev=580196&view=auto ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtils.java (added) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtils.java Thu Sep 27 19:26:19 2007 @@ -0,0 +1,251 @@ +package org.apache.maven.plugins.enforcer.utils; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.model.Model; +import org.apache.maven.model.Parent; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +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 EnforcerRuleUtils +{ + ArtifactFactory factory; + + ArtifactResolver resolver; + + ArtifactRepository local; + + List remoteRepositories; + + Log log; + + MavenProject project; + + public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal, + List theRemoteRepositories, MavenProject project, Log theLog ) + { + super(); + this.factory = theFactory; + this.resolver = theResolver; + this.local = theLocal; + this.remoteRepositories = theRemoteRepositories; + this.log = theLog; + this.project = project; + } + + public EnforcerRuleUtils( EnforcerRuleHelper helper ) + { + // get the various expressions out of the + // helper. + + try + { + factory = (ArtifactFactory) helper.getComponent( ArtifactFactory.class ); + resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class ); + local = (ArtifactRepository) helper.evaluate( "${localRepository}" ); + project = (MavenProject) helper.evaluate( "${project}" ); + remoteRepositories = project.getRemoteArtifactRepositories(); + } + catch ( ComponentLookupException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch ( ExpressionEvaluationException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * Gets the pom model for this file. + * + * @param pom + * @return + * @throws IOException + * @throws XmlPullParserException + */ + private Model readModel ( File pom ) + throws IOException, XmlPullParserException + { + Reader reader = new FileReader( pom ); + MavenXpp3Reader xpp3 = new MavenXpp3Reader(); + Model model = null; + try + { + model = xpp3.read( reader ); + } + finally + { + reader.close(); + reader = null; + } + return model; + } + + /** + * This method gets the model for the defined artifact. + * Looks first in the filesystem, then tries to get it + * from the repo. + * + * @param factory + * @param groupId + * @param artifactId + * @param version + * @return + * @throws ArtifactResolutionException + * @throws ArtifactNotFoundException + * @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. + boolean found = false; + try + { + 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. + } + catch ( XmlPullParserException e ) + { + // 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 ) + { + Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" ); + resolver.resolve( pomArtifact, remoteRepositories, local ); + model = readModel( pomArtifact.getFile() ); + } + + return model; + } + + /** + * This method loops through all the parents, getting + * each pom model and then its parent. + * + * @param groupId + * @param artifactId + * @param version + * @return + * @throws ArtifactResolutionException + * @throws ArtifactNotFoundException + * @throws IOException + * @throws XmlPullParserException + */ + public List getModelsRecursively ( String groupId, String artifactId, String version, File pom ) + throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException + { + List models = null; + Model model = getPomModel( groupId, artifactId, version, pom ); + + Parent parent = model.getParent(); + + // recurse into the parent + if ( parent != null ) + { + // get the relative path + String relativePath = parent.getRelativePath(); + if ( StringUtils.isEmpty( relativePath ) ) + { + relativePath = "../pom.xml"; + } + // calculate the recursive path + File parentPom = new File( pom.getParent(), relativePath ); + + models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom ); + } + else + { + // only create it here since I'm not at the top + models = new ArrayList(); + } + models.add( model ); + + return models; + } + + /** + * Make sure the model is the one I'm expecting. + * + * @param groupId + * @param artifactId + * @param version + * @param model Model being checked. + * @return + */ + protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model ) + { + // 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(); + } + } + 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. + } + return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils + .equals( artifactId, model.getArtifactId() ) ); + } +} Modified: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java?rev=580196&r1=580195&r2=580196&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java Thu Sep 27 19:26:19 2007 @@ -26,10 +26,9 @@ 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.plugins.enforcer.utils.EnforcerRuleUtils; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -41,40 +40,7 @@ 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(); @@ -133,59 +99,7 @@ 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 @@ -203,6 +117,7 @@ project.setVersion( "1.0" ); project.setBaseDir( projectDir ); + rule.setUtils( new EnforcerRuleUtils(EnforcerTestUtils.getHelper( project )) ); List plugins = rule.getAllPluginEntries( project ); // there should be 3 Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/TestEnforcerRuleUtils.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/TestEnforcerRuleUtils.java?rev=580196&view=auto ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/TestEnforcerRuleUtils.java (added) +++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/TestEnforcerRuleUtils.java Thu Sep 27 19:26:19 2007 @@ -0,0 +1,112 @@ +package org.apache.maven.plugins.enforcer.utils; + +import java.io.File; +import java.io.IOException; +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.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugins.enforcer.EnforcerTestUtils; +import org.apache.maven.plugins.enforcer.RequirePluginVersions; +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 TestEnforcerRuleUtils + extends AbstractMojoTestCase +{ + public void testCheckIfModelMatches () + { + + EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper() ); + + 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( utils.checkIfModelMatches( "", "", "", model ) ); + assertFalse( utils.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( utils.checkIfModelMatches( "foo-group", "foo", "1.0", model ) ); + + // check that the version and group are inherited + // from the parent. + assertTrue( utils.checkIfModelMatches( "foo-group", "", "1.0", model ) ); + + // check handling of nulls + assertFalse( utils.checkIfModelMatches( "foo-group", null, "1.0", model ) ); + } + + public void testGetModelsRecursivelyBottom () + throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException + { + String path = "target/test-classes/requirePluginVersions/getPomRecursively/b/c"; + + StringUtils.replace( path, "/", File.separator ); + + File pom = new File( getBasedir() + File.separator + path, "pom.xml" ); + + EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper() ); + List models = utils.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 + { + String path = "target/test-classes/requirePluginVersions/getPomRecursively"; + + StringUtils.replace( path, "/", File.separator ); + + File pom = new File( getBasedir() + File.separator + path, "pom.xml" ); + + EnforcerRuleUtils utils = new EnforcerRuleUtils( EnforcerTestUtils.getHelper() ); + + List models = utils.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 ); + } +} Propchange: maven/enforcer/trunk/maven-enforcer-plugin/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Thu Sep 27 19:26:19 2007 @@ -3,3 +3,4 @@ .project .settings cobertura.ser +archetype.properties Modified: maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/util/EnforcerUtils.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/util/EnforcerUtils.java?rev=580196&r1=580195&r2=580196&view=diff ============================================================================== --- maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/util/EnforcerUtils.java (original) +++ maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/util/EnforcerUtils.java Thu Sep 27 19:26:19 2007 @@ -1,10 +1,5 @@ -package org.apache.maven.plugin.enforcer.util; +package org.apache.maven.plugins.enforcer.util; -import java.util.Iterator; - -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.Restriction; -import org.apache.maven.artifact.versioning.VersionRange; /** * @author <a href="mailto:[EMAIL PROTECTED]">Brian Fox</a> Added: maven/enforcer/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/EnforcerTestUtils.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/EnforcerTestUtils.java?rev=580196&view=auto ============================================================================== --- maven/enforcer/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/EnforcerTestUtils.java (added) +++ maven/enforcer/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/EnforcerTestUtils.java Thu Sep 27 19:26:19 2007 @@ -0,0 +1,72 @@ +package org.apache.maven.plugins; + +/* + * 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.util.Date; +import java.util.Properties; + +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.plugins.enforcer.DefaultEnforcementRuleHelper; +import org.apache.maven.plugins.enforcer.EnforcerExpressionEvaluator; +import org.apache.maven.plugins.enforcer.MockPathTranslator; +import org.apache.maven.plugins.enforcer.MockPlexusContainer; +import org.apache.maven.plugins.enforcer.MockProject; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; + +/** + * @author <a href="mailto:[EMAIL PROTECTED]">Brian Fox</a> + * + */ +public class EnforcerTestUtils +{ + public static MavenSession getMavenSession() + { + return new MavenSession( new MockPlexusContainer(), null, null, null, null, null, null, new Properties(), + new Date() ); + } + + public static EnforcerRuleHelper getHelper() + { + MavenSession session = getMavenSession(); + ExpressionEvaluator eval = new EnforcerExpressionEvaluator( session, new MockPathTranslator(), + new MockProject() ); + return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog() ); + } + + public static EnforcerRuleHelper getHelper(MavenProject project) { + MavenSession session = getMavenSession(); + 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; + } +}