forked from sevntu-checkstyle/sevntu.checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sevntu-checkstyle#134 from daniilyar/master
Refactoring of BaseCheckTestsSupport and better support for UTs
- Loading branch information
Showing
4 changed files
with
292 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
271 changes: 146 additions & 125 deletions
271
sevntu-checks/src/test/java/com/github/sevntu/checkstyle/BaseCheckTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,161 @@ | ||
package com.github.sevntu.checkstyle; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import com.google.common.collect.Lists; | ||
import com.puppycrawl.tools.checkstyle.Checker; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.DefaultLogger; | ||
import com.puppycrawl.tools.checkstyle.TreeWalker; | ||
import com.puppycrawl.tools.checkstyle.api.AuditEvent; | ||
import com.puppycrawl.tools.checkstyle.api.Configuration; | ||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.LineNumberReader; | ||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.util.Collections; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Properties; | ||
|
||
public abstract class BaseCheckTestSupport | ||
import org.junit.Assert; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.puppycrawl.tools.checkstyle.Checker; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.DefaultLogger; | ||
import com.puppycrawl.tools.checkstyle.TreeWalker; | ||
import com.puppycrawl.tools.checkstyle.api.AuditEvent; | ||
import com.puppycrawl.tools.checkstyle.api.Configuration; | ||
|
||
public abstract class BaseCheckTestSupport extends Assert | ||
{ | ||
/** a brief logger that only display info about errors */ | ||
protected static class BriefLogger | ||
extends DefaultLogger | ||
{ | ||
public BriefLogger(OutputStream out) | ||
{ | ||
super(out, true); | ||
} | ||
@Override | ||
public void auditStarted(AuditEvent evt) {} | ||
@Override | ||
public void fileFinished(AuditEvent evt) {} | ||
@Override | ||
public void fileStarted(AuditEvent evt) {} | ||
} | ||
|
||
protected final ByteArrayOutputStream mBAOS = new ByteArrayOutputStream(); | ||
protected final PrintStream mStream = new PrintStream(mBAOS); | ||
protected final Properties mProps = new Properties(); | ||
|
||
public static DefaultConfiguration createCheckConfig(Class<?> aClazz) | ||
{ | ||
final DefaultConfiguration checkConfig = | ||
new DefaultConfiguration(aClazz.getName()); | ||
return checkConfig; | ||
} | ||
|
||
protected Checker createChecker(Configuration aCheckConfig) | ||
throws Exception | ||
{ | ||
final DefaultConfiguration dc = createCheckerConfig(aCheckConfig); | ||
final Checker c = new Checker(); | ||
// make sure the tests always run with english error messages | ||
// so the tests don't fail in supported locales like german | ||
final Locale locale = Locale.ENGLISH; | ||
c.setLocaleCountry(locale.getCountry()); | ||
c.setLocaleLanguage(locale.getLanguage()); | ||
c.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); | ||
c.configure(dc); | ||
c.addListener(new BriefLogger(mStream)); | ||
return c; | ||
} | ||
|
||
protected DefaultConfiguration createCheckerConfig(Configuration aConfig) | ||
{ | ||
final DefaultConfiguration dc = | ||
new DefaultConfiguration("configuration"); | ||
final DefaultConfiguration twConf = createCheckConfig(TreeWalker.class); | ||
// make sure that the tests always run with this charset | ||
dc.addAttribute("charset", "iso-8859-1"); | ||
dc.addChild(twConf); | ||
twConf.addChild(aConfig); | ||
return dc; | ||
} | ||
|
||
protected String getPath(String aFilename) | ||
throws IOException | ||
{ | ||
return new File(getClass().getResource(aFilename).getPath()).getCanonicalPath(); | ||
// final File f = new File(System.getProperty("testinputs.dir"), | ||
// aFilename); | ||
// return f.getCanonicalPath(); | ||
} | ||
|
||
protected void verify(Configuration aConfig, String aFileName, String[] aExpected) | ||
throws Exception | ||
{ | ||
verify(createChecker(aConfig), aFileName, aFileName, aExpected); | ||
} | ||
|
||
protected void verify(Checker aC, String aFileName, String[] aExpected) | ||
throws Exception | ||
{ | ||
verify(aC, aFileName, aFileName, aExpected); | ||
} | ||
|
||
protected void verify(Checker aC, | ||
String aProcessedFilename, | ||
String aMessageFileName, | ||
String[] aExpected) | ||
throws Exception | ||
{ | ||
verify(aC, | ||
new File[] {new File(aProcessedFilename)}, | ||
aMessageFileName, aExpected); | ||
} | ||
|
||
protected void verify(Checker aC, | ||
File[] aProcessedFiles, | ||
String aMessageFileName, | ||
String[] aExpected) | ||
throws Exception | ||
{ | ||
mStream.flush(); | ||
final List<File> theFiles = Lists.newArrayList(); | ||
Collections.addAll(theFiles, aProcessedFiles); | ||
final int errs = aC.process(theFiles); | ||
|
||
// process each of the lines | ||
final ByteArrayInputStream bais = | ||
new ByteArrayInputStream(mBAOS.toByteArray()); | ||
final LineNumberReader lnr = | ||
new LineNumberReader(new InputStreamReader(bais)); | ||
|
||
for (int i = 0; i < aExpected.length; i++) { | ||
final String expected = aMessageFileName + ":" + aExpected[i]; | ||
final String actual = lnr.readLine(); | ||
assertEquals("error message " + i, expected, actual); | ||
} | ||
|
||
assertEquals("unexpected output: " + lnr.readLine(), | ||
aExpected.length, errs); | ||
aC.destroy(); | ||
} | ||
/** A brief logger that only display errors */ | ||
protected static class BriefLogger extends DefaultLogger | ||
{ | ||
|
||
public BriefLogger(OutputStream out) | ||
{ | ||
super(out, true); | ||
} | ||
|
||
@Override | ||
public void auditStarted(AuditEvent evt) { | ||
} | ||
|
||
@Override | ||
public void fileFinished(AuditEvent evt) { | ||
} | ||
|
||
@Override | ||
public void fileStarted(AuditEvent evt) { | ||
} | ||
} | ||
|
||
private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
private final PrintStream printStream = new PrintStream(baos); | ||
|
||
public static DefaultConfiguration createCheckConfig(Class<?> aClazz) | ||
{ | ||
return new DefaultConfiguration(aClazz.getName()); | ||
} | ||
|
||
protected void verify(Configuration aConfig, String aFileName, String[] aExpected) | ||
throws Exception | ||
{ | ||
verify(createChecker(aConfig), aFileName, aFileName, aExpected); | ||
} | ||
|
||
protected void verify(Checker aC, String aFileName, String[] aExpected) throws Exception | ||
{ | ||
verify(aC, aFileName, aFileName, aExpected); | ||
} | ||
|
||
protected void verify(Checker aC, String aProcessedFilename, String aMessageFileName, | ||
String[] aExpected) throws Exception | ||
{ | ||
verify(aC, new File[] { new File(aProcessedFilename) }, aMessageFileName, aExpected); | ||
} | ||
|
||
protected void verify(Checker checker, File[] aProcessedFiles, String aMessageFileName, | ||
String[] aExpected) throws Exception | ||
{ | ||
printStream.flush(); | ||
List<File> testInputFiles = Lists.newArrayList(aProcessedFiles); | ||
int foundErrorsCount = checker.process(testInputFiles); | ||
|
||
// Process each output line | ||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(bais)); | ||
|
||
try { | ||
for (int i = 0; i < aExpected.length; i++) { | ||
final String expected = aMessageFileName + ":" + aExpected[i]; | ||
final String actual = br.readLine(); | ||
assertEquals("error message " + i, expected, actual); | ||
} | ||
|
||
assertEquals("Check generated unexpected warning: " + br.readLine(), aExpected.length, foundErrorsCount); | ||
checker.destroy(); | ||
} finally { | ||
br.close(); | ||
bais.close(); | ||
} | ||
} | ||
|
||
protected Checker createChecker(Configuration aCheckConfig) throws Exception | ||
{ | ||
Checker checker = new Checker(); | ||
// make sure the tests always run with english error messages | ||
// so the tests don't fail in supported locales like german | ||
Locale locale = Locale.ENGLISH; | ||
checker.setLocaleCountry(locale.getCountry()); | ||
checker.setLocaleLanguage(locale.getLanguage()); | ||
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); | ||
|
||
DefaultConfiguration defaultConfig = createCheckerConfig(aCheckConfig); | ||
checker.configure(defaultConfig); | ||
|
||
checker.addListener(new BriefLogger(printStream)); | ||
return checker; | ||
} | ||
|
||
protected DefaultConfiguration createCheckerConfig(Configuration aConfig) | ||
{ | ||
DefaultConfiguration result = new DefaultConfiguration("configuration"); | ||
DefaultConfiguration treeWalkerConfig = createCheckConfig(TreeWalker.class); | ||
// make sure that the tests always run with this charset | ||
result.addAttribute("charset", "iso-8859-1"); | ||
result.addChild(treeWalkerConfig); | ||
treeWalkerConfig.addChild(aConfig); | ||
return result; | ||
} | ||
|
||
protected String getPath(String aFilename) | ||
{ | ||
String result = null; | ||
try { | ||
URL resource = getClass().getResource(aFilename); | ||
if (resource == null) { | ||
throw new RuntimeException(String.format("Resource '%s' can NOT be found " | ||
+ "(does not exist or just not visible for current classloader)", | ||
aFilename)); | ||
} else { | ||
result = new File(resource.getPath()).getCanonicalPath(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error while getting path for resource: " + aFilename, e); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Gets the check message 'as is' from appropriate 'messages.properties' file. | ||
* @param messageKey the key of message in 'messages.properties' file. | ||
*/ | ||
public String getCheckMessage(String messageKey) { | ||
Properties pr = new Properties(); | ||
try { | ||
pr.load(getClass().getResourceAsStream("messages.properties")); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
return pr.getProperty(messageKey); | ||
} | ||
|
||
} |
Oops, something went wrong.