Search in sources :

Example 21 with CheckstyleException

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

the class RegexpOnFilenameCheckTest method testException.

@Test
public void testException() throws Exception {
    // escape character needed for testing IOException from File.getCanonicalPath on all OSes
    final File file = new File(getPath("") + "" + File.separatorChar + "Test");
    try {
        final RegexpOnFilenameCheck check = new RegexpOnFilenameCheck();
        check.setFileNamePattern(Pattern.compile("BAD"));
        check.process(file, null);
        fail("CheckstyleException expected");
    } catch (CheckstyleException ex) {
        assertEquals("unable to create canonical path names for " + file.getAbsolutePath(), ex.getMessage());
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) File(java.io.File) Test(org.junit.Test)

Example 22 with CheckstyleException

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

the class CommonUtils method getUriByFilename.

/**
     * Resolve the specified filename to a URI.
     * @param filename name os the file
     * @return resolved header file URI
     * @throws CheckstyleException on failure
     */
public static URI getUriByFilename(String filename) throws CheckstyleException {
    // figure out if this is a File or a URL
    URI uri;
    try {
        final URL url = new URL(filename);
        uri = url.toURI();
    } catch (final URISyntaxException | MalformedURLException ignored) {
        uri = null;
    }
    if (uri == null) {
        final File file = new File(filename);
        if (file.exists()) {
            uri = file.toURI();
        } else {
            // check to see if the file is in the classpath
            try {
                final URL configUrl = CommonUtils.class.getResource(filename);
                if (configUrl == null) {
                    throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename);
                }
                uri = configUrl.toURI();
            } catch (final URISyntaxException ex) {
                throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename, ex);
            }
        }
    }
    return uri;
}
Also used : MalformedURLException(java.net.MalformedURLException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) URL(java.net.URL)

Example 23 with CheckstyleException

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

the class MainFrameModel method openFile.

/**
     * Open file and load the file.
     * @param file the file to open.
     * @throws CheckstyleException if the file can not be parsed.
     */
public void openFile(File file) throws CheckstyleException {
    if (file != null) {
        try {
            currentFile = file;
            title = "Checkstyle GUI : " + file.getName();
            reloadActionEnabled = true;
            final DetailAST parseTree;
            switch(parseMode) {
                case PLAIN_JAVA:
                    parseTree = parseFile(file);
                    break;
                case JAVA_WITH_COMMENTS:
                case JAVA_WITH_JAVADOC_AND_COMMENTS:
                    parseTree = parseFileWithComments(file);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown mode: " + parseMode);
            }
            parseTreeTableModel.setParseTree(parseTree);
            parseTreeTableModel.setParseMode(parseMode);
            final String[] sourceLines = getFileText(file).toLinesArray();
            // clear for each new file
            linesToPosition.clear();
            // starts line counting at 1
            linesToPosition.add(0);
            final StringBuilder sb = new StringBuilder();
            // insert the contents of the file to the text area
            for (final String element : sourceLines) {
                linesToPosition.add(sb.length());
                sb.append(element).append(System.lineSeparator());
            }
            text = sb.toString();
        } catch (IOException | ANTLRException ex) {
            final String exceptionMsg = String.format(Locale.ROOT, "%s occurred while opening file %s.", ex.getClass().getSimpleName(), file.getPath());
            throw new CheckstyleException(exceptionMsg, ex);
        }
    }
}
Also used : ANTLRException(antlr.ANTLRException) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException)

Example 24 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)

Example 25 with CheckstyleException

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

the class ImportOrderCheckTest method testGroupWithSlashes.

@Test
public void testGroupWithSlashes() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(ImportOrderCheck.class);
    checkConfig.addAttribute("groups", "/^javax");
    try {
        final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
        verify(checkConfig, getPath("InputImportOrder.java"), expected);
        fail("exception expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "Cannot set property 'groups' to '/^javax' in module"));
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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