View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.design;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class DesignForExtensionCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/design/designforextension";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final DesignForExtensionCheck checkObj = new DesignForExtensionCheck();
42          final int[] expected = {TokenTypes.METHOD_DEF};
43          assertWithMessage("Default required tokens are invalid")
44              .that(checkObj.getRequiredTokens())
45              .isEqualTo(expected);
46      }
47  
48      @Test
49      public void testIt() throws Exception {
50          final String[] expected = {
51              "50:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtension", "doh"),
52              "104:9: " + getCheckMessage(MSG_KEY, "anotherNonFinalClass", "someMethod"),
53          };
54          verifyWithInlineConfigParser(
55                  getPath("InputDesignForExtension.java"), expected);
56      }
57  
58      @Test
59      public void testGetAcceptableTokens() {
60          final DesignForExtensionCheck obj = new DesignForExtensionCheck();
61          final int[] expected = {TokenTypes.METHOD_DEF};
62          assertWithMessage("Default acceptable tokens are invalid")
63              .that(obj.getAcceptableTokens())
64              .isEqualTo(expected);
65      }
66  
67      @Test
68      public void testOverridableMethods() throws Exception {
69          final String[] expected = {
70              "14:9: " + getCheckMessage(MSG_KEY, "A", "foo1"),
71              "38:9: " + getCheckMessage(MSG_KEY, "A", "foo8"),
72              "43:9: " + getCheckMessage(MSG_KEY, "A", "foo9"),
73              "50:9: " + getCheckMessage(MSG_KEY, "A", "foo10"),
74              "57:9: " + getCheckMessage(MSG_KEY, "A", "foo11"),
75              "62:9: " + getCheckMessage(MSG_KEY, "A", "foo12"),
76              "69:9: " + getCheckMessage(MSG_KEY, "A", "foo13"),
77              "76:9: " + getCheckMessage(MSG_KEY, "A", "foo14"),
78              "98:9: " + getCheckMessage(MSG_KEY, "A", "foo22"),
79              "104:9: " + getCheckMessage(MSG_KEY, "A", "foo23"),
80              "113:9: " + getCheckMessage(MSG_KEY, "A", "foo25"),
81              "118:9: " + getCheckMessage(MSG_KEY, "A", "foo26"),
82              "125:9: " + getCheckMessage(MSG_KEY, "A", "foo27"),
83              "137:9: " + getCheckMessage(MSG_KEY, "A", "foo29"),
84              "159:9: " + getCheckMessage(MSG_KEY, "A", "foo31"),
85              "170:9: " + getCheckMessage(MSG_KEY, "A", "foo33"),
86              "176:9: " + getCheckMessage(MSG_KEY, "A", "foo34"),
87              "198:9: " + getCheckMessage(MSG_KEY, "A", "foo39"),
88              "205:9: " + getCheckMessage(MSG_KEY, "A", "foo41"),
89          };
90          verifyWithInlineConfigParser(
91                  getPath("InputDesignForExtensionOverridableMethods.java"), expected);
92      }
93  
94      @Test
95      public void testIgnoredAnnotationsOption() throws Exception {
96          final String[] expected = {
97              "39:5: "
98                  + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo1"),
99              "149:5: "
100                 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo21"),
101             "154:5: "
102                 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "setAge"),
103             "169:5: "
104                 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo24"),
105             "176:5: "
106                 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "dontUse4"),
107         };
108         verifyWithInlineConfigParser(
109                 getPath("InputDesignForExtensionIgnoredAnnotations.java"), expected);
110     }
111 
112     @Test
113     public void testIgnoreAnnotationsOptionWithMultipleAnnotations() throws Exception {
114         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115         verifyWithInlineConfigParser(
116                 getPath("InputDesignForExtensionMultipleAnnotations.java"), expected);
117     }
118 
119     @Test
120     public void testNativeMethods() throws Exception {
121         final String[] expected = {
122             "16:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo1"),
123             "32:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo6"),
124         };
125         verifyWithInlineConfigParser(
126                 getPath("InputDesignForExtensionNativeMethods.java"), expected);
127     }
128 
129     @Test
130     public void testDesignForExtensionRecords() throws Exception {
131 
132         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
133 
134         verifyWithInlineConfigParser(
135                 getNonCompilablePath("InputDesignForExtensionRecords.java"), expected);
136     }
137 
138     @Test
139     public void testRequiredJavadocPhrase() throws Exception {
140         final String className = "InputDesignForExtensionRequiredJavadocPhrase";
141         final String[] expected = {
142             "41:5: " + getCheckMessage(MSG_KEY, className, "foo5"),
143             "48:5: " + getCheckMessage(MSG_KEY, className, "foo8"),
144             "51:5: " + getCheckMessage(MSG_KEY, className, "foo9"),
145             "67:5: " + getCheckMessage(MSG_KEY, className, "foo12"),
146         };
147         verifyWithInlineConfigParser(
148                 getPath("InputDesignForExtensionRequiredJavadocPhrase.java"), expected);
149     }
150 
151     @Test
152     public void testRequiredJavadocPhraseMultiLine() throws Exception {
153         final String className = "InputDesignForExtensionRequiredJavadocPhraseMultiLine";
154         final String[] expected = {
155             "23:5: " + getCheckMessage(MSG_KEY, className, "foo2"),
156         };
157         verifyWithInlineConfigParser(
158                 getPath("InputDesignForExtensionRequiredJavadocPhraseMultiLine.java"),
159             expected);
160     }
161 
162     @Test
163     public void testInterfaceMemberScopeIsPublic() throws Exception {
164         final String[] expected = {
165             "15:9: " + getCheckMessage(MSG_KEY, "Inner", "getProperty"),
166         };
167         verifyWithInlineConfigParser(
168                 getPath("InputDesignForExtensionInterfaceMemberScopeIsPublic.java"),
169             expected);
170     }
171 
172 }