Bump Checkstyle version from 6.2 to 6.16
[yangtools.git] / common / checkstyle-logging / src / test / java / org / opendaylight / yangtools / checkstyle / CheckstyleTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.checkstyle;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
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;
23 import java.io.File;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.xml.sax.InputSource;
28
29 public class CheckstyleTest {
30
31     private Checker checker;
32     private ByteArrayOutputStream baos;
33
34     @Before
35     public void setup() throws CheckstyleException {
36         baos = new ByteArrayOutputStream();
37         final AuditListener listener = new DefaultLogger(baos, false);
38
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);
43
44         checker = new Checker();
45         checker.setModuleClassLoader(Checker.class.getClassLoader());
46         checker.configure(configuration);
47         checker.addListener(listener);
48     }
49
50     @After
51     public void destroy() {
52         checker.destroy();
53     }
54
55     @Test
56     public void testLoggerChecks() throws Exception {
57         verify(CheckLoggingTestClass.class, true,
58                 "15: Logger must be declared as private static final",
59                 "15: Logger name should be LOG",
60                 "15: LoggerFactory.getLogger Class argument is incorrect",
61                 "17: Logger might be declared only once",
62                 "16: Logger must be slf4j",
63                 "22: Line contains printStacktrace",
64                 "23: Line contains console output",
65                 "24: Line contains console output",
66                 "26: Log message placeholders count is incorrect",
67                 "32: Log message placeholders count is incorrect",
68                 "41: Log message contains string concatenation");
69     }
70
71     @Test
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.");
81     }
82
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();
88         System.out.println();
89         if (checkCount) {
90             final int count = output.split("\n").length - 2;
91             assertEquals(expectedMessages.length, count);
92         }
93         for(final String message : expectedMessages) {
94             assertTrue("Expected message not found: " + message + "; output: " + output, output.contains(message));
95         }
96     }
97 }