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