Skip to content

Commit

Permalink
Issue checkstyle#11604: allows test structure to make it more easily …
Browse files Browse the repository at this point in the history
…expandable
  • Loading branch information
rnveach committed May 4, 2022
1 parent cacb326 commit c0b7b29
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
package com.google.checkstyle.test.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.checkstyle.base.AbstractItModuleTestSupport;

import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
Expand Down Expand Up @@ -76,11 +73,6 @@ protected ModuleCreationOption findModuleCreationOption(String moduleName) {
return moduleCreationOption;
}

@Override
protected DefaultConfiguration createModuleConfig(Class<?> clazz) {
return new DefaultConfiguration(clazz.getName());
}

/**
* Returns {@link Configuration} instance for the given module name.
* This implementation uses {@link #getModuleConfig(String, String)} method inside.
Expand All @@ -102,54 +94,7 @@ protected static Configuration getModuleConfig(String moduleName) {
* @throws IllegalStateException if there is a problem retrieving the module or config.
*/
protected static Configuration getModuleConfig(String moduleName, String moduleId) {
final Configuration result;
final List<Configuration> configs = getModuleConfigs(moduleName);
if (configs.size() == 1) {
result = configs.get(0);
}
else if (configs.isEmpty()) {
throw new IllegalStateException("no instances of the Module was found: " + moduleName);
}
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);
}
})
.findFirst()
.orElseThrow(() -> new IllegalStateException("problem with module config"));
}

return result;
}

/**
* Returns a list of all {@link Configuration} instances for the given module name.
*
* @param moduleName module name.
* @return {@link Configuration} instance for the given module name.
*/
protected static List<Configuration> getModuleConfigs(String moduleName) {
final List<Configuration> result = new ArrayList<>();
for (Configuration currentConfig : CONFIGURATION.getChildren()) {
if ("TreeWalker".equals(currentConfig.getName())) {
for (Configuration moduleConfig : currentConfig.getChildren()) {
if (moduleName.equals(moduleConfig.getName())) {
result.add(moduleConfig);
}
}
}
else if (moduleName.equals(currentConfig.getName())) {
result.add(currentConfig);
}
}
return result;
return getModuleConfig(CONFIGURATION, moduleName, moduleId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
package com.sun.checkstyle.test.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.checkstyle.base.AbstractItModuleTestSupport;

import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
Expand Down Expand Up @@ -76,11 +73,6 @@ protected ModuleCreationOption findModuleCreationOption(String moduleName) {
return moduleCreationOption;
}

@Override
protected DefaultConfiguration createModuleConfig(Class<?> clazz) {
return new DefaultConfiguration(clazz.getName());
}

/**
* Returns {@link Configuration} instance for the given module name.
* This implementation uses {@link #getModuleConfig(String, String)} method inside.
Expand All @@ -102,54 +94,7 @@ protected static Configuration getModuleConfig(String moduleName) {
* @throws IllegalStateException if there is a problem retrieving the module or config.
*/
protected static Configuration getModuleConfig(String moduleName, String moduleId) {
final Configuration result;
final List<Configuration> configs = getModuleConfigs(moduleName);
if (configs.size() == 1) {
result = configs.get(0);
}
else if (configs.isEmpty()) {
throw new IllegalStateException("no instances of the Module was found: " + moduleName);
}
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);
}
})
.findFirst()
.orElseThrow(() -> new IllegalStateException("problem with module config"));
}

return result;
}

/**
* Returns a list of all {@link Configuration} instances for the given module name.
*
* @param moduleName module name.
* @return {@link Configuration} instance for the given module name.
*/
protected static List<Configuration> getModuleConfigs(String moduleName) {
final List<Configuration> result = new ArrayList<>();
for (Configuration currentConfig : CONFIGURATION.getChildren()) {
if ("TreeWalker".equals(currentConfig.getName())) {
for (Configuration moduleConfig : currentConfig.getChildren()) {
if (moduleName.equals(moduleConfig.getName())) {
result.add(moduleConfig);
}
}
}
else if (moduleName.equals(currentConfig.getName())) {
result.add(currentConfig);
}
}
return result;
return getModuleConfig(CONFIGURATION, moduleName, moduleId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.checkstyle.base;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;

public abstract class AbstractCheckstyleModuleTestSupport extends AbstractItModuleTestSupport {
Expand All @@ -44,9 +43,4 @@ protected ModuleCreationOption findModuleCreationOption(String moduleName) {
return moduleCreationOption;
}

@Override
protected DefaultConfiguration createModuleConfig(Class<?> clazz) {
return new DefaultConfiguration(clazz.getName());
}

}
90 changes: 80 additions & 10 deletions src/it/java/org/checkstyle/base/AbstractItModuleTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.internal.utils.BriefUtLogger;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
Expand Down Expand Up @@ -84,14 +85,6 @@ public enum ModuleCreationOption {
*/
protected abstract ModuleCreationOption findModuleCreationOption(String moduleName);

/**
* Creates {@link DefaultConfiguration} instance for the given module class.
*
* @param clazz module class.
* @return {@link DefaultConfiguration} instance.
*/
protected abstract DefaultConfiguration createModuleConfig(Class<?> clazz);

/**
* Returns test logger.
*
Expand All @@ -101,6 +94,83 @@ protected final BriefUtLogger getBriefUtLogger() {
return new BriefUtLogger(stream);
}

/**
* Creates a default module configuration {@link DefaultConfiguration} for a given object
* of type {@link Class}.
*
* @param clazz a {@link Class} type object.
* @return default module configuration for the given {@link Class} instance.
*/
protected static DefaultConfiguration createModuleConfig(Class<?> clazz) {
return new DefaultConfiguration(clazz.getName());
}

/**
* Returns {@link Configuration} instance for the given module name pulled
* from the {@code masterConfig}.
*
* @param masterConfig The master configuration to examine.
* @param moduleName module name.
* @param moduleId module id.
* @return {@link Configuration} instance for the given module name.
* @throws IllegalStateException if there is a problem retrieving the module
* or config.
*/
protected static Configuration getModuleConfig(Configuration masterConfig, String moduleName,
String moduleId) {
final Configuration result;
final List<Configuration> configs = getModuleConfigs(masterConfig, moduleName);
if (configs.size() == 1) {
result = configs.get(0);
}
else if (configs.isEmpty()) {
throw new IllegalStateException("no instances of the Module was found: " + moduleName);
}
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);
}
})
.findFirst()
.orElseThrow(() -> new IllegalStateException("problem with module config"));
}

return result;
}

/**
* Returns a list of all {@link Configuration} instances for the given
* module name pulled from the {@code masterConfig}.
*
* @param masterConfig The master configuration to examine.
* @param moduleName module name.
* @return {@link Configuration} instance for the given module name.
*/
protected static List<Configuration> getModuleConfigs(Configuration masterConfig,
String moduleName) {
final List<Configuration> result = new ArrayList<>();
for (Configuration currentConfig : masterConfig.getChildren()) {
if ("TreeWalker".equals(currentConfig.getName())) {
for (Configuration moduleConfig : currentConfig.getChildren()) {
if (moduleName.equals(moduleConfig.getName())) {
result.add(moduleConfig);
}
}
}
else if (moduleName.equals(currentConfig.getName())) {
result.add(currentConfig);
}
}
return result;
}

/**
* Creates {@link Checker} instance based on the given {@link Configuration} instance.
*
Expand Down Expand Up @@ -158,9 +228,9 @@ else if (ROOT_MODULE_NAME.equals(moduleConfig.getName())) {
* @return {@link DefaultConfiguration} for the {@link TreeWalker}
* based on the given {@link Configuration} instance.
*/
protected final DefaultConfiguration createTreeWalkerConfig(Configuration config) {
protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
final DefaultConfiguration dc =
new DefaultConfiguration("configuration");
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());
Expand Down

0 comments on commit c0b7b29

Please sign in to comment.