use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class CustomImportOrderCheckTest method testSamePackageDepthNegative.
@Test
public void testSamePackageDepthNegative() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class);
checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
checkConfig.addAttribute("separateLineBetweenGroups", "false");
checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(-1)");
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputCustomImportOrder.java"), expected);
fail("exception expected");
} catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "Cannot set property 'customImportOrderRules' to " + "'SAME_PACKAGE(-1)' in module"));
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class CustomImportOrderCheckTest method testSamePackageDepthNotInt.
@Test
public void testSamePackageDepthNotInt() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class);
checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(INT_IS_REQUIRED_HERE)");
checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputCustomImportOrder.java"), expected);
fail("exception expected");
} catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "Cannot set property 'customImportOrderRules' to " + "'SAME_PACKAGE(INT_IS_REQUIRED_HERE)' in module"));
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class ImportControlLoader method load.
/**
* Loads the import control file from a file.
* @param uri the uri of the file to load.
* @return the root {@link ImportControl} object.
* @throws CheckstyleException if an error occurs.
*/
public static ImportControl load(final URI uri) throws CheckstyleException {
final InputStream inputStream;
try {
inputStream = uri.toURL().openStream();
} catch (final MalformedURLException ex) {
throw new CheckstyleException("syntax error in url " + uri, ex);
} catch (final IOException ex) {
throw new CheckstyleException("unable to find " + uri, ex);
}
final InputSource source = new InputSource(inputStream);
return load(source, uri);
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class XdocsPagesTest method validateCheckstyleXml.
private static void validateCheckstyleXml(String fileName, String code, String unserializedSource) throws IOException, CheckstyleException {
// can't process non-existent examples, or out of context snippets
if (!code.contains("com.mycompany") && !code.contains("checkstyle-packages") && !code.contains("MethodLimit") && !code.contains("<suppress ") && !code.contains("<import-control ") && !unserializedSource.startsWith("<property ") && !unserializedSource.startsWith("<taskdef ")) {
// validate checkstyle structure and contents
try {
final Properties properties = new Properties();
properties.setProperty("checkstyle.header.file", new File("config/java.header").getCanonicalPath());
final PropertiesExpander expander = new PropertiesExpander(properties);
final Configuration config = ConfigurationLoader.loadConfiguration(new InputSource(new StringReader(code)), expander, false);
final Checker checker = new Checker();
try {
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
checker.setModuleClassLoader(moduleClassLoader);
checker.configure(config);
} finally {
checker.destroy();
}
} catch (CheckstyleException ex) {
throw new CheckstyleException(fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex);
}
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class CommonUtilsTest method testLoadSuppressionsUriSyntaxException.
@Test
@PrepareForTest({ CommonUtils.class, CommonUtilsTest.class })
@SuppressWarnings("unchecked")
public void testLoadSuppressionsUriSyntaxException() throws Exception {
final URL configUrl = mock(URL.class);
when(configUrl.toURI()).thenThrow(URISyntaxException.class);
mockStatic(CommonUtils.class, Mockito.CALLS_REAL_METHODS);
final String fileName = "suppressions_none.xml";
when(CommonUtils.class.getResource(fileName)).thenReturn(configUrl);
try {
CommonUtils.getUriByFilename(fileName);
fail("Exception is expected");
} catch (CheckstyleException ex) {
assertTrue(ex.getCause() instanceof URISyntaxException);
assertEquals("Unable to find: " + fileName, ex.getMessage());
}
}
Aggregations