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   * stock {@code JavadocPackage} check, in particular the rule
20   * relaxing it for {@code src/test/java} when the parallel
21   * {@code src/main/java} package already declares
22   * {@code package-info.java}. See <a href=
23   * "https://github.com/yegor256/qulice/issues/865">#865</a>.
24   * @since 0.25.1
25   */
26  final class CheckstyleJavadocPackageTest {
27  
28      @Test
29      void allowsMissingPackageInfoInTestPackageWhenMainHasIt() throws Exception {
30          final String file = "ValidIT.java";
31          final Environment.Mock mock = new Environment.Mock();
32          final Environment env = mock.withParam(
33              "license",
34              String.format(
35                  "file:%s",
36                  new License().savePackageInfo(
37                      new File(mock.basedir(), "src/main/java/foo")
38                  ).withLines("Hello.")
39                      .withEol(String.valueOf('\n')).file()
40              )
41          ).withFile(
42              String.format("src/test/java/foo/%s", file),
43              new IoCheckedText(
44                  new TextOf(
45                      new ResourceOf(
46                          new FormattedText("com/qulice/checkstyle/%s", file)
47                      )
48                  )
49              ).asString()
50          );
51          MatcherAssert.assertThat(
52              "JavadocPackage must not fire for src/test/java when src/main/java has package-info.java",
53              new CheckstyleValidator(env).validate(env.files(file)),
54              Matchers.not(
55                  Matchers.hasItem(
56                      new ViolationMatcher(
57                          "Missing package-info.java file", file, "", "JavadocPackageCheck"
58                      )
59                  )
60              )
61          );
62      }
63  
64      @Test
65      void reportsMissingPackageInfoInTestPackageWhenMainHasNone() throws Exception {
66          final String file = "ValidIT.java";
67          final Environment.Mock mock = new Environment.Mock();
68          final Environment env = mock.withParam(
69              "license",
70              String.format(
71                  "file:%s",
72                  new License().withLines("Hello.")
73                      .withEol(String.valueOf('\n')).file()
74              )
75          ).withFile(
76              String.format("src/test/java/foo/%s", file),
77              new IoCheckedText(
78                  new TextOf(
79                      new ResourceOf(
80                          new FormattedText("com/qulice/checkstyle/%s", file)
81                      )
82                  )
83              ).asString()
84          );
85          MatcherAssert.assertThat(
86              "JavadocPackage must fire when neither test nor main package has package-info.java",
87              new CheckstyleValidator(env).validate(env.files(file)),
88              Matchers.hasItem(
89                  new ViolationMatcher(
90                      "Missing package-info.java file", file, "", "JavadocPackageCheck"
91                  )
92              )
93          );
94      }
95  }