Search in sources :

Example 11 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class SuppressWarningsHolderTest method testIsSuppressed.

@Test
public void testIsSuppressed() throws Exception {
    final Class<?> entry = Class.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
    final Constructor<?> entryConstructor = entry.getDeclaredConstructor(String.class, int.class, int.class, int.class, int.class);
    entryConstructor.setAccessible(true);
    final Object entryInstance = entryConstructor.newInstance("MockEntry", 100, 100, 350, 350);
    final List<Object> entriesList = new ArrayList<>();
    entriesList.add(entryInstance);
    final ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
    PowerMockito.doReturn(entriesList).when(threadLocal, "get");
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final Field entries = holder.getClass().getDeclaredField("ENTRIES");
    entries.setAccessible(true);
    entries.set(holder, threadLocal);
    final Checker source = new Checker();
    final LocalizedMessage message = new LocalizedMessage(100, 10, null, null, null, "id", MemberNameCheck.class, "message");
    final AuditEvent event = new AuditEvent(source, "fileName", message);
    assertFalse(SuppressWarningsHolder.isSuppressed(event));
}
Also used : Field(java.lang.reflect.Field) Checker(com.puppycrawl.tools.checkstyle.Checker) ArrayList(java.util.ArrayList) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class FileSetCheckLifecycleTest method testProcessCallsFinishBeforeCallingDestroy.

@Test
public void testProcessCallsFinishBeforeCallingDestroy() throws Exception {
    final DefaultConfiguration dc = new DefaultConfiguration("configuration");
    final DefaultConfiguration twConf = createCheckConfig(TreeWalker.class);
    dc.addAttribute("charset", "UTF-8");
    dc.addChild(twConf);
    twConf.addChild(new DefaultConfiguration(FileContentsHolder.class.getName()));
    twConf.addChild(new DefaultConfiguration(AvoidStarImportCheck.class.getName()));
    final Checker checker = new Checker();
    final Locale locale = Locale.ROOT;
    checker.setLocaleCountry(locale.getCountry());
    checker.setLocaleLanguage(locale.getLanguage());
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(dc);
    checker.addListener(new BriefUtLogger(stream));
    checker.addFileSetCheck(new TestFileSetCheck());
    final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
    verify(checker, getPath("InputIllegalTokens.java"), expected);
    assertTrue("FileContent should be available during finishProcessing() call", TestFileSetCheck.isFileContentAvailable());
}
Also used : Locale(java.util.Locale) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) Test(org.junit.Test)

Example 13 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class AbstractJavadocCheckTest method testWithMultipleChecks.

@Test
public void testWithMultipleChecks() throws Exception {
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
    final DefaultConfiguration checksConfig = createCheckConfig(TreeWalker.class);
    checksConfig.addChild(createCheckConfig(AtclauseOrderCheck.class));
    checksConfig.addChild(createCheckConfig(JavadocParagraphCheck.class));
    checkerConfig.addChild(checksConfig);
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(checkerConfig);
    verify(checker, getPath("InputCorrectJavaDocParagraph.java"));
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Test(org.junit.Test)

Example 14 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class IndentationCheckTest method verify.

private void verify(Configuration config, String filePath, String[] expected, final IndentComment... linesWithWarn) throws Exception {
    final Checker checker = createChecker(config);
    checker.addListener(new IndentAudit(linesWithWarn));
    checker.setLocaleCountry(Locale.ROOT.getCountry());
    checker.setLocaleLanguage(Locale.ROOT.getLanguage());
    checker.finishLocalSetup();
    verify(checker, new File[] { new File(filePath) }, filePath, expected);
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) File(java.io.File)

Example 15 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class IndentationCheckTest method testInvalidArrayInitWithChecker.

@Test
public void testInvalidArrayInitWithChecker() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);
    checkConfig.addAttribute("arrayInitIndent", "4");
    checkConfig.addAttribute("basicOffset", "4");
    checkConfig.addAttribute("braceAdjustment", "0");
    checkConfig.addAttribute("caseIndent", "4");
    checkConfig.addAttribute("forceStrictCondition", "false");
    checkConfig.addAttribute("lineWrappingIndentation", "4");
    checkConfig.addAttribute("tabWidth", "4");
    checkConfig.addAttribute("throwsIndent", "4");
    final String fileName = getPath("InputInvalidArrayInitIndent.java");
    final String[] expected = { "21: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4), "22: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4), "24: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4), "28: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4), "29: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 8, 10), "30: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 4, "6, 10"), "33: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 9, 8), "34: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 7, 8), "35: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 9, 8), "40: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"), "44: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "4, 8"), "48: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"), "52: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 20, "8, 31, 33"), "53: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 4, "8, 31, 33"), "58: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 6, 8), "63: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4), "65: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4), "66: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 2, "6, 10"), "69: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 6, 8), "76: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12), "89: " + getCheckMessage(MSG_ERROR, "1", 8, 12), "100: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12), "101: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12), "104: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12), "105: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12), "106: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"), "109: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 6, "8, 12"), "110: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12), "111: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12), "112: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12") };
    final Checker checker = createChecker(checkConfig);
    checker.setLocaleCountry(Locale.ROOT.getCountry());
    checker.setLocaleLanguage(Locale.ROOT.getLanguage());
    checker.finishLocalSetup();
    //Test input for this test case is not checked due to issue #693.
    verify(checker, fileName, expected);
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Test(org.junit.Test)

Aggregations

Checker (com.puppycrawl.tools.checkstyle.Checker)22 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)17 Test (org.junit.Test)13 BriefUtLogger (com.puppycrawl.tools.checkstyle.BriefUtLogger)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Locale (java.util.Locale)5 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)4 File (java.io.File)4 IllegalCatchCheck (com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck)3 ArrayList (java.util.ArrayList)3 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)2 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)2 FileContentsHolder (com.puppycrawl.tools.checkstyle.checks.FileContentsHolder)2 Field (java.lang.reflect.Field)2 Properties (java.util.Properties)2 BaseCheckTestSupport (com.puppycrawl.tools.checkstyle.BaseCheckTestSupport)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PackageObjectFactory (com.puppycrawl.tools.checkstyle.PackageObjectFactory)1