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"));
}
}
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());
}
}
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());
}
}
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()));
}
}
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());
}
}
Aggregations