@Test public void testFileSetCheckInitWhenAddedAsChild() throws Exception { final Checker checker = new Checker(); final DummyFileSet fileSet = new DummyFileSet(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()) { @Override public Object createModule(String name) throws CheckstyleException { Object check = fileSet; if (!name.equals(DummyFileSet.class.getName())) { check = super.createModule(name); } return check; } }; checker.setModuleFactory(factory); checker.finishLocalSetup(); checker.setupChild(createModuleConfig(DummyFileSet.class)); assertTrue("FileSetCheck.init() wasn't called", fileSet.isInitCalled()); }
@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 testNoClassLoaderNoModuleFactory() { final Checker checker = new Checker(); try { checker.finishLocalSetup(); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Error message is not expected", "if no custom moduleFactory is set, moduleClassLoader must be specified", ex.getMessage()); } }
@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); }