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
23   * Javadoc {@code @param} type descriptions start with a capital
24   * letter, see https://github.com/yegor256/qulice/issues/705.
25   * @since 0.25.1
26   */
27  final class CheckstyleTypeParamDescriptionTest {
28  
29      @Test
30      @SuppressWarnings("unchecked")
31      void rejectsLowercaseTypeParamDescription() throws Exception {
32          final String file = "InvalidTypeParamDescription.java";
33          final String message =
34              "@param tag description should start with capital letter";
35          final String name = "RegexpSinglelineCheck";
36          MatcherAssert.assertThat(
37              "Both class and method type parameter descriptions must be reported",
38              this.runValidation(file, false),
39              Matchers.hasItems(
40                  new ViolationMatcher(message, file, "8", name),
41                  new ViolationMatcher(message, file, "15", name)
42              )
43          );
44      }
45  
46      @Test
47      void allowsUppercaseTypeParamDescription() throws Exception {
48          Assertions.assertDoesNotThrow(
49              () -> this.runValidation("ValidTypeParamDescription.java", true)
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  }