Search in sources :

Example 1 with CheckstyleAntTaskLogStub

use of com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub in project checkstyle by checkstyle.

the class CheckstyleAntTaskTest method testPathsFileWithLogVerification.

@Test
public final void testPathsFileWithLogVerification() throws IOException {
    // given
    TestRootModuleChecker.reset();
    final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
    antTask.setConfig(getPath(CUSTOM_ROOT_CONFIG_FILE));
    antTask.setProject(new Project());
    final FileSet examinationFileSet = new FileSet();
    examinationFileSet.setFile(new File(getPath(FLAWLESS_INPUT)));
    final Path sourcePath = new Path(antTask.getProject());
    sourcePath.addFileset(examinationFileSet);
    antTask.addPath(sourcePath);
    antTask.addPath(new Path(new Project()));
    // when
    antTask.execute();
    // then
    final List<MessageLevelPair> loggedMessages = antTask.getLoggedMessages();
    assertWithMessage("Scanning path was not logged").that(loggedMessages.stream().filter(msg -> msg.getMsg().startsWith("1) Scanning path")).count()).isEqualTo(1);
    assertWithMessage("Scanning path was not logged").that(loggedMessages.stream().filter(msg -> msg.getMsg().startsWith("1) Adding 1 files from path")).count()).isEqualTo(1);
    assertWithMessage("Scanning empty was logged").that(loggedMessages.stream().filter(msg -> msg.getMsg().startsWith("2) Adding 0 files from path ")).count()).isEqualTo(0);
    assertWithMessage("Checker is not processed").that(TestRootModuleChecker.isProcessed()).isTrue();
    final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
    assertWithMessage("There are more files to check than expected").that(filesToCheck).hasSize(1);
    assertWithMessage("The path of file differs from expected").that(filesToCheck.get(0).getAbsolutePath()).isEqualTo(getPath(FLAWLESS_INPUT));
}
Also used : Path(org.apache.tools.ant.types.Path) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TestUtil(com.puppycrawl.tools.checkstyle.internal.utils.TestUtil) MessageLevelPair(com.puppycrawl.tools.checkstyle.internal.testmodules.MessageLevelPair) Arrays(java.util.Arrays) URL(java.net.URL) CheckstyleAntTaskLogStub(com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub) FileResource(org.apache.tools.ant.types.resources.FileResource) Path(org.apache.tools.ant.types.Path) TestRootModuleChecker(com.puppycrawl.tools.checkstyle.internal.testmodules.TestRootModuleChecker) ResourceBundle(java.util.ResourceBundle) Locale(java.util.Locale) FileSet(org.apache.tools.ant.types.FileSet) Map(java.util.Map) Project(org.apache.tools.ant.Project) Definitions(com.puppycrawl.tools.checkstyle.Definitions) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Files(java.nio.file.Files) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) CheckstyleAntTaskStub(com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskStub) List(java.util.List) AbstractPathTestSupport(com.puppycrawl.tools.checkstyle.AbstractPathTestSupport) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) XMLLogger(com.puppycrawl.tools.checkstyle.XMLLogger) Reference(org.apache.tools.ant.types.Reference) Project(org.apache.tools.ant.Project) FileSet(org.apache.tools.ant.types.FileSet) MessageLevelPair(com.puppycrawl.tools.checkstyle.internal.testmodules.MessageLevelPair) CheckstyleAntTaskLogStub(com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with CheckstyleAntTaskLogStub

use of com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub in project checkstyle by checkstyle.

the class CheckstyleAntTaskTest method testExecuteLogOutput.

@Test
public final void testExecuteLogOutput() throws Exception {
    final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL();
    final ResourceBundle bundle = ResourceBundle.getBundle(Definitions.CHECKSTYLE_BUNDLE, Locale.ROOT);
    final String auditStartedMessage = bundle.getString(DefaultLogger.AUDIT_STARTED_MESSAGE);
    final String auditFinishedMessage = bundle.getString(DefaultLogger.AUDIT_FINISHED_MESSAGE);
    final List<MessageLevelPair> expectedList = Arrays.asList(new MessageLevelPair("checkstyle version .*", Project.MSG_VERBOSE), new MessageLevelPair("Adding standalone file for audit", Project.MSG_VERBOSE), new MessageLevelPair("To locate the files took \\d+ ms.", Project.MSG_VERBOSE), new MessageLevelPair("Running Checkstyle  on 1 files", Project.MSG_INFO), new MessageLevelPair("Using configuration file:.*", Project.MSG_VERBOSE), new MessageLevelPair(auditStartedMessage, Project.MSG_DEBUG), new MessageLevelPair(auditFinishedMessage, Project.MSG_DEBUG), new MessageLevelPair("To process the files took \\d+ ms.", Project.MSG_VERBOSE), new MessageLevelPair("Total execution took \\d+ ms.", Project.MSG_VERBOSE));
    final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
    antTask.setProject(new Project());
    antTask.setConfig(url.toString());
    antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
    antTask.execute();
    final List<MessageLevelPair> loggedMessages = antTask.getLoggedMessages();
    assertWithMessage("Amount of log messages is unexpected").that(loggedMessages).hasSize(expectedList.size());
    for (int i = 0; i < expectedList.size(); i++) {
        final MessageLevelPair expected = expectedList.get(i);
        final MessageLevelPair actual = loggedMessages.get(i);
        assertWithMessage("Log messages should match").that(actual.getMsg()).matches(expected.getMsg());
        assertWithMessage("Log levels should be equal").that(actual.getLevel()).isEqualTo(expected.getLevel());
    }
}
Also used : Project(org.apache.tools.ant.Project) MessageLevelPair(com.puppycrawl.tools.checkstyle.internal.testmodules.MessageLevelPair) ResourceBundle(java.util.ResourceBundle) File(java.io.File) CheckstyleAntTaskLogStub(com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 3 with CheckstyleAntTaskLogStub

use of com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub in project checkstyle by checkstyle.

the class CheckstyleAntTaskTest method testPathsDirectoryWithNestedFile.

@Test
public final void testPathsDirectoryWithNestedFile() throws IOException {
    // given
    TestRootModuleChecker.reset();
    final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
    antTask.setConfig(getPath(CUSTOM_ROOT_CONFIG_FILE));
    antTask.setProject(new Project());
    final FileResource fileResource = new FileResource(antTask.getProject(), getPath(""));
    final Path sourcePath = new Path(antTask.getProject());
    sourcePath.add(fileResource);
    antTask.addPath(sourcePath);
    // when
    antTask.execute();
    // then
    assertWithMessage("Checker is not processed").that(TestRootModuleChecker.isProcessed()).isTrue();
    final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
    assertWithMessage("There are more files to check than expected").that(filesToCheck).hasSize(8);
    assertWithMessage("The path of file differs from expected").that(filesToCheck.get(5).getAbsolutePath()).isEqualTo(getPath(FLAWLESS_INPUT));
    assertWithMessage("Amount of logged messages in unexpected").that(antTask.getLoggedMessages()).hasSize(8);
}
Also used : Path(org.apache.tools.ant.types.Path) Project(org.apache.tools.ant.Project) FileResource(org.apache.tools.ant.types.resources.FileResource) CheckstyleAntTaskLogStub(com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

CheckstyleAntTaskLogStub (com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub)3 File (java.io.File)3 Project (org.apache.tools.ant.Project)3 Test (org.junit.jupiter.api.Test)3 MessageLevelPair (com.puppycrawl.tools.checkstyle.internal.testmodules.MessageLevelPair)2 URL (java.net.URL)2 ResourceBundle (java.util.ResourceBundle)2 Path (org.apache.tools.ant.types.Path)2 FileResource (org.apache.tools.ant.types.resources.FileResource)2 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 AbstractPathTestSupport (com.puppycrawl.tools.checkstyle.AbstractPathTestSupport)1 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)1 Definitions (com.puppycrawl.tools.checkstyle.Definitions)1 XMLLogger (com.puppycrawl.tools.checkstyle.XMLLogger)1 CheckstyleAntTaskStub (com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskStub)1 TestRootModuleChecker (com.puppycrawl.tools.checkstyle.internal.testmodules.TestRootModuleChecker)1 TestUtil (com.puppycrawl.tools.checkstyle.internal.utils.TestUtil)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1