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.collect.Lists;
15 import com.puppycrawl.tools.checkstyle.Checker;
16 import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
17 import com.puppycrawl.tools.checkstyle.DefaultLogger;
18 import com.puppycrawl.tools.checkstyle.PropertiesExpander;
19 import com.puppycrawl.tools.checkstyle.api.AuditListener;
20 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
21 import com.puppycrawl.tools.checkstyle.api.Configuration;
22 import java.io.ByteArrayOutputStream;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.xml.sax.InputSource;
29 public class CheckstyleTest {
31 private Checker checker;
32 private ByteArrayOutputStream baos;
35 public void setup() throws CheckstyleException {
36 baos = new ByteArrayOutputStream();
37 final AuditListener listener = new DefaultLogger(baos, false);
39 final InputSource inputSource = new InputSource(CheckstyleTest.class.getClassLoader().getResourceAsStream(
40 "checkstyle-logging.xml"));
41 final Configuration configuration = ConfigurationLoader.loadConfiguration(inputSource,
42 new PropertiesExpander(System.getProperties()), false);
44 checker = new Checker();
45 checker.setModuleClassLoader(Checker.class.getClassLoader());
46 checker.configure(configuration);
47 checker.addListener(listener);
51 public void destroy() {
56 public void testLoggerChecks() throws Exception {
57 verify(CheckLoggingTestClass.class, true,
58 "16: Logger must be declared as private static final",
59 "16: Logger name should be LOG",
60 "16: LoggerFactory.getLogger Class argument is incorrect",
61 "18: Logger might be declared only once",
62 "17: Logger must be slf4j",
63 "23: Line contains printStacktrace",
64 "24: Line contains console output",
65 "25: Line contains console output",
66 "27: Log message placeholders count is incorrect",
67 "33: Log message placeholders count is incorrect",
68 "42: Log message contains string concatenation");
72 public void testCodingChecks() throws Exception {
73 verify(CheckCodingStyleTestClass.class, false,
74 "9: Line has Windows line delimiter.",
75 "14: Wrong order for",
76 "24:1: Line contains a tab character.",
77 "22: Line has trailing spaces.",
78 "22: 'ctor def' child have incorrect indentation level 16, expected level should be 8.",
79 "17:8: Unused import",
80 "23: Line has trailing spaces.");
83 private void verify(final Class<?> testClass, final boolean checkCount, final String... expectedMessages) throws CheckstyleException {
84 final String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "java" + File.separator + testClass.getName().replaceAll("\\.", "/") + ".java";
85 final File testFile = new File(filePath);
86 checker.process(Lists.newArrayList(testFile));
87 final String output = baos.toString();
90 final int count = output.split("\n").length - 2;
91 assertEquals(expectedMessages.length, count);
93 for(final String message : expectedMessages) {
94 assertTrue("Expected message not found: " + message + "; output: " + output, output.contains(message));