View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.qulice.spi.Environment;
8   import java.io.File;
9   import org.cactoos.io.ResourceOf;
10  import org.cactoos.text.FormattedText;
11  import org.cactoos.text.IoCheckedText;
12  import org.cactoos.text.TextOf;
13  import org.hamcrest.MatcherAssert;
14  import org.hamcrest.Matchers;
15  import org.junit.jupiter.api.Test;
16  
17  /**
18   * Test case for {@link CheckstyleValidator}'s handling of the
19   * {@code SingleSpaceSeparatorCheck}.
20   *
21   * @since 0.24.2
22   */
23  final class CheckstyleSingleSpaceSeparatorTest {
24  
25      @Test
26      void rejectsDoubleWhitespaceInFieldDeclaration() throws Exception {
27          final String file = "DoubleWhitespaceFieldDecl.java";
28          final Environment.Mock mock = new Environment.Mock();
29          final Environment env = mock.withParam(
30              "license",
31              String.format(
32                  "file:%s",
33                  new License().savePackageInfo(
34                      new File(mock.basedir(), "src/main/java/foo")
35                  ).withLines("Hello.")
36                      .withEol(String.valueOf('\n')).file()
37              )
38          ).withFile(
39              String.format("src/main/java/foo/%s", file),
40              new IoCheckedText(
41                  new TextOf(
42                      new ResourceOf(
43                          new FormattedText("com/qulice/checkstyle/%s", file)
44                      )
45                  )
46              ).asString()
47          );
48          MatcherAssert.assertThat(
49              "double whitespace in a field declaration must be reported",
50              new CheckstyleValidator(env).validate(env.files(file)),
51              Matchers.hasItem(
52                  new ViolationMatcher(
53                      "Use a single space to separate non-whitespace characters.",
54                      file, "18", "SingleSpaceSeparatorCheck"
55                  )
56              )
57          );
58      }
59  }