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