From 61afeed50313d002dfac205f292ab0b231ce8acf Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Tue, 17 Jan 2017 20:27:07 +0100 Subject: [PATCH] Add rule: banProfiles Signed-off-by: Mikko Johannes Koivunalho --- .../maven/plugins/enforcer/BanProfiles.java | 128 +++++++++++++++ .../src/site/apt/banProfiles.apt.vm | 76 +++++++++ enforcer-rules/src/site/apt/index.apt | 2 + .../src/site/apt/requireActiveProfile.apt.vm | 2 + .../plugins/enforcer/BanProfilesTest.java | 150 ++++++++++++++++++ pom.xml | 12 ++ 6 files changed, 370 insertions(+) create mode 100644 enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanProfiles.java create mode 100644 enforcer-rules/src/site/apt/banProfiles.apt.vm create mode 100644 enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanProfilesTest.java 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 00000000..bc6d67a0 --- /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 bannedActiveProfiles = new ArrayList(); + 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 true if profile is active, otherwise false + */ + private boolean isProfileActive( final MavenProject project, final String profileName ) + { + @SuppressWarnings( "unchecked" ) + final List 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 00000000..5addcad2 --- /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 ! + + Sample Plugin Configuration: + ++---+ + + [...] + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${project.version} + + + enforce-all-profiles-are-activated + + enforce + + + + + Banned profiles activated! + first,second + + + true + + + + + + + [...] + ++---+ diff --git a/enforcer-rules/src/site/apt/index.apt b/enforcer-rules/src/site/apt/index.apt index e3c0a706..6ab25f75 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 f6392239..776ca904 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 00000000..041d65ca --- /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.emptyList()); + + rule.execute(helper); + assertTrue(true); + } + + @Test + public void testNoActiveProfileAndOneBannedProfile() + throws EnforcerRuleException { + when(project.getActiveProfiles()).thenReturn(Collections.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 d1c88988..987821c6 100644 --- a/pom.xml +++ b/pom.xml @@ -165,6 +165,18 @@ wangyf2010@gmail.com eBay Inc. + + Mikko Koivunalho + mikko.koivunalho@iki.fi + http://www.linkedin.com/in/mikkokoivunalho/ + + developer + + Europe/Stockholm + + https://github.com/mikkoi + +