/** * Implements the Contextualizable interface using bean introspection. * @see Contextualizable */ @Override public final void contextualize(Context context) throws CheckstyleException { final Collection<String> attributes = context.getAttributeNames(); for (final String key : attributes) { final Object value = context.get(key); tryCopyProperty(getClass().getName(), key, value, false); } }
@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")); }
@Test public void testFinishLocalSetupFullyInitialized() { final TreeWalker treeWalker = new TreeWalker(); final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); treeWalker.setClassLoader(contextClassLoader); treeWalker.setSeverity("error"); treeWalker.setTabWidth(100); treeWalker.finishLocalSetup(); final Context context = Whitebox.getInternalState(treeWalker, "childContext"); assertEquals("Classloader object differs from expected", contextClassLoader, context.get("classLoader")); assertEquals("Severity differs from expected", "error", context.get("severity")); assertEquals("Tab width differs from expected", String.valueOf(100), context.get("tabWidth")); }
/** * Implements the Contextualizable interface using bean introspection. * @see Contextualizable */ @Override public final void contextualize(Context context) throws CheckstyleException { final Collection<String> attributes = context.getAttributeNames(); for (final String key : attributes) { final Object value = context.get(key); tryCopyProperty(getClass().getName(), key, value, false); } }
@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); }