Example #1
0
  @Test
  public void testWithCheckNotHavingTreeWalkerAsParent() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(JavadocPackageCheck.class);

    try {
      final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
      verify(checkConfig, temporaryFolder.newFile().getPath(), expected);
      fail("CheckstyleException is expected");
    } catch (CheckstyleException exception) {
      assertTrue(exception.getMessage().contains("TreeWalker is not allowed as a parent of"));
    }
  }
Example #2
0
  @Test
  public void testForInvalidCheckImplementation() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(BadJavaDocCheck.class);
    final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();

    try {
      final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
      verify(checkConfig, pathToEmptyFile, expected);
    } catch (CheckstyleException ex) {
      assertTrue(ex.getMessage().contains("isCommentNodesRequired"));
    }
  }
Example #3
0
  @Test
  public void testNoClassLoaderNoModuleFactory() throws Exception {
    final Checker checker = new Checker();

    try {
      checker.finishLocalSetup();
      fail("Exception is expected");
    } catch (CheckstyleException ex) {
      assertEquals(
          "if no custom moduleFactory is set, " + "moduleClassLoader must be specified",
          ex.getMessage());
    }
  }
  @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
 public void testEmptyFilename() throws Exception {
   final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
   checkConfig.addAttribute("headerFile", "");
   try {
     createChecker(checkConfig);
     fail("Checker creation should not succeed with invalid headerFile");
   } catch (CheckstyleException ex) {
     assertEquals(
         "cannot initialize module"
             + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
             + " - Cannot set property 'headerFile' to '' in module"
             + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck",
         ex.getMessage());
   }
 }
Example #6
0
  @Test
  public void testSetupChildExceptions() throws Exception {
    final Checker checker = new Checker();
    PackageObjectFactory factory =
        new PackageObjectFactory(
            new HashSet<String>(), Thread.currentThread().getContextClassLoader());
    checker.setModuleFactory(factory);

    Configuration config = new DefaultConfiguration("java.lang.String");
    try {
      checker.setupChild(config);
      fail("Exception is expected");
    } catch (CheckstyleException ex) {
      assertEquals("java.lang.String is not allowed as a child in Checker", ex.getMessage());
    }
  }
 @Test
 public void testInvalidCharset() throws Exception {
   final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
   checkConfig.addAttribute("headerFile", getConfigPath("java.header"));
   checkConfig.addAttribute("charset", "XSO-8859-1");
   try {
     createChecker(checkConfig);
     fail("CheckstyleException is expected");
   } catch (CheckstyleException ex) {
     assertEquals(
         "cannot initialize module"
             + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
             + " - Cannot set property 'charset' to 'XSO-8859-1' in module"
             + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck",
         ex.getMessage());
   }
 }
 @Test
 public void testNonExistingHeaderFile() throws Exception {
   final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
   checkConfig.addAttribute("headerFile", getPath("nonExisting.file"));
   try {
     createChecker(checkConfig);
     fail("CheckstyleException is expected");
   } catch (CheckstyleException ex) {
     assertTrue(
         ex.getMessage()
             .startsWith(
                 "cannot initialize module"
                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
                     + " - Unable to find: "));
     assertTrue(ex.getMessage().endsWith("nonExisting.file"));
   }
 }
Example #9
0
  @Test
  public void testProcessWithParserThrowable() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.configure(createCheckConfig(TypeNameCheck.class));
    final PackageObjectFactory factory =
        new PackageObjectFactory(
            new HashSet<String>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    treeWalker.setupChild(createCheckConfig(TypeNameCheck.class));
    final File file = temporaryFolder.newFile("file.java");
    final List<String> lines = new ArrayList<>();
    lines.add(" classD a {} ");

    try {
      treeWalker.processFiltered(file, lines);
    } catch (CheckstyleException exception) {
      assertTrue(exception.getMessage().contains("occurred during the analysis of file"));
    }
  }
  @Test
  public void testAcceptableTokens() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
    checkConfig.addAttribute("tokens", "VARIABLE_DEF, ENUM_DEF, CLASS_DEF, METHOD_DEF," + "IMPORT");
    final String[] expected = {};

    try {
      verify(checkConfig, getPath("InputHiddenField.java"), expected);
      Assert.fail();
    } catch (CheckstyleException e) {
      String errorMsg = e.getMessage();
      Assert.assertTrue(
          errorMsg.contains(
              "cannot initialize module"
                  + " com.puppycrawl.tools.checkstyle.TreeWalker - Token \"IMPORT\""
                  + " was not found in Acceptable tokens list in check"
                  + " com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck"));
    }
  }
Example #11
0
  @Test
  public void testRequiredTokenIsNotInDefaultTokens() throws Exception {
    final DefaultConfiguration checkConfig =
        createCheckConfig(RequiredTokenIsNotInDefaultsCheck.class);
    final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();

    try {
      final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
      verify(checkConfig, pathToEmptyFile, expected);
      fail("CheckstyleException is expected");
    } catch (CheckstyleException ex) {
      assertTrue(
          ex.getMessage()
              .startsWith(
                  "cannot initialize module"
                      + " com.puppycrawl.tools.checkstyle.TreeWalker - Token \""
                      + TokenTypes.ASSIGN
                      + "\" from required"
                      + " tokens was not found in default tokens list in check"));
    }
  }
  @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);
    }
  }
Example #13
0
  @Test
  public void testAcceptableTokens() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
    checkConfig.addAttribute("tokens", "VARIABLE_DEF, ENUM_DEF, CLASS_DEF, METHOD_DEF," + "IMPORT");
    try {
      final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
      verify(checkConfig, getPath("InputMain.java"), expected);
      fail("CheckstyleException is expected");
    } catch (CheckstyleException ex) {
      final String errorMsg = ex.getMessage();
      final Pattern expected =
          Pattern.compile(
              Pattern.quote(
                      "cannot initialize module"
                          + " com.puppycrawl.tools.checkstyle.TreeWalker - Token ")
                  + "\"(ENUM_DEF|CLASS_DEF|METHOD_DEF|IMPORT)\""
                  + Pattern.quote(
                      " was not found in Acceptable tokens list in check"
                          + " com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck"));

      final Matcher errorMsgMatcher = expected.matcher(errorMsg);
      assertTrue("Failure for: " + errorMsg, errorMsgMatcher.matches());
    }
  }
  @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());
    }
  }