@Override public Object createModule(String name) throws CheckstyleException { Object filter = fileFilter; if (!name.equals(TestBeforeExecutionFileFilter.class.getName())) { filter = super.createModule(name); } return filter; } };
@Override public Object createModule(String name) throws CheckstyleException { Object adapter = auditAdapter; if (!name.equals(DebugAuditAdapter.class.getName())) { adapter = super.createModule(name); } return adapter; } };
@Override public Object createModule(String name) throws CheckstyleException { Object check = fileSet; if (!name.equals(DummyFileSet.class.getName())) { check = super.createModule(name); } return check; } };
@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 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 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 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 testMakeObjectFromName() throws CheckstyleException { final Checker checker = (Checker) factory.createModule( "com.puppycrawl.tools.checkstyle.Checker"); assertNotNull("Checker should not be null when creating module from name", checker); }
@Test public void testConstructorFailure() { try { factory.createModule(FailConstructorFileSet.class.getName()); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "Unable to instantiate com.puppycrawl.tools.checkstyle." + "PackageObjectFactoryTest$FailConstructorFileSet", ex.getMessage()); assertEquals("Invalid exception cause class", "IllegalArgumentException", ex.getCause().getCause().getClass() .getSimpleName()); } }
@Test public void testCreateObjectFromFullModuleNamesWithExceptionByBruteForce() { final String package1 = BASE_PACKAGE + ".wrong1"; final String package2 = BASE_PACKAGE + ".wrong2"; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory( new LinkedHashSet<>(Arrays.asList(package1, package2)), classLoader, TRY_IN_ALL_REGISTERED_PACKAGES); final String name = "FooCheck"; final String checkName = name + CHECK_SUFFIX; try { objectFactory.createModule(name); fail("Exception is expected"); } catch (CheckstyleException ex) { final String attemptedNames = package1 + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + checkName + STRING_SEPARATOR + package1 + PACKAGE_SEPARATOR + checkName + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + checkName; final LocalizedMessage exceptionMessage = new LocalizedMessage(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] {name, attemptedNames}, null, getClass(), null); assertEquals("Invalid exception message", exceptionMessage.getMessage(), ex.getMessage()); } }
@Test public void testCreateObjectFromFullModuleNamesWithAmbiguousException() { final String barPackage = BASE_PACKAGE + ".packageobjectfactory.bar"; final String fooPackage = BASE_PACKAGE + ".packageobjectfactory.foo"; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory( new LinkedHashSet<>(Arrays.asList(barPackage, fooPackage)), classLoader); final String name = "FooCheck"; try { objectFactory.createModule(name); fail("Exception is expected"); } catch (CheckstyleException ex) { final String optionalNames = barPackage + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + fooPackage + PACKAGE_SEPARATOR + name; final LocalizedMessage exceptionMessage = new LocalizedMessage(1, Definitions.CHECKSTYLE_BUNDLE, AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, new String[] {name, optionalNames}, null, getClass(), null); assertEquals("Invalid exception message", exceptionMessage.getMessage(), ex.getMessage()); } }
@Test public void testCreateObjectFromFullModuleNamesWithCantInstantiateException() { final String package1 = BASE_PACKAGE + ".wrong1"; final String package2 = BASE_PACKAGE + ".wrong2"; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory( new LinkedHashSet<>(Arrays.asList(package1, package2)), classLoader); final String name = "FooCheck"; final String checkName = name + CHECK_SUFFIX; try { objectFactory.createModule(name); fail("Exception is expected"); } catch (CheckstyleException ex) { final String attemptedNames = package1 + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + checkName + STRING_SEPARATOR + package1 + PACKAGE_SEPARATOR + checkName + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + checkName; final LocalizedMessage exceptionMessage = new LocalizedMessage(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] {name, attemptedNames}, null, getClass(), null); assertEquals("Invalid exception message", exceptionMessage.getMessage(), ex.getMessage()); } }
@Test public void testMakeCheckFromName() { final String name = "com.puppycrawl.tools.checkstyle.checks.naming.ConstantName"; try { factory.createModule(name); fail("Exception is expected"); } catch (CheckstyleException ex) { final LocalizedMessage exceptionMessage = new LocalizedMessage(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] {name, null}, null, factory.getClass(), null); assertEquals("Invalid exception message", exceptionMessage.getMessage(), ex.getMessage()); } }
@Test public void testCreateModuleWithNonExistName() { final String[] names = {"NonExistClassOne", "NonExistClassTwo", }; for (String name : names) { try { factory.createModule(name); fail("Exception is expected"); } catch (CheckstyleException ex) { final String attemptedNames = BASE_PACKAGE + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + name + CHECK_SUFFIX + STRING_SEPARATOR + BASE_PACKAGE + PACKAGE_SEPARATOR + name + CHECK_SUFFIX; final LocalizedMessage exceptionMessage = new LocalizedMessage(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] {name, attemptedNames}, null, factory.getClass(), null); assertEquals("Invalid exception message", exceptionMessage.getMessage(), ex.getMessage()); } } }
assertNull("Expected uninitialized field", nullMap); final Object instance = objectFactory.createModule(name); assertEquals("Expected empty string", "", instance);