use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.
the class AllChecksTest method testAllChecksWithDefaultConfiguration.
@Test
public void testAllChecksWithDefaultConfiguration() throws Exception {
final String inputFilePath = getPath("InputDefaultConfig.java");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
for (Class<?> check : CheckUtil.getCheckstyleChecks()) {
final DefaultConfiguration checkConfig = createCheckConfig(check);
final Checker checker;
if (AbstractCheck.class.isAssignableFrom(check)) {
// Checks which have Check as a parent.
if (check.equals(ImportControlCheck.class)) {
// ImportControlCheck must have the import control configuration file to avoid
// violation.
checkConfig.addAttribute("file", getPath("import-control_complete.xml"));
}
checker = createChecker(checkConfig);
} else {
// Checks which have TreeWalker as a parent.
BaseCheckTestSupport testSupport = new BaseCheckTestSupport() {
@Override
protected DefaultConfiguration createCheckerConfig(Configuration config) {
final DefaultConfiguration dc = new DefaultConfiguration("root");
dc.addChild(checkConfig);
return dc;
}
};
checker = testSupport.createChecker(checkConfig);
}
verify(checker, inputFilePath, expected);
}
}
use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.
the class XdocsPagesTest method validateCheckstyleXml.
private static void validateCheckstyleXml(String fileName, String code, String unserializedSource) throws IOException, CheckstyleException {
// can't process non-existent examples, or out of context snippets
if (!code.contains("com.mycompany") && !code.contains("checkstyle-packages") && !code.contains("MethodLimit") && !code.contains("<suppress ") && !code.contains("<import-control ") && !unserializedSource.startsWith("<property ") && !unserializedSource.startsWith("<taskdef ")) {
// validate checkstyle structure and contents
try {
final Properties properties = new Properties();
properties.setProperty("checkstyle.header.file", new File("config/java.header").getCanonicalPath());
final PropertiesExpander expander = new PropertiesExpander(properties);
final Configuration config = ConfigurationLoader.loadConfiguration(new InputSource(new StringReader(code)), expander, false);
final Checker checker = new Checker();
try {
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
checker.setModuleClassLoader(moduleClassLoader);
checker.configure(config);
} finally {
checker.destroy();
}
} catch (CheckstyleException ex) {
throw new CheckstyleException(fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex);
}
}
}
use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.
the class Main method runCheckstyle.
/**
* Executes required Checkstyle actions based on passed parameters.
* @param cliOptions
* pojo object that contains all options
* @return number of violations of ERROR level
* @throws FileNotFoundException
* when output file could not be found
* @throws CheckstyleException
* when properties file could not be loaded
*/
private static int runCheckstyle(CliOptions cliOptions) throws CheckstyleException, FileNotFoundException {
// setup the properties
final Properties props;
if (cliOptions.propertiesLocation == null) {
props = System.getProperties();
} else {
props = loadProperties(new File(cliOptions.propertiesLocation));
}
// create a configuration
final Configuration config = ConfigurationLoader.loadConfiguration(cliOptions.configLocation, new PropertiesExpander(props));
// create a listener for output
final AuditListener listener = createListener(cliOptions.format, cliOptions.outputLocation);
// create RootModule object and run it
int errorCounter = 0;
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);
try {
rootModule.setModuleClassLoader(moduleClassLoader);
rootModule.configure(config);
rootModule.addListener(listener);
// run RootModule
errorCounter = rootModule.process(cliOptions.files);
} finally {
rootModule.destroy();
}
return errorCounter;
}
use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.
the class CatchParameterNameCheckTest method testDefaultConfigurationOnFileWithViolations.
@Test
public void testDefaultConfigurationOnFileWithViolations() throws Exception {
final Configuration checkConfig = createCheckConfig(CatchParameterNameCheck.class);
final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+)$";
final String[] expected = { "18:28: " + getCheckMessage(MSG_INVALID_PATTERN, "exception1", defaultFormat), "28:39: " + getCheckMessage(MSG_INVALID_PATTERN, "ie", defaultFormat), "31:28: " + getCheckMessage(MSG_INVALID_PATTERN, "iException", defaultFormat), "34:28: " + getCheckMessage(MSG_INVALID_PATTERN, "ok", defaultFormat), "38:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e1", defaultFormat), "40:32: " + getCheckMessage(MSG_INVALID_PATTERN, "e2", defaultFormat), "44:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t1", defaultFormat), "46:32: " + getCheckMessage(MSG_INVALID_PATTERN, "t2", defaultFormat) };
verify(checkConfig, getPath("InputCatchParameterName.java"), expected);
}
use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.
the class PropertyCacheFileTest method testNonExistingResource.
/**
* This SuppressWarning("unchecked") required to suppress
* "Unchecked generics array creation for varargs parameter" during mock
* @throws IOException when smth wrong with file creation or cache.load
*/
@SuppressWarnings("unchecked")
@Test
public void testNonExistingResource() throws IOException {
final Configuration config = new DefaultConfiguration("myName");
final String filePath = temporaryFolder.newFile().getPath();
final PropertyCacheFile cache = new PropertyCacheFile(config, filePath);
// create cache with one file
cache.load();
final String myFile = "myFile";
cache.put(myFile, 1);
final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY);
assertNotNull(hash);
mockStatic(ByteStreams.class);
when(ByteStreams.toByteArray(any(BufferedInputStream.class))).thenThrow(IOException.class);
// apply new external resource to clear cache
final Set<String> resources = new HashSet<>();
final String resource = "/com/puppycrawl/tools/checkstyle/java.header";
resources.add(resource);
cache.putExternalResources(resources);
assertFalse(cache.isInCache(myFile, 1));
assertFalse(cache.isInCache(resource, 1));
}
Aggregations