2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.checkstyle;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.base.Charsets;
15 import com.google.common.collect.Lists;
16 import com.google.common.io.Files;
17 import com.puppycrawl.tools.checkstyle.Checker;
18 import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
19 import com.puppycrawl.tools.checkstyle.DefaultLogger;
20 import com.puppycrawl.tools.checkstyle.PropertiesExpander;
21 import com.puppycrawl.tools.checkstyle.api.AuditListener;
22 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
23 import com.puppycrawl.tools.checkstyle.api.Configuration;
24 import java.io.ByteArrayOutputStream;
26 import java.util.List;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.xml.sax.InputSource;
32 public class CheckstyleTest {
34 private Checker checker;
35 private ByteArrayOutputStream baos;
38 public void setup() throws CheckstyleException {
39 baos = new ByteArrayOutputStream();
40 final AuditListener listener = new DefaultLogger(baos, false);
42 final InputSource inputSource = new InputSource(CheckstyleTest.class.getClassLoader().getResourceAsStream(
43 "checkstyle-logging.xml"));
44 final Configuration configuration = ConfigurationLoader.loadConfiguration(inputSource,
45 new PropertiesExpander(System.getProperties()), false);
47 checker = new Checker();
48 checker.setModuleClassLoader(Checker.class.getClassLoader());
49 checker.configure(configuration);
50 checker.addListener(listener);
54 public void destroy() {
59 public void testLoggerChecks() throws Exception {
60 verify(CheckLoggingTestClass.class, true,
61 "16: Logger must be declared as private static final",
62 "16: Logger name should be LOG",
63 "16: LoggerFactory.getLogger Class argument is incorrect",
64 "18: Logger might be declared only once",
65 "17: Logger must be slf4j",
66 "27: Log message placeholders count is incorrect",
67 "36: Log message placeholders count is incorrect",
68 "45: Log message contains string concatenation");
72 public void testLogMessageExtractorCheck() throws Exception {
73 File logMessageReport = LogMessageExtractorCheck.DEFAULT_REPORT_FILE;
74 logMessageReport.delete();
75 verify(CheckLoggingTestClass.class, false);
76 List<String> reportLines = Files.readLines(logMessageReport, Charsets.UTF_8);
77 assertEquals(6, reportLines.size());
78 assertEquals("src/test/java/org/opendaylight/yangtools/checkstyle/CheckLoggingTestClass.java:27:\"foo {} {}\"", reportLines.get(0));
79 // TODO assertEquals("src/test/java/org/opendaylight/yangtools/checkstyle/CheckLoggingTestClass.java:28:\"foo {} bar {}\"", reportLines.get(1));
82 private void verify(final Class<?> testClass, final boolean checkCount, final String... expectedMessages) throws CheckstyleException {
83 final String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "java" + File.separator + testClass.getName().replaceAll("\\.", "/") + ".java";
84 final File testFile = new File(filePath);
85 checker.process(Lists.newArrayList(testFile));
86 final String output = baos.toString();
89 final int count = output.split("\n").length - 2;
90 assertEquals(expectedMessages.length, count);
92 for(final String message : expectedMessages) {
93 assertTrue("Expected message not found: " + message + "; output: " + output, output.contains(message));