diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java index 244ff01fd1..02981054d6 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java @@ -240,6 +240,7 @@ public abstract class RuleTst { System.out.println(" -> Expected messages: " + test.getExpectedMessages()); System.out.println(" -> Expected line numbers: " + test.getExpectedLineNumbers()); System.out.println("Test Method Name: " + test.getTestMethodName()); + System.out.println(" @org.junit.Test public void " + test.getTestMethodName() + "() {}"); System.out.println(); TextRenderer renderer = new TextRenderer(); renderer.setWriter(new StringWriter()); diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java index 706f5f154b..8161ef4155 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java @@ -155,6 +155,7 @@ public class TestDescriptor { + "_" + getDescription() .replaceAll("\n|\r", "_") - .replaceAll("[\\.\\(\\)]|\\s", "_"); + .replaceAll("[^\\w\\d_$]", "_") + .replaceAll("\\s+", "_"); } } diff --git a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java new file mode 100644 index 0000000000..ae8024ed13 --- /dev/null +++ b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java @@ -0,0 +1,27 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.testframework; + +import org.junit.Assert; +import org.junit.Test; + +import net.sourceforge.pmd.lang.rule.MockRule; + +public class TestDescriptorTest { + @Test + public void testMethodName() { + Assert.assertEquals("MockRule_1_Name", create("Name")); + Assert.assertEquals("MockRule_1_Tests_xyz", create("Tests xyz")); + Assert.assertEquals("MockRule_1_Tests_xyz__false_positive_", create("Tests xyz (false positive)")); + Assert.assertEquals("MockRule_1_Tests_xyz__123", create("Tests xyz #123")); + } + + private String create(String description) { + TestDescriptor descriptor = new TestDescriptor("foo", description, 0, + new MockRule("MockRule", "desc", "msg", "ruleset")); + descriptor.setNumberInDocument(1); + return descriptor.getTestMethodName(); + } +}