/** * Creates a new instance of the root module that will control and run * Checkstyle. * @param name The name of the module. This will either be a short name that * will have to be found or the complete package name. * @param moduleClassLoader Class loader used to load the root module. * @return The new instance of the root module. * @throws CheckstyleException if no module can be instantiated from name */ private static RootModule getRootModule(String name, ClassLoader moduleClassLoader) throws CheckstyleException { final ModuleFactory factory = new PackageObjectFactory( Checker.class.getPackage().getName(), moduleClassLoader); return (RootModule) factory.createModule(name); }
/** * Returns the default PackageObjectFactory with the default package names. * @return the default PackageObjectFactory. */ public static PackageObjectFactory getPackageObjectFactory() throws CheckstyleException { final ClassLoader cl = TestUtil.class.getClassLoader(); final Set<String> packageNames = PackageNamesLoader.getPackageNames(cl); return new PackageObjectFactory(packageNames, cl); }
@Test public void testCreateCheckByBruteForce() throws Exception { final String checkName = "AnnotationLocation"; final Method createModuleByBruteForce = PackageObjectFactory.class.getDeclaredMethod( "createModuleByTryInEachPackage", String.class); final PackageObjectFactory packageObjectFactory = new PackageObjectFactory( new HashSet<>(Arrays.asList(BASE_PACKAGE, BASE_PACKAGE + ".checks.annotation")), Thread.currentThread().getContextClassLoader(), TRY_IN_ALL_REGISTERED_PACKAGES); createModuleByBruteForce.setAccessible(true); final AnnotationLocationCheck check = (AnnotationLocationCheck) createModuleByBruteForce .invoke(packageObjectFactory, checkName); assertNotNull("Check should not be null when creating module from name", check); }
@Test public void testCreateCheckWithPartialPackageNameByBruteForce() throws Exception { final String checkName = "checks.annotation.AnnotationLocation"; final PackageObjectFactory packageObjectFactory = new PackageObjectFactory( new HashSet<>(Collections.singletonList(BASE_PACKAGE)), Thread.currentThread().getContextClassLoader(), TRY_IN_ALL_REGISTERED_PACKAGES); final AnnotationLocationCheck check = (AnnotationLocationCheck) packageObjectFactory .createModule(checkName); assertNotNull("Check should not be null when creating module from name", check); }
@Test public void testCreateStandardCheckModuleObjectFromMap() throws Exception { final String moduleName = "TypeName"; final String packageName = BASE_PACKAGE + ".packageobjectfactory.bar"; final String fullName = BASE_PACKAGE + PACKAGE_SEPARATOR + "checks" + PACKAGE_SEPARATOR + "naming" + PACKAGE_SEPARATOR + moduleName + CHECK_SUFFIX; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory(packageName, classLoader); final Object instance = objectFactory.createModule(moduleName); assertEquals("Invalid canonical name", fullName, instance.getClass().getCanonicalName()); }
@Test public void testCtorNullPackageException3() { final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { final Object test = new PackageObjectFactory(Collections.singleton(null), classLoader, TRY_IN_ALL_REGISTERED_PACKAGES); fail("Exception is expected but got " + test); } catch (IllegalArgumentException ex) { assertEquals("Invalid exception message", NULL_PACKAGE_MESSAGE, ex.getMessage()); } }
@Test public void testCtorNullPackageException1() { final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { final Object test = new PackageObjectFactory(Collections.singleton(null), classLoader); fail("Exception is expected but got " + test); } catch (IllegalArgumentException ex) { assertEquals("Invalid exception message", NULL_PACKAGE_MESSAGE, ex.getMessage()); } }
@Test public void testCreateStandardModuleObjectFromMap() throws Exception { final String moduleName = "TreeWalker"; final String packageName = BASE_PACKAGE + ".packageobjectfactory.bar"; final String fullName = BASE_PACKAGE + PACKAGE_SEPARATOR + moduleName; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory(packageName, classLoader); final Object instance = objectFactory.createModule(moduleName); assertEquals("Invalid canonical name", fullName, instance.getClass().getCanonicalName()); }
@Test public void testCtorNullLoaderException1() { try { final Object test = new PackageObjectFactory(new HashSet<>(), null); fail("Exception is expected but got " + test); } catch (IllegalArgumentException ex) { assertEquals("Invalid exception message", NULL_LOADER_MESSAGE, ex.getMessage()); } }
@Test public void testCtorNullPackageException2() { final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { final Object test = new PackageObjectFactory((String) null, classLoader); fail("Exception is expected but got " + test); } catch (IllegalArgumentException ex) { assertEquals("Invalid exception message", NULL_PACKAGE_MESSAGE, ex.getMessage()); } }
@Test public void testCtorNullLoaderException2() { try { final Object test = new PackageObjectFactory("test", null); fail("Exception is expected but got " + test); } catch (IllegalArgumentException ex) { assertEquals("Invalid exception message", NULL_LOADER_MESSAGE, ex.getMessage()); } }
@Test public void testCreateObjectFromMap() throws Exception { final String moduleName = "Foo"; final String name = moduleName + CHECK_SUFFIX; final String packageName = BASE_PACKAGE + ".packageobjectfactory.bar"; final String fullName = packageName + PACKAGE_SEPARATOR + name; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory(packageName, classLoader); final Object instance1 = objectFactory.createModule(name); assertEquals("Invalid canonical name", fullName, instance1.getClass().getCanonicalName()); final Object instance2 = objectFactory.createModule(moduleName); assertEquals("Invalid canonical name", fullName, instance2.getClass().getCanonicalName()); }
@Test public void testWithCacheWithNoViolation() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(HiddenFieldCheck.class); final Checker checker = createChecker(checkConfig); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); final File file = temporaryFolder.newFile("file.java"); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checker, file.getPath(), expected); }
@Test public void testSetupChildListener() throws Exception { final Checker checker = new Checker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); final Configuration config = new DefaultConfiguration( DebugAuditAdapter.class.getCanonicalName()); checker.setupChild(config); final List<AuditListener> listeners = Whitebox.getInternalState(checker, "listeners"); assertTrue("Invalid child listener class", listeners.get(listeners.size() - 1) instanceof DebugAuditAdapter); }
@Test public void testBehaviourWithZeroChecks() throws Exception { final TreeWalker treeWalker = new TreeWalker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalker.setModuleFactory(factory); // create file that should throw exception final File file = temporaryFolder.newFile("file.java"); final FileText fileText = new FileText(file, new ArrayList<>()); treeWalker.processFiltered(file, fileText); final Collection<Checks> checks = Whitebox.getInternalState(treeWalker, "ordinaryChecks"); assertTrue("No checks -> No parsing", checks.isEmpty()); }
@Test public void testBehaviourWithOnlyOrdinaryChecks() throws Exception { final TreeWalker treeWalkerSpy = spy(new TreeWalker()); final Class<?> classAstState = Class.forName("com.puppycrawl.tools.checkstyle.TreeWalker$AstState"); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalkerSpy.configure(createModuleConfig(TypeNameCheck.class)); treeWalkerSpy.setModuleFactory(factory); treeWalkerSpy.setupChild(createModuleConfig(TypeNameCheck.class)); final File file = temporaryFolder.newFile("file.java"); final List<String> lines = new ArrayList<>(); lines.add("class Test {}"); treeWalkerSpy.processFiltered(file, new FileText(file, lines)); verifyPrivate(treeWalkerSpy, times(1)).invoke("walk", any(DetailAST.class), any(FileContents.class), any(classAstState)); verifyPrivate(treeWalkerSpy, times(0)).invoke("getFilteredMessages", any(String.class), any(FileContents.class), any(DetailAST.class)); }
@Test public void testSetupChildExceptions() { final Checker checker = new Checker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); final Configuration config = new DefaultConfiguration("java.lang.String"); try { checker.setupChild(config); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Error message is not expected", "java.lang.String is not allowed as a child in Checker", ex.getMessage()); } }
@Test public void testSetupChildExceptions() { final TreeWalker treeWalker = new TreeWalker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalker.setModuleFactory(factory); final Configuration config = new DefaultConfiguration("java.lang.String"); try { treeWalker.setupChild(config); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Error message is not expected", "TreeWalker is not allowed as a parent of java.lang.String Please review " + "'Parent Module' section for this Check in web documentation if " + "Check is standard.", ex.getMessage()); } }
@Test public void testSetters() { // all that is set by reflection, so just make code coverage be happy final Checker checker = new Checker(); checker.setClassLoader(getClass().getClassLoader()); checker.setBasedir("some"); checker.setSeverity("ignore"); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); checker.setFileExtensions((String[]) null); checker.setFileExtensions(".java", "xml"); try { checker.setCharset("UNKNOWN-CHARSET"); fail("Exception is expected"); } catch (UnsupportedEncodingException ex) { assertEquals("Error message is not expected", "unsupported charset: 'UNKNOWN-CHARSET'", ex.getMessage()); } }
@Test public void testDestroyCheckerWithWrongCacheFileNameLength() throws Exception { final Checker checker = new Checker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); checker.configure(new DefaultConfiguration("default config")); // We set wrong file name length in order to reproduce IOException on OS Linux, OS Windows. // The maximum file name length which is allowed in most UNIX, Windows file systems is 255. // See https://en.wikipedia.org/wiki/Filename; checker.setCacheFile(String.format(Locale.ENGLISH, "%0300d", 0)); try { checker.destroy(); fail("Exception did not happen"); } catch (IllegalStateException ex) { assertTrue("Cause of exception differs from IOException", ex.getCause() instanceof IOException); } }