You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "juliojgd (via GitHub)" <gi...@apache.org> on 2026/09/28 18:56:59 UTC

[PR] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

juliojgd opened a new pull request, #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039

   Resolves [MENFORCER-433](https://issues.apache.org/jira/browse/MENFORCER-433)
   
   While building this feature, this contribution has been developed and validated inside InditexTech (team `maven-enforcer-maintainers`), with the intention of contributing it upstream as requested by the Jira issue.
   
   **What does this PR add?**
   
   A new built-in rule `banPropertyPrefix` that fails the build when the project declares any Maven property whose name starts with one of the configured banned prefixes. It is the counterpart of `requireProperty` and covers the feature request described in MENFORCER-433: sometimes a property must not be set at all (no longer supported, reserved for other tooling, etc.); until now there was no built-in way to enforce that.
   
   **Example configuration:**
   
   ```xml
   <rules>
     <banPropertyPrefix>
       <prefixes>
         <prefix>amiga.service</prefix>
         <prefix>amiga.common</prefix>
       </prefixes>
     </banPropertyPrefix>
   </rules>
   ```
   
   Each `<prefix>` entry may also contain several comma-separated prefixes:
   
   ```xml
   <rules>
     <banPropertyPrefix>
       <prefixes>
         <prefix>amiga.service,amiga.common</prefix>
       </prefixes>
     </banPropertyPrefix>
   </rules>
   ```
   
   When a banned property is found, the rule fails listing every offending property and its value:
   
   ```
   Banned properties found (prefixes: amiga.service): amiga.service.timeout=5000
   ```
   
   An optional custom `<message>` can override the default failure message.
   
   **Details:**
   
   - Implemented following the existing rule patterns ("standard" rule extending `AbstractStandardEnforcerRule`, JSR-330 `@Named("banPropertyPrefix")`, injected `MavenProject`)
   - Sensible behaviour: empty/blank prefixes are ignored, duplicate prefixes are deduplicated, `stringPropertyNames` traversal, sorted deterministic output in the error message
   - Unit tests added (`TestBanPropertyPrefix`, 10 tests) covering: banned property found, no match, multiple prefixes, comma-separated entries, blank/duplicate trimming, custom message, cache id
   - Site documentation added (`banPropertyPrefix.md.vm`) and listed in the built-in rules index
   
   Followed the "Making and Submitting Changes" guidelines: topic branch created from `master`, logical unit commits, minimal diffs respecting the existing code style, and all module tests passing (277 tests, `mvn test -pl enforcer-rules`).
   
   One note: the same need was recently implemented as a custom rule inside our own framework as well, but we believe having it built-in benefits the whole community — hence this contribution.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "juliojgd (via GitHub)" <gi...@apache.org>.
juliojgd commented on code in PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#discussion_r4131319155


##########
enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/property/TestBanPropertyPrefix.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+package org.apache.maven.enforcer.rules.property;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test class for {@link BanPropertyPrefix}.
+ */
+@ExtendWith(MockitoExtension.class)
+class TestBanPropertyPrefix {
+
+    private static final String BANNED_PREFIX = "secured.system";
+
+    private static final String OTHER_PREFIX = "other.prefix";
+
+    @Mock
+    private MavenProject project;
+
+    @InjectMocks
+    private BanPropertyPrefix rule;
+
+    private Properties properties;
+
+    @Test
+    void shouldFailWhenPropertyWithBannedPrefixExists() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        try {
+            rule.execute();
+            fail("Expected an exception.");
+        } catch (EnforcerRuleException e) {
+            assertThat(e.getMessage()).contains(BANNED_PREFIX + ".timeout").contains(BANNED_PREFIX);
+        }
+    }
+
+    @Test
+    void shouldPassWhenNoPropertyWithBannedPrefixExists() {
+        properties.setProperty(OTHER_PREFIX + ".value", "x");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        try {
+            rule.execute();
+        } catch (EnforcerRuleException e) {
+            fail("This should not throw an exception");

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "juliojgd (via GitHub)" <gi...@apache.org>.
juliojgd commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5887576093

   It seems that the IT failures were previoulsy existent, not related with the changes in my pr @slawekjaranowski 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski merged PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "juliojgd (via GitHub)" <gi...@apache.org>.
juliojgd commented on code in PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#discussion_r4131300564


##########
enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/property/TestBanPropertyPrefix.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+package org.apache.maven.enforcer.rules.property;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test class for {@link BanPropertyPrefix}.
+ */
+@ExtendWith(MockitoExtension.class)
+class TestBanPropertyPrefix {
+
+    private static final String BANNED_PREFIX = "secured.system";
+
+    private static final String OTHER_PREFIX = "other.prefix";
+
+    @Mock
+    private MavenProject project;
+
+    @InjectMocks
+    private BanPropertyPrefix rule;
+
+    private Properties properties;
+
+    @Test
+    void shouldFailWhenPropertyWithBannedPrefixExists() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        try {
+            rule.execute();
+            fail("Expected an exception.");

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5888276866

   > It seems that the IT failures were previoulsy existent, not related with the changes in my pr @slawekjaranowski
   
   exactly, it need to be fixed on master first, after upgrade Maven 4.0.0-rc-6 to rc-7
   
   now we have in assertions:
   
   ```
   [INFO] BUILD FAILURE
   ```
   
   but in 4.0.0-rc-7 and 3.10.0  is
   
   ```
   [ERROR] BUILD FAILURE
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5889624879

   @juliojgd fixed on master, please refresh your PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "juliojgd (via GitHub)" <gi...@apache.org>.
juliojgd commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5886642068

   > @juliojgd - please preserve in PR description at least one:
   > 
   > ```
   > [] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   > [] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   > ```
   > 
   > for PR template
   
   Done, i used the first declaration


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on code in PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#discussion_r4131229025


##########
enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/property/TestBanPropertyPrefix.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+package org.apache.maven.enforcer.rules.property;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test class for {@link BanPropertyPrefix}.
+ */
+@ExtendWith(MockitoExtension.class)
+class TestBanPropertyPrefix {
+
+    private static final String BANNED_PREFIX = "secured.system";
+
+    private static final String OTHER_PREFIX = "other.prefix";
+
+    @Mock
+    private MavenProject project;
+
+    @InjectMocks
+    private BanPropertyPrefix rule;
+
+    private Properties properties;
+
+    @Test
+    void shouldFailWhenPropertyWithBannedPrefixExists() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        try {
+            rule.execute();
+            fail("Expected an exception.");

Review Comment:
   Please use `assertThrows` here and in all similar place



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "juliojgd (via GitHub)" <gi...@apache.org>.
juliojgd commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5907828740

   > @juliojgd fixed on master, please refresh your PR
   
   Refreshed


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5888723207

   - https://github.com/apache/maven-enforcer/pull/1040


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#issuecomment-5886541226

   @juliojgd - please preserve in PR description at least one:
   
   ```
   [] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   [] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   ```
   
   for PR template


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on code in PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#discussion_r4131233756


##########
enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/property/TestBanPropertyPrefix.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+package org.apache.maven.enforcer.rules.property;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test class for {@link BanPropertyPrefix}.
+ */
+@ExtendWith(MockitoExtension.class)
+class TestBanPropertyPrefix {
+
+    private static final String BANNED_PREFIX = "secured.system";
+
+    private static final String OTHER_PREFIX = "other.prefix";
+
+    @Mock
+    private MavenProject project;
+
+    @InjectMocks
+    private BanPropertyPrefix rule;
+
+    private Properties properties;
+
+    @Test
+    void shouldFailWhenPropertyWithBannedPrefixExists() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        try {
+            rule.execute();
+            fail("Expected an exception.");
+        } catch (EnforcerRuleException e) {
+            assertThat(e.getMessage()).contains(BANNED_PREFIX + ".timeout").contains(BANNED_PREFIX);
+        }
+    }
+
+    @Test
+    void shouldPassWhenNoPropertyWithBannedPrefixExists() {
+        properties.setProperty(OTHER_PREFIX + ".value", "x");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        try {
+            rule.execute();
+        } catch (EnforcerRuleException e) {
+            fail("This should not throw an exception");

Review Comment:
   please use `assertDoesNotThrow`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MENFORCER-433] Add banPropertyPrefix rule to ban Maven properties by configured prefixes [maven-enforcer]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on code in PR #1039:
URL: https://github.com/apache/maven-enforcer/pull/1039#discussion_r4149635128


##########
enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/property/TestBanPropertyPrefix.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.
+ */
+package org.apache.maven.enforcer.rules.property;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test class for {@link BanPropertyPrefix}.
+ */
+@ExtendWith(MockitoExtension.class)
+class TestBanPropertyPrefix {
+
+    private static final String BANNED_PREFIX = "secured.system";
+
+    private static final String OTHER_PREFIX = "other.prefix";
+
+    @Mock
+    private MavenProject project;
+
+    @InjectMocks
+    private BanPropertyPrefix rule;
+
+    private Properties properties;
+
+    @Test
+    void shouldFailWhenPropertyWithBannedPrefixExists() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
+        assertThat(e.getMessage()).contains(BANNED_PREFIX + ".timeout").contains(BANNED_PREFIX);
+    }
+
+    @Test
+    void shouldPassWhenNoPropertyWithBannedPrefixExists() {
+        properties.setProperty(OTHER_PREFIX + ".value", "x");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        assertDoesNotThrow(rule::execute);
+    }
+
+    @Test
+    void shouldPassWhenProjectHasNoProperties() {
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        assertDoesNotThrow(rule::execute);
+    }
+
+    @Test
+    void shouldMatchAnyConfiguredPrefix() {
+        properties.setProperty(OTHER_PREFIX + ".value", "x");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Arrays.asList(BANNED_PREFIX, OTHER_PREFIX));
+
+        EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
+        assertThat(e.getMessage()).contains(OTHER_PREFIX + ".value");
+    }
+
+    @Test
+    void shouldListAllOffendingPropertiesSorted() {
+        properties.setProperty(BANNED_PREFIX + ".two", "2");
+        properties.setProperty("keep.me", "ok");
+        properties.setProperty(BANNED_PREFIX + ".one", "1");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+
+        EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
+        assertThat(e.getMessage()).contains(BANNED_PREFIX + ".one", BANNED_PREFIX + ".two");
+        assertThat(e.getMessage()).containsSubsequence(BANNED_PREFIX + ".one", BANNED_PREFIX + ".two");
+        assertThat(e.getMessage()).doesNotContain("keep.me");
+        assertThat(e.getMessage()).doesNotContain("=1", "=2");
+    }
+
+    @Test
+    void shouldSupportCommaSeparatedEntry() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX + "," + OTHER_PREFIX));
+
+        EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
+        assertThat(e.getMessage()).contains(BANNED_PREFIX + ".timeout");
+    }
+
+    @Test
+    void shouldIgnoreBlankAndDuplicatePrefixes() {
+        properties.setProperty(OTHER_PREFIX + ".value", "x");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Arrays.asList(" " + BANNED_PREFIX + " ", " ", " " + BANNED_PREFIX));
+
+        assertDoesNotThrow(rule::execute);
+    }
+
+    @Test
+    void shouldUseCustomMessageWhenConfigured() {
+        properties.setProperty(BANNED_PREFIX + ".timeout", "5000");
+        when(project.getProperties()).thenReturn(properties);
+        rule.setPrefixes(Collections.singletonList(BANNED_PREFIX));
+        rule.setMessage("Custom failure message");
+
+        EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
+        assertThat(e.getMessage()).isEqualTo("Custom failure message");
+    }
+
+    @Test
+    void shouldPassWhenNoPrefixesConfigured() {
+        assertDoesNotThrow(rule::execute);
+    }
+
+    @Test
+    void ruleShouldNotBeCached() {
+        assertThat(rule.getCacheId()).isNull();
+    }
+
+    @org.junit.jupiter.api.BeforeEach

Review Comment:
   import can be used



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org