diff options
Diffstat (limited to 'src/test/java/com/puppycrawl/tools/checkstyle/filters/XpathFilterTest.java')
-rw-r--r-- | src/test/java/com/puppycrawl/tools/checkstyle/filters/XpathFilterTest.java | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/XpathFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/XpathFilterTest.java index f74af313e..4580cd3e4 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/XpathFilterTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/XpathFilterTest.java @@ -61,7 +61,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testMatching() throws Exception { final String xpath = "/CLASS_DEF[@text='InputXpathFilterSuppressByXpath']"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(3, 0, TokenTypes.CLASS_DEF); assertFalse("Event should be rejected", filter.accept(ev)); @@ -71,7 +71,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingTokenType() throws Exception { final String xpath = "//METHOD_DEF[@text='countTokens']"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(3, 0, TokenTypes.CLASS_DEF); assertTrue("Event should be accepted", filter.accept(ev)); @@ -81,7 +81,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingLineNumber() throws Exception { final String xpath = "/CLASS_DEF[@text='InputXpathFilterSuppressByXpath']"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(100, 0, TokenTypes.CLASS_DEF); assertTrue("Event should be accepted", filter.accept(ev)); @@ -91,7 +91,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingColumnNumber() throws Exception { final String xpath = "/CLASS_DEF[@text='InputXpathFilterSuppressByXpath']"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(3, 100, TokenTypes.CLASS_DEF); assertTrue("Event should be accepted", filter.accept(ev)); @@ -103,7 +103,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { + "../..[@text='countTokens']] " + "| //VARIABLE_DEF[@text='someVariable' and ../..[@text='sum']]"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent eventOne = getEvent(5, 8, TokenTypes.VARIABLE_DEF); final TreeWalkerAuditEvent eventTwo = getEvent(10, 4, @@ -120,7 +120,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { final String xpath = "1@#"; try { final Object test = new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, - xpath); + null, xpath); fail("Exception was expected but got " + test); } catch (IllegalStateException ex) { @@ -134,15 +134,15 @@ public class XpathFilterTest extends AbstractModuleTestSupport { final TreeWalkerAuditEvent event = getEvent(15, 8, TokenTypes.VARIABLE_DEF); final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null); - assertTrue("Event should be accepted", filter.accept(event)); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, null); + assertFalse("Event should be accepted", filter.accept(event)); } @Test public void testNullFileName() { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, null, null, null); assertTrue("Event should be accepted", filter.accept(ev)); @@ -152,7 +152,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingFileRegexp() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = - new XpathFilter("NonMatchingRegexp", "Test", null, xpath); + new XpathFilter("NonMatchingRegexp", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(3, 0, TokenTypes.CLASS_DEF); assertTrue("Event should be accepted", filter.accept(ev)); @@ -162,7 +162,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNullLocalizedMessage() { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, file.getName(), null, null); assertTrue("Event should be accepted", filter.accept(ev)); @@ -172,7 +172,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingModuleId() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", "id19", xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, "id19", xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id20", getClass(), null); @@ -185,7 +185,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testMatchingModuleId() throws Exception { final String xpath = "/CLASS_DEF[@text='InputXpathFilterSuppressByXpath']"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", "id19", xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, "id19", xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null); @@ -198,7 +198,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingChecks() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = new XpathFilter("InputXpathFilterSuppressByXpath", - "NonMatchingRegexp", "id19", xpath); + "NonMatchingRegexp", null, "id19", xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null); @@ -211,7 +211,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNonMatchingFileNameModuleIdAndCheck() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", null, null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", null, null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(3, 0, TokenTypes.CLASS_DEF); assertTrue("Event should be accepted", filter.accept(ev)); @@ -221,17 +221,29 @@ public class XpathFilterTest extends AbstractModuleTestSupport { public void testNullModuleIdAndNonMatchingChecks() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = new XpathFilter("InputXpathFilterSuppressByXpath", - "NonMatchingRegexp", null, xpath); + "NonMatchingRegexp", null, null, xpath); final TreeWalkerAuditEvent ev = getEvent(3, 0, TokenTypes.CLASS_DEF); assertTrue("Event should be accepted", filter.accept(ev)); } @Test + public void testDecideByMessage() throws Exception { + final LocalizedMessage message = new LocalizedMessage(0, 0, TokenTypes.CLASS_DEF, "", "", + null, null, null, getClass(), "Test"); + final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), + message, TestUtil.parseFile(file)); + final XpathFilter filter1 = new XpathFilter(null, null, "Test", null, null); + final XpathFilter filter2 = new XpathFilter(null, null, "Bad", null, null); + assertFalse("Message match", filter1.accept(ev)); + assertTrue("Message not match", filter2.accept(ev)); + } + + @Test public void testThrowException() { final String xpath = "/CLASS_DEF[@text='InputXpathFilterSuppressByXpath']"; final XpathFilter filter = - new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, xpath); + new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, null, xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null); @@ -254,7 +266,7 @@ public class XpathFilterTest extends AbstractModuleTestSupport { xpathEvaluator.createExpression("//METHOD_DEF"), xpathEvaluator.createExpression("//VARIABLE_DEF")) .usingGetClass() - .withIgnoredFields("fileRegexp", "checkRegexp", "xpathExpression") + .withIgnoredFields("fileRegexp", "checkRegexp", "messageRegexp", "xpathExpression") .verify(); } |