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.list.ListOf;
14  import org.cactoos.text.FormattedText;
15  import org.cactoos.text.IoCheckedText;
16  import org.cactoos.text.TextOf;
17  import org.hamcrest.MatcherAssert;
18  import org.hamcrest.Matchers;
19  import org.hamcrest.collection.IsIterableContainingInOrder;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Test case for {@link CheckstyleValidator}'s detection of banned
24   * API usages such as Guava {@code Lists.newArrayList()} with no
25   * size, Apache Commons {@code CharEncoding}, and interface type
26   * parameter naming.
27   * @since 0.25.1
28   */
29  final class CheckstyleBannedApiTest {
30  
31      @Test
32      void reportsGuavaNewArrayListWithoutSize() throws Exception {
33          final String file = "GuavaNewArrayList.java";
34          MatcherAssert.assertThat(
35              "Guava Lists.newArrayList without size must be reported",
36              this.runValidation(file, false),
37              Matchers.hasItem(
38                  new ViolationMatcher(
39                      "Lists.newArrayList should be initialized with a size parameter",
40                      file
41                  )
42              )
43          );
44      }
45  
46      @Test
47      @SuppressWarnings("unchecked")
48      void reportsAllCharEncodingUsages() throws Exception {
49          final String message =
50              "Use java.nio.charset.StandardCharsets instead";
51          final String file = "DoNotUseCharEncoding.java";
52          final String name = "RegexpSinglelineCheck";
53          MatcherAssert.assertThat(
54              "8 violations should be found",
55              this.runValidation(file, false),
56              new IsIterableContainingInOrder<>(
57                  new ListOf<>(
58                      new ViolationMatcher(message, file, "6", name),
59                      new ViolationMatcher(message, file, "7", name),
60                      new ViolationMatcher(message, file, "8", name),
61                      new ViolationMatcher(message, file, "9", name),
62                      new ViolationMatcher(message, file, "23", name),
63                      new ViolationMatcher(message, file, "24", name),
64                      new ViolationMatcher(message, file, "25", name),
65                      new ViolationMatcher(message, file, "26", name)
66                  )
67              )
68          );
69      }
70  
71      @Test
72      void reportsInvalidInterfaceTypeParameterName() throws Exception {
73          final String file = "InterfaceTypeParameterName.java";
74          MatcherAssert.assertThat(
75              "Interface type parameter violation must be reported",
76              this.runValidation(file, false),
77              Matchers.hasItem(
78                  new ViolationMatcher(
79                      "Name 'wRoNg' must match pattern", file,
80                      "11", "InterfaceTypeParameterNameCheck"
81                  )
82              )
83          );
84      }
85  
86      @Test
87      @SuppressWarnings("unchecked")
88      void reportsUnnecessaryJavaLangPrefix() throws Exception {
89          final String message =
90              "Unnecessary java.lang. prefix, use the simple class name";
91          final String file = "UnnecessaryJavaLang.java";
92          final String name = "RegexpSinglelineCheck";
93          MatcherAssert.assertThat(
94              "cannot find all java.lang. violations",
95              this.runValidation(file, false),
96              Matchers.hasItems(
97                  new ViolationMatcher(message, file, "10", name),
98                  new ViolationMatcher(message, file, "18", name),
99                  new ViolationMatcher(message, file, "23", name),
100                 new ViolationMatcher(message, file, "25", name)
101             )
102         );
103     }
104 
105     private Collection<Violation> runValidation(final String file,
106         final boolean passes) throws IOException {
107         final Environment.Mock mock = new Environment.Mock();
108         final Environment env = mock.withParam(
109             "license",
110             String.format(
111                 "file:%s",
112                 new License().savePackageInfo(
113                     new File(mock.basedir(), "src/main/java/foo")
114                 ).withLines("Hello.")
115                     .withEol(String.valueOf('\n')).file()
116             )
117         ).withFile(
118             String.format("src/main/java/foo/%s", file),
119             new IoCheckedText(
120                 new TextOf(
121                     new ResourceOf(
122                         new FormattedText("com/qulice/checkstyle/%s", file)
123                     )
124                 )
125             ).asString()
126         );
127         final Collection<Violation> results =
128             new CheckstyleValidator(env).validate(env.files(file));
129         if (passes) {
130             MatcherAssert.assertThat(
131                 results,
132                 Matchers.<Violation>empty()
133             );
134         } else {
135             MatcherAssert.assertThat(
136                 results,
137                 Matchers.not(Matchers.<Violation>empty())
138             );
139         }
140         return results;
141     }
142 }