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