RuleSetFactory { public RuleSet createRuleSet(String referenceString) throws RuleSetNotFoundException { List<RuleSetReferenceId> references = RuleSetReferenceId.parse(referenceString); if (references.isEmpty()) { throw new RuleSetNotFoundException( "No RuleSetReferenceId can be parsed from the string: <" + referenceString + '>'); } return createRuleSet(references.get(0)); } @Deprecated // to be removed with PMD 7.0.0.  RuleSetFactory(); @Deprecated // to be removed with PMD 7.0.0.  RuleSetFactory(final ClassLoader classLoader, final RulePriority minimumPriority,
                          final boolean warnDeprecated, final boolean enableCompatibility); @Deprecated // to be hidden with PMD 7.0.0.  RuleSetFactory(final ResourceLoader resourceLoader, final RulePriority minimumPriority,
                          final boolean warnDeprecated, final boolean enableCompatibility);  RuleSetFactory(final RuleSetFactory factory, final boolean warnDeprecated); Iterator<RuleSet> getRegisteredRuleSets(); RuleSets createRuleSets(String referenceString); RuleSets createRuleSets(List<RuleSetReferenceId> ruleSetReferenceIds); RuleSet createRuleSet(String referenceString); RuleSet createRuleSet(RuleSetReferenceId ruleSetReferenceId); RuleSet createRuleSetCopy(RuleSet original); RuleSet createNewRuleSet(String name,
                                    String description,
                                    String fileName,
                                    Collection<String> excludePatterns,
                                    Collection<String> includePatterns,
                                    Collection<Rule> rules); RuleSet createSingleRuleRuleSet(final Rule rule);  }
@Test public void testRuleSetFileName() throws RuleSetNotFoundException { RuleSet rs = loadRuleSet(EMPTY_RULESET); assertNull("RuleSet file name not expected", rs.getFileName()); RuleSetFactory rsf = RulesetsFactoryUtils.defaultFactory(); rs = rsf.createRuleSet("net/sourceforge/pmd/TestRuleset1.xml"); assertEquals("wrong RuleSet file name", rs.getFileName(), "net/sourceforge/pmd/TestRuleset1.xml"); }
@Test public void testRefs() throws Exception { RuleSetFactory rsf = RulesetsFactoryUtils.defaultFactory(); RuleSet rs = rsf.createRuleSet("net/sourceforge/pmd/TestRuleset1.xml"); assertNotNull(rs.getRuleByName("TestRuleRef")); }
@Test(expected = RuleSetNotFoundException.class) public void testRuleSetNotFound() throws RuleSetNotFoundException { RuleSetFactory rsf = RulesetsFactoryUtils.defaultFactory(); rsf.createRuleSet("fooooo"); }
@Test public void testReferencePriority() throws RuleSetNotFoundException { ResourceLoader rl = new ResourceLoader(); RuleSetFactory rsf = new RuleSetFactory(rl, RulePriority.LOW, false, true); RuleSet ruleSet = rsf.createRuleSet(createRuleSetReferenceId(REF_INTERNAL_TO_INTERNAL_CHAIN)); assertEquals("Number of Rules", 3, ruleSet.getRules().size()); assertNotNull(ruleSet.getRuleByName("MockRuleName")); assertNotNull(ruleSet.getRuleByName("MockRuleNameRef")); assertNotNull(ruleSet.getRuleByName("MockRuleNameRefRef")); rsf = new RuleSetFactory(rl, RulePriority.MEDIUM_HIGH, false, true); ruleSet = rsf.createRuleSet(createRuleSetReferenceId(REF_INTERNAL_TO_INTERNAL_CHAIN)); assertEquals("Number of Rules", 2, ruleSet.getRules().size()); assertNotNull(ruleSet.getRuleByName("MockRuleNameRef")); assertNotNull(ruleSet.getRuleByName("MockRuleNameRefRef")); rsf = new RuleSetFactory(rl, RulePriority.HIGH, false, true); ruleSet = rsf.createRuleSet(createRuleSetReferenceId(REF_INTERNAL_TO_INTERNAL_CHAIN)); assertEquals("Number of Rules", 1, ruleSet.getRules().size()); assertNotNull(ruleSet.getRuleByName("MockRuleNameRefRef")); rsf = new RuleSetFactory(rl, RulePriority.LOW, false, true); ruleSet = rsf.createRuleSet(createRuleSetReferenceId(REF_INTERNAL_TO_EXTERNAL_CHAIN)); assertEquals("Number of Rules", 3, ruleSet.getRules().size()); assertNotNull(ruleSet.getRuleByName("ExternalRefRuleName")); assertNotNull(ruleSet.getRuleByName("ExternalRefRuleNameRef")); assertNotNull(ruleSet.getRuleByName("ExternalRefRuleNameRefRef")); rsf = new RuleSetFactory(rl, RulePriority.MEDIUM_HIGH, false, true); ruleSet = rsf.createRuleSet(createRuleSetReferenceId(REF_INTERNAL_TO_EXTERNAL_CHAIN)); assertEquals("Number of Rules", 2, ruleSet.getRules().size()); assertNotNull(ruleSet.getRuleByName("ExternalRefRuleNameRef")); assertNotNull(ruleSet.getRuleByName("ExternalRefRuleNameRefRef")); rsf = new RuleSetFactory(rl, RulePriority.HIGH, false, true); ruleSet = rsf.createRuleSet(createRuleSetReferenceId(REF_INTERNAL_TO_EXTERNAL_CHAIN)); assertEquals("Number of Rules", 1, ruleSet.getRules().size()); assertNotNull(ruleSet.getRuleByName("ExternalRefRuleNameRefRef")); }
@Test public void testOverridePriorityLoadWithMinimum() throws RuleSetNotFoundException { RuleSetFactory rsf = new RuleSetFactory(new ResourceLoader(), RulePriority.MEDIUM_LOW, true, true); RuleSet ruleset = rsf.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-minimum-priority.xml"); assertEquals("Number of Rules", 1, ruleset.getRules().size()); assertNull(ruleset.getRuleByName("DummyBasicMockRule")); assertNotNull(ruleset.getRuleByName("SampleXPathRule")); rsf = RulesetsFactoryUtils.defaultFactory(); ruleset = rsf.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-minimum-priority.xml"); assertEquals("Number of Rules", 2, ruleset.getRules().size()); Rule dummyBasicMockRule = ruleset.getRuleByName("DummyBasicMockRule"); assertEquals("Wrong Priority", RulePriority.LOW, dummyBasicMockRule.getPriority()); }
@Test public void testExcludeWithMinimumPriority() throws RuleSetNotFoundException { RuleSetFactory rsf = RulesetsFactoryUtils.createFactory(RulePriority.HIGH, true, true); RuleSet ruleset = rsf.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-minimum-priority-exclusion.xml"); assertEquals("Number of Rules", 0, ruleset.getRules().size()); rsf = RulesetsFactoryUtils.defaultFactory(); ruleset = rsf.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-minimum-priority-exclusion.xml"); assertEquals("Number of Rules", 1, ruleset.getRules().size()); assertNull(ruleset.getRuleByName("DummyBasicMockRule")); assertNotNull(ruleset.getRuleByName("SampleXPathRule")); }
@Test public void testSetPriority() throws RuleSetNotFoundException { ResourceLoader rl = new ResourceLoader(); RuleSetFactory rsf = new RuleSetFactory(rl, RulePriority.MEDIUM_HIGH, false, true); assertEquals(0, rsf.createRuleSet(createRuleSetReferenceId(SINGLE_RULE)).size()); rsf = new RuleSetFactory(rl, RulePriority.MEDIUM_LOW, false, true); assertEquals(1, rsf.createRuleSet(createRuleSetReferenceId(SINGLE_RULE)).size()); }
@Test public void testDeprecatedRuleSetReference() throws RuleSetNotFoundException { RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); RuleSet ruleSet = ruleSetFactory.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-deprecated.xml"); assertEquals(2, ruleSet.getRules().size()); }
@Test(expected = RuleSetNotFoundException.class) public void testBug1202() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<ruleset>\n" + " <rule ref=\"net.sourceforge.pmd.rules.XPathRule\">\n" + " <priority>1</priority>\n" + " <properties>\n" + " <property name=\"xpath\" value=\" + " <property name=\"message\" value=\"Foo\" />\n" + " </properties>\n" + " </rule>\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); ruleSetFactory.createRuleSet(ref); }
@Test public void testEmptyRuleSetFile() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n" + "<ruleset name=\"Custom ruleset\" xmlns=\"http: + " xmlns:xsi=\"http:www.w3.org/2001/XMLSchema-instance\"\n" + " xsi:schemaLocation=\"http: + " <description>PMD Ruleset.</description>\n" + "\n" + " <exclude-pattern>.*Test.*</exclude-pattern>\n" + "\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); RuleSet ruleset = ruleSetFactory.createRuleSet(ref); assertEquals(0, ruleset.getRules().size()); }
@Test public void testEmptyRuleSetReferencedShouldNotBeDeprecated() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n" + "<ruleset name=\"Custom ruleset\" xmlns=\"http: + " xmlns:xsi=\"http:www.w3.org/2001/XMLSchema-instance\"\n" + " xsi:schemaLocation=\"http: + " <description>Ruleset which references a empty ruleset</description>\n" + "\n" + " <rule ref=\"rulesets/dummy/empty-ruleset.xml\" />\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory = new RuleSetFactory(new ResourceLoader(), RulePriority.LOW, true, true); RuleSet ruleset = ruleSetFactory.createRuleSet(ref); assertEquals(0, ruleset.getRules().size()); assertTrue(logging.getLog().isEmpty()); }
@Test(expected = IllegalArgumentException.class) public void testWrongRuleNameReferenced() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\"?>\n" + "<ruleset name=\"Custom ruleset for tests\"\n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <description>Custom ruleset for tests</description>\n" + " <rule ref=\"net/sourceforge/pmd/TestRuleset1.xml/ThisRuleDoesNotExist\"/>\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); ruleSetFactory.createRuleSet(ref); }
@Test public void testRuleReferenceWithNameOverridden() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<ruleset xmlns=\"http: + " xmlns:xsi=\"http: + " name=\"pmd-eclipse\"\n" + " xsi:schemaLocation=\"http: + " <description>PMD Plugin preferences rule set</description>\n" + "\n" + "<rule name=\"OverriddenDummyBasicMockRule\"\n" + " ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\">\n" + "</rule>\n" + "\n" + "</ruleset>"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); RuleSet rs = ruleSetFactory.createRuleSet(ref); Rule r = rs.getRules().toArray(new Rule[1])[0]; assertEquals("OverriddenDummyBasicMockRule", r.getName()); RuleReference ruleRef = (RuleReference) r; assertEquals("DummyBasicMockRule", ruleRef.getRule().getName()); }
@Test public void testWrongRuleNameExcluded() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId( "<?xml version=\"1.0\"?>\n" + "<ruleset name=\"Custom ruleset for tests\"\n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <description>Custom ruleset for tests</description>\n" + " <rule ref=\"net/sourceforge/pmd/TestRuleset1.xml\">\n" + " <exclude name=\"ThisRuleDoesNotExist\"/>\n" + " </rule>\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); RuleSet ruleset = ruleSetFactory.createRuleSet(ref); assertEquals(4, ruleset.getRules().size()); }
@Test public void testExcludeAndImportTwice() throws Exception { RuleSetReferenceId ref1 = createRuleSetReferenceId( "<?xml version=\"1.0\"?>\n" + "<ruleset name=\"Custom ruleset for tests\"\n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <description>Custom ruleset for tests</description>\n" + " <rule ref=\"rulesets/dummy/basic.xml\">\n" + " <exclude name=\"DummyBasicMockRule\"/>\n" + " </rule>\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); RuleSet ruleset = ruleSetFactory.createRuleSet(ref1); assertNull(ruleset.getRuleByName("DummyBasicMockRule")); RuleSetReferenceId ref2 = createRuleSetReferenceId( "<?xml version=\"1.0\"?>\n" + "<ruleset name=\"Custom ruleset for tests\"\n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <description>Custom ruleset for tests</description>\n" + " <rule ref=\"rulesets/dummy/basic.xml\">\n" + " <exclude name=\"DummyBasicMockRule\"/>\n" + " </rule>\n" + " <rule ref=\"rulesets/dummy/basic.xml\"/>\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory2 = RulesetsFactoryUtils.defaultFactory(); RuleSet ruleset2 = ruleSetFactory2.createRuleSet(ref2); assertNotNull(ruleset2.getRuleByName("DummyBasicMockRule")); RuleSetReferenceId ref3 = createRuleSetReferenceId( "<?xml version=\"1.0\"?>\n" + "<ruleset name=\"Custom ruleset for tests\"\n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <description>Custom ruleset for tests</description>\n" + " <rule ref=\"rulesets/dummy/basic.xml\"/>\n" + " <rule ref=\"rulesets/dummy/basic.xml\">\n" + " <exclude name=\"DummyBasicMockRule\"/>\n" + " </rule>\n" + "</ruleset>\n"); RuleSetFactory ruleSetFactory3 = RulesetsFactoryUtils.defaultFactory(); RuleSet ruleset3 = ruleSetFactory3.createRuleSet(ref3); assertNotNull(ruleset3.getRuleByName("DummyBasicMockRule")); }
@Test public void testMissingRuleSetNameIsWarning() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId( "<?xml version=\"1.0\"?>\n" + "<ruleset \n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <description>Custom ruleset for tests</description>\n" + " <rule ref=\"rulesets/dummy/basic.xml\"/>\n" + " </ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); ruleSetFactory.createRuleSet(ref); assertTrue(logging.getLog().contains("RuleSet name is missing.")); }
@Test public void testMissingRuleSetDescriptionIsWarning() throws Exception { RuleSetReferenceId ref = createRuleSetReferenceId( "<?xml version=\"1.0\"?>\n" + "<ruleset name=\"then name\"\n" + " xmlns=\"http: + " xmlns:xsi=\"http: + " xsi:schemaLocation=\"http: + " <rule ref=\"rulesets/dummy/basic.xml\"/>\n" + " </ruleset>\n"); RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); ruleSetFactory.createRuleSet(ref); assertTrue(logging.getLog().contains("RuleSet description is missing.")); }