maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bri...@apache.org
Subject svn commit: r546359 - in /maven/plugins/trunk/maven-enforcer-plugin: ./ src/main/java/org/apache/maven/plugin/enforcer/ src/site/apt/rules/ src/test/java/org/apache/maven/plugin/enforcer/
Date Tue, 12 Jun 2007 02:30:50 GMT
Author: brianf
Date: Mon Jun 11 19:30:49 2007
New Revision: 546359

URL: http://svn.apache.org/viewvc?view=rev&rev=546359
Log:
MENFORCER-5: added new rule from Paul Gier

Added:
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
    maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt
    maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequireProperty.java
Modified:
    maven/plugins/trunk/maven-enforcer-plugin/pom.xml
    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

Modified: maven/plugins/trunk/maven-enforcer-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/pom.xml?view=diff&rev=546359&r1=546358&r2=546359
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/pom.xml Mon Jun 11 19:30:49 2007
@@ -28,7 +28,7 @@
 	<artifactId>maven-enforcer-plugin</artifactId>
 	<packaging>maven-plugin</packaging>
 	<name>Maven Enforcer Plugin</name>
-	<description>The Loving Iron First of Maven</description>
+	<description>The Loving Iron Fist of Maven</description>
 	<version>1.0-alpha-3-SNAPSHOT</version>
 	<issueManagement>
 		<system>JIRA</system>

Added: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java?view=auto&rev=546359
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
(added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
Mon Jun 11 19:30:49 2007
@@ -0,0 +1,103 @@
+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 org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+
+/**
+ * This rule checks that certain properties are set.
+ * 
+ * @author Paul Gier
+ * 
+ */
+public class RequireProperty implements EnforcerRule
+{
+	
+    /**
+     * Specify the required property.
+     * 
+     * @parameter
+     * @required
+     */
+    public String property = null;
+	
+    /**
+     * Specify a warning message if the property is not set.
+     * 
+     * @parameter
+     */
+    public String message = null;
+
+    /**
+     * Match the property value to a given regular expresssion.
+     * Defaults to null (any value is ok).
+     * 
+     * @parameter
+     */
+    public String regex = null;
+	
+    /**
+     * Specify a warning message if the regular expression is not matched.
+     * 
+     * @parameter
+     */
+    public String regexMessage = null;
+
+    /**
+     * Execute the rule.
+     */
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+    	Object propValue = null;
+    	try 
+    	{
+    		propValue = (String)helper.evaluate("${" + property + "}");
+    	} 
+    	catch (ExpressionEvaluationException eee) 
+    	{
+    		throw new EnforcerRuleException("Unable to evaluate property: " + property, eee);
+    	}
+    	
+    	// Check that the property is not null or empty string
+    	if (propValue==null)
+    	{
+    		if (message==null) 
+    		{
+                message = "Property \"" + property + "\" is required for this build.";
+    		}
+    		throw new EnforcerRuleException(message);
+    	}
+    	// If there is a regex, check that the property matches it
+    	if (regex != null && !propValue.toString().matches(regex))
+    	{
+    		if (regexMessage == null) 
+    		{
+    			regexMessage = "Property \"" + property + "\" evaluates to \"" + propValue + "\".
 " +
+	    				"This does not match the regular expression \"" + regex + "\"";
+    		}
+    		throw new EnforcerRuleException(regexMessage);
+    	}
+    }
+
+}

Added: 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?view=auto&rev=546359
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt (added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/requireProperty.apt Mon Jun
11 19:30:49 2007
@@ -0,0 +1,78 @@
+~~ 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 Property
+  ------
+  Brian Fox
+  ------
+  Mar 2007
+  ------
+
+Require Property
+
+  This rule can enforce that a declared property is set and optionally evaluate it against
a regular expression.
+
+
+   The following parameters are supported by this rule:
+   
+   * property - the property to evaluate.
+   
+   * message - an optional message to the user if the rule fails. Default is: "Property 'xxx'
is required for this build".	
+   
+   * regex - a regular expression used to check the value of the property.
+   
+   * regexMessage - an optional message to the user if the regex check fails.
+   
+   []
+
+  Sample Plugin Configuration:
+  
++---+
+<project>
+  [...]
+  <build>
+   <plugins>
+     <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>enforce-property</id>
+            <goals>
+              <goal>enforce-once</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireProperty>
+                  <property>basedir</property>
+	        <message>"You must have a basedir!"</message>
+	        <regex>"\d"</regex>
+	        <regexMessage>"You must have a digit in your baseDir!"</regexMessage>
+                </requireProperty>
+             </rules>  
+            <fail>true</fail>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
+  
\ No newline at end of file

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?view=diff&rev=546359&r1=546358&r2=546359
==============================================================================
--- 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
Mon Jun 11 19:30:49 2007
@@ -24,6 +24,7 @@
 
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.logging.SystemStreamLog;
+import org.apache.maven.project.MavenProject;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
 
@@ -44,6 +45,13 @@
         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() );
     }
 }

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?view=diff&rev=546359&r1=546358&r2=546359
==============================================================================
--- 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
Mon Jun 11 19:30:49 2007
@@ -153,6 +153,8 @@
     private String defaultGoal;
 
     private Set artifacts;
+    
+    private Properties properties = new Properties();
 
     public MockProject()
     {
@@ -999,7 +1001,12 @@
 
     public Properties getProperties()
     {
-        return new Properties();
+        return this.properties;
+    }
+    
+    public void setProperty(String key, String value) 
+    {
+    	properties.setProperty(key, value);
     }
 
     public List getFilters()

Added: maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequireProperty.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequireProperty.java?view=auto&rev=546359
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequireProperty.java
(added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestRequireProperty.java
Mon Jun 11 19:30:49 2007
@@ -0,0 +1,97 @@
+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 junit.framework.TestCase;
+
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ * @author Paul Gier
+ * 
+ */
+public class TestRequireProperty extends TestCase
+{
+    public void testRule() throws EnforcerRuleException
+    {
+    	MockProject project = new MockProject();
+    	project.setProperty("testProp", "This is a test.");
+        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(project);
+
+        RequireProperty rule = new RequireProperty();
+        // this property should not be set
+        rule.property = "testPropJunk";
+
+        try
+        {
+            rule.execute( helper );
+            fail( "Expected an exception." );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            // expected to catch this.
+        }
+
+        // this property should be set by the surefire plugin
+        rule.property = "testProp";
+        try 
+        {
+        	rule.execute( helper );
+        } 
+        catch ( EnforcerRuleException e )
+        {
+        	fail("This should not throw an exception");
+        }
+    }
+    
+    public void testRuleWithRegex() throws EnforcerRuleException
+    {
+    	MockProject project = new MockProject();
+    	project.setProperty("testProp", "This is a test.");
+        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(project);
+
+        RequireProperty rule = new RequireProperty();
+        rule.property = "testProp";
+        // This expression should not match the property value
+        rule.regex = "[^abc]";
+        
+        try
+        {
+            rule.execute( helper );
+            fail( "Expected an exception." );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            // expected to catch this.
+        }
+
+        // this expr should match the property
+        rule.regex = "[This].*[.]";
+        try 
+        {
+        	rule.execute( helper );
+        } 
+        catch ( EnforcerRuleException e )
+        {
+        	fail("This should not throw an exception");
+        }
+    }
+}



Mime
View raw message