@Override public String getAttribute(String attributeName) throws CheckstyleException { if (!attributeMap.containsKey(attributeName)) { throw new CheckstyleException( "missing key '" + attributeName + "' in " + name); } return attributeMap.get(attributeName); }
@Test public void testMissingPropertyValue() throws Exception { try { loadConfiguration("InputConfigurationLoaderMissingPropertyValue.xml"); fail("missing property value"); } catch (CheckstyleException ex) { assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"value\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"property\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().endsWith(":8:43")); } }
/** * 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 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()); } }
throw new CheckstyleException("cannot initialize module " + name + " - " + ex.getMessage(), ex); throw new CheckstyleException(name + " is not allowed as a child in Checker");
@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()); } }
exitStatus = EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE; errorCounter = 1; ex.printStackTrace();
@Test public void testInputStreamThatFailsOnClose() throws Exception { final InputStream inputStream = PowerMockito.mock(InputStream.class); Mockito.doThrow(IOException.class).when(inputStream).close(); 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) { final Throwable[] suppressed = ex.getSuppressed(); assertEquals("Expected one suppressed exception", 1, suppressed.length); assertSame("Invalid exception class", IOException.class, suppressed[0].getClass()); } Mockito.verify(inputStream).close(); }
@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()); } }
throw new CheckstyleException(fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex);
exitStatus = EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE; errorCounter = 1; ex.printStackTrace();
/** * Retrieves the folder path from the given {@code file}. * * @param file Input file to examine. * @return The folder path. * @throws CheckstyleException if there is an error getting the canonical * path of the {@code file}. */ private static String getFolderPath(File file) throws CheckstyleException { try { return file.getCanonicalFile().getParent(); } catch (IOException ex) { throw new CheckstyleException("unable to create canonical path names for " + file.getAbsolutePath(), ex); } }
@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 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 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()); } }
/** * Loads the import control file from a file. * @param uri the uri of the file to load. * @return the root {@link PkgImportControl} object. * @throws CheckstyleException if an error occurs. */ public static PkgImportControl load(URI uri) throws CheckstyleException { try (InputStream inputStream = uri.toURL().openStream()) { final InputSource source = new InputSource(inputStream); return load(source, uri); } catch (MalformedURLException ex) { throw new CheckstyleException("syntax error in url " + uri, ex); } catch (IOException ex) { throw new CheckstyleException("unable to find " + uri, ex); } }
@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 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 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()); } }
/** * Reads the file provided and parses it with package names loader. * @param packageFile file from package * @param namesLoader package names loader * @throws SAXException if an error while parsing occurs * @throws CheckstyleException if unable to open file */ private static void processFile(URL packageFile, PackageNamesLoader namesLoader) throws SAXException, CheckstyleException { try (InputStream stream = new BufferedInputStream(packageFile.openStream())) { final InputSource source = new InputSource(stream); namesLoader.parseInputSource(source); } catch (IOException ex) { throw new CheckstyleException("unable to open " + packageFile, ex); } }