@Test
  @SuppressWarnings("unchecked")
  public void testPackagesWithDots() throws Exception {

    Constructor<PackageNamesLoader> constructor = PackageNamesLoader.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    PackageNamesLoader loader = constructor.newInstance();

    Attributes attributes = mock(Attributes.class);
    when(attributes.getValue("name")).thenReturn("coding.");
    loader.startElement("", "", "package", attributes);
    loader.endElement("", "", "package");

    Field field = PackageNamesLoader.class.getDeclaredField("packageNames");
    field.setAccessible(true);
    LinkedHashSet<String> list = (LinkedHashSet<String>) field.get(loader);
    assertEquals("coding.", list.iterator().next());
  }
  @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());
    }
  }
 @Test
 public void testDefault() throws CheckstyleException {
   final Set<String> packageNames =
       PackageNamesLoader.getPackageNames(Thread.currentThread().getContextClassLoader());
   validatePackageNames(packageNames);
 }