/** * Performs verification of the file with the given file name. Uses specified configuration. * Expected messages are represented by the array of strings. * This implementation uses overloaded * {@link AbstractModuleTestSupport#verify(Checker, File[], String, String...)} method inside. * @param aConfig configuration. * @param fileName file name to verify. * @param expected an array of expected messages. * @throws Exception if exception occurs during verification process. */ protected final void verify(Configuration aConfig, String fileName, String... expected) throws Exception { verify(createChecker(aConfig), fileName, fileName, expected); }
/** * Gets the check message 'as is' from appropriate 'messages.properties' * file. * * @param messageKey the key of message in 'messages.properties' file. * @param arguments the arguments of message in 'messages.properties' file. * @return The message of the check with the arguments applied. */ protected final String getCheckMessage(String messageKey, Object... arguments) { return internalGetCheckMessage(getMessageBundle(), messageKey, arguments); }
/** * Returns canonical path for the file with the given file name. * The path is formed base on the non-compilable resources location. * This implementation uses 'src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/' * as a non-compilable resource location. * @param filename file name. * @return canonical path for the file with the given file name. * @throws IOException if I/O exception occurs while forming the path. */ protected final String getNonCompilablePath(String filename) throws IOException { return new File("src/test/resources-noncompilable/" + getPackageLocation() + "/" + filename).getCanonicalPath(); }
/** * Creates {@link Checker} instance based on the given {@link Configuration} instance. * @param moduleConfig {@link Configuration} instance. * @param moduleCreationOption {@code IN_TREEWALKER} if the {@code moduleConfig} should be added * under {@link TreeWalker}. * @return {@link Checker} instance based on the given {@link Configuration} instance. * @throws Exception if an exception occurs during checker configuration. */ public final Checker createChecker(Configuration moduleConfig, ModuleCreationOption moduleCreationOption) throws Exception { final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); if (moduleCreationOption == ModuleCreationOption.IN_TREEWALKER) { final Configuration dc = createTreeWalkerConfig(moduleConfig); checker.configure(dc); } else if (ROOT_MODULE_NAME.equals(moduleConfig.getName())) { checker.configure(moduleConfig); } else { final Configuration dc = createRootConfig(moduleConfig); checker.configure(dc); } checker.addListener(new BriefUtLogger(stream)); return checker; }
/** * Performs verification of the file with the given file name. * Uses provided {@link Checker} instance. * Expected messages are represented by the array of strings. * This implementation uses overloaded * {@link AbstractModuleTestSupport#verify(Checker, String, String, String...)} method inside. * @param checker {@link Checker} instance. * @param fileName file name to verify. * @param expected an array of expected messages. * @throws Exception if exception occurs during verification process. */ protected void verify(Checker checker, String fileName, String... expected) throws Exception { verify(checker, fileName, fileName, expected); }
private String getMessageBundle() { final String className = getClass().getName(); return getMessageBundle(className); }
/** * Creates {@link Checker} instance based on the given {@link Configuration} instance. * @param moduleConfig {@link Configuration} instance. * @return {@link Checker} instance based on the given {@link Configuration} instance. * @throws Exception if an exception occurs during checker configuration. */ public final Checker createChecker(Configuration moduleConfig) throws Exception { ModuleCreationOption moduleCreationOption = ModuleCreationOption.IN_CHECKER; final String moduleName = moduleConfig.getName(); if (!ROOT_MODULE_NAME.equals(moduleName)) { try { final Class<?> moduleClass = Class.forName(moduleName); if (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(moduleClass) || ModuleReflectionUtil.isTreeWalkerFilterModule(moduleClass)) { moduleCreationOption = ModuleCreationOption.IN_TREEWALKER; } } catch (ClassNotFoundException ignore) { // ignore exception, assume it is not part of TreeWalker } } return createChecker(moduleConfig, moduleCreationOption); }
final Map<String, List<String>> actualViolations = getActualViolations(errs); final Map<String, List<String>> realExpectedViolations = Maps.filterValues(expectedViolations, input -> !input.isEmpty());
/** * Creates {@link DefaultConfiguration} for the {@link TreeWalker} * based on the given {@link Configuration} instance. * @param config {@link Configuration} instance. * @return {@link DefaultConfiguration} for the {@link TreeWalker} * based on the given {@link Configuration} instance. */ protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) { final DefaultConfiguration dc = new DefaultConfiguration("configuration"); final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class); // make sure that the tests always run with this charset dc.addAttribute("charset", StandardCharsets.UTF_8.name()); dc.addChild(twConf); twConf.addChild(config); return dc; }
/** * Performs verification of the file with the given file name. * Uses provided {@link Checker} instance. * Expected messages are represented by the array of strings. * This implementation uses overloaded * {@link AbstractModuleTestSupport#verify(Checker, File[], String, String...)} method inside. * @param checker {@link Checker} instance. * @param processedFilename file name to verify. * @param messageFileName message file name. * @param expected an array of expected messages. * @throws Exception if exception occurs during verification process. */ protected final void verify(Checker checker, String processedFilename, String messageFileName, String... expected) throws Exception { verify(checker, new File[] {new File(processedFilename)}, messageFileName, expected); }
/** * Gets the check message 'as is' from appropriate 'messages.properties' * file. * * @param clazz the related check class. * @param messageKey the key of message in 'messages.properties' file. * @param arguments the arguments of message in 'messages.properties' file. * @return The message of the check with the arguments applied. */ protected static String getCheckMessage( Class<?> clazz, String messageKey, Object... arguments) { return internalGetCheckMessage(getMessageBundle(clazz.getName()), messageKey, arguments); }
/** * Returns URI-representation of the path for the given file name. * The path is formed base on the root location. * This implementation uses 'src/test/resources/com/puppycrawl/tools/checkstyle/' * as a root location. * @param filename file name. * @return URI-representation of the path for the file with the given file name. */ protected final String getUriString(String filename) { return new File("src/test/resources/" + getPackageLocation() + "/" + filename).toURI() .toString(); }