/** * 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 testDefault() throws CheckstyleException { final Set<String> packageNames = PackageNamesLoader .getPackageNames(Thread.currentThread() .getContextClassLoader()); assertEquals("pkgNames.length.", 0, packageNames.size()); }
@Test @SuppressWarnings("unchecked") public void testPackagesWithSaxException() throws Exception { final URLConnection mockConnection = Mockito.mock(URLConnection.class); when(mockConnection.getInputStream()).thenReturn( new ByteArrayInputStream(EMPTY_BYTE_ARRAY)); final URL url = getMockUrl(mockConnection); final Enumeration<URL> enumeration = mock(Enumeration.class); when(enumeration.hasMoreElements()).thenReturn(true); when(enumeration.nextElement()).thenReturn(url); final ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration); try { PackageNamesLoader.getPackageNames(classLoader); fail("CheckstyleException is expected"); } catch (CheckstyleException ex) { assertTrue("Invalid exception cause class", ex.getCause() instanceof SAXException); } }
@Test @SuppressWarnings("unchecked") public void testPackagesWithIoException() throws Exception { final URLConnection mockConnection = Mockito.mock(URLConnection.class); when(mockConnection.getInputStream()).thenReturn(null); final URL url = getMockUrl(mockConnection); final Enumeration<URL> enumer = mock(Enumeration.class); when(enumer.hasMoreElements()).thenReturn(true); when(enumer.nextElement()).thenReturn(url); final ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumer); try { PackageNamesLoader.getPackageNames(classLoader); fail("CheckstyleException is expected"); } catch (CheckstyleException ex) { assertTrue("Invalid exception cause class", ex.getCause() instanceof IOException); assertNotEquals("Invalid exception message", "unable to get package file resources", ex.getMessage()); } }
when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration); final Set<String> actualPackageNames = PackageNamesLoader.getPackageNames(classLoader); final String[] expectedPackageNames = { "com.puppycrawl.tools.checkstyle",
@Override protected void finishLocalSetup() throws CheckstyleException { final Locale locale = new Locale(localeLanguage, localeCountry); LocalizedMessage.setLocale(locale); if (moduleFactory == null) { if (moduleClassLoader == null) { throw new CheckstyleException( "if no custom moduleFactory is set, " + "moduleClassLoader must be specified"); } final Set<String> packageNames = PackageNamesLoader .getPackageNames(moduleClassLoader); moduleFactory = new PackageObjectFactory(packageNames, moduleClassLoader); } final DefaultContext context = new DefaultContext(); context.add("charset", charset); context.add("classLoader", classLoader); context.add("moduleFactory", moduleFactory); context.add("severity", severity.getName()); context.add("basedir", basedir); childContext = context; }
@Test public void testPackagesWithIoExceptionGetResources() throws Exception { final ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenThrow(IOException.class); try { PackageNamesLoader.getPackageNames(classLoader); fail("CheckstyleException is expected"); } catch (CheckstyleException ex) { assertTrue("Invalid exception cause class", ex.getCause() instanceof IOException); assertEquals("Invalid exception message", "unable to get package file resources", ex.getMessage()); } }
private ModuleFactory createModuleFactory() { try { ClassLoader classLoader = AbstractCheck.class.getClassLoader(); Set<String> packageNames = PackageNamesLoader.getPackageNames(classLoader); return new PackageObjectFactory(packageNames, classLoader); } catch (CheckstyleException ex) { throw new IllegalStateException(ex); } }
@Override protected void finishLocalSetup() throws CheckstyleException { final Locale locale = new Locale(localeLanguage, localeCountry); LocalizedMessage.setLocale(locale); if (moduleFactory == null) { if (moduleClassLoader == null) { throw new CheckstyleException( "if no custom moduleFactory is set, " + "moduleClassLoader must be specified"); } final Set<String> packageNames = PackageNamesLoader .getPackageNames(moduleClassLoader); moduleFactory = new PackageObjectFactory(packageNames, moduleClassLoader); } final DefaultContext context = new DefaultContext(); context.add("charset", charset); context.add("classLoader", classLoader); context.add("moduleFactory", moduleFactory); context.add("severity", severity.getName()); context.add("basedir", basedir); childContext = context; }