View Javadoc
1   package com.puppycrawl.tools.checkstyle;
2   
3   import static java.text.MessageFormat.format;
4   import static org.junit.Assert.assertEquals;
5   
6   import com.google.common.collect.Lists;
7   import com.puppycrawl.tools.checkstyle.api.AuditEvent;
8   import com.puppycrawl.tools.checkstyle.api.Configuration;
9   import java.io.ByteArrayInputStream;
10  import java.io.ByteArrayOutputStream;
11  import java.io.File;
12  import java.io.IOException;
13  import java.io.InputStreamReader;
14  import java.io.LineNumberReader;
15  import java.io.OutputStream;
16  import java.io.PrintStream;
17  import java.util.Collections;
18  import java.util.List;
19  import java.util.Locale;
20  import java.util.Properties;
21  
22  public abstract class BaseCheckTestSupport {
23      /**
24       * a brief logger that only display info about errors
25       */
26      protected static class BriefLogger
27              extends DefaultLogger {
28          public BriefLogger(OutputStream out) {
29              super(out, true);
30          }
31  
32          @Override
33          public void auditStarted(AuditEvent evt) {
34          }
35  
36          @Override
37          public void fileFinished(AuditEvent evt) {
38          }
39  
40          @Override
41          public void fileStarted(AuditEvent evt) {
42          }
43      }
44  
45      protected final ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
46      protected final PrintStream stream = new PrintStream(BAOS);
47      protected final Properties props = new Properties();
48  
49      public static DefaultConfiguration createCheckConfig(Class<?> clazz) {
50          final DefaultConfiguration checkConfig =
51                  new DefaultConfiguration(clazz.getName());
52          return checkConfig;
53      }
54  
55      protected Checker createChecker(Configuration checkConfig)
56              throws Exception {
57          final DefaultConfiguration dc = createCheckerConfig(checkConfig);
58          final Checker c = new Checker();
59          // make sure the tests always run with english error messages
60          // so the tests don't fail in supported locales like german
61          final Locale locale = Locale.ENGLISH;
62          c.setLocaleCountry(locale.getCountry());
63          c.setLocaleLanguage(locale.getLanguage());
64          c.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
65          c.configure(dc);
66          c.addListener(new BriefLogger(stream));
67          return c;
68      }
69  
70      protected DefaultConfiguration createCheckerConfig(Configuration config) {
71          final DefaultConfiguration dc =
72                  new DefaultConfiguration("configuration");
73          final DefaultConfiguration twConf = createCheckConfig(TreeWalker.class);
74          // make sure that the tests always run with this charset
75          dc.addAttribute("charset", "UTF-8");
76          dc.addChild(twConf);
77          twConf.addChild(config);
78          return dc;
79      }
80  
81      protected static String getPath(String filename)
82              throws IOException {
83          return new File("src/test/resources/com/puppycrawl/tools/checkstyle/" + filename).getCanonicalPath();
84      }
85  
86      protected static String getSrcPath(String filename) throws IOException {
87  
88          return new File("src/test/java/com/puppycrawl/tools/checkstyle/" + filename).getCanonicalPath();
89      }
90  
91      protected void verify(Configuration aConfig, String fileName, String[] expected)
92              throws Exception {
93          verify(createChecker(aConfig), fileName, fileName, expected);
94      }
95  
96      protected void verify(Checker c, String fileName, String[] expected)
97              throws Exception {
98          verify(c, fileName, fileName, expected);
99      }
100 
101     protected void verify(Checker c,
102                           String processedFilename,
103                           String messageFileName,
104                           String[] expected)
105             throws Exception {
106         verify(c,
107                 new File[]{new File(processedFilename)},
108                 messageFileName, expected);
109     }
110 
111     protected void verify(Checker c,
112                           File[] processedFiles,
113                           String messageFileName,
114                           String[] expected)
115             throws Exception {
116         stream.flush();
117         final List<File> theFiles = Lists.newArrayList();
118         Collections.addAll(theFiles, processedFiles);
119         final int errs = c.process(theFiles);
120 
121         // process each of the lines
122         final ByteArrayInputStream bais =
123                 new ByteArrayInputStream(BAOS.toByteArray());
124         final LineNumberReader lnr =
125                 new LineNumberReader(new InputStreamReader(bais));
126 
127         for (int i = 0; i < expected.length; i++) {
128             final String expectedResult = messageFileName + ":" + expected[i];
129             final String actual = lnr.readLine();
130             assertEquals("error message " + i, expectedResult, actual);
131         }
132 
133         assertEquals("unexpected output: " + lnr.readLine(),
134                 expected.length, errs);
135         c.destroy();
136     }
137 
138     /**
139      * Gets the check message 'as is' from appropriate 'messages.properties'
140      * file.
141      *
142      * @param messageKey the key of message in 'messages.properties' file.
143      * @param arguments  the arguments of message in 'messages.properties' file.
144      */
145     public String getCheckMessage(String messageKey, Object... arguments) {
146         Properties pr = new Properties();
147         try {
148             pr.load(getClass().getResourceAsStream("messages.properties"));
149         } catch (IOException e) {
150             return null;
151         }
152         return format(pr.getProperty(messageKey), arguments);
153     }
154 }