You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2012/09/08 23:35:02 UTC
svn commit: r1382370 - in /maven/enforcer/trunk/enforcer-rules/src:
main/java/org/apache/maven/plugins/enforcer/
test/java/org/apache/maven/plugins/enforcer/
Author: olamy
Date: Sat Sep 8 21:35:02 2012
New Revision: 1382370
URL: http://svn.apache.org/viewvc?rev=1382370&view=rev
Log:
MENFORCER-136] New enforcer for environment variables.
Submitted by Marvin Froeder.
Missed to add files.
Added:
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java (with props)
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java (with props)
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java (with props)
Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java?rev=1382370&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java (added)
+++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java Sat Sep 8 21:35:02 2012
@@ -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
Propchange: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java?rev=1382370&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java (added)
+++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java Sat Sep 8 21:35:02 2012
@@ -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";
+ }
+}
Propchange: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireEnvironmentVariable.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java?rev=1382370&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java (added)
+++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java Sat Sep 8 21:35:02 2012
@@ -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" );
+ }
+ }
+
+}
Propchange: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireEnvironmentVariable.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision