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