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 handling of
23   * {@link IfThenThrowElseCheck}.
24   * @since 0.24
25   */
26  final class IfThenThrowElseCheckTest {
27  
28      @Test
29      void rejectsElseBranchAfterThenThrow() throws Exception {
30          final String file = "InvalidIfThenThrowElse.java";
31          final String name = "IfThenThrowElseCheck";
32          final String message = "Avoid 'else' when 'then' branch ends with 'throw'";
33          MatcherAssert.assertThat(
34              "All three if-throw-else patterns should be flagged",
35              this.runValidation(file, false),
36              Matchers.hasItems(
37                  new ViolationMatcher(message, file, "30", name),
38                  new ViolationMatcher(message, file, "42", name),
39                  new ViolationMatcher(message, file, "54", name)
40              )
41          );
42      }
43  
44      @Test
45      void allowsGuardThrowWithoutElse() throws Exception {
46          Assertions.assertDoesNotThrow(
47              () -> this.runValidation("ValidIfThenThrowElse.java", true)
48          );
49      }
50  
51      private Collection<Violation> runValidation(final String file,
52          final boolean passes) throws IOException {
53          final Environment.Mock mock = new Environment.Mock();
54          final Environment env = mock.withParam(
55              "license",
56              String.format(
57                  "file:%s",
58                  new License().savePackageInfo(
59                      new File(mock.basedir(), "src/main/java/foo")
60                  ).withLines("Hello.")
61                      .withEol(String.valueOf('\n')).file()
62              )
63          ).withFile(
64              String.format("src/main/java/foo/%s", file),
65              new IoCheckedText(
66                  new TextOf(
67                      new ResourceOf(
68                          new FormattedText("com/qulice/checkstyle/%s", file)
69                      )
70                  )
71              ).asString()
72          );
73          final Collection<Violation> results =
74              new CheckstyleValidator(env).validate(env.files(file));
75          MatcherAssert.assertThat(
76              "validation result should match expected state",
77              results.isEmpty(),
78              Matchers.is(passes)
79          );
80          return results;
81      }
82  }