From 4cdfe5c510b5a9975320aee2f58ebc9357ad6d68 Mon Sep 17 00:00:00 2001 From: rnveach Date: Wed, 18 Nov 2015 11:31:11 -0500 Subject: [PATCH] minor: added verification of google/sun checks in config --- config/suppressions.xml | 2 +- .../tools/checkstyle/internal/CheckUtil.java | 76 +++++++++---------- .../checkstyle/internal/XDocsPagesTest.java | 14 ++++ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/config/suppressions.xml b/config/suppressions.xml index fe1321dd2..3d31d336b 100644 --- a/config/suppressions.xml +++ b/config/suppressions.xml @@ -74,7 +74,7 @@ files="AbstractClassNameCheckTest.java|AbstractTypeAwareCheckTest.java|AbstractJavadocCheckTest.java|AbstractViolationReporterTest.java"/> - + diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java index 980c601e0..e966821e3 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java @@ -28,13 +28,11 @@ import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; import com.google.common.collect.ImmutableSet; import com.google.common.reflect.ClassPath; @@ -46,15 +44,15 @@ public final class CheckUtil { private CheckUtil() { } - public static Set getConfigCheckStyleChecks() throws Exception { + public static Set getConfigCheckStyleChecks() { return getCheckStyleChecksReferencedInConfig("config/checkstyle_checks.xml"); } - public static Set getConfigSunStyleChecks() throws Exception { + public static Set getConfigSunStyleChecks() { return getCheckStyleChecksReferencedInConfig("src/main/resources/sun_checks.xml"); } - public static Set getConfigGoogleStyleChecks() throws Exception { + public static Set getConfigGoogleStyleChecks() { return getCheckStyleChecksReferencedInConfig("src/main/resources/google_checks.xml"); } @@ -64,49 +62,49 @@ public final class CheckUtil { * @param configFilePath * file path of checkstyle_checks.xml. * @return names of checkstyle's checks which are referenced in checkstyle_checks.xml. - * @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies - * the configuration requested. - * @throws IOException if any IO errors occur. - * @throws SAXException if any parse errors occur. */ - private static Set getCheckStyleChecksReferencedInConfig( - String configFilePath) throws Exception { - final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + private static Set getCheckStyleChecksReferencedInConfig(String configFilePath) { + try { + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - // Validations of XML file make parsing too slow, that is why we disable all - // validations. - factory.setNamespaceAware(false); - factory.setValidating(false); - factory.setFeature("http://xml.org/sax/features/namespaces", false); - factory.setFeature("http://xml.org/sax/features/validation", false); - factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", - false); - factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", - false); + // Validations of XML file make parsing too slow, that is why we + // disable all validations. + factory.setNamespaceAware(false); + factory.setValidating(false); + factory.setFeature("http://xml.org/sax/features/namespaces", false); + factory.setFeature("http://xml.org/sax/features/validation", false); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", + false); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", + false); - final DocumentBuilder builder = factory.newDocumentBuilder(); - final Document document = builder.parse(new File(configFilePath)); + final DocumentBuilder builder = factory.newDocumentBuilder(); + final Document document = builder.parse(new File(configFilePath)); - // optional, but recommended - // FYI: - // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how- - // does-it-work - document.getDocumentElement().normalize(); + // optional, but recommended + // FYI: + // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java- + // how-does-it-work + document.getDocumentElement().normalize(); - final NodeList nodeList = document.getElementsByTagName("module"); + final NodeList nodeList = document.getElementsByTagName("module"); - final Set checksReferencedInCheckstyleChecksXml = new HashSet<>(); - for (int i = 0; i < nodeList.getLength(); i++) { - final Node currentNode = nodeList.item(i); - if (currentNode.getNodeType() == Node.ELEMENT_NODE) { - final Element module = (Element) currentNode; - final String checkName = module.getAttribute("name"); - if (!"Checker".equals(checkName) && !"TreeWalker".equals(checkName)) { - checksReferencedInCheckstyleChecksXml.add(checkName); + final Set checksReferencedInCheckstyleChecksXml = new HashSet<>(); + for (int i = 0; i < nodeList.getLength(); i++) { + final Node currentNode = nodeList.item(i); + if (currentNode.getNodeType() == Node.ELEMENT_NODE) { + final Element module = (Element) currentNode; + final String checkName = module.getAttribute("name"); + if (!"Checker".equals(checkName) && !"TreeWalker".equals(checkName)) { + checksReferencedInCheckstyleChecksXml.add(checkName); + } } } + return checksReferencedInCheckstyleChecksXml; + } + catch (Exception exception) { + throw new IllegalStateException(exception); } - return checksReferencedInCheckstyleChecksXml; } /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java index 84ad8f7f0..37003c8d9 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java @@ -47,6 +47,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import com.google.common.collect.ImmutableSet; import com.google.common.io.Files; import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.ConfigurationLoader; @@ -114,6 +115,11 @@ public class XDocsPagesTest { "SuppressionCommentFilter.fileContents" ); + private static final Set SUN_CHECKS = ImmutableSet.copyOf(CheckUtil + .getConfigSunStyleChecks()); + private static final Set GOOGLE_CHECKS = ImmutableSet.copyOf(CheckUtil + .getConfigGoogleStyleChecks()); + @Test public void testAllChecksPresentOnAvailableChecksPage() throws IOException { final String availableChecks = Files.toString(AVAILABLE_CHECKS_FILE, UTF_8); @@ -496,12 +502,20 @@ public class XDocsPagesTest { + "path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+" + "repo%3Acheckstyle%2Fcheckstyle+" + sectionName; + + Assert.assertTrue(fileName + " section '" + sectionName + + "' should be in google_checks.xml or not reference 'Google Style'", + GOOGLE_CHECKS.contains(sectionName)); } else if ("Sun Style".equals(linkText)) { expectedUrl = "https://github.com/search?q=" + "path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+" + "repo%3Acheckstyle%2Fcheckstyle+" + sectionName; + + Assert.assertTrue(fileName + " section '" + sectionName + + "' should be in sun_checks.xml or not reference 'Sun Style'", + SUN_CHECKS.contains(sectionName)); } Assert.assertEquals(fileName + " section '" + sectionName