View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.utils;
21  
22  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.api.DetailAST;
30  import com.puppycrawl.tools.checkstyle.api.Scope;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  
33  public class ScopeUtilsTest {
34  
35      @Test
36      public void testIsProperUtilsClass() throws ReflectiveOperationException {
37          assertTrue("Constructor is not private",
38                  isUtilsClassHasPrivateConstructor(ScopeUtils.class, true));
39      }
40  
41      @Test
42      public void testInEnumBlock() {
43          assertFalse("Should return false when passed is not enum",
44                  ScopeUtils.isInEnumBlock(new DetailAST()));
45          assertFalse("Should return false when passed is not enum",
46                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.LITERAL_NEW,
47                          TokenTypes.MODIFIERS)));
48          assertTrue("Should return true when passed is enum",
49                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.OBJBLOCK, TokenTypes.ENUM_DEF,
50                          TokenTypes.MODIFIERS)));
51          assertFalse("Should return false when passed is not enum",
52                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.ENUM_DEF, TokenTypes.INTERFACE_DEF,
53                          TokenTypes.MODIFIERS)));
54          assertFalse("Should return false when passed is not enum",
55                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF,
56                          TokenTypes.MODIFIERS)));
57          assertFalse("Should return false when passed is not enum",
58                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.ENUM_DEF, TokenTypes.CLASS_DEF,
59                          TokenTypes.MODIFIERS)));
60          assertFalse("Should return false when passed is not enum",
61                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.ENUM_DEF, TokenTypes.LITERAL_NEW,
62                          TokenTypes.IDENT)));
63          assertFalse("Should return false when passed is not expected",
64                  ScopeUtils.isInEnumBlock(getNode(TokenTypes.PACKAGE_DEF, TokenTypes.DOT)));
65      }
66  
67      @Test
68      public void testIsInCodeBlock() {
69          assertFalse("invalid result", ScopeUtils.isInCodeBlock(getNode(TokenTypes.CLASS_DEF)));
70          assertFalse("invalid result",
71                  ScopeUtils.isInCodeBlock(getNode(TokenTypes.ASSIGN, TokenTypes.VARIABLE_DEF)));
72          assertTrue("invalid result",
73                  ScopeUtils.isInCodeBlock(getNode(TokenTypes.METHOD_DEF, TokenTypes.OBJBLOCK)));
74          assertTrue("invalid result",
75                  ScopeUtils.isInCodeBlock(getNode(TokenTypes.CTOR_DEF, TokenTypes.OBJBLOCK)));
76          assertTrue("invalid result",
77                  ScopeUtils.isInCodeBlock(getNode(TokenTypes.INSTANCE_INIT, TokenTypes.OBJBLOCK)));
78          assertTrue("invalid result",
79                  ScopeUtils.isInCodeBlock(getNode(TokenTypes.STATIC_INIT, TokenTypes.OBJBLOCK)));
80          assertTrue("invalid result",
81                  ScopeUtils.isInCodeBlock(getNode(TokenTypes.LAMBDA, TokenTypes.ASSIGN)));
82      }
83  
84      @Test
85      public void testIsOuterMostTypeInterface() {
86          assertFalse("Should return false when passed is not outer most type",
87                  ScopeUtils.isOuterMostType(getNode(TokenTypes.INTERFACE_DEF,
88                          TokenTypes.MODIFIERS)));
89      }
90  
91      @Test
92      public void testIsOuterMostTypeAnnotation() {
93          assertFalse("Should return false when passed is not outer most type",
94                  ScopeUtils.isOuterMostType(getNode(TokenTypes.ANNOTATION_DEF,
95                          TokenTypes.MODIFIERS)));
96      }
97  
98      @Test
99      public void testIsOuterMostTypeEnum() {
100         assertFalse("Should return false when passed is not outer most type",
101                 ScopeUtils.isOuterMostType(getNode(TokenTypes.ENUM_DEF, TokenTypes.MODIFIERS)));
102     }
103 
104     @Test
105     public void testIsOuterMostTypeClass() {
106         assertFalse("Should return false when passed is not outer most type",
107                 ScopeUtils.isOuterMostType(getNode(TokenTypes.CLASS_DEF, TokenTypes.MODIFIERS)));
108     }
109 
110     @Test
111     public void testIsOuterMostTypePackageDef() {
112         assertTrue("Should return false when passed is not outer most type",
113                 ScopeUtils.isOuterMostType(getNode(TokenTypes.PACKAGE_DEF, TokenTypes.DOT)));
114     }
115 
116     @Test
117     public void testIsLocalVariableDefCatch() {
118         assertTrue("Should return true when passed is variable def",
119                 ScopeUtils.isLocalVariableDef(getNode(TokenTypes.LITERAL_CATCH,
120                         TokenTypes.PARAMETER_DEF)));
121     }
122 
123     @Test
124     public void testIsLocalVariableDefUnexpected() {
125         assertFalse("Should return false when passed is not variable def",
126                 ScopeUtils.isLocalVariableDef(getNode(TokenTypes.LITERAL_CATCH)));
127         assertFalse("Should return false when passed is not variable def",
128                 ScopeUtils.isLocalVariableDef(getNode(TokenTypes.COMMA, TokenTypes.PARAMETER_DEF)));
129     }
130 
131     @Test
132     public void testIsLocalVariableDefResource() {
133         assertTrue("invalid result",
134                 ScopeUtils.isLocalVariableDef(getNode(TokenTypes.RESOURCE)));
135     }
136 
137     @Test
138     public void testIsLocalVariableDefVariable() {
139         assertTrue("invalid result",
140                 ScopeUtils.isLocalVariableDef(getNode(TokenTypes.SLIST, TokenTypes.VARIABLE_DEF)));
141         assertTrue("invalid result", ScopeUtils.isLocalVariableDef(getNode(TokenTypes.FOR_INIT,
142                 TokenTypes.VARIABLE_DEF)));
143         assertTrue("invalid result", ScopeUtils.isLocalVariableDef(getNode(
144                 TokenTypes.FOR_EACH_CLAUSE, TokenTypes.VARIABLE_DEF)));
145         assertFalse("invalid result", ScopeUtils.isLocalVariableDef(getNode(TokenTypes.CLASS_DEF,
146                 TokenTypes.VARIABLE_DEF)));
147     }
148 
149     @Test
150     public void testIsClassFieldDef() {
151         assertTrue("Should return true when passed is class field def",
152                 ScopeUtils.isClassFieldDef(getNode(TokenTypes.CLASS_DEF,
153                         TokenTypes.OBJBLOCK, TokenTypes.VARIABLE_DEF)));
154         assertFalse("Should return false when passed is unexpected",
155                 ScopeUtils.isClassFieldDef(getNode(TokenTypes.CLASS_DEF)));
156         assertFalse("Should return false when passed is method variable def",
157                 ScopeUtils.isClassFieldDef(getNode(TokenTypes.METHOD_DEF,
158                         TokenTypes.SLIST, TokenTypes.VARIABLE_DEF)));
159     }
160 
161     @Test
162     public void testSurroundingScope() {
163         assertEquals("Invalid surrounding scope",
164                 Scope.PUBLIC, ScopeUtils.getSurroundingScope(getNodeWithParentScope(
165             TokenTypes.LITERAL_PUBLIC, "public", TokenTypes.ANNOTATION_DEF)));
166         assertEquals("Invalid surrounding scope",
167                 Scope.PROTECTED, ScopeUtils.getSurroundingScope(
168             getNodeWithParentScope(TokenTypes.LITERAL_PROTECTED, "protected",
169             TokenTypes.INTERFACE_DEF)));
170         assertEquals("Invalid surrounding scope",
171                 Scope.PRIVATE, ScopeUtils.getSurroundingScope(
172             getNodeWithParentScope(TokenTypes.LITERAL_PRIVATE, "private", TokenTypes.ENUM_DEF)));
173         assertEquals("Invalid surrounding scope",
174                 Scope.PACKAGE, ScopeUtils.getSurroundingScope(
175             getNodeWithParentScope(TokenTypes.LITERAL_STATIC, "static", TokenTypes.CLASS_DEF)));
176     }
177 
178     @Test
179     public void testIsInScope() {
180         assertTrue("Should return true when node is in valid scope",
181                 ScopeUtils.isInScope(getNodeWithParentScope(TokenTypes.LITERAL_PUBLIC,
182                 "public", TokenTypes.ANNOTATION_DEF), Scope.PUBLIC));
183         assertFalse("Should return false when node is in invalid scope",
184                 ScopeUtils.isInScope(getNodeWithParentScope(TokenTypes.LITERAL_PROTECTED,
185                 "protected", TokenTypes.INTERFACE_DEF), Scope.PRIVATE));
186     }
187 
188     @Test
189     public void testSurroundingScopeOfNodeChildOfLiteralNewIsAnoninner() {
190         assertEquals("Invalid surrounding scope",
191                 Scope.ANONINNER, ScopeUtils.getSurroundingScope(
192                 getNode(TokenTypes.LITERAL_NEW, TokenTypes.IDENT)));
193     }
194 
195     @Test
196     public void testIsInInterfaceBlock() {
197         final DetailAST ast = getNode(TokenTypes.INTERFACE_DEF, TokenTypes.OBJBLOCK,
198                 TokenTypes.CLASS_DEF, TokenTypes.MODIFIERS);
199 
200         assertTrue("Should return true when node is interface block",
201                 ScopeUtils.isInInterfaceBlock(ast.getParent()));
202         assertFalse("Should return false when node is not interface block",
203                 ScopeUtils.isInInterfaceBlock(ast));
204     }
205 
206     @Test
207     public void testIsInAnnotationBlock() {
208         final DetailAST ast = getNode(TokenTypes.ANNOTATION_DEF, TokenTypes.OBJBLOCK,
209                 TokenTypes.INTERFACE_DEF, TokenTypes.MODIFIERS);
210 
211         assertTrue("Should return true when node is annotation block",
212                 ScopeUtils.isInAnnotationBlock(ast.getParent()));
213         assertFalse("Should return false when node is not annotation block",
214                 ScopeUtils.isInAnnotationBlock(ast));
215     }
216 
217     @Test
218     public void testisInInterfaceOrAnnotationBlock() {
219         assertTrue("Should return true when node is in interface or annotation block",
220                 ScopeUtils.isInInterfaceOrAnnotationBlock(
221                 getNode(TokenTypes.ANNOTATION_DEF, TokenTypes.OBJBLOCK)));
222         assertTrue("Should return true when node is in interface or annotation block",
223                 ScopeUtils.isInInterfaceOrAnnotationBlock(
224                 getNode(TokenTypes.INTERFACE_DEF, TokenTypes.OBJBLOCK)));
225         assertFalse("Should return false when node is not in interface or annotation block",
226                 ScopeUtils.isInInterfaceOrAnnotationBlock(
227                 getNode(TokenTypes.CLASS_DEF, TokenTypes.OBJBLOCK)));
228         assertFalse("Should return false when node is not in interface or annotation block",
229                 ScopeUtils.isInInterfaceOrAnnotationBlock(
230                 getNode(TokenTypes.LITERAL_NEW, TokenTypes.IDENT)));
231         assertFalse("Should return false when node is not in interface or annotation block",
232                 ScopeUtils.isInInterfaceOrAnnotationBlock(
233                 getNode(TokenTypes.ENUM_DEF, TokenTypes.OBJBLOCK)));
234     }
235 
236     private static DetailAST getNode(int... nodeTypes) {
237         DetailAST ast = new DetailAST();
238         ast.setType(nodeTypes[0]);
239         for (int i = 1; i < nodeTypes.length; i++) {
240             final DetailAST astChild = new DetailAST();
241             astChild.setType(nodeTypes[i]);
242             ast.addChild(astChild);
243             ast = astChild;
244         }
245         return ast;
246     }
247 
248     private static DetailAST getNodeWithParentScope(int literal, String scope,
249                                                     int parentTokenType) {
250         final DetailAST ast = getNode(parentTokenType, TokenTypes.MODIFIERS, literal);
251         ast.setText(scope);
252         final DetailAST ast2 = getNode(TokenTypes.OBJBLOCK);
253         ast.getParent().getParent().addChild(ast2);
254         return ast;
255     }
256 }