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