From b3157825d8b16240be8ee9713b21fbd79d0955aa Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 5 Oct 2024 11:00:13 +0200 Subject: [PATCH] [java] Rename rule DefaultLabelNotLastInSwitch - as it applies to both switch statements and switch expressions - extend the test cases to cover new java syntax Note: For patterns in switch, the java compiler already makes sure, that default is the last case. --- docs/pages/release_notes.md | 21 +-- .../resources/category/java/bestpractices.xml | 12 +- .../resources/rulesets/java/quickstart.xml | 2 +- ...a => DefaultLabelNotLastInSwitchTest.java} | 2 +- .../xml/DefaultLabelNotLastInSwitch.xml | 136 ++++++++++++++++++ .../xml/DefaultLabelNotLastInSwitchStmt.xml | 59 -------- 6 files changed, 157 insertions(+), 75 deletions(-) rename pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/{DefaultLabelNotLastInSwitchStmtTest.java => DefaultLabelNotLastInSwitchTest.java} (78%) create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitch.xml delete mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitchStmt.xml diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 197b7a4aec..00fa297731 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -17,15 +17,16 @@ This is a {{ site.pmd.release_type }} release. ### 🌟 Rule Changes #### Renamed Rules -Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called -after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG. - -* {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} (Java Best Practices) has been renamed from `JUnitAssertionsShouldIncludeMessage`. -* {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} (Java Best Practices) has been renamed from `JUnitTestContainsTooManyAsserts`. -* {% rule java/bestpractices/UnitTestShouldIncludeAssert %} (Java Best Practices) has been renamed from `JUnitTestsShouldIncludeAssert`. -* {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseAfterAnnotation`. -* {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseBeforeAnnotation`. -* {% rule java/bestpractices/UnitTestShouldUseTestAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseTestAnnotation`. +* Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called + after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG. + * {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} (Java Best Practices) has been renamed from `JUnitAssertionsShouldIncludeMessage`. + * {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} (Java Best Practices) has been renamed from `JUnitTestContainsTooManyAsserts`. + * {% rule java/bestpractices/UnitTestShouldIncludeAssert %} (Java Best Practices) has been renamed from `JUnitTestsShouldIncludeAssert`. + * {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseAfterAnnotation`. + * {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseBeforeAnnotation`. + * {% rule java/bestpractices/UnitTestShouldUseTestAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseTestAnnotation`. +* {% rule java/bestpractices/DefaultLabelNotLastInSwitch %} (Java Best Practices) has been renamed from `DefaultLabelNotLastInSwitch`, as it also + applies to Switch Expressions. The old rule names still work but are deprecated. @@ -43,7 +44,7 @@ The old rule names still work but are deprecated. * The old rule name `JUnitAssertionsShouldIncludeMessage` has been deprecated. Use the new name {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} instead. * The old rule name `JUnitTestContainsTooManyAsserts` has been deprecated. Use the new name {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} instead. * The old rule name `JUnitTestsShouldIncludeAssert` has been deprecated. Use the new name {% rule java/bestpractices/UnitTestShouldIncludeAssert %} instead. - + * The old rule name `DefaultLabelNotLastInSwitch` has been deprecated. Use the new name {% rule java/bestpractices/DefaultLabelNotLastInSwitch %} instead. ### ✨ Merged pull requests * [#4965](https://github.com/pmd/pmd/pull/4965): \[java] Rename JUnit rules with overly restrictive names - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod) diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 03b23ad1ca..834651971c 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -457,14 +457,18 @@ public interface YetAnotherConstantInterface { - + + + externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#defaultlabelnotlastinswitch"> -By convention, the default label should be the last label in a switch statement. +By convention, the default label should be the last label in a switch statement or switch expression. + +Note: This rule has been renamed from "DefaultLabelNotLastInSwitchStmt" with PMD 7.7.0. 3 diff --git a/pmd-java/src/main/resources/rulesets/java/quickstart.xml b/pmd-java/src/main/resources/rulesets/java/quickstart.xml index 3ac03a493c..a195090891 100644 --- a/pmd-java/src/main/resources/rulesets/java/quickstart.xml +++ b/pmd-java/src/main/resources/rulesets/java/quickstart.xml @@ -18,7 +18,7 @@ - + diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/DefaultLabelNotLastInSwitchStmtTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/DefaultLabelNotLastInSwitchTest.java similarity index 78% rename from pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/DefaultLabelNotLastInSwitchStmtTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/DefaultLabelNotLastInSwitchTest.java index b316a1881f..a012153e0e 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/DefaultLabelNotLastInSwitchStmtTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/DefaultLabelNotLastInSwitchTest.java @@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices; import net.sourceforge.pmd.test.PmdRuleTst; -class DefaultLabelNotLastInSwitchStmtTest extends PmdRuleTst { +class DefaultLabelNotLastInSwitchTest extends PmdRuleTst { // no additional unit tests } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitch.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitch.xml new file mode 100644 index 0000000000..5212dbe022 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitch.xml @@ -0,0 +1,136 @@ + + + + + ok + 0 + + + + + bad + 1 + + + + + ok, no default + 0 + + + + + switch expression, ok + 0 + + + + + switch expression, not ok + 1 + + + + + switch arrow, ok + 0 + System.out.println("a"); + default -> System.out.println("default"); + } + } +} + ]]> + + + + switch arrow, not ok + 1 + System.out.println("a"); + default -> System.out.println("default"); + case 2 -> System.out.println("b"); + } + } +} + ]]> + + + + switch with pattern and default case, ok + 0 + + + diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitchStmt.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitchStmt.xml deleted file mode 100644 index df892c6141..0000000000 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/DefaultLabelNotLastInSwitchStmt.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - ok - 0 - - - - - bad - 1 - - - - - ok, no default - 0 - - -