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 "27: Log message placeholders count is incorrect",
64 "33: Log message placeholders count is incorrect",
65 "42: Log message contains string concatenation");
69 private void verify(final Class<?> testClass, final boolean checkCount, final String... expectedMessages) throws CheckstyleException {
70 final String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "java" + File.separator + testClass.getName().replaceAll("\\.", "/") + ".java";
71 final File testFile = new File(filePath);
72 checker.process(Lists.newArrayList(testFile));
73 final String output = baos.toString();
76 final int count = output.split("\n").length - 2;
77 assertEquals(expectedMessages.length, count);
79 for(final String message : expectedMessages) {
80 assertTrue("Expected message not found: " + message + "; output: " + output, output.contains(message));