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 PackageDeclaration}/line-wrap check.
23   *
24   * @since 0.25.1
25   */
26  final class CheckstylePackageDeclarationTest {
27  
28      @Test
29      void reportsErrorWhenLineWrap() throws Exception {
30          final String file = "LineWrapPackage.java";
31          MatcherAssert.assertThat(
32              "line-wrapped package declaration must be reported",
33              this.runValidation(file, false),
34              Matchers.hasItem(
35                  new ViolationMatcher("should not be line-wrapped", file)
36              )
37          );
38      }
39  
40      private Collection<Violation> runValidation(final String file,
41          final boolean passes) throws IOException {
42          final Environment.Mock mock = new Environment.Mock();
43          final Environment env = mock.withParam(
44              "license",
45              String.format(
46                  "file:%s",
47                  new License().savePackageInfo(
48                      new File(mock.basedir(), "src/main/java/foo")
49                  ).withLines("Hello.")
50                      .withEol(String.valueOf('\n')).file()
51              )
52          ).withFile(
53              String.format("src/main/java/foo/%s", file),
54              new IoCheckedText(
55                  new TextOf(
56                      new ResourceOf(
57                          new FormattedText("com/qulice/checkstyle/%s", file)
58                      )
59                  )
60              ).asString()
61          );
62          final Collection<Violation> results =
63              new CheckstyleValidator(env).validate(env.files(file));
64          MatcherAssert.assertThat(
65              "validation result should match expected state",
66              results.isEmpty(),
67              Matchers.is(passes)
68          );
69          return results;
70      }
71  }