/** * Returns String message of original exception that was thrown in * ImportControlCheck.setUrl or ImportControlCheck.setFile * and caught in test (it was caught and re-thrown twice after that) * Note: this is helper method with hard-coded structure of exception causes. It works * fine for methods mentioned, you may need to adjust it if you try to use it for other needs * @param exception Exception * @return String message of original exception */ private static String getCheckstyleExceptionMessage(CheckstyleException exception) { return exception.getCause().getCause().getCause().getMessage(); }
@Test public void testWrongFormatUri() throws Exception { try { ImportControlLoader.load(new URI("aaa://" + getPath("InputImportControlLoaderComplete.xml"))); fail("exception expected"); } catch (CheckstyleException ex) { assertSame("Invalid exception class", MalformedURLException.class, ex.getCause().getClass()); assertEquals("Invalid exception message", "unknown protocol: aaa", ex.getCause().getMessage()); } }
@Test public void testParseFileThrowable() throws Exception { final File input = new File(getNonCompilablePath("InputAstTreeStringPrinter.java")); try { AstTreeStringPrinter.printFileAst(input, JavaParser.Options.WITHOUT_COMMENTS); Assert.fail("exception expected"); } catch (CheckstyleException ex) { Assert.assertSame("Invalid class", NoViableAltException.class, ex.getCause().getClass()); Assert.assertEquals("Invalid exception message", input.getAbsolutePath() + ":2:1: unexpected token: classD", ex.getCause().toString()); } }
@Test public void testNonExistentPropertyName() throws Exception { try { loadConfiguration("InputConfigurationLoaderNonexistentProperty.xml"); fail("exception in expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "unable to parse configuration stream", ex.getMessage()); assertSame("Expected cause of type SAXException", SAXException.class, ex.getCause().getClass()); assertSame("Expected cause of type CheckstyleException", CheckstyleException.class, ex.getCause().getCause().getClass()); assertEquals("Invalid exception cause message", "Property ${nonexistent} has not been set", ex.getCause().getCause().getMessage()); } }
@Test public void testParseFileTextThrowable() throws Exception { final File input = new File(getNonCompilablePath("InputSuppressionsStringPrinter.java")); final String lineAndColumnNumber = "2:3"; final int tabWidth = 2; try { SuppressionsStringPrinter.printSuppressions(input, lineAndColumnNumber, tabWidth); Assert.fail("exception expected"); } catch (CheckstyleException ex) { Assert.assertSame("Invalid class", NoViableAltException.class, ex.getCause().getClass()); Assert.assertEquals("Invalid exception message", input.getAbsolutePath() + ":2:1: unexpected token: classD", ex.getCause().toString()); } } }
@Test public void testParseException() throws Exception { final File input = new File(getNonCompilablePath("InputJavaParser.java")); try { JavaParser.parseFile(input, JavaParser.Options.WITH_COMMENTS); Assert.fail("exception expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", CheckstyleException.class.getName() + ": NoViableAltException occurred while parsing file " + input.getAbsolutePath() + ".", ex.toString()); Assert.assertSame("Invalid class", NoViableAltException.class, ex.getCause().getClass()); assertEquals("Invalid exception message", input.getAbsolutePath() + ":2:1: unexpected token: classD", ex.getCause().toString()); } }
@Test public void testConstructorFailure() { try { factory.createModule(FailConstructorFileSet.class.getName()); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "Unable to instantiate com.puppycrawl.tools.checkstyle." + "PackageObjectFactoryTest$FailConstructorFileSet", ex.getMessage()); assertEquals("Invalid exception cause class", "IllegalArgumentException", ex.getCause().getCause().getClass() .getSimpleName()); } }
@Test public void testParseError() throws Exception { try { JavadocPropertiesGenerator.main( getNonCompilablePath("InputJavadocPropertiesGeneratorParseError.java"), "--destfile", DESTFILE.getAbsolutePath()); fail("Exception was expected"); } catch (CheckstyleException ex) { assertTrue("Invalid error message", ex.getMessage().contains("InputJavadocPropertiesGeneratorParseError.java")); final Throwable cause = ex.getCause(); assertTrue("Invalid error message", cause instanceof ANTLRException); assertTrue("Invalid error message", cause.getMessage().contains("Unexpected character 0x23 in identifier")); } }
@Test public void testInputStreamFailsOnRead() throws Exception { final InputStream inputStream = PowerMockito.mock(InputStream.class); final int available = Mockito.doThrow(IOException.class).when(inputStream).available(); final URL url = PowerMockito.mock(URL.class); BDDMockito.given(url.openStream()).willReturn(inputStream); final URI uri = PowerMockito.mock(URI.class); BDDMockito.given(uri.toURL()).willReturn(url); try { ImportControlLoader.load(uri); //Using available to bypass 'ignored result' warning fail("exception expected " + available); } catch (CheckstyleException ex) { assertSame("Invalid exception class", SAXParseException.class, ex.getCause().getClass()); } }
@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 testInvalidCheckFormat() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressionCommentFilter.class); filterConfig.addAttribute("checkFormat", "e[l"); try { final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY; verifySuppressed(filterConfig, suppressed); fail("Exception is expected"); } catch (CheckstyleException ex) { final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause(); assertEquals("Invalid exception message", "unable to parse expanded comment e[l", cause.getMessage()); } }
@Test public void testInvalidMessageFormat() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressionCommentFilter.class); filterConfig.addAttribute("messageFormat", "e[l"); try { final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY; verifySuppressed(filterConfig, suppressed); fail("Exception is expected"); } catch (CheckstyleException ex) { final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause(); assertEquals("Invalid exception message", "unable to parse expanded comment e[l", cause.getMessage()); } }
@Test public void testInvalidInfluenceFormat() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressWithNearbyCommentFilter.class); filterConfig.addAttribute("influenceFormat", "a"); try { final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY; verifySuppressed(filterConfig, suppressed); fail("Exception is expected"); } catch (CheckstyleException ex) { final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause(); assertEquals("Invalid exception message", "unable to parse influence" + " from 'SUPPRESS CHECKSTYLE MemberNameCheck' using a", cause.getMessage()); } }
@Test public void testInvalidCheckFormat() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressWithNearbyCommentFilter.class); filterConfig.addAttribute("checkFormat", "a[l"); try { final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY; verifySuppressed(filterConfig, suppressed); fail("Exception is expected"); } catch (CheckstyleException ex) { final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause(); assertEquals("Invalid exception message", "unable to parse expanded comment a[l", cause.getMessage()); } }
@Test public void testInvalidCharset() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class); checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header")); checkConfig.addAttribute("charset", "XSO-8859-1"); try { createChecker(checkConfig); fail("CheckstyleException is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "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()); assertEquals("Invalid exception message", "unsupported charset: 'XSO-8859-1'", ex.getCause().getCause().getCause().getMessage()); } }
@Test public void testConfigureNoSuchAttribute() { final TestBean testBean = new TestBean(); final DefaultConfiguration conf = new DefaultConfiguration("testConf"); conf.addAttribute("NonExistent", "doesn't matter"); try { testBean.configure(conf); fail("Exception is expected"); } catch (CheckstyleException ex) { final String expected = "Property 'NonExistent' in module "; assertNull("Exceptions cause should be null", ex.getCause()); assertTrue("Invalid exception message, should start with: " + expected, ex.getMessage().startsWith(expected)); } }
@Test public void testContextualizeInvocationTargetException() { final TestBean testBean = new TestBean(); final DefaultContext context = new DefaultContext(); context.add("exceptionalMethod", 123.0f); try { testBean.contextualize(context); fail("InvocationTargetException is expected"); } catch (CheckstyleException ex) { final String expected = "Cannot set property "; assertTrue("Invalid exception cause, should be: InvocationTargetException", ex.getCause() instanceof InvocationTargetException); assertTrue("Invalid exception message, should start with: " + expected, ex.getMessage().startsWith(expected)); } }
@Test public void testContextualizeConversionException() { final TestBean testBean = new TestBean(); final DefaultContext context = new DefaultContext(); context.add("val", "some string"); try { testBean.contextualize(context); fail("InvocationTargetException is expected"); } catch (CheckstyleException ex) { final String expected = "illegal value "; assertTrue("Invalid exception cause, should be: ConversionException", ex.getCause() instanceof ConversionException); assertTrue("Invalid exception message, should start with: " + expected, ex.getMessage().startsWith(expected)); } }
@Test public void testConfigureNoSuchAttribute2() { final TestBean testBean = new TestBean(); final DefaultConfiguration conf = new DefaultConfiguration("testConf"); conf.addAttribute("privateField", "doesn't matter"); try { testBean.configure(conf); fail("Exception is expected"); } catch (CheckstyleException ex) { final String expected = "Property 'privateField' in module "; assertNull("Exceptions cause should be null", ex.getCause()); assertTrue("Invalid exception message, should start with: " + expected, ex.getMessage().startsWith(expected)); } }
@Test public void testInvalidDestinationSpecified() throws Exception { try { // Passing a folder name will cause the FileNotFoundException. JavadocPropertiesGenerator.main("--destfile", "..", getPath("InputJavadocPropertiesGeneratorCorrect.java")); fail("Exception was expected"); } catch (CheckstyleException ex) { final String expectedError = "Failed to write javadoc properties of '" + getPath("InputJavadocPropertiesGeneratorCorrect.java") + "' to '..'"; assertEquals("Invalid error message", expectedError, ex.getMessage()); final Throwable cause = ex.getCause(); assertTrue("Invalid error message", cause instanceof FileNotFoundException); assertTrue("Invalid error message", cause.getMessage().contains("..")); } assertEquals("Unexpected error log", "", systemErr.getLog()); assertEquals("Unexpected output log", "", systemOut.getLog()); }