Search in sources :

Example 6 with CheckstyleException

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

the class NewlineAtEndOfFileCheckTest method testSetLineSeparatorFailure.

@Test
public void testSetLineSeparatorFailure() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(NewlineAtEndOfFileCheck.class);
    checkConfig.addAttribute("lineSeparator", "ct");
    try {
        createChecker(checkConfig);
        fail("exception expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle." + "checks.NewlineAtEndOfFileCheck - " + "Cannot set property 'lineSeparator' to 'ct' in module"));
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 7 with CheckstyleException

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

the class SuppressionCommentFilterTest method testInvalidCheckFormat.

@Test
public void testInvalidCheckFormat() throws Exception {
    final DefaultConfiguration filterConfig = createFilterConfig(SuppressionCommentFilter.class);
    filterConfig.addAttribute("checkFormat", "e[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 e[l", cause.getMessage());
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 8 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 9 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 10 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)

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