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 com.qulice.spi.Violation;
9   import java.io.File;
10  import java.io.IOException;
11  import java.util.Collection;
12  import org.cactoos.io.ResourceOf;
13  import org.cactoos.text.FormattedText;
14  import org.cactoos.text.IoCheckedText;
15  import org.cactoos.text.TextOf;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Test case for {@link CheckstyleValidator}'s enforcement that a
23   * fluent call opening a multi-line block must be attached to the
24   * previous line, see https://github.com/yegor256/qulice/issues/670.
25   *
26   * @since 0.25.1
27   */
28  final class CheckstyleFluentCallFormattingTest {
29  
30      @Test
31      void rejectsFluentCallAloneOnLineOpeningMultiLineBlock() throws Exception {
32          final String file = "InvalidFluentCallFormatting.java";
33          MatcherAssert.assertThat(
34              "Fluent call opening a multi-line block alone on a line must be reported",
35              this.runValidation(file, false),
36              Matchers.hasItem(
37                  new ViolationMatcher(
38                      "A fluent call opening a multi-line block must be attached to the previous line",
39                      file, "22", "RegexpSinglelineCheck"
40                  )
41              )
42          );
43      }
44  
45      @Test
46      void acceptsFluentCallAttachedToPreviousLine() throws Exception {
47          Assertions.assertDoesNotThrow(
48              () -> this.runValidation("ValidFluentCallFormatting.java", true)
49          );
50      }
51  
52      private Collection<Violation> runValidation(final String file,
53          final boolean passes) throws IOException {
54          final Environment.Mock mock = new Environment.Mock();
55          final Environment env = mock.withParam(
56              "license",
57              String.format(
58                  "file:%s",
59                  new License().savePackageInfo(
60                      new File(mock.basedir(), "src/main/java/foo")
61                  ).withLines("Hello.")
62                      .withEol(String.valueOf('\n')).file()
63              )
64          ).withFile(
65              String.format("src/main/java/foo/%s", file),
66              new IoCheckedText(
67                  new TextOf(
68                      new ResourceOf(
69                          new FormattedText("com/qulice/checkstyle/%s", file)
70                      )
71                  )
72              ).asString()
73          );
74          final Collection<Violation> results =
75              new CheckstyleValidator(env).validate(env.files(file));
76          MatcherAssert.assertThat(
77              "validation result should match expected state",
78              results.isEmpty(),
79              Matchers.is(passes)
80          );
81          return results;
82      }
83  }