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 org.hamcrest.Matchers;
8   import org.junit.jupiter.api.Test;
9   
10  /**
11   * Test case for {@link PmdValidator} class.
12   * @since 0.15
13   */
14  @SuppressWarnings("PMD.TooManyMethods")
15  final class PmdEmptyTest {
16      /**
17       * Makes sure that empty static initializers fail with an error.
18       * @throws Exception when something goes wrong
19       */
20      @Test
21      void failsForEmptyStaticInitializer() throws Exception {
22          new PmdAssert(
23              "EmptyStaticInitializer.java",
24              Matchers.is(false),
25              Matchers.containsString(
26                  "Empty initializer statement (EmptyControlStatement)"
27              )
28          ).validate();
29      }
30  
31      /**
32       * Makes sure that empty statement blocks fail with an error.
33       * @throws Exception when something goes wrong
34       */
35      @Test
36      void failsForEmptyStatementBlock() throws Exception {
37          new PmdAssert(
38              "EmptyStatementBlock.java",
39              Matchers.is(false),
40              Matchers.containsString(
41                  "Empty block "
42              )
43          ).validate();
44      }
45  
46      /**
47       * Makes sure that empty initializers fail with an error.
48       * @throws Exception when something goes wrong
49       */
50      @Test
51      void failsForEmptyInitializer() throws Exception {
52          new PmdAssert(
53              "EmptyInitializer.java",
54              Matchers.is(false),
55              Matchers.containsString(
56                  "Empty initializer statement "
57              )
58          ).validate();
59      }
60  
61      /**
62       * Makes sure that empty statement not in a loop fail with an error.
63       * @throws Exception when something goes wrong
64       */
65      @Test
66      void failsForEmptyNonLoopStatement() throws Exception {
67          new PmdAssert(
68              "EmptyStatementNotInLoop.java",
69              Matchers.is(false),
70              Matchers.containsString(
71                  "Unnecessary semicolon "
72              )
73          ).validate();
74      }
75  
76      /**
77       * Makes sure that empty synchronized statements fail with an error.
78       * @throws Exception when something goes wrong
79       */
80      @Test
81      void failsForEmptySynchronizedBlock() throws Exception {
82          new PmdAssert(
83              "EmptySynchronizedBlock.java",
84              Matchers.is(false),
85              Matchers.containsString(
86                  "Empty synchronized statement "
87              )
88          ).validate();
89      }
90  
91      /**
92       * Makes sure that empty switch statements fail with an error.
93       * @throws Exception when something goes wrong
94       */
95      @Test
96      void failsForEmptySwitchStatement() throws Exception {
97          new PmdAssert(
98              "EmptySwitchStmt.java",
99              Matchers.is(false),
100             Matchers.containsString(
101                 "Empty switch statement "
102             )
103         ).validate();
104     }
105 
106     /**
107      * Makes sure that empty finally blocks fail with an error.
108      * @throws Exception when something goes wrong
109      */
110     @Test
111     void failsForEmptyFinallyBlock() throws Exception {
112         new PmdAssert(
113             "EmptyFinallyBlock.java",
114             Matchers.is(false),
115             Matchers.containsString("Empty finally clause")
116         ).validate();
117     }
118 
119     /**
120      * Makes sure that empty while statements fail with an error.
121      * @throws Exception when something goes wrong
122      */
123     @Test
124     void failsForEmptyWhileStatement() throws Exception {
125         new PmdAssert(
126             "EmptyWhileStmt.java",
127             Matchers.is(false),
128             Matchers.containsString("Empty while statement ")
129         ).validate();
130     }
131 
132     /**
133      * Makes sure that empty if blocks fail with an error.
134      * @throws Exception when something goes wrong
135      */
136     @Test
137     void failsForEmptyIfStatement() throws Exception {
138         new PmdAssert(
139             "EmptyIfStmt.java",
140             Matchers.is(false),
141             Matchers.containsString("Empty if statement ")
142         ).validate();
143     }
144 
145     /**
146      * Makes sure that empty catch blocks fail with an error.
147      * @throws Exception when something goes wrong
148      */
149     @Test
150     void failsForEmptyCatchBlock() throws Exception {
151         new PmdAssert(
152             "EmptyCatchBlock.java",
153             Matchers.is(false),
154             Matchers.containsString("Avoid empty catch blocks")
155         ).validate();
156     }
157 }