[GitHub] asfgit closed pull request #21: Add rule: banProfiles

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] asfgit closed pull request #21: Add rule: banProfiles

GitBox
asfgit closed pull request #21: Add rule: banProfiles
URL: https://github.com/apache/maven-enforcer/pull/21
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanProfiles.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanProfiles.java
new file mode 100644
index 0000000..bc6d67a
--- /dev/null
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanProfiles.java
@@ -0,0 +1,128 @@
+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.codehaus.plexus.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This rule checks that one or more profiles are not active.
+ */
+public class BanProfiles
+        extends AbstractNonCacheableEnforcerRule
+{
+
+    /**
+     * Comma separated list of profiles to check.
+     *
+     * @see #setProfiles(String)
+     * @see #getProfiles()
+     */
+    private String profiles;
+
+    public final String getProfiles()
+    {
+        return profiles;
+    }
+
+    public final void setProfiles( final String profiles )
+    {
+        this.profiles = profiles;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
+     */
+    public void execute( final EnforcerRuleHelper theHelper )
+            throws EnforcerRuleException
+    {
+        final List<String> bannedActiveProfiles = new ArrayList<String>();
+        try
+        {
+            final MavenProject project = (MavenProject) theHelper.evaluate( "${project}" ) ;
+            if ( StringUtils.isNotEmpty( profiles ) )
+            {
+                final String[] profs = profiles.split( "," );
+                for ( final String profile : profs )
+                {
+                    if ( isProfileActive( project, profile ) )
+                    {
+                        bannedActiveProfiles.add( profile );
+                    }
+                }
+
+                if ( !bannedActiveProfiles.isEmpty() )
+                {
+                    final String message = getMessage();
+                    final StringBuilder buf = new StringBuilder();
+                    if ( message != null )
+                    {
+                        buf.append( message ).append( '\n' );
+                    }
+
+                    for ( final String profile : bannedActiveProfiles )
+                    {
+                        buf.append( "Profile \"" ).append( profile ).append( "\" is active.\n" );
+                    }
+
+                    throw new EnforcerRuleException( buf.toString() );
+                }
+            }
+        }
+        catch ( ExpressionEvaluationException e )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the project.", e );
+        }
+    }
+
+    /**
+     * Checks if profile is active.
+     *
+     * @param project     the project
+     * @param profileName the profile name
+     * @return <code>true</code> if profile is active, otherwise <code>false</code>
+     */
+    private boolean isProfileActive( final MavenProject project, final String profileName )
+    {
+        @SuppressWarnings( "unchecked" )
+        final List<Profile> activeProfiles = project.getActiveProfiles();
+        if ( activeProfiles != null && !activeProfiles.isEmpty() )
+        {
+            for ( final Profile profile : activeProfiles )
+            {
+                if ( profile.getId().equals( profileName ) )
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+}
diff --git a/enforcer-rules/src/site/apt/banProfiles.apt.vm b/enforcer-rules/src/site/apt/banProfiles.apt.vm
new file mode 100644
index 0000000..5addcad
--- /dev/null
+++ b/enforcer-rules/src/site/apt/banProfiles.apt.vm
@@ -0,0 +1,76 @@
+~~ 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.    
+
+  ------
+  Ban Profiles
+  ------
+  Mikko Koivunalho
+  ------
+  January 2017
+  ------
+
+Ban Profiles
+
+  This rule checks that none of the specified profiles are active.
+
+  For the opposite, please see rule {{{./requireActiveProfile.html}requireActiveProfile}}.
+
+
+   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.
+
+   []
+
+   * N.B. Unlike rule RequireActiveProfile, this rule does not have element <all>!
+  
+  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>
+                <BanProfiles>
+                  <message>Banned profiles activated!</message>
+                  <profiles>first,second</profiles>
+                </BanProfiles>
+              </rules>
+              <fail>true</fail>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
diff --git a/enforcer-rules/src/site/apt/index.apt b/enforcer-rules/src/site/apt/index.apt
index e3c0a70..6ab25f7 100644
--- a/enforcer-rules/src/site/apt/index.apt
+++ b/enforcer-rules/src/site/apt/index.apt
@@ -39,6 +39,8 @@ Standard Rules
   
   * {{{./bannedPlugins.html}bannedPlugins}} - enforces that specific plugins aren't included in the build.
   
+  * {{{./banProfiles.html}banProfiles}} - enforces one or more profiles are not active.
+
   * {{{./bannedRepositories.html}bannedRepositories}} - enforces to not include banned repositories.
 
   * {{{./banTransitiveDependencies.html}banTransitiveDependencies}} - enforces that project doesn't have transitive dependencies.
diff --git a/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm b/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm
index f639223..776ca90 100644
--- a/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm
+++ b/enforcer-rules/src/site/apt/requireActiveProfile.apt.vm
@@ -27,6 +27,8 @@ Require Active Profile
 
   This rule checks that a specified list of profiles is activated.
 
+  For the opposite, please see rule {{{./banProfiles.html}banProfiles}}.
+
 
    The following parameters are supported by this rule:
   
diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanProfilesTest.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanProfilesTest.java
new file mode 100644
index 0000000..041d65c
--- /dev/null
+++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanProfilesTest.java
@@ -0,0 +1,150 @@
+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.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Check the BanProfiles rule.
+ */
+public class BanProfilesTest {
+    private MavenProject project;
+
+    private EnforcerRuleHelper helper;
+
+    private BanProfiles rule;
+
+    @Before
+    public void before()
+            throws ExpressionEvaluationException {
+        project = mock(MavenProject.class);
+        helper = mock(EnforcerRuleHelper.class);
+        when(helper.evaluate("${project}")).thenReturn(project);
+        rule = new BanProfiles();
+    }
+
+    @Test
+    public void testNoActiveProfilesInProjectAndNoBannedProfiles()
+            throws EnforcerRuleException {
+        when(project.getActiveProfiles()).thenReturn(Collections.<Profile>emptyList());
+
+        rule.execute(helper);
+        assertTrue(true);
+    }
+
+    @Test
+    public void testNoActiveProfileAndOneBannedProfile()
+            throws EnforcerRuleException {
+        when(project.getActiveProfiles()).thenReturn(Collections.<Profile>emptyList());
+
+        rule.setProfiles("profile-1");
+
+        rule.execute(helper);
+        assertTrue(true);
+    }
+
+    @Test
+    public void testOneActiveProfileAndNoBannedProfile()
+            throws EnforcerRuleException {
+        when(project.getActiveProfiles()).thenReturn(Collections.singletonList(
+                createProfile("profile-2")
+        ));
+
+        rule.execute(helper);
+        assertTrue(true);
+    }
+
+    @Test(expected = EnforcerRuleException.class)
+    public void testOneActiveProfileAndSameBannedProfile()
+            throws EnforcerRuleException {
+        when(project.getActiveProfiles()).thenReturn(Collections.singletonList(
+                createProfile("profile-2")
+        ));
+
+        rule.setProfiles("profile-2");
+
+        rule.execute(helper);
+        // intentionally no assertTrue(...)
+    }
+
+    @Test(expected = EnforcerRuleException.class)
+    public void testTwoActiveProfilesAndOneSameBannedProfile()
+            throws EnforcerRuleException {
+
+        when(project.getActiveProfiles()).thenReturn(Arrays.asList(
+                createProfile("profile-1"),
+                createProfile("profile-2")
+        ));
+
+        rule.setProfiles("profile-2");
+
+        rule.execute(helper);
+        // intentionally no assertTrue(...)
+    }
+
+    @Test(expected = EnforcerRuleException.class)
+    public void testOneActiveProfileAndTwoBannedProfilesOneSame()
+            throws EnforcerRuleException {
+        when(project.getActiveProfiles()).thenReturn(Collections.singletonList(
+                createProfile("profile-1")
+        ));
+
+        rule.setProfiles("profile-1,profile-2");
+
+        rule.execute(helper);
+        // intentionally no assertTrue(...)
+    }
+
+    @Test(expected = EnforcerRuleException.class)
+    public void testThreeActiveProfilesAndThreeBannedProfilesThreeSame()
+            throws EnforcerRuleException {
+        when(project.getActiveProfiles()).thenReturn(Arrays.asList(
+                createProfile("profile-1"),
+                createProfile("profile-2"),
+                createProfile("profile-3"),
+                createProfile("profile-4")
+        ));
+
+        rule.setProfiles("profile-2,profile-3,profile-4");
+
+        rule.execute(helper);
+        // intentionally no assertTrue(...)
+    }
+
+    private Profile createProfile(String profileId) {
+        Profile p = new Profile();
+        p.setId(profileId);
+        return p;
+    }
+
+}
diff --git a/pom.xml b/pom.xml
index d1c8898..987821c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -165,6 +165,18 @@
       <email>[hidden email]</email>
       <organization>eBay Inc.</organization>
     </contributor>
+    <contributor>
+      <name>Mikko Koivunalho</name>
+      <email>[hidden email]</email>
+      <url>http://www.linkedin.com/in/mikkokoivunalho/</url>
+      <roles>
+        <role>developer</role>
+      </roles>
+      <timezone>Europe/Stockholm</timezone>
+      <properties>
+        <gitHubUrl>https://github.com/mikkoi</gitHubUrl>
+      </properties>
+    </contributor>
   </contributors>
   <build>
     <pluginManagement>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[hidden email]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]