View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.pmd;
6   
7   import com.qulice.spi.Environment;
8   import com.qulice.spi.Violation;
9   import java.io.File;
10  import java.util.Collections;
11  import java.util.stream.Collectors;
12  import org.cactoos.text.TextOf;
13  import org.hamcrest.MatcherAssert;
14  import org.hamcrest.Matchers;
15  import org.junit.jupiter.api.Test;
16  
17  /**
18   * Test case for UnnecessaryLocalRule.
19   * @since 0.24
20   */
21  final class UnnecessaryLocalRuleTest {
22  
23      @Test
24      void detectsUnnecessaryLocalVariableOnReturn() throws Exception {
25          final String file = "UnnecessaryLocal.java";
26          final Environment.Mock mock = new Environment.Mock();
27          final String name = String.format("src/main/java/foo/%s", file);
28          final Environment env = mock.withFile(
29              name,
30              new TextOf(
31                  this.getClass().getResourceAsStream(file)
32              ).asString()
33          );
34          MatcherAssert.assertThat(
35              "UnnecessaryLocalRule should fire on a local variable used only in return",
36              new PmdValidator(env).validate(
37                  Collections.singletonList(new File(env.basedir(), name))
38              ).stream().map(Violation::name).collect(Collectors.toList()),
39              Matchers.hasItem("UnnecessaryLocalRule")
40          );
41      }
42  
43      @Test
44      void doesNotFireWhenVariableIsUsedInLoop() throws Exception {
45          final String file = "UnnecessaryLocalInLoop.java";
46          final Environment.Mock mock = new Environment.Mock();
47          final String name = String.format("src/main/java/foo/%s", file);
48          final Environment env = mock.withFile(
49              name,
50              new TextOf(
51                  this.getClass().getResourceAsStream(file)
52              ).asString()
53          );
54          MatcherAssert.assertThat(
55              "UnnecessaryLocalRule should not fire when variable is used in a loop",
56              new PmdValidator(env).validate(
57                  Collections.singletonList(new File(env.basedir(), name))
58              ).stream().map(Violation::name).collect(Collectors.toList()),
59              Matchers.not(Matchers.hasItem("UnnecessaryLocalRule"))
60          );
61      }
62  
63      @Test
64      void doesNotFireWhenVariableIsUsedMoreThanOnce() throws Exception {
65          final String file = "UnnecessaryLocalUsedTwice.java";
66          final Environment.Mock mock = new Environment.Mock();
67          final String name = String.format("src/main/java/foo/%s", file);
68          final Environment env = mock.withFile(
69              name,
70              new TextOf(
71                  this.getClass().getResourceAsStream(file)
72              ).asString()
73          );
74          MatcherAssert.assertThat(
75              "UnnecessaryLocalRule should not fire when variable is used more than once",
76              new PmdValidator(env).validate(
77                  Collections.singletonList(new File(env.basedir(), name))
78              ).stream().map(Violation::name).collect(Collectors.toList()),
79              Matchers.not(Matchers.hasItem("UnnecessaryLocalRule"))
80          );
81      }
82  }