Search in sources :

Example 51 with CheckstyleException

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

the class SuppressWithNearbyCommentFilterTest method testInvalidInfluenceFormat.

@Test
public void testInvalidInfluenceFormat() throws Exception {
    final DefaultConfiguration filterConfig = createFilterConfig(SuppressWithNearbyCommentFilter.class);
    filterConfig.addAttribute("influenceFormat", "a");
    try {
        final String[] suppressed = CommonUtils.EMPTY_STRING_ARRAY;
        verifySuppressed(filterConfig, suppressed);
    } catch (CheckstyleException ex) {
        final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause();
        assertEquals("unable to parse influence" + " from 'SUPPRESS CHECKSTYLE MemberNameCheck' using a", cause.getMessage());
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Example 52 with CheckstyleException

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

the class MainTest method testLoadPropertiesIoException.

@Test
public void testLoadPropertiesIoException() throws Exception {
    final Class<?>[] param = new Class<?>[1];
    param[0] = File.class;
    final Method method = Main.class.getDeclaredMethod("loadProperties", param);
    method.setAccessible(true);
    try {
        if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("windows")) {
            // https://support.microsoft.com/en-us/kb/177506 but this only for NTFS
            // WindowsServer 2012 use Resilient File System (ReFS), so any name is ok
            final File file = new File(File.separator + ":invalid");
            if (file.exists()) {
                file.delete();
            }
            method.invoke(null, new File(file.getAbsolutePath()));
        } else {
            method.invoke(null, new File(File.separator + "\0:invalid"));
        }
        fail("Exception was expected");
    } catch (InvocationTargetException ex) {
        assertTrue(ex.getCause() instanceof CheckstyleException);
        // We do separate validation for message as in Windows
        // disk drive letter appear in message,
        // so we skip that drive letter for compatibility issues
        assertTrue(ex.getCause().getMessage().startsWith("Unable to load properties from file '"));
        assertTrue(ex.getCause().getMessage().endsWith(":invalid'."));
    }
}
Also used : BeforeClass(org.junit.BeforeClass) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Method(java.lang.reflect.Method) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 53 with CheckstyleException

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

the class EmptyBlockCheckTest method testInvalidOption.

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

Example 54 with CheckstyleException

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

the class HeaderCheckTest method testNoHeader.

@Test
public void testNoHeader() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
    try {
        createChecker(checkConfig);
        final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
        verify(checkConfig, getPath("InputRegexpHeader1.java"), expected);
    } catch (CheckstyleException ex) {
        // Exception is not expected
        fail("Exception is not expected");
    }
}
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)

Example 55 with CheckstyleException

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

the class HeaderCheckTest method testEmptyFilename.

@Test
public void testEmptyFilename() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
    checkConfig.addAttribute("headerFile", "");
    try {
        createChecker(checkConfig);
        fail("Checker creation should not succeed with invalid headerFile");
    } catch (CheckstyleException ex) {
        assertEquals("cannot initialize module" + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck" + " - Cannot set property 'headerFile' to '' in module" + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck", ex.getMessage());
    }
}
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