X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=common%2Fcheckstyle-logging%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fcheckstyle%2FCheckstyleTest.java;h=0fe027b0af2e46d23e622e620ce4af4ceba77b40;hb=refs%2Fchanges%2F11%2F38211%2F10;hp=a12daf92bfa8733c0a5293ba5fc0edec56370ffc;hpb=f9cca8e6d90812f6fe7fc7914699daca77bab846;p=yangtools.git diff --git a/common/checkstyle-logging/src/test/java/org/opendaylight/yangtools/checkstyle/CheckstyleTest.java b/common/checkstyle-logging/src/test/java/org/opendaylight/yangtools/checkstyle/CheckstyleTest.java index a12daf92bf..0fe027b0af 100644 --- a/common/checkstyle-logging/src/test/java/org/opendaylight/yangtools/checkstyle/CheckstyleTest.java +++ b/common/checkstyle-logging/src/test/java/org/opendaylight/yangtools/checkstyle/CheckstyleTest.java @@ -8,16 +8,9 @@ package org.opendaylight.yangtools.checkstyle; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.io.ByteArrayOutputStream; -import java.io.File; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.xml.sax.InputSource; - import com.google.common.collect.Lists; import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.ConfigurationLoader; @@ -26,6 +19,12 @@ import com.puppycrawl.tools.checkstyle.PropertiesExpander; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; +import java.io.ByteArrayOutputStream; +import java.io.File; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.xml.sax.InputSource; public class CheckstyleTest { @@ -35,11 +34,11 @@ public class CheckstyleTest { @Before public void setup() throws CheckstyleException { baos = new ByteArrayOutputStream(); - AuditListener listener = new DefaultLogger(baos, false); + final AuditListener listener = new DefaultLogger(baos, false); - InputSource inputSource = new InputSource(CheckstyleTest.class.getClassLoader().getResourceAsStream( + final InputSource inputSource = new InputSource(CheckstyleTest.class.getClassLoader().getResourceAsStream( "checkstyle-logging.xml")); - Configuration configuration = ConfigurationLoader.loadConfiguration(inputSource, + final Configuration configuration = ConfigurationLoader.loadConfiguration(inputSource, new PropertiesExpander(System.getProperties()), false); checker = new Checker(); @@ -55,20 +54,44 @@ public class CheckstyleTest { @Test public void testLoggerChecks() throws Exception { - verify(CheckLoggingTestClass.class, "15: Logger must be declared as private static final.", "15: Logger name should be LOG.", - "16: Logger might be declared only once.", "16: Logger must be slf4j.", "22: Line contains printStacktrace", - "23: Line contains console output", "24: Line contains console output", - "15: LoggerFactory.getLogger Class argument is incorrect.", "20: Log message contains string concatenation.", - "26: Log message placeholders count is incorrect.", "32: Log message placeholders count is incorrect"); + verify(CheckLoggingTestClass.class, true, + "15: Logger must be declared as private static final", + "15: Logger name should be LOG", + "15: LoggerFactory.getLogger Class argument is incorrect", + "17: Logger might be declared only once", + "16: Logger must be slf4j", + "22: Line contains printStacktrace", + "23: Line contains console output", + "24: Line contains console output", + "26: Log message placeholders count is incorrect", + "32: Log message placeholders count is incorrect", + "41: Log message contains string concatenation"); } - private void verify(final Class testClass, final String... expectedMessages) { + @Test + public void testCodingChecks() throws Exception { + verify(CheckCodingStyleTestClass.class, false, + "9: Line has Windows line delimiter.", + "14: Wrong order for", + "24:1: Line contains a tab character.", + "22: Line has trailing spaces.", + "22: 'ctor def' child have incorrect indentation level 16, expected level should be 8.", + "17:8: Unused import", + "23: Line has trailing spaces."); + } + + private void verify(final Class testClass, final boolean checkCount, final String... expectedMessages) throws CheckstyleException { final String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "java" + File.separator + testClass.getName().replaceAll("\\.", "/") + ".java"; final File testFile = new File(filePath); checker.process(Lists.newArrayList(testFile)); final String output = baos.toString(); + System.out.println(); + if (checkCount) { + final int count = output.split("\n").length - 2; + assertEquals(expectedMessages.length, count); + } for(final String message : expectedMessages) { - assertTrue("Expected message not found: " + message, output.contains(message)); + assertTrue("Expected message not found: " + message + "; output: " + output, output.contains(message)); } } }