Search in sources :

Example 1 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class CustomImportOrderCheckTest method testSamePackageDepthNegative.

@Test
public void testSamePackageDepthNegative() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class);
    checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
    checkConfig.addAttribute("separateLineBetweenGroups", "false");
    checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(-1)");
    try {
        final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
        verify(checkConfig, getPath("InputCustomImportOrder.java"), expected);
        fail("exception expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "Cannot set property 'customImportOrderRules' to " + "'SAME_PACKAGE(-1)' in module"));
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 2 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class CustomImportOrderCheckTest method testSamePackageDepthNotInt.

@Test
public void testSamePackageDepthNotInt() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class);
    checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(INT_IS_REQUIRED_HERE)");
    checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
    try {
        final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
        verify(checkConfig, getPath("InputCustomImportOrder.java"), expected);
        fail("exception expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "Cannot set property 'customImportOrderRules' to " + "'SAME_PACKAGE(INT_IS_REQUIRED_HERE)' in module"));
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 3 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class ImportControlLoader method load.

/**
     * Loads the import control file from a file.
     * @param uri the uri of the file to load.
     * @return the root {@link ImportControl} object.
     * @throws CheckstyleException if an error occurs.
     */
public static ImportControl load(final URI uri) throws CheckstyleException {
    final InputStream inputStream;
    try {
        inputStream = uri.toURL().openStream();
    } catch (final MalformedURLException ex) {
        throw new CheckstyleException("syntax error in url " + uri, ex);
    } catch (final IOException ex) {
        throw new CheckstyleException("unable to find " + uri, ex);
    }
    final InputSource source = new InputSource(inputStream);
    return load(source, uri);
}
Also used : MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException)

Example 4 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class XdocsPagesTest method validateCheckstyleXml.

private static void validateCheckstyleXml(String fileName, String code, String unserializedSource) throws IOException, CheckstyleException {
    // can't process non-existent examples, or out of context snippets
    if (!code.contains("com.mycompany") && !code.contains("checkstyle-packages") && !code.contains("MethodLimit") && !code.contains("<suppress ") && !code.contains("<import-control ") && !unserializedSource.startsWith("<property ") && !unserializedSource.startsWith("<taskdef ")) {
        // validate checkstyle structure and contents
        try {
            final Properties properties = new Properties();
            properties.setProperty("checkstyle.header.file", new File("config/java.header").getCanonicalPath());
            final PropertiesExpander expander = new PropertiesExpander(properties);
            final Configuration config = ConfigurationLoader.loadConfiguration(new InputSource(new StringReader(code)), expander, false);
            final Checker checker = new Checker();
            try {
                final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
                checker.setModuleClassLoader(moduleClassLoader);
                checker.configure(config);
            } finally {
                checker.destroy();
            }
        } catch (CheckstyleException ex) {
            throw new CheckstyleException(fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) StringReader(java.io.StringReader) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) File(java.io.File)

Example 5 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class CommonUtilsTest method testLoadSuppressionsUriSyntaxException.

@Test
@PrepareForTest({ CommonUtils.class, CommonUtilsTest.class })
@SuppressWarnings("unchecked")
public void testLoadSuppressionsUriSyntaxException() throws Exception {
    final URL configUrl = mock(URL.class);
    when(configUrl.toURI()).thenThrow(URISyntaxException.class);
    mockStatic(CommonUtils.class, Mockito.CALLS_REAL_METHODS);
    final String fileName = "suppressions_none.xml";
    when(CommonUtils.class.getResource(fileName)).thenReturn(configUrl);
    try {
        CommonUtils.getUriByFilename(fileName);
        fail("Exception is expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getCause() instanceof URISyntaxException);
        assertEquals("Unable to find: " + fileName, ex.getMessage());
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)145 Test (org.junit.jupiter.api.Test)58 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)56 File (java.io.File)32 IOException (java.io.IOException)27 Test (org.junit.Test)26 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)18 Properties (java.util.Properties)15 ArrayList (java.util.ArrayList)14 URL (java.net.URL)11 Violation (com.puppycrawl.tools.checkstyle.api.Violation)10 FileText (com.puppycrawl.tools.checkstyle.api.FileText)8 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)7 SAXException (org.xml.sax.SAXException)7 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)6 InputStream (java.io.InputStream)6 URI (java.net.URI)6 Set (java.util.Set)6 Checker (com.puppycrawl.tools.checkstyle.Checker)5 MalformedURLException (java.net.MalformedURLException)5