You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2014/01/09 00:13:04 UTC

svn commit: r1556666 - in /maven/enforcer/trunk/enforcer-rules/src: site/apt/index.apt site/apt/requireActiveProfile.apt.vm test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java

Author: khmarbaise
Date: Wed Jan  8 23:13:04 2014
New Revision: 1556666

URL: http://svn.apache.org/r1556666
Log:
[MENFORCER-169]
 - Added unit test for requireActiveProfile
 - Added link to docs.

Added:
    maven/enforcer/trunk/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm
    maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java
Modified:
    maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt

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=1556666&r1=1556665&r2=1556666&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt (original)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt Wed Jan  8 23:13:04 2014
@@ -41,7 +41,7 @@ Standard Rules
 
   * {{{./evaluateBeanshell.html}evaluateBeanshell}} - evaluates a beanshell script.
 
-  * requireActiveProfile - enforces one or more active profiles.
+  * {{{./requireActiveProfile.html}requireActiveProfile}} - enforces one or more active profiles.
   
   * {{{./requireEnvironmentVariable .html}requireEnvironmentVariable}} - enforces the existence of an environment variable
   

Added: maven/enforcer/trunk/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm?rev=1556666&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm (added)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm Wed Jan  8 23:13:04 2014
@@ -0,0 +1,109 @@
+~~ 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 Active Profile
+  ------
+  Karl-Heinz Marbaise
+  ------
+  December 2013
+  ------
+
+Require Active Profile
+
+  This rule checks that a specified list of profiles is activated.
+
+
+   The following parameters are supported by this rule:
+   
+   * message - an optional message to the user if the rule fails.
+   
+   * profiles - A comma separated list of profile to check.
+   
+   * all - Checks if all given profiles are active. Default is true.
+   
+   []
+
+   
+  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-all-profiles-are-activated</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireActiveProfile>
+                  <profiles>first,second</profiles>
+                </requireActiveProfile>
+              </rules>
+              <fail>true</fail>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
+
+  Sample Plugin Configuration to check if one of the given profiles is active:
+  
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>enforce-first-or-second-profile-is-active</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireActiveProfile>
+                  <profiles>first,second</profiles>
+                  <all>false</all>
+                </requireActiveProfile>
+              </rules>
+              <fail>true</fail>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+

Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java?rev=1556666&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java (added)
+++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java Wed Jan  8 23:13:04 2014
@@ -0,0 +1,198 @@
+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.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.model.Profile;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.junit.Before;
+import org.junit.Test;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Check the profile rule.
+ *
+ * @author <a href="mailto:khmarbaise@apache.org">Karl Heinz Marbaise</a>
+ */
+public class RequireActiveProfileTest
+{
+    private MavenProject project;
+
+    private EnforcerRuleHelper helper;
+
+    private RequireActiveProfile rule;
+
+    @Before
+    public void before()
+        throws ExpressionEvaluationException
+    {
+        project = mock( MavenProject.class );
+        helper = mock( EnforcerRuleHelper.class );
+        when( helper.evaluate( "${project}" ) ).thenReturn( project );
+        rule = new RequireActiveProfile();
+    }
+
+    @Test
+    public void testNoActiveProfilesInProjectAndNoProfilesExpectedToBeActivated()
+        throws EnforcerRuleException
+    {
+        when( project.getActiveProfiles() ).thenReturn( Collections.<Profile> emptyList() );
+
+        rule.execute( helper );
+
+        assertTrue( true );
+    }
+
+    @Test
+    public void testActiveProfileAndExpectedActiveProfile()
+        throws EnforcerRuleException
+    {
+        List<Profile> profiles = Collections.<Profile> singletonList( createProfile( "profile-2" ) );
+
+        when( project.getActiveProfiles() ).thenReturn( profiles );
+
+        rule.setProfiles( "profile-2" );
+
+        rule.execute( helper );
+        assertTrue( true );
+    }
+
+    @Test( expected = EnforcerRuleException.class )
+    public void testNoActiveProfileButTheRuleRequestedAnActiveProfile()
+        throws EnforcerRuleException
+    {
+        when( project.getActiveProfiles() ).thenReturn( Collections.<Profile> emptyList() );
+
+        rule.setProfiles( "profile-2" );
+
+        rule.execute( helper );
+        // intentionally no assertTrue(...)
+    }
+
+    @Test( expected = EnforcerRuleException.class )
+    public void testNoActiveProfileButWeExpectToGetAnExceptionWithAll()
+        throws EnforcerRuleException
+    {
+        when( project.getActiveProfiles() ).thenReturn( Collections.<Profile> emptyList() );
+
+        rule.setProfiles( "profile-2" );
+        rule.setAll( true );
+
+        rule.execute( helper );
+        // intentionally no assertTrue(...)
+    }
+
+    @Test
+    public void testTwoActiveProfilesWithOneRequiredProfile()
+        throws EnforcerRuleException
+    {
+        List<Profile> profiles = new ArrayList<Profile>();
+        profiles.add( createProfile( "profile-1" )  );
+        profiles.add( createProfile( "profile-2" )  );
+
+        when( project.getActiveProfiles() ).thenReturn( profiles );
+
+        rule.setProfiles( "profile-2" );
+
+        rule.execute( helper );
+        assertTrue( true );
+    }
+
+    @Test
+    public void testTwoActiveProfilesWhereOneProfileIsRequiredToBeActivated()
+        throws EnforcerRuleException
+    {
+        List<Profile> profiles = new ArrayList<Profile>();
+        profiles.add( createProfile( "profile-1" )  );
+        profiles.add( createProfile( "profile-2" )  );
+
+        when( project.getActiveProfiles() ).thenReturn( profiles );
+
+        rule.setProfiles( "profile-2" );
+        rule.setAll( true );
+
+        rule.execute( helper );
+        assertTrue( true );
+    }
+
+    @Test( expected = EnforcerRuleException.class )
+    public void testTwoActiveProfilesWithTwoRequiredProfilesWhereOneOfThemIsNotPartOfTheActiveProfiles()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        List<Profile> profiles = new ArrayList<Profile>();
+        profiles.add( createProfile( "profile-X" )  );
+        profiles.add( createProfile( "profile-Y" )  );
+
+        when( project.getActiveProfiles() ).thenReturn( profiles );
+
+        rule.setProfiles( "profile-Z,profile-X" );
+        rule.setAll( true );
+
+        rule.execute( helper );
+        // intentionally no assertTrue(..)
+    }
+
+    @Test( expected = EnforcerRuleException.class )
+    public void testOneActiveProfilesWithTwoRequiredProfiles()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        List<Profile> profiles = new ArrayList<Profile>();
+        profiles.add( createProfile( "profile-X" )  );
+
+        when( project.getActiveProfiles() ).thenReturn( profiles );
+
+        rule.setProfiles( "profile-X,profile-Y" );
+        rule.setAll( true );
+
+        rule.execute( helper );
+        // intentionally no assertTrue(..)
+    }
+
+    @Test
+    public void testOneActiveProfileWithTwoProfilesButNotAll()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        List<Profile> profiles = new ArrayList<Profile>();
+        profiles.add( createProfile( "profile-X" )  );
+
+        when( project.getActiveProfiles() ).thenReturn( profiles );
+
+        rule.setProfiles( "profile-X,profile-Y" );
+        rule.setAll( false );
+
+        rule.execute( helper );
+        // intentionally no assertTrue(..)
+    }
+
+    private Profile createProfile( String profileId )
+    {
+        Profile p = new Profile();
+        p.setId( profileId );
+        return p;
+    }
+
+}