Search in sources :

Example 1 with AUDIT_STARTED_MESSAGE

use of com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE in project checkstyle by checkstyle.

the class CheckerTest method testDuplicatedModule.

@Test
public void testDuplicatedModule() throws Exception {
    // we need to test a module with two instances, one with id and the other not
    final DefaultConfiguration moduleConfig1 = createModuleConfig(NewlineAtEndOfFileCheck.class);
    final DefaultConfiguration moduleConfig2 = createModuleConfig(NewlineAtEndOfFileCheck.class);
    moduleConfig2.addProperty("id", "ModuleId");
    final DefaultConfiguration root = new DefaultConfiguration("root");
    root.addChild(moduleConfig1);
    root.addChild(moduleConfig2);
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(root);
    // BriefUtLogger does not print the module name or id postfix,
    // so we need to set logger manually
    final ByteArrayOutputStream out = TestUtil.getInternalState(this, "stream");
    final DefaultLogger logger = new DefaultLogger(out, OutputStreamOptions.CLOSE, out, OutputStreamOptions.NONE, new AuditEventDefaultFormatter());
    checker.addListener(logger);
    final String path = File.createTempFile("file", ".java", temporaryFolder).getPath();
    final String violationMessage = getCheckMessage(NewlineAtEndOfFileCheck.class, MSG_KEY_NO_NEWLINE_EOF);
    final String[] expected = { "1: " + violationMessage + " [NewlineAtEndOfFile]", "1: " + violationMessage + " [ModuleId]" };
    // super.verify does not work here, for we change the logger
    out.flush();
    final int errs = checker.process(Collections.singletonList(new File(path)));
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
        // we need to ignore the unrelated lines
        final List<String> actual = lnr.lines().filter(line -> !getCheckMessage(AUDIT_STARTED_MESSAGE).equals(line)).filter(line -> !getCheckMessage(AUDIT_FINISHED_MESSAGE).equals(line)).limit(expected.length).sorted().collect(Collectors.toList());
        Arrays.sort(expected);
        for (int i = 0; i < expected.length; i++) {
            final String expectedResult = "[ERROR] " + path + ":" + expected[i];
            assertWithMessage("error message " + i).that(actual.get(i)).isEqualTo(expectedResult);
        }
        assertWithMessage("unexpected output: " + lnr.readLine()).that(errs).isEqualTo(expected.length);
    }
    checker.destroy();
}
Also used : Context(com.puppycrawl.tools.checkstyle.api.Context) Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) AUDIT_STARTED_MESSAGE(com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE) CloseAndFlushTestByteArrayOutputStream(com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) OutputStreamOptions(com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions) MSG_KEY_NO_NEWLINE_EOF(com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF) Method(java.lang.reflect.Method) SuppressionFilter(com.puppycrawl.tools.checkstyle.filters.SuppressionFilter) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) TestFileSetCheck(com.puppycrawl.tools.checkstyle.internal.testmodules.TestFileSetCheck) CommonUtil(com.puppycrawl.tools.checkstyle.utils.CommonUtil) AbstractFileSetCheck(com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck) Set(java.util.Set) LineNumberReader(java.io.LineNumberReader) ExternalResourceHolder(com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) TokenTypes(com.puppycrawl.tools.checkstyle.api.TokenTypes) List(java.util.List) TempDir(org.junit.jupiter.api.io.TempDir) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) HiddenFieldCheck(com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck) TestUtil(com.puppycrawl.tools.checkstyle.internal.utils.TestUtil) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EXCEPTION_MSG(com.puppycrawl.tools.checkstyle.Checker.EXCEPTION_MSG) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TestBeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.TestBeforeExecutionFileFilter) MessageDispatcher(com.puppycrawl.tools.checkstyle.api.MessageDispatcher) Violation(com.puppycrawl.tools.checkstyle.api.Violation) TranslationCheck(com.puppycrawl.tools.checkstyle.checks.TranslationCheck) Filter(com.puppycrawl.tools.checkstyle.api.Filter) Properties(java.util.Properties) Files(java.nio.file.Files) IOException(java.io.IOException) AUDIT_FINISHED_MESSAGE(com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_FINISHED_MESSAGE) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileText(com.puppycrawl.tools.checkstyle.api.FileText) IOError(java.io.IOError) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) NewlineAtEndOfFileCheck(com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck) BufferedReader(java.io.BufferedReader) DebugFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter) Collections(java.util.Collections) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) CloseAndFlushTestByteArrayOutputStream(com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineNumberReader(java.io.LineNumberReader) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 EXCEPTION_MSG (com.puppycrawl.tools.checkstyle.Checker.EXCEPTION_MSG)1 AUDIT_FINISHED_MESSAGE (com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_FINISHED_MESSAGE)1 AUDIT_STARTED_MESSAGE (com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE)1 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)1 AbstractFileSetCheck (com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck)1 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)1 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)1 OutputStreamOptions (com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 Context (com.puppycrawl.tools.checkstyle.api.Context)1 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)1 ExternalResourceHolder (com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder)1 FileText (com.puppycrawl.tools.checkstyle.api.FileText)1 Filter (com.puppycrawl.tools.checkstyle.api.Filter)1 FilterSet (com.puppycrawl.tools.checkstyle.api.FilterSet)1 MessageDispatcher (com.puppycrawl.tools.checkstyle.api.MessageDispatcher)1 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)1 Violation (com.puppycrawl.tools.checkstyle.api.Violation)1