Search in sources :

Example 26 with CheckstyleException

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

the class SuppressWithNearbyCommentFilterTest method testInvalidCheckFormat.

@Test
public void testInvalidCheckFormat() throws Exception {
    final DefaultConfiguration filterConfig = createFilterConfig(SuppressWithNearbyCommentFilter.class);
    filterConfig.addAttribute("checkFormat", "a[l");
    try {
        final String[] suppressed = CommonUtils.EMPTY_STRING_ARRAY;
        verifySuppressed(filterConfig, suppressed);
    } catch (CheckstyleException ex) {
        final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause();
        assertEquals("unable to parse expanded comment a[l", cause.getMessage());
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 27 with CheckstyleException

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

the class PackageNamesLoaderTest method testPackagesWithSaxException.

@Test
@SuppressWarnings("unchecked")
public void testPackagesWithSaxException() throws Exception {
    final URLConnection mockConnection = Mockito.mock(URLConnection.class);
    when(mockConnection.getInputStream()).thenReturn(new ByteArrayInputStream(EMPTY_BYTE_ARRAY));
    final URL url = getMockUrl(mockConnection);
    final Enumeration<URL> enumeration = mock(Enumeration.class);
    when(enumeration.hasMoreElements()).thenReturn(true);
    when(enumeration.nextElement()).thenReturn(url);
    final ClassLoader classLoader = mock(ClassLoader.class);
    when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration);
    try {
        PackageNamesLoader.getPackageNames(classLoader);
        fail("CheckstyleException is expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getCause() instanceof SAXException);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URLConnection(java.net.URLConnection) URL(java.net.URL) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 28 with CheckstyleException

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

the class TreeWalkerTest method testProcessWithRecognitionException.

@Test
public void testProcessWithRecognitionException() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.configure(createCheckConfig(TypeNameCheck.class));
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), 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(" class a%$# {} ");
    try {
        treeWalker.processFiltered(file, lines);
    } catch (CheckstyleException exception) {
        assertTrue(exception.getMessage().contains("TokenStreamRecognitionException occurred during the analysis of file"));
    }
}
Also used : ArrayList(java.util.ArrayList) TypeNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) File(java.io.File) Test(org.junit.Test)

Example 29 with CheckstyleException

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

the class ConfigurationLoaderTest method testLoadConfigurationFromClassPath.

@Test
public void testLoadConfigurationFromClassPath() {
    try {
        final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration("/com/puppycrawl/tools/checkstyle/configs/" + "config_with_ignore.xml", new PropertiesExpander(new Properties()), true);
        final Configuration[] children = config.getChildren();
        assertEquals(0, children[0].getChildren().length);
    } catch (CheckstyleException ex) {
        fail("unexpected exception");
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with CheckstyleException

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

the class ImportControlLoaderTest method testWrongFormatUri.

@Test
public void testWrongFormatUri() throws Exception {
    try {
        ImportControlLoader.load(new URI("aaa://" + getPath("import-control_complete.xml")));
        fail("exception expected");
    } catch (CheckstyleException ex) {
        assertSame(MalformedURLException.class, ex.getCause().getClass());
        assertEquals("unknown protocol: aaa", ex.getCause().getMessage());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URI(java.net.URI) Test(org.junit.Test)

Aggregations

CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)78 Test (org.junit.Test)46 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)33 File (java.io.File)15 IOException (java.io.IOException)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)9 Properties (java.util.Properties)8 URL (java.net.URL)6 SAXException (org.xml.sax.SAXException)5 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)4 URI (java.net.URI)4 Checker (com.puppycrawl.tools.checkstyle.Checker)3 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)3 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3