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