Skip to content

Commit

Permalink
Issue checkstyle#5157: removed custom create configuration methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Nov 23, 2017
1 parent 83656b1 commit 77cf8a9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 110 deletions.
Expand Up @@ -35,7 +35,6 @@
import org.junit.rules.TemporaryFolder;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
Expand Down Expand Up @@ -335,15 +334,20 @@ public void testUrlInFilePropertyUnableToLoad() throws Exception {
public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
checkConfig.addAttribute("file", getPath("InputImportControlOneRegExp.xml"));

final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
treeWalkerConfig.addChild(checkConfig);

final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
final File cacheFile = temporaryFolder.newFile();
final Checker checker = createMockCheckerWithCache(checkConfig, cacheFile);
checkerConfig.addAttribute("cacheFile", cacheFile.getPath());

final String filePath = temporaryFolder.newFile("EmptyFile.java").getPath();
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checker, filePath, filePath, expected);
verify(checkerConfig, filePath, expected);
// One more time to use cache.
verify(checker, filePath, filePath, expected);
verify(checkerConfig, filePath, expected);

assertTrue("External resourse is not present in cache",
new String(Files.readAllBytes(cacheFile.toPath()),
Expand Down Expand Up @@ -390,22 +394,6 @@ public void testPathRegexDoesntMatchPartially() throws Exception {
verify(checkConfig, getPath("InputImportControl.java"), expected);
}

private Checker createMockCheckerWithCache(DefaultConfiguration checkConfig,
File cacheFile) throws CheckstyleException {
final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
treeWalkerConfig.addChild(checkConfig);

final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyle_checks");
checkerConfig.addChild(treeWalkerConfig);
checkerConfig.addAttribute("cacheFile", cacheFile.getPath());

final Checker checker = new Checker();
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(getBriefUtLogger());
return checker;
}

/**
* Returns String message of original exception that was thrown in
* ImportControlCheck.setUrl or ImportControlCheck.setFile
Expand Down
Expand Up @@ -21,13 +21,11 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.stream.Collectors;

import org.junit.Test;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.Configuration;
Expand Down Expand Up @@ -98,54 +96,52 @@ public void testDefault() throws Exception {
verifySuppressed(filterConfig, suppressed);
}

private void verifySuppressed(Configuration aFilterConfig,
String... aSuppressed) throws Exception {
verify(createChecker(aFilterConfig),
getPath("InputSuppressWarningsFilter.java"),
removeSuppressed(ALL_MESSAGES, aSuppressed));
private void verifySuppressed(Configuration moduleConfig,
String... aSuppressed)
throws Exception {
verifySuppressed(moduleConfig, getPath("InputSuppressWarningsFilter.java"),
ALL_MESSAGES, aSuppressed);
}

@Override
public Checker createChecker(Configuration moduleConfig)
throws Exception {
final DefaultConfiguration checkerConfig =
new DefaultConfiguration("configuration");
final DefaultConfiguration checksConfig =
createModuleConfig(TreeWalker.class);
private void verifySuppressed(Configuration moduleConfig, String fileName,
String[] expectedViolations, String... suppressedViolations) throws Exception {
final DefaultConfiguration holderConfig =
createModuleConfig(SuppressWarningsHolder.class);
holderConfig.addAttribute("aliasList",
"com.puppycrawl.tools.checkstyle.checks.sizes."
+ "ParameterNumberCheck=paramnum");
checksConfig.addChild(holderConfig);

final DefaultConfiguration memberNameCheckConfig =
createModuleConfig(MemberNameCheck.class);
memberNameCheckConfig.addAttribute("id", "ignore");
checksConfig.addChild(memberNameCheckConfig);

final DefaultConfiguration constantNameCheckConfig =
createModuleConfig(ConstantNameCheck.class);
constantNameCheckConfig.addAttribute("id", "");
checksConfig.addChild(constantNameCheckConfig);
checksConfig.addChild(createModuleConfig(ParameterNumberCheck.class));
checksConfig.addChild(createModuleConfig(IllegalCatchCheck.class));

final DefaultConfiguration uncommentedMainCheckConfig =
createModuleConfig(UncommentedMainCheck.class);
uncommentedMainCheckConfig.addAttribute("id", "ignore");
checksConfig.addChild(uncommentedMainCheckConfig);
checksConfig.addChild(createModuleConfig(JavadocTypeCheck.class));
checkerConfig.addChild(checksConfig);

final DefaultConfiguration treewalkerConfig =
createModuleConfig(TreeWalker.class);
treewalkerConfig.addChild(holderConfig);
treewalkerConfig.addChild(memberNameCheckConfig);
treewalkerConfig.addChild(constantNameCheckConfig);
treewalkerConfig.addChild(createModuleConfig(ParameterNumberCheck.class));
treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class));
treewalkerConfig.addChild(uncommentedMainCheckConfig);
treewalkerConfig.addChild(createModuleConfig(JavadocTypeCheck.class));

final DefaultConfiguration checkerConfig =
createRootConfig(treewalkerConfig);
if (moduleConfig != null) {
checkerConfig.addChild(moduleConfig);
}
final Checker checker = new Checker();
final Locale locale = Locale.ROOT;
checker.setLocaleCountry(locale.getCountry());
checker.setLocaleLanguage(locale.getLanguage());
checker.setModuleClassLoader(Thread.currentThread()
.getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(getBriefUtLogger());
return checker;

verify(checkerConfig,
fileName,
removeSuppressed(expectedViolations, suppressedViolations));
}

private static String[] removeSuppressed(String[] from, String... remove) {
Expand All @@ -168,8 +164,7 @@ public void testSuppressById() throws Exception {
"8: Uncommented main method found.",
};

verify(createChecker(filterConfig),
getPath("InputSuppressWarningsFilterById.java"),
removeSuppressed(expectedViolationMessages, suppressedViolationMessages));
verifySuppressed(filterConfig, getPath("InputSuppressWarningsFilterById.java"),
expectedViolationMessages, suppressedViolationMessages);
}
}
Expand Up @@ -26,14 +26,12 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
Expand Down Expand Up @@ -195,41 +193,37 @@ public void testEqualsAndHashCodeOfTagClass() {
EqualsVerifier.forClass(SuppressWithNearbyCommentFilter.Tag.class).usingGetClass().verify();
}

private void verifySuppressed(Configuration filterConfig,
String... suppressed)
private void verifySuppressed(Configuration moduleConfig,
String... aSuppressed)
throws Exception {
verify(createChecker(filterConfig),
getPath("InputSuppressWithNearbyCommentFilter.java"),
removeSuppressed(ALL_MESSAGES, suppressed));
verifySuppressed(moduleConfig, getPath("InputSuppressWithNearbyCommentFilter.java"),
ALL_MESSAGES, aSuppressed);
}

@Override
public Checker createChecker(Configuration moduleConfig)
throws CheckstyleException {
final DefaultConfiguration checkerConfig =
new DefaultConfiguration("configuration");
final DefaultConfiguration checksConfig = createModuleConfig(TreeWalker.class);
private void verifySuppressed(Configuration moduleConfig, String fileName,
String[] expectedViolations, String... suppressedViolations) throws Exception {
final DefaultConfiguration memberNameCheckConfig =
createModuleConfig(MemberNameCheck.class);
memberNameCheckConfig.addAttribute("id", "ignore");
checksConfig.addChild(memberNameCheckConfig);

final DefaultConfiguration constantNameCheckConfig =
createModuleConfig(ConstantNameCheck.class);
constantNameCheckConfig.addAttribute("id", null);
checksConfig.addChild(constantNameCheckConfig);
checksConfig.addChild(createModuleConfig(IllegalCatchCheck.class));
checkerConfig.addChild(checksConfig);

final DefaultConfiguration treewalkerConfig = createModuleConfig(TreeWalker.class);
treewalkerConfig.addChild(memberNameCheckConfig);
treewalkerConfig.addChild(constantNameCheckConfig);
treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class));

if (moduleConfig != null) {
checksConfig.addChild(moduleConfig);
treewalkerConfig.addChild(moduleConfig);
}
final Checker checker = new Checker();
final Locale locale = Locale.ROOT;
checker.setLocaleCountry(locale.getCountry());
checker.setLocaleLanguage(locale.getLanguage());
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(getBriefUtLogger());
return checker;

final DefaultConfiguration checkerConfig = createRootConfig(treewalkerConfig);

verify(checkerConfig,
fileName,
removeSuppressed(expectedViolations, suppressedViolations));
}

private static String[] removeSuppressed(String[] from, String... remove) {
Expand Down Expand Up @@ -339,9 +333,9 @@ public void testSuppressById() throws Exception {
"11:18: Name 'ID' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
};

verify(createChecker(filterConfig),
verifySuppressed(filterConfig,
getPath("InputSuppressWithNearbyCommentFilterById.java"),
removeSuppressed(expectedViolationMessages, suppressedViolationMessages));
expectedViolationMessages, suppressedViolationMessages);
}

@Test
Expand Down
Expand Up @@ -26,15 +26,13 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
Expand Down Expand Up @@ -196,41 +194,36 @@ public void testMessage() throws Exception {
verifySuppressed(filterConfig, suppressed);
}

private void verifySuppressed(Configuration aFilterConfig,
private void verifySuppressed(Configuration moduleConfig,
String... aSuppressed)
throws Exception {
verify(createChecker(aFilterConfig),
getPath("InputSuppressionCommentFilter.java"),
removeSuppressed(ALL_MESSAGES, aSuppressed));
verifySuppressed(moduleConfig, getPath("InputSuppressionCommentFilter.java"),
ALL_MESSAGES, aSuppressed);
}

@Override
public Checker createChecker(Configuration moduleConfig)
throws CheckstyleException {
final DefaultConfiguration checkerConfig =
new DefaultConfiguration("configuration");
final DefaultConfiguration checksConfig = createModuleConfig(TreeWalker.class);
private void verifySuppressed(Configuration moduleConfig, String fileName,
String[] expectedViolations, String... suppressedViolations) throws Exception {
final DefaultConfiguration memberNameCheckConfig =
createModuleConfig(MemberNameCheck.class);
memberNameCheckConfig.addAttribute("id", "ignore");
checksConfig.addChild(memberNameCheckConfig);

final DefaultConfiguration constantNameCheckConfig =
createModuleConfig(ConstantNameCheck.class);
constantNameCheckConfig.addAttribute("id", null);
checksConfig.addChild(constantNameCheckConfig);
checksConfig.addChild(createModuleConfig(IllegalCatchCheck.class));
checkerConfig.addChild(checksConfig);

final DefaultConfiguration treewalkerConfig = createModuleConfig(TreeWalker.class);
treewalkerConfig.addChild(memberNameCheckConfig);
treewalkerConfig.addChild(constantNameCheckConfig);
treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class));

if (moduleConfig != null) {
checksConfig.addChild(moduleConfig);
treewalkerConfig.addChild(moduleConfig);
}
final Checker checker = new Checker();
final Locale locale = Locale.ROOT;
checker.setLocaleCountry(locale.getCountry());
checker.setLocaleLanguage(locale.getLanguage());
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(getBriefUtLogger());
return checker;

final DefaultConfiguration checkerConfig = createRootConfig(treewalkerConfig);

verify(checkerConfig, fileName,
removeSuppressed(expectedViolations, suppressedViolations));
}

private static String[] removeSuppressed(String[] from, String... remove) {
Expand Down Expand Up @@ -319,9 +312,8 @@ public void testSuppressById() throws Exception {
"15:18: Name 'ID' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
};

verify(createChecker(filterConfig),
getPath("InputSuppressionCommentFilterSuppressById.java"),
removeSuppressed(expectedViolationMessages, suppressedViolationMessages));
verifySuppressed(filterConfig, getPath("InputSuppressionCommentFilterSuppressById.java"),
expectedViolationMessages, suppressedViolationMessages);
}

@Test
Expand Down

0 comments on commit 77cf8a9

Please sign in to comment.