Marvin Froeder created MENFORCER-136:
----------------------------------------

             Summary: New enforcer for environment variables
                 Key: MENFORCER-136
                 URL: https://jira.codehaus.org/browse/MENFORCER-136
             Project: Maven 2.x Enforcer Plugin
          Issue Type: New Feature
          Components: Standard Rules
            Reporter: Marvin Froeder


I made this rule for my project, but I think it would be a nice inclusion from 
the project.

This is particularly useful when running integration tests that needs 
application server, like when using arquillian + jboss 
(http://arquillian.org/guides/getting_started/#test_across_containers)

{noformat}
Index: 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
===================================================================
--- 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
    (revision 0)
+++ 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
    (revision 0)
@@ -0,0 +1,82 @@
+package org.apache.maven.plugins.enforcer;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ * Abstract enforcer rule that give a foundation to validate properties from 
multiple sources.
+ * 
+ * @author Paul Gier
+ * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin 
Froeder</a>
+ * @version $Id: AbstractPropertyEnforcerRule.java $
+ */
+public abstract class AbstractPropertyEnforcerRule
+    extends AbstractNonCacheableEnforcerRule
+{
+
+    /**
+     * Match the property value to a given regular expression. Defaults to 
<code>null</code> (any value is ok).
+     */
+    public String regex = null;
+
+    /** Specify a warning message if the regular expression is not matched. */
+    public String regexMessage = null;
+
+    public AbstractPropertyEnforcerRule()
+    {
+        super();
+    }
+
+    /**
+     * Execute the rule.
+     * 
+     * @param helper the helper
+     * @throws EnforcerRuleException the enforcer rule exception
+     */
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        Object propValue = resolveValue( helper );
+
+        // Check that the property is not null or empty string
+        if ( propValue == null )
+        {
+            if ( message == null )
+            {
+                message = getName() + " \"" + getPropertyName() + "\" 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 =
+                    getName() + " \"" + getPropertyName() + "\" evaluates to 
\"" + propValue + "\".  "
+                        + "This does not match the regular expression \"" + 
regex + "\"";
+            }
+            throw new EnforcerRuleException( regexMessage );
+        }
+    }
+
+    /**
+     * How the property that is being evaluated is called
+     */
+    public abstract String getName();
+
+    /**
+     * The name of the property currently being evaluated, this is used for 
default message pourpouses only
+     */
+    public abstract String getPropertyName();
+
+    /**
+     * Resolves the property value
+     * 
+     * @param helper
+     * @throws EnforcerRuleException
+     */
+    public abstract Object resolveValue( EnforcerRuleHelper helper )
+        throws EnforcerRuleException;
+
+}
\ No newline at end of file
Index: 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
===================================================================
--- 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
      (revision 0)
+++ 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
      (revision 0)
@@ -0,0 +1,73 @@
+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.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ * This rule checks that certain environment variable is set.
+ * 
+ * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin 
Froeder</a>
+ */
+public class RequireEnvironmentVariable
+    extends AbstractPropertyEnforcerRule
+{
+
+    /** Specify the required variable. */
+    public String variableName = null;
+
+    @Override
+    public String resolveValue( EnforcerRuleHelper helper )
+    {
+        String envValue = System.getenv( variableName );
+        return envValue;
+    }
+
+    public boolean isCacheable()
+    {
+        // environment variables won't change while maven is on the run
+        return true;
+    }
+
+    public boolean isResultValid( EnforcerRule cachedRule )
+    {
+        // this rule shall always have the same result, since environment
+        // variables are set before maven is launched
+        return true;
+    }
+
+    public String getCacheId()
+    {
+        return variableName;
+    }
+
+    @Override
+    public String getPropertyName()
+    {
+        return variableName;
+    }
+
+    @Override
+    public String getName()
+    {
+        return "Environment variable";
+    }
+}
Index: 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProperty.java
===================================================================
--- 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProperty.java
 (revision 1362071)
+++ 
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProperty.java
 (working copy)
@@ -25,29 +25,18 @@
 
 /**
  * This rule checks that certain properties are set.
- *
+ * 
  * @author Paul Gier
  */
 public class RequireProperty
-    extends AbstractNonCacheableEnforcerRule
+    extends AbstractPropertyEnforcerRule
 {
 
     /** Specify the required property. */
     public String property = null;
 
-    /** Match the property value to a given regular expression. Defaults to 
<code>null</code> (any value is ok). */
-    public String regex = null;
-
-    /** Specify a warning message if the regular expression is not matched. */
-    public String regexMessage = null;
-
-    /**
-     * Execute the rule.
-     *
-     * @param helper the helper
-     * @throws EnforcerRuleException the enforcer rule exception
-     */
-    public void execute( EnforcerRuleHelper helper )
+    @Override
+    public Object resolveValue( EnforcerRuleHelper helper )
         throws EnforcerRuleException
     {
         Object propValue = null;
@@ -59,26 +48,23 @@
         {
             throw new EnforcerRuleException( "Unable to evaluate property: " + 
property, eee );
         }
+        return propValue;
+    }
 
-        // 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 );
-        }
+    protected String resolveValue()
+    {
+        return null;
     }
+
+    @Override
+    public String getPropertyName()
+    {
+        return property;
+    }
+
+    @Override
+    public String getName()
+    {
+        return "Property";
+    }
 }
Index: 
enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
===================================================================
--- 
enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
  (revision 0)
+++ 
enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
  (revision 0)
@@ -0,0 +1,112 @@
+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 junit.framework.TestCase;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ * Unit test for {@link RequireEnvironmentVariable}}
+ *
+ * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin 
Froeder</a>
+ */
+public class TestRequireEnvironmentVariable
+    extends TestCase
+{
+
+    /**
+     * Test rule.
+     *
+     * @throws EnforcerRuleException the enforcer rule exception
+     */
+    public void testRule()
+        throws EnforcerRuleException
+    {
+        MockProject project = new MockProject();
+        project.setProperty( "testProp", "This is a test." );
+        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
+
+        RequireEnvironmentVariable rule = new RequireEnvironmentVariable();
+        // this env variable should not be set
+        rule.variableName = "JUNK";
+
+        try
+        {
+            rule.execute( helper );
+            fail( "Expected an exception." );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            // expected to catch this.
+        }
+
+        // PATH shall be common to windows and linux
+        rule.variableName = "PATH";
+        try
+        {
+            rule.execute( helper );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            fail( "This should not throw an exception" );
+        }
+    }
+
+    /**
+     * Test rule with regex.
+     *
+     * @throws EnforcerRuleException the enforcer rule exception
+     */
+    public void testRuleWithRegex()
+        throws EnforcerRuleException
+    {
+        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
+
+        RequireEnvironmentVariable rule = new RequireEnvironmentVariable();
+        rule.variableName = "PATH";
+        // 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.
+        }
+
+        // can't really predict what a PATH will looks like, just enforce it 
ain't empty
+        rule.regex = ".{1,}";
+        try
+        {
+            rule.execute( helper );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            fail( "This should not throw an exception" );
+        }
+    }
+
+}
Index: pom.xml
===================================================================
--- pom.xml     (revision 1362071)
+++ pom.xml     (working copy)
@@ -48,6 +48,13 @@
     </developer>
   </developers>
 
+  <contributors>
+    <contributor>
+      <name>Marvin Froeder</name>
+      <email>[email protected]</email>
+    </contributor>
+  </contributors>
+
   <mailingLists>
     <mailingList>
       <name>Maven User List</name>
@@ -262,6 +269,33 @@
             
<tagBase>https://svn.apache.org/repos/asf/maven/enforcer/tags</tagBase>
           </configuration>
         </plugin>
+        <!--This plugin's configuration is used to store Eclipse m2e settings 
only. It has no influence on the Maven build itself.-->
+        <plugin>
+               <groupId>org.eclipse.m2e</groupId>
+               <artifactId>lifecycle-mapping</artifactId>
+               <version>1.0.0</version>
+               <configuration>
+                       <lifecycleMappingMetadata>
+                               <pluginExecutions>
+                                       <pluginExecution>
+                                               <pluginExecutionFilter>
+                                                       
<groupId>org.codehaus.plexus</groupId>
+                                                       <artifactId>
+                                                               
plexus-maven-plugin
+                                                       </artifactId>
+                                                       
<versionRange>[1.3.8,)</versionRange>
+                                                       <goals>
+                                                               
<goal>descriptor</goal>
+                                                       </goals>
+                                               </pluginExecutionFilter>
+                                               <action>
+                                                       <ignore></ignore>
+                                               </action>
+                                       </pluginExecution>
+                               </pluginExecutions>
+                       </lifecycleMappingMetadata>
+               </configuration>
+        </plugin>
       </plugins>
     </pluginManagement>
   </build>
{noformat}


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://jira.codehaus.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to