maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rfscho...@apache.org
Subject svn commit: r1493515 - in /maven/enforcer/trunk: ./ enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/ enforcer-rules/src/site/apt/ enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/
Date Sun, 16 Jun 2013 14:33:52 GMT
Author: rfscholte
Date: Sun Jun 16 14:33:52 2013
New Revision: 1493515

URL: http://svn.apache.org/r1493515
Log:
[MENFORCER-155] Add rule RequirePrerequisites 

Added:
    maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePrerequisite.java
    maven/enforcer/trunk/enforcer-rules/src/site/apt/requirePrerequisites.apt.vm
    maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequirePrerequisiteTest.java
Modified:
    maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt
    maven/enforcer/trunk/pom.xml

Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePrerequisite.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePrerequisite.java?rev=1493515&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePrerequisite.java
(added)
+++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePrerequisite.java
Sun Jun 16 14:33:52 2013
@@ -0,0 +1,92 @@
+package org.apache.maven.plugins.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 org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.model.Prerequisites;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+
+/**
+ * 
+ * @author Robert Scholte
+ * @since 1.3
+ */
+public class RequirePrerequisite extends AbstractNonCacheableEnforcerRule
+{
+    /**
+     * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)}
+     */
+    private String mavenVersion;
+
+    public void setMavenVersion( String mavenVersion )
+    {
+        this.mavenVersion = mavenVersion;
+    }
+    
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        try
+        {
+            MavenProject project = (MavenProject) helper.evaluate( "${project}" );
+            
+            Prerequisites prerequisites = project.getPrerequisites(); 
+            
+            if( prerequisites == null )
+            {
+                throw new EnforcerRuleException( "Requires prerequisite not set" );
+            }
+
+            if( mavenVersion != null )
+            {
+                
+                VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion
);
+                
+                if( !requiredVersionRange.hasRestrictions() )
+                {
+                    requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion
+ ",)" );
+                }
+                
+                VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven()
);
+                
+                VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion
);
+                
+                if ( restrictedVersionRange.getRecommendedVersion() == null )
+                {
+                    throw new EnforcerRuleException( "The specified Maven prerequisite( "
+ specifiedVersion + " ) doesn't match the required version: " + mavenVersion );
+                }
+            }
+        }
+        catch ( ExpressionEvaluationException e )
+        {
+            throw new EnforcerRuleException( e.getMessage(), e );
+        }
+        catch ( InvalidVersionSpecificationException e )
+        {
+            throw new EnforcerRuleException( e.getMessage(), e );
+        }
+    }
+
+}

Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt?rev=1493515&r1=1493514&r2=1493515&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt (original)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt Sun Jun 16 14:33:52 2013
@@ -61,6 +61,8 @@ Standard Rules
 
   * {{{./requirePluginVersions.html}requirePluginVersions}} - enforces that all plugins have
a specified version.
 
+  * {{{./requirePrerequisites.html}requirePrerequisites}} - enforces that prerequisites have
been specified.
+   
   * {{{./requireProperty.html}requireProperty}} - enforces the existence and values of properties.
    
   * {{{./requireReleaseDeps.html}requireReleaseDeps}} - enforces that no snapshots are included
as dependencies.

Added: maven/enforcer/trunk/enforcer-rules/src/site/apt/requirePrerequisites.apt.vm
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requirePrerequisites.apt.vm?rev=1493515&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/requirePrerequisites.apt.vm (added)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/requirePrerequisites.apt.vm Sun Jun 16
14:33:52 2013
@@ -0,0 +1,74 @@
+ ~~ 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 Same Version
+ -----
+ Robert Scholte
+ -----
+ 2013-06-16
+ -----
+ 
+  Require Prerequisites
+
+  This rule enforces that the prerequisite is specified since it is not inherited from its
parent. 
+  Defining the prerequisites is especially useful for plugins to enforce at least a certain
version of Maven. 
+
+
+   The following parameters are supported by this rule:
+   
+   * mavenVersion - an optional value contain a versionrange or the version at least required.
+   
+   []
+
+   
+  Sample Plugin Configuration:
+  
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>enforce-prerequisites</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requirePrerequisites>
+                  <mavenVersion>2.2.1</mavenVersion>
+                </requirePrerequisites>
+                <!-- same rule, different notation -->
+                <requirePrerequisites>
+                  <mavenVersion>[2.2.1,)</mavenVersion>
+                </requirePrerequisites>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
\ No newline at end of file

Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequirePrerequisiteTest.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequirePrerequisiteTest.java?rev=1493515&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequirePrerequisiteTest.java
(added)
+++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequirePrerequisiteTest.java
Sun Jun 16 14:33:52 2013
@@ -0,0 +1,110 @@
+package org.apache.maven.plugins.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 static org.mockito.Mockito.*;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.model.Prerequisites;
+import org.apache.maven.project.MavenProject;
+import org.junit.Test;
+
+public class RequirePrerequisiteTest
+{
+    @Test( expected = EnforcerRuleException.class )
+    public void testNoPrerequisite()
+        throws Exception
+    {
+        RequirePrerequisite rule = new RequirePrerequisite();
+
+        MavenProject project = mock( MavenProject.class );
+
+        EnforcerRuleHelper helper = mock( EnforcerRuleHelper.class );
+        when( helper.evaluate( "${project}" ) ).thenReturn( project );
+
+        rule.execute( helper );
+    }
+    
+    @Test
+    public void testNoSpecifiedPrerequisite()
+        throws Exception
+    {
+        RequirePrerequisite rule = new RequirePrerequisite();
+
+        MavenProject project = mock( MavenProject.class );
+        when( project.getPrerequisites() ).thenReturn( new Prerequisites() );
+
+        EnforcerRuleHelper helper = mock( EnforcerRuleHelper.class );
+        when( helper.evaluate( "${project}" ) ).thenReturn( project );
+
+        rule.execute( helper );
+    }
+
+    @Test( expected = EnforcerRuleException.class )
+    public void testLowerMavenPrerequisite()
+        throws Exception
+    {
+        RequirePrerequisite rule = new RequirePrerequisite();
+        rule.setMavenVersion( "3.0" );
+
+        MavenProject project = mock( MavenProject.class );
+        when( project.getPrerequisites() ).thenReturn( new Prerequisites() );
+
+        EnforcerRuleHelper helper = mock( EnforcerRuleHelper.class );
+        when( helper.evaluate( "${project}" ) ).thenReturn( project );
+
+        rule.execute( helper );
+    }
+
+    @Test( expected = EnforcerRuleException.class )
+    public void testLowerMavenRangePrerequisite()
+        throws Exception
+    {
+        RequirePrerequisite rule = new RequirePrerequisite();
+        rule.setMavenVersion( "[3.0,)" );
+
+        MavenProject project = mock( MavenProject.class );
+        when( project.getPrerequisites() ).thenReturn( new Prerequisites() );
+
+        EnforcerRuleHelper helper = mock( EnforcerRuleHelper.class );
+        when( helper.evaluate( "${project}" ) ).thenReturn( project );
+
+        rule.execute( helper );
+    }
+
+    @Test
+    public void testValidPrerequisite()
+                    throws Exception
+    {
+        RequirePrerequisite rule = new RequirePrerequisite();
+        rule.setMavenVersion( "2.2.1" );
+
+        MavenProject project = mock( MavenProject.class );
+        Prerequisites prerequisites = new Prerequisites();
+        prerequisites.setMaven( "3.0" );
+        when( project.getPrerequisites() ).thenReturn( prerequisites );
+
+        EnforcerRuleHelper helper = mock( EnforcerRuleHelper.class );
+        when( helper.evaluate( "${project}" ) ).thenReturn( project );
+
+        rule.execute( helper );
+    }
+}

Modified: maven/enforcer/trunk/pom.xml
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/pom.xml?rev=1493515&r1=1493514&r2=1493515&view=diff
==============================================================================
--- maven/enforcer/trunk/pom.xml (original)
+++ maven/enforcer/trunk/pom.xml Sun Jun 16 14:33:52 2013
@@ -197,7 +197,7 @@
       <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
-        <version>3.8.2</version>
+        <version>4.11</version>
         <scope>test</scope>
       </dependency>
       <dependency>



Mime
View raw message