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.Test;
19  
20  /**
21   * Test case for {@link CheckstyleValidator}'s handling of the
22   * stock {@code AtclauseOrder} check.
23   * @since 0.25.1
24   */
25  final class CheckstyleAtClauseOrderTest {
26  
27      @Test
28      void allowsOnlyProperlyOrderedAtClauses() throws Exception {
29          final String file = "AtClauseOrder.java";
30          final String message = "tags have to appear in the order";
31          final String name = "AtclauseOrderCheck";
32          MatcherAssert.assertThat(
33              "3 tags with wrong order should be found",
34              this.runValidation(file, false),
35              Matchers.contains(
36                  new ViolationMatcher(
37                      "Javadoc comment at column 3 has parse error.",
38                      file,
39                      "12",
40                      "MissingDeprecatedCheck"
41                  ),
42                  new ViolationMatcher(
43                      "Javadoc comment at column 3 has parse error.",
44                      file,
45                      "12",
46                      name
47                  ),
48                  new ViolationMatcher(
49                      message, file, "21", name
50                  ),
51                  new ViolationMatcher(
52                      message, file, "46", name
53                  ),
54                  new ViolationMatcher(
55                      "Class Class should be declared as final.",
56                      file,
57                      "56",
58                      "FinalClassCheck"
59                  )
60              )
61          );
62      }
63  
64      private Collection<Violation> runValidation(final String file,
65          final boolean passes) throws IOException {
66          final Environment.Mock mock = new Environment.Mock();
67          final Environment env = mock.withParam(
68              "license",
69              String.format(
70                  "file:%s",
71                  new License().savePackageInfo(
72                      new File(mock.basedir(), "src/main/java/foo")
73                  ).withLines("Hello.")
74                      .withEol(String.valueOf('\n')).file()
75              )
76          ).withFile(
77              String.format("src/main/java/foo/%s", file),
78              new IoCheckedText(
79                  new TextOf(
80                      new ResourceOf(
81                          new FormattedText("com/qulice/checkstyle/%s", file)
82                      )
83                  )
84              ).asString()
85          );
86          final Collection<Violation> results =
87              new CheckstyleValidator(env).validate(env.files(file));
88          MatcherAssert.assertThat(
89              "validation result should match expected state",
90              results.isEmpty(),
91              Matchers.is(passes)
92          );
93          return results;
94      }
95  }