View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import org.junit.jupiter.api.Test;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
26  
27  public class MagicNumberCheckExamplesTest extends AbstractExamplesModuleTestSupport {
28  
29      @Override
30      public String getPackageLocation() {
31          return "com/puppycrawl/tools/checkstyle/checks/coding/magicnumber";
32      }
33  
34      @Test
35      public void testExample1() throws Exception {
36          final String[] expected = {
37              "16:13: " + getCheckMessage(
38                      MagicNumberCheck.MSG_KEY, 6),
39              "18:23: " + getCheckMessage(
40                      MagicNumberCheck.MSG_KEY, 7),
41              "22:13: " + getCheckMessage(
42                      MagicNumberCheck.MSG_KEY, 8),
43              "45:12: " + getCheckMessage(
44                      MagicNumberCheck.MSG_KEY, 10),
45          };
46  
47          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
48      }
49  
50      @Test
51      public void testExample2() throws Exception {
52          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
53  
54          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
55      }
56  
57      @Test
58      public void testExample3() throws Exception {
59          final String[] expected = {
60              "14:13: " + getCheckMessage(
61                      MagicNumberCheck.MSG_KEY, 6),
62              "20:13: " + getCheckMessage(
63                      MagicNumberCheck.MSG_KEY, 8),
64              "43:12: " + getCheckMessage(
65                      MagicNumberCheck.MSG_KEY, 10),
66          };
67  
68          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
69      }
70  
71      @Test
72      public void testExample4() throws Exception {
73          final String[] expected = {
74              "14:13: " + getCheckMessage(
75                      MagicNumberCheck.MSG_KEY, 6),
76              "16:23: " + getCheckMessage(
77                      MagicNumberCheck.MSG_KEY, 7),
78              "20:13: " + getCheckMessage(
79                      MagicNumberCheck.MSG_KEY, 8),
80              "43:12: " + getCheckMessage(
81                      MagicNumberCheck.MSG_KEY, 10),
82              "46:25: " + getCheckMessage(
83                      MagicNumberCheck.MSG_KEY, 10),
84              "47:29: " + getCheckMessage(
85                      MagicNumberCheck.MSG_KEY, 10),
86          };
87  
88          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
89      }
90  
91      @Test
92      public void testExample5() throws Exception {
93          final String[] expected = {
94              "15:13: " + getCheckMessage(
95                      MagicNumberCheck.MSG_KEY, 6),
96              "17:23: " + getCheckMessage(
97                      MagicNumberCheck.MSG_KEY, 7),
98              "21:13: " + getCheckMessage(
99                      MagicNumberCheck.MSG_KEY, 8),
100             "24:48: " + getCheckMessage(
101                     MagicNumberCheck.MSG_KEY, 62),
102             "30:27: " + getCheckMessage(
103                     MagicNumberCheck.MSG_KEY, 10),
104             "30:31: " + getCheckMessage(
105                     MagicNumberCheck.MSG_KEY, 20),
106             "32:19: " + getCheckMessage(
107                     MagicNumberCheck.MSG_KEY, 3),
108             "32:23: " + getCheckMessage(
109                     MagicNumberCheck.MSG_KEY, 4),
110             "36:19: " + getCheckMessage(
111                     MagicNumberCheck.MSG_KEY, 3),
112             "36:23: " + getCheckMessage(
113                     MagicNumberCheck.MSG_KEY, 4),
114             "44:12: " + getCheckMessage(
115                     MagicNumberCheck.MSG_KEY, 10),
116         };
117 
118         verifyWithInlineConfigParser(getPath("Example5.java"), expected);
119     }
120 
121     @Test
122     public void testExample6() throws Exception {
123         final String[] expected = {
124             "14:13: " + getCheckMessage(
125                     MagicNumberCheck.MSG_KEY, 6),
126             "16:23: " + getCheckMessage(
127                     MagicNumberCheck.MSG_KEY, 7),
128             "20:13: " + getCheckMessage(
129                     MagicNumberCheck.MSG_KEY, 8),
130         };
131 
132         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
133     }
134 
135     @Test
136     public void testExample7() throws Exception {
137         final String[] expected = {
138             "14:13: " + getCheckMessage(
139                     MagicNumberCheck.MSG_KEY, 6),
140             "16:23: " + getCheckMessage(
141                     MagicNumberCheck.MSG_KEY, 7),
142         };
143 
144         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
145     }
146 
147     @Test
148     public void testExample8() throws Exception {
149         final String[] expected = {
150             "16:23: " + getCheckMessage(
151                     MagicNumberCheck.MSG_KEY, 7),
152             "20:13: " + getCheckMessage(
153                     MagicNumberCheck.MSG_KEY, 8),
154             "43:12: " + getCheckMessage(
155                     MagicNumberCheck.MSG_KEY, 10),
156         };
157 
158         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
159     }
160 
161 }