checker.setModuleClassLoader(moduleClassLoader); checker.configure(config);
@Test public void testNoModuleFactory() throws Exception { final Checker checker = new Checker(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); checker.setModuleClassLoader(classLoader); checker.finishLocalSetup(); final Context actualCtx = Whitebox.getInternalState(checker, "childContext"); assertNotNull("Default module factory should be created when it is not specified", actualCtx.get("moduleFactory")); assertEquals("Invalid classLoader", classLoader, actualCtx.get("classLoader")); }
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(getBriefUtLogger());
@Test public void testXmlLoggerClosesItStreams() throws Exception { final Checker checker = new Checker(); try (CloseAndFlushTestByteArrayOutputStream testInfoOutputStream = new CloseAndFlushTestByteArrayOutputStream()) { checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.addListener(new XMLLogger(testInfoOutputStream, true)); final File tmpFile = temporaryFolder.newFile("file.java"); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checker, tmpFile.getPath(), tmpFile.getPath(), expected); assertEquals("Close count was not expected", 1, testInfoOutputStream.getCloseCount()); assertEquals("Flush count was not expected", 0, testInfoOutputStream.getFlushCount()); } }
@Test public void testClearCache() throws Exception { final DefaultConfiguration violationCheck = createModuleConfig(DummyFileSetViolationCheck.class); final DefaultConfiguration checkerConfig = new DefaultConfiguration("myConfig"); checkerConfig.addAttribute("charset", "UTF-8"); final File cacheFile = temporaryFolder.newFile(); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); checkerConfig.addChild(violationCheck); final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(getBriefUtLogger()); checker.process(Collections.singletonList(new File("dummy.java"))); checker.clearCache(); // invoke destroy to persist cache final PropertyCacheFile cache = Whitebox.getInternalState(checker, "cacheFile"); cache.persist(); final Properties cacheAfterClear = new Properties(); cacheAfterClear.load(Files.newBufferedReader(cacheFile.toPath())); assertEquals("Cache has unexpected size", 1, cacheAfterClear.size()); }
root.addChild(moduleConfig2); final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(root);
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig);
@Test public void testFinishLocalSetupFullyInitialized() throws Exception { final Checker checker = new Checker(); final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); checker.setModuleClassLoader(contextClassLoader); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), contextClassLoader); checker.setModuleFactory(factory); checker.setBasedir("testBaseDir"); checker.setLocaleLanguage("it"); checker.setLocaleCountry("IT"); checker.finishLocalSetup(); final Context context = Whitebox.getInternalState(checker, "childContext"); assertEquals("Charset was different than expected", System.getProperty("file.encoding", StandardCharsets.UTF_8.name()), context.get("charset")); assertEquals("Was used insufficient classloader", contextClassLoader, context.get("classLoader")); assertEquals("Severity is set to unexpected value", "error", context.get("severity")); assertEquals("Basedir is set to unexpected value", "testBaseDir", context.get("basedir")); final Field sLocale = LocalizedMessage.class.getDeclaredField("sLocale"); sLocale.setAccessible(true); final Locale locale = (Locale) sLocale.get(null); assertEquals("Locale is set to unexpected value", Locale.ITALY, locale); }
/** * 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; }
@Test public void testIgnoredFileExtensions() throws Exception { final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration"); checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name()); checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath()); final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); final DebugAuditAdapter auditAdapter = new DebugAuditAdapter(); checker.addListener(auditAdapter); final List<File> allIgnoredFiles = new ArrayList<>(); final File ignoredFile = new File("file.pdf"); allIgnoredFiles.add(ignoredFile); final String[] fileExtensions = {"java", "xml", "properties"}; checker.setFileExtensions(fileExtensions); checker.setCacheFile(temporaryFolder.newFile().getPath()); final int counter = checker.process(allIgnoredFiles); // comparing to 0 as there is no legal file in input final int numLegalFiles = 0; assertEquals("There were more legal files than expected", numLegalFiles, counter); assertEquals("Audit was started on larger amount of files than expected", numLegalFiles, auditAdapter.getNumFilesStarted()); assertEquals("Audit was finished on larger amount of files than expected", numLegalFiles, auditAdapter.getNumFilesFinished()); }
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.addFileSetCheck(check); checker.addFilter(new DummyFilterSet());
@Test public void testDefaultLoggerClosesItStreams() throws Exception { final Checker checker = new Checker(); try (CloseAndFlushTestByteArrayOutputStream testInfoOutputStream = new CloseAndFlushTestByteArrayOutputStream(); CloseAndFlushTestByteArrayOutputStream testErrorOutputStream = new CloseAndFlushTestByteArrayOutputStream()) { checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.addListener(new DefaultLogger(testInfoOutputStream, true, testErrorOutputStream, true)); final File tmpFile = temporaryFolder.newFile("file.java"); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checker, tmpFile.getPath(), expected); assertEquals("Close count was not expected", 1, testInfoOutputStream.getCloseCount()); assertEquals("Flush count was not expected", 3, testInfoOutputStream.getFlushCount()); assertEquals("Close count was not expected", 1, testErrorOutputStream.getCloseCount()); assertEquals("Flush count was not expected", 1, testErrorOutputStream.getFlushCount()); } }
/** * Constructor. * @param env Environment to use. */ @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") public CheckstyleValidator(final Environment env) { this.checker = new Checker(); this.checker.setClassLoader(env.classloader()); this.checker.setModuleClassLoader( Thread.currentThread().getContextClassLoader() ); try { this.checker.configure(this.configuration(env)); } catch (final CheckstyleException ex) { throw new IllegalStateException("Failed to configure checker", ex); } this.listener = new CheckstyleListener(env); this.checker.addListener(this.listener); }
/** * Constructor. * @param env Environment to use. */ @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") public CheckstyleValidator(final Environment env) { this.checker = new Checker(); this.checker.setClassLoader(env.classloader()); this.checker.setModuleClassLoader( Thread.currentThread().getContextClassLoader() ); try { this.checker.configure(this.configuration(env)); } catch (final CheckstyleException ex) { throw new IllegalStateException("Failed to configure checker", ex); } this.listener = new CheckstyleListener(env); this.checker.addListener(this.listener); }
try { checker.setClassLoader(projectClassloader); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.addListener(listener);
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.addListener(listener);
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.addListener(listener);
public CheckstyleResult analyze() { final Checker checker = new Checker(); try { final LoggingAuditListener listener = new LoggingAuditListener(); checker.addListener(listener); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(ConfigurationLoader.loadConfiguration(checks.location, createPropertyResolver())); checker.process(config.getSources()); return createResult(listener.events); } catch (CheckstyleException e) { throw new AnalyzerException("Problem executing Checkstyle.", e); } finally { checker.destroy(); } }
public CheckstyleResult analyze() { final Checker checker = new Checker(); try { final LoggingAuditListener listener = new LoggingAuditListener(); checker.addListener(listener); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(ConfigurationLoader.loadConfiguration(checks.location, createPropertyResolver())); checker.process(config.getSources()); return createResult(listener.events); } catch (CheckstyleException e) { throw new AnalyzerException("Problem executing Checkstyle.", e); } finally { checker.destroy(); } }
checker.setModuleClassLoader( Thread.currentThread().getContextClassLoader() );