@Test public void testMissingConfigName() throws Exception { try { loadConfiguration("InputConfigurationLoaderMissingConfigName.xml"); fail("missing module name"); } catch (CheckstyleException ex) { assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"name\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"module\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().endsWith(":7:23")); } }
@Test public void testMissingConfigParent() throws Exception { try { loadConfiguration("InputConfigurationLoaderMissingConfigParent.xml"); fail("missing module parent"); } catch (CheckstyleException ex) { assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"property\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"module\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().endsWith(":8:38")); } }
@Test public void testBadInt() throws IOException { final String fn = getPath("InputSuppressionsLoaderBadInt.xml"); try { SuppressionsLoader.loadSuppressions(fn); fail("Exception is expected"); } catch (CheckstyleException ex) { assertTrue( ex.getMessage(), ex.getMessage().startsWith("Number format exception " + fn + " - ")); } }
@Test public void testOpenFileNonExistentFile() throws IOException { final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT)); try { model.openFile(nonExistentFile); fail("Expected CheckstyleException is not thrown."); } catch (CheckstyleException ex) { final String expectedMsg = String.format(Locale.ROOT, "FileNotFoundException occurred while opening file %s.", nonExistentFile.getPath()); assertEquals("Invalid exception message", expectedMsg, ex.getMessage()); } }
@Test public void testOpenFileNonCompilableFile() throws IOException { final File nonCompilableFile = new File(getNonCompilablePath(FILE_NAME_NON_COMPILABLE)); try { model.openFile(nonCompilableFile); fail("Expected CheckstyleException is not thrown."); } catch (CheckstyleException ex) { final String expectedMsg = String.format(Locale.ROOT, "NoViableAltException occurred while parsing file %s.", nonCompilableFile.getPath()); assertEquals("Invalid exception message", expectedMsg, ex.getMessage()); } }
@Test public void testForInvalidCheckImplementation() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(BadJavaDocCheck.class); final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath(); try { final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, pathToEmptyFile, expected); fail("Exception is expected"); } catch (CheckstyleException ex) { assertTrue("Error message is unexpected", ex.getMessage().contains("isCommentNodesRequired")); } }
@Test public void testLoadFromMalformedUrl() { try { SuppressionsLoader.loadSuppressions("http"); fail("exception expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to find: http", ex.getMessage()); } }
@Test public void testLoadFromNonExistentUrl() { try { SuppressionsLoader.loadSuppressions("http://^%$^* %&% %^&"); fail("exception expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to find: http://^%$^* %&% %^&", ex.getMessage()); } }
@Test public void testNonExistentSuppressionFileWithFalseOptional() { final String fileName = "non_existent_suppression_file.xml"; try { final boolean optional = false; createSuppressionFilter(fileName, optional); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to find: " + fileName, ex.getMessage()); } }
@Test public void testWithCheckNotHavingTreeWalkerAsParent() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(JavadocPackageCheck.class); try { final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(createChecker(checkConfig, ModuleCreationOption.IN_TREEWALKER), temporaryFolder.newFile().getPath(), expected); fail("CheckstyleException is expected"); } catch (CheckstyleException exception) { assertTrue("Error message is unexpected", exception.getMessage().contains("TreeWalker is not allowed as a parent of")); } }
@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()); } }
@Test public void testNoCheckNoId() throws IOException { final String fn = getPath("InputSuppressionsLoaderNoCheckAndId.xml"); try { SuppressionsLoader.loadSuppressions(fn); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to parse " + fn + " - missing checks or id or message attribute", ex.getMessage()); } }
@Test public void testInvalidFileFormat() throws IOException { final String fn = getPath("InputSuppressionsLoaderInvalidFile.xml"); try { SuppressionsLoader.loadSuppressions(fn); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to parse " + fn + " - invalid files or checks or message format", ex.getMessage()); } }
@Test public void testNonExistentSuppressionFileWithFalseOptional() throws Exception { final String fileName = getPath("non_existent_suppression_file.xml"); try { final boolean optional = false; createSuppressionXpathFilter(fileName, optional); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to find: " + fileName, ex.getMessage()); } }
@Test public void testNoClassLoaderNoModuleFactory() { final Checker checker = new Checker(); try { checker.finishLocalSetup(); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Error message is not expected", "if no custom moduleFactory is set, moduleClassLoader must be specified", ex.getMessage()); } }
@Test public void testNotImplementedTag() throws Exception { try { JavadocPropertiesGenerator.main( getPath("InputJavadocPropertiesGeneratorNotImplementedTag.java"), "--destfile", DESTFILE.getAbsolutePath()); fail("Exception was expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unsupported inline tag LINK_LITERAL", ex.getMessage()); } assertEquals("File '" + DESTFILE + "' must be empty", 0, FileUtils.sizeOf(DESTFILE)); }
@Test public void testExistingInvalidSuppressionFileWithTrueOptional() throws IOException { final String fileName = getPath("InputSuppressionFilterInvalidFile.xml"); try { final boolean optional = true; createSuppressionFilter(fileName, optional); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to parse " + fileName + " - invalid files or checks or message format", ex.getMessage()); } }
@Test public void testXpathInvalidFileFormat() throws IOException { final String fn = getPath("InputSuppressionsLoaderXpathInvalidFile.xml"); try { SuppressionsLoader.loadXpathSuppressions(fn); fail("Exception should be thrown"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to parse " + fn + " - invalid files or checks or message format for " + "suppress-xpath", ex.getMessage()); } }
@Test public void testExistingInvalidSuppressionFileWithTrueOptional() throws Exception { final String fileName = getPath("InputSuppressionXpathFilterInvalidFile.xml"); try { final boolean optional = true; createSuppressionXpathFilter(fileName, optional); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid error message", "Unable to parse " + fileName + " - invalid files or checks or message format for suppress-xpath", ex.getMessage()); } }
@Test public void testHaltOnException() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(CheckWhichThrowsError.class); final String filePath = getPath("InputChecker.java"); try { verify(checkConfig, filePath); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Error message is not expected", "Exception was thrown while processing " + filePath, ex.getMessage()); } }