Skip to content

Commit

Permalink
minor: changes for module test support
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed May 15, 2022
1 parent cd8ef61 commit 3627e1f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
48 changes: 29 additions & 19 deletions src/it/java/org/checkstyle/base/AbstractItModuleTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,31 @@ else if (moduleId == null) {
throw new IllegalStateException("multiple instances of the same Module are detected");
}
else {
result = configs.stream().filter(conf -> {
try {
return conf.getProperty("id").equals(moduleId);
}
catch (CheckstyleException ex) {
throw new IllegalStateException("problem to get ID attribute from " + conf, ex);
}
})
result = configs.stream().filter(conf -> isSameModuleId(conf, moduleId))
.findFirst()
.orElseThrow(() -> new IllegalStateException("problem with module config"));
}

return result;
}

/**
* Verifies if the configuration's ID matches the expected {@code moduleId}.
*
* @param conf The config to examine.
* @param moduleId The module ID to match against.
* @return {@code true} if it matches.
* @throws IllegalStateException If there is an issue with finding the ID.
*/
private static boolean isSameModuleId(Configuration conf, String moduleId) {
try {
return conf.getProperty("id").equals(moduleId);
}
catch (CheckstyleException ex) {
throw new IllegalStateException("problem to get ID attribute from " + conf, ex);
}
}

/**
* Returns a list of all {@link Configuration} instances for the given
* module name pulled from the {@code masterConfig}.
Expand Down Expand Up @@ -206,15 +216,15 @@ protected final Checker createChecker(Configuration moduleConfig,
checker.setLocaleLanguage(locale.getLanguage());

if (moduleCreationOption == ModuleCreationOption.IN_TREEWALKER) {
final Configuration dc = createTreeWalkerConfig(moduleConfig);
checker.configure(dc);
final Configuration config = createTreeWalkerConfig(moduleConfig);
checker.configure(config);
}
else if (ROOT_MODULE_NAME.equals(moduleConfig.getName())) {
checker.configure(moduleConfig);
}
else {
final Configuration dc = createRootConfig(moduleConfig);
checker.configure(dc);
final Configuration config = createRootConfig(moduleConfig);
checker.configure(config);
}
checker.addListener(getBriefUtLogger());
return checker;
Expand All @@ -229,14 +239,14 @@ else if (ROOT_MODULE_NAME.equals(moduleConfig.getName())) {
* based on the given {@link Configuration} instance.
*/
protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
final DefaultConfiguration dc =
final DefaultConfiguration rootConfig =
new DefaultConfiguration(ROOT_MODULE_NAME);
final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
// make sure that the tests always run with this charset
dc.addProperty("charset", StandardCharsets.UTF_8.name());
dc.addChild(twConf);
rootConfig.addProperty("charset", StandardCharsets.UTF_8.name());
rootConfig.addChild(twConf);
twConf.addChild(config);
return dc;
return rootConfig;
}

/**
Expand All @@ -246,9 +256,9 @@ protected static DefaultConfiguration createTreeWalkerConfig(Configuration confi
* @return {@link DefaultConfiguration} for the given {@link Configuration} instance.
*/
protected static DefaultConfiguration createRootConfig(Configuration config) {
final DefaultConfiguration dc = new DefaultConfiguration(ROOT_MODULE_NAME);
dc.addChild(config);
return dc;
final DefaultConfiguration rootConfig = new DefaultConfiguration(ROOT_MODULE_NAME);
rootConfig.addChild(config);
return rootConfig;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ protected final Checker createChecker(Configuration moduleConfig)
checker.configure(moduleConfig);
}
else {
final Class<?> moduleClass = Class.forName(moduleName);

configureChecker(checker, moduleClass, moduleConfig);
configureChecker(checker, moduleConfig);
}

checker.addListener(getBriefUtLogger());
Expand All @@ -115,20 +113,20 @@ protected final Checker createChecker(Configuration moduleConfig)
* Configures the {@code checker} instance with {@code moduleConfig}.
*
* @param checker {@link Checker} instance.
* @param moduleClass {@link Class} of the module involved.
* @param moduleConfig {@link Configuration} instance.
* @throws Exception if an exception occurs during configuration.
*/
protected void configureChecker(Checker checker, Class<?> moduleClass,
Configuration moduleConfig) throws Exception {
protected void configureChecker(Checker checker, Configuration moduleConfig) throws Exception {
final Class<?> moduleClass = Class.forName(moduleConfig.getName());

if (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(moduleClass)
|| ModuleReflectionUtil.isTreeWalkerFilterModule(moduleClass)) {
final Configuration dc = createTreeWalkerConfig(moduleConfig);
checker.configure(dc);
final Configuration config = createTreeWalkerConfig(moduleConfig);
checker.configure(config);
}
else {
final Configuration dc = createRootConfig(moduleConfig);
checker.configure(dc);
final Configuration config = createRootConfig(moduleConfig);
checker.configure(config);
}
}

Expand All @@ -141,14 +139,14 @@ protected void configureChecker(Checker checker, Class<?> moduleClass,
* based on the given {@link Configuration} instance.
*/
protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
final DefaultConfiguration dc =
final DefaultConfiguration rootConfig =
new DefaultConfiguration(ROOT_MODULE_NAME);
final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
// make sure that the tests always run with this charset
dc.addProperty("charset", StandardCharsets.UTF_8.name());
dc.addChild(twConf);
rootConfig.addProperty("charset", StandardCharsets.UTF_8.name());
rootConfig.addChild(twConf);
twConf.addChild(config);
return dc;
return rootConfig;
}

/**
Expand All @@ -158,11 +156,11 @@ protected static DefaultConfiguration createTreeWalkerConfig(Configuration confi
* @return {@link DefaultConfiguration} for the given {@link Configuration} instance.
*/
protected static DefaultConfiguration createRootConfig(Configuration config) {
final DefaultConfiguration dc = new DefaultConfiguration(ROOT_MODULE_NAME);
final DefaultConfiguration rootConfig = new DefaultConfiguration(ROOT_MODULE_NAME);
if (config != null) {
dc.addChild(config);
rootConfig.addChild(config);
}
return dc;
return rootConfig;
}

/**
Expand Down

0 comments on commit 3627e1f

Please sign in to comment.