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, "15: Logger must be declared as private static final.", "15: Logger name should be LOG.",
58 "15: LoggerFactory.getLogger Class argument is incorrect.",
59 "17: Logger might be declared only once.", "16: Logger must be slf4j.", "22: Line contains printStacktrace",
60 "23: Line contains console output", "24: Line contains console output", "26: Log message placeholders count is incorrect.",
61 "32: Log message placeholders count is incorrect", "41: Log message contains string concatenation.");
65 public void testCodingChecks() {
66 verify(CheckCodingStyleTestClass.class, false, "9: Line has Windows line delimiter.", "14: Wrong order for", "24:1: Line contains a tab character.",
67 "22: Line has trailing spaces.", "22: 'ctor def' child have incorrect indentation level 16, expected level should be 8.", "17:8: Unused import",
68 "23: Line has trailing spaces.");
71 private void verify(final Class<?> testClass, final boolean checkCount, final String... expectedMessages) {
72 final String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "java" + File.separator + testClass.getName().replaceAll("\\.", "/") + ".java";
73 final File testFile = new File(filePath);
74 checker.process(Lists.newArrayList(testFile));
75 final String output = baos.toString();
78 final int count = output.split("\n").length - 2;
79 assertEquals(expectedMessages.length, count);
81 for(final String message : expectedMessages) {
82 assertTrue("Expected message not found: " + message, output.contains(message));