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