@Test @SuppressWarnings("unchecked") public void testPackagesWithIoException_getResources() throws Exception { ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenThrow(IOException.class); try { PackageNamesLoader.getPackageNames(classLoader); fail(); } catch (CheckstyleException ex) { assertTrue(ex.getCause() instanceof IOException); assertEquals("unable to get package file resources", ex.getMessage()); } }
@Test @SuppressWarnings("unchecked") public void testPackagesWithSaxException() throws Exception { final URLConnection mockConnection = Mockito.mock(URLConnection.class); when(mockConnection.getInputStream()).thenReturn(new ByteArrayInputStream(EMPTY_BYTE_ARRAY)); URL url = getMockUrl(mockConnection); Enumeration<URL> enumer = (Enumeration<URL>) mock(Enumeration.class); when(enumer.hasMoreElements()).thenReturn(true); when(enumer.nextElement()).thenReturn(url); ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumer); try { PackageNamesLoader.getPackageNames(classLoader); fail(); } catch (CheckstyleException ex) { assertTrue(ex.getCause() instanceof SAXException); } }
@Test @SuppressWarnings("unchecked") public void testPackagesWithIoException() throws Exception { final URLConnection mockConnection = Mockito.mock(URLConnection.class); when(mockConnection.getInputStream()).thenReturn(null); URL url = getMockUrl(mockConnection); Enumeration<URL> enumer = (Enumeration<URL>) mock(Enumeration.class); when(enumer.hasMoreElements()).thenReturn(true); when(enumer.nextElement()).thenReturn(url); ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumer); try { PackageNamesLoader.getPackageNames(classLoader); fail(); } catch (CheckstyleException ex) { assertTrue(ex.getCause() instanceof IOException); assertNotEquals("unable to get package file resources", ex.getMessage()); } }