Search in sources :

Example 46 with CheckstyleException

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

the class TreeWalkerTest method testProcessWithParserThrowable.

@Test
public void testProcessWithParserThrowable() 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(" classD a {} ");
    try {
        treeWalker.processFiltered(file, lines);
    } catch (CheckstyleException exception) {
        assertTrue(exception.getMessage().contains("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 47 with CheckstyleException

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

the class PackageNamesLoaderTest method testPackagesWithIoException.

@Test
@SuppressWarnings("unchecked")
public void testPackagesWithIoException() throws Exception {
    final URLConnection mockConnection = Mockito.mock(URLConnection.class);
    when(mockConnection.getInputStream()).thenReturn(null);
    final URL url = getMockUrl(mockConnection);
    final Enumeration<URL> enumer = mock(Enumeration.class);
    when(enumer.hasMoreElements()).thenReturn(true);
    when(enumer.nextElement()).thenReturn(url);
    final ClassLoader classLoader = mock(ClassLoader.class);
    when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumer);
    try {
        PackageNamesLoader.getPackageNames(classLoader);
        fail("CheckstyleException is expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getCause() instanceof IOException);
        assertNotEquals("unable to get package file resources", ex.getMessage());
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 48 with CheckstyleException

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

the class AbstractTypeAwareCheckTest method testWithoutLogErrors.

@Test
public void testWithoutLogErrors() throws Exception {
    final DefaultConfiguration config = createCheckConfig(JavadocMethodCheck.class);
    config.addAttribute("logLoadErrors", "false");
    config.addAttribute("allowUndeclaredRTE", "true");
    final String[] expected = { "7:8: " + getCheckMessage(MSG_CLASS_INFO, "@throws", "InvalidExceptionName") };
    try {
        verify(config, getPath("InputLoadErrors.java"), expected);
    } catch (CheckstyleException ex) {
        final IllegalStateException cause = (IllegalStateException) ex.getCause();
        assertEquals(getCheckMessage(MSG_CLASS_INFO, "@throws", "InvalidExceptionName"), cause.getMessage());
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 49 with CheckstyleException

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

the class AllChecksTest method validateAllCheckTokensAreReferencedInConfigFile.

private static void validateAllCheckTokensAreReferencedInConfigFile(String configName, Configuration configuration, Map<String, Set<String>> tokensToIgnore) throws Exception {
    final ModuleFactory moduleFactory = TestUtils.getPackageObjectFactory();
    final Set<Configuration> configChecks = ConfigurationUtil.getChecks(configuration);
    final Map<String, Set<String>> configCheckTokens = new HashMap<>();
    final Map<String, Set<String>> checkTokens = new HashMap<>();
    for (Configuration checkConfig : configChecks) {
        final String checkName = checkConfig.getName();
        final Object instance;
        try {
            instance = moduleFactory.createModule(checkName);
        } catch (CheckstyleException ex) {
            throw new CheckstyleException("Couldn't find check: " + checkName, ex);
        }
        if (instance instanceof AbstractCheck) {
            final AbstractCheck check = (AbstractCheck) instance;
            Set<String> configTokens = configCheckTokens.get(checkName);
            if (configTokens == null) {
                configTokens = new HashSet<>();
                configCheckTokens.put(checkName, configTokens);
                // add all overriden tokens
                final Set<String> overrideTokens = tokensToIgnore.get(checkName);
                if (overrideTokens != null) {
                    configTokens.addAll(overrideTokens);
                }
                configTokens.addAll(CheckUtil.getTokenNameSet(check.getDefaultTokens()));
                checkTokens.put(checkName, CheckUtil.getTokenNameSet(check.getAcceptableTokens()));
            }
            try {
                configTokens.addAll(Arrays.asList(checkConfig.getAttribute("tokens").trim().split(",\\s*")));
            } catch (CheckstyleException ex) {
            // no tokens defined, so it is using default
            }
        }
    }
    for (Entry<String, Set<String>> entry : checkTokens.entrySet()) {
        Assert.assertEquals("'" + entry.getKey() + "' should have all acceptable tokens from check in " + configName + " config or specify an override to ignore the specific tokens", entry.getValue(), configCheckTokens.get(entry.getKey()));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) HashMap(java.util.HashMap) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck)

Example 50 with CheckstyleException

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

the class MainFrameModelTest method testOpenFileNonCompilableFile.

@Test
public void testOpenFileNonCompilableFile() {
    final File nonCompilableFile = new File(getNonCompilablePath(FILE_NAME_NON_COMPILABLE));
    try {
        model.openFile(nonCompilableFile);
        fail("Expected CheckstyleException is not thrown.");
    } catch (CheckstyleException ex) {
        final String expectedMsg = String.format(Locale.ROOT, "NoViableAltException occurred while opening file %s.", nonCompilableFile.getPath());
        assertEquals(expectedMsg, ex.getMessage());
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) File(java.io.File) 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