View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.pmd;
32  
33  import org.hamcrest.Matchers;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test case for {@link PmdValidator} class.
38   * @since 0.15
39   */
40  @SuppressWarnings("PMD.TooManyMethods")
41  final class PmdEmptyTest {
42      /**
43       * Makes sure that empty static initializers fail with an error.
44       * @throws Exception when something goes wrong
45       */
46      @Test
47      void failsForEmptyStaticInitializer() throws Exception {
48          new PmdAssert(
49              "EmptyStaticInitializer.java",
50              Matchers.is(false),
51              Matchers.containsString(
52                  "Empty initializer statement (EmptyControlStatement)"
53              )
54          ).validate();
55      }
56  
57      /**
58       * Makes sure that empty statement blocks fail with an error.
59       * @throws Exception when something goes wrong
60       */
61      @Test
62      void failsForEmptyStatementBlock() throws Exception {
63          new PmdAssert(
64              "EmptyStatementBlock.java",
65              Matchers.is(false),
66              Matchers.containsString(
67                  "Empty block "
68              )
69          ).validate();
70      }
71  
72      /**
73       * Makes sure that empty initializers fail with an error.
74       * @throws Exception when something goes wrong
75       */
76      @Test
77      void failsForEmptyInitializer() throws Exception {
78          new PmdAssert(
79              "EmptyInitializer.java",
80              Matchers.is(false),
81              Matchers.containsString(
82                  "Empty initializer statement "
83              )
84          ).validate();
85      }
86  
87      /**
88       * Makes sure that empty statement not in a loop fail with an error.
89       * @throws Exception when something goes wrong
90       */
91      @Test
92      void failsForEmptyNonLoopStatement() throws Exception {
93          new PmdAssert(
94              "EmptyStatementNotInLoop.java",
95              Matchers.is(false),
96              Matchers.containsString(
97                  "Unnecessary semicolon "
98              )
99          ).validate();
100     }
101 
102     /**
103      * Makes sure that empty synchronized statements fail with an error.
104      * @throws Exception when something goes wrong
105      */
106     @Test
107     void failsForEmptySynchronizedBlock() throws Exception {
108         new PmdAssert(
109             "EmptySynchronizedBlock.java",
110             Matchers.is(false),
111             Matchers.containsString(
112                 "Empty synchronized statement "
113             )
114         ).validate();
115     }
116 
117     /**
118      * Makes sure that empty switch statements fail with an error.
119      * @throws Exception when something goes wrong
120      */
121     @Test
122     void failsForEmptySwitchStatement() throws Exception {
123         new PmdAssert(
124             "EmptySwitchStmt.java",
125             Matchers.is(false),
126             Matchers.containsString(
127                 "Empty switch statement "
128             )
129         ).validate();
130     }
131 
132     /**
133      * Makes sure that empty finally blocks fail with an error.
134      * @throws Exception when something goes wrong
135      */
136     @Test
137     void failsForEmptyFinallyBlock() throws Exception {
138         new PmdAssert(
139             "EmptyFinallyBlock.java",
140             Matchers.is(false),
141             Matchers.containsString("Empty finally clause")
142         ).validate();
143     }
144 
145     /**
146      * Makes sure that empty while statements fail with an error.
147      * @throws Exception when something goes wrong
148      */
149     @Test
150     void failsForEmptyWhileStatement() throws Exception {
151         new PmdAssert(
152             "EmptyWhileStmt.java",
153             Matchers.is(false),
154             Matchers.containsString("Empty while statement ")
155         ).validate();
156     }
157 
158     /**
159      * Makes sure that empty if blocks fail with an error.
160      * @throws Exception when something goes wrong
161      */
162     @Test
163     void failsForEmptyIfStatement() throws Exception {
164         new PmdAssert(
165             "EmptyIfStmt.java",
166             Matchers.is(false),
167             Matchers.containsString("Empty if statement ")
168         ).validate();
169     }
170 
171     /**
172      * Makes sure that empty catch blocks fail with an error.
173      * @throws Exception when something goes wrong
174      */
175     @Test
176     void failsForEmptyCatchBlock() throws Exception {
177         new PmdAssert(
178             "EmptyCatchBlock.java",
179             Matchers.is(false),
180             Matchers.containsString("Avoid empty catch blocks")
181         ).validate();
182     }
183 }