001: package com.puppycrawl.tools.checkstyle.checks.coding;
002:
003: import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
004: import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
005:
006: public class HiddenFieldCheckTest extends BaseCheckTestCase {
007: public void testNoParameters() throws Exception {
008: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
009: checkConfig.addAttribute("tokens", "VARIABLE_DEF");
010: final String[] expected = { "18:13: 'hidden' hides a field.",
011: "27:13: 'hidden' hides a field.",
012: "32:18: 'hidden' hides a field.",
013: "46:17: 'innerHidden' hides a field.",
014: "55:17: 'innerHidden' hides a field.",
015: "56:17: 'hidden' hides a field.",
016: "61:22: 'innerHidden' hides a field.",
017: "64:22: 'hidden' hides a field.",
018: "76:17: 'innerHidden' hides a field.",
019: "77:17: 'hidden' hides a field.",
020: "82:13: 'hidden' hides a field.",
021: "138:13: 'hidden' hides a field.",
022: "143:13: 'hidden' hides a field.",
023: "148:13: 'hidden' hides a field.",
024: "152:13: 'hidden' hides a field.",
025: "200:17: 'hidden' hides a field.",
026: "217:13: 'hidden' hides a field.",
027: "223:13: 'hiddenStatic' hides a field.", };
028: verify(checkConfig, getPath("InputHiddenField.java"), expected);
029: }
030:
031: public void testDefault() throws Exception {
032: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
033: final String[] expected = { "18:13: 'hidden' hides a field.",
034: "21:33: 'hidden' hides a field.",
035: "27:13: 'hidden' hides a field.",
036: "32:18: 'hidden' hides a field.",
037: "36:33: 'hidden' hides a field.",
038: "46:17: 'innerHidden' hides a field.",
039: "49:26: 'innerHidden' hides a field.",
040: "55:17: 'innerHidden' hides a field.",
041: "56:17: 'hidden' hides a field.",
042: "61:22: 'innerHidden' hides a field.",
043: "64:22: 'hidden' hides a field.",
044: "69:17: 'innerHidden' hides a field.",
045: "70:17: 'hidden' hides a field.",
046: "76:17: 'innerHidden' hides a field.",
047: "77:17: 'hidden' hides a field.",
048: "82:13: 'hidden' hides a field.",
049: "100:29: 'prop' hides a field.",
050: "106:29: 'prop' hides a field.",
051: "112:29: 'prop' hides a field.",
052: "124:28: 'prop' hides a field.",
053: "138:13: 'hidden' hides a field.",
054: "143:13: 'hidden' hides a field.",
055: "148:13: 'hidden' hides a field.",
056: "152:13: 'hidden' hides a field.",
057: "179:23: 'y' hides a field.",
058: "200:17: 'hidden' hides a field.",
059: "210:20: 'hidden' hides a field.",
060: "217:13: 'hidden' hides a field.",
061: "223:13: 'hiddenStatic' hides a field.",
062: "230:41: 'x' hides a field.", };
063: verify(checkConfig, getPath("InputHiddenField.java"), expected);
064: }
065:
066: /** tests ignoreFormat property */
067: public void testIgnoreFormat() throws Exception {
068: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
069: checkConfig.addAttribute("ignoreFormat", "^i.*$");
070: final String[] expected = { "18:13: 'hidden' hides a field.",
071: "21:33: 'hidden' hides a field.",
072: "27:13: 'hidden' hides a field.",
073: "32:18: 'hidden' hides a field.",
074: "36:33: 'hidden' hides a field.",
075: "56:17: 'hidden' hides a field.",
076: "64:22: 'hidden' hides a field.",
077: "70:17: 'hidden' hides a field.",
078: "77:17: 'hidden' hides a field.",
079: "82:13: 'hidden' hides a field.",
080: "100:29: 'prop' hides a field.",
081: "106:29: 'prop' hides a field.",
082: "112:29: 'prop' hides a field.",
083: "124:28: 'prop' hides a field.",
084: "138:13: 'hidden' hides a field.",
085: "143:13: 'hidden' hides a field.",
086: "148:13: 'hidden' hides a field.",
087: "152:13: 'hidden' hides a field.",
088: "179:23: 'y' hides a field.",
089: "200:17: 'hidden' hides a field.",
090: "210:20: 'hidden' hides a field.",
091: "217:13: 'hidden' hides a field.",
092: "223:13: 'hiddenStatic' hides a field.",
093: "230:41: 'x' hides a field.", };
094: verify(checkConfig, getPath("InputHiddenField.java"), expected);
095: }
096:
097: /** tests ignoreSetter property */
098: public void testIgnoreSetter() throws Exception {
099: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
100: checkConfig.addAttribute("ignoreSetter", "true");
101: final String[] expected = { "18:13: 'hidden' hides a field.",
102: "21:33: 'hidden' hides a field.",
103: "27:13: 'hidden' hides a field.",
104: "32:18: 'hidden' hides a field.",
105: "36:33: 'hidden' hides a field.",
106: "46:17: 'innerHidden' hides a field.",
107: "49:26: 'innerHidden' hides a field.",
108: "55:17: 'innerHidden' hides a field.",
109: "56:17: 'hidden' hides a field.",
110: "61:22: 'innerHidden' hides a field.",
111: "64:22: 'hidden' hides a field.",
112: "69:17: 'innerHidden' hides a field.",
113: "70:17: 'hidden' hides a field.",
114: "76:17: 'innerHidden' hides a field.",
115: "77:17: 'hidden' hides a field.",
116: "82:13: 'hidden' hides a field.",
117: "106:29: 'prop' hides a field.",
118: "112:29: 'prop' hides a field.",
119: "124:28: 'prop' hides a field.",
120: "138:13: 'hidden' hides a field.",
121: "143:13: 'hidden' hides a field.",
122: "148:13: 'hidden' hides a field.",
123: "152:13: 'hidden' hides a field.",
124: "179:23: 'y' hides a field.",
125: "200:17: 'hidden' hides a field.",
126: "210:20: 'hidden' hides a field.",
127: "217:13: 'hidden' hides a field.",
128: "223:13: 'hiddenStatic' hides a field.",
129: "230:41: 'x' hides a field.", };
130: verify(checkConfig, getPath("InputHiddenField.java"), expected);
131: }
132:
133: /** tests ignoreConstructorParameter property */
134: public void testIgnoreConstructorParameter() throws Exception {
135: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
136: checkConfig.addAttribute("ignoreConstructorParameter", "true");
137: final String[] expected = { "18:13: 'hidden' hides a field.",
138: "27:13: 'hidden' hides a field.",
139: "32:18: 'hidden' hides a field.",
140: "36:33: 'hidden' hides a field.",
141: "46:17: 'innerHidden' hides a field.",
142: "55:17: 'innerHidden' hides a field.",
143: "56:17: 'hidden' hides a field.",
144: "61:22: 'innerHidden' hides a field.",
145: "64:22: 'hidden' hides a field.",
146: "69:17: 'innerHidden' hides a field.",
147: "70:17: 'hidden' hides a field.",
148: "76:17: 'innerHidden' hides a field.",
149: "77:17: 'hidden' hides a field.",
150: "82:13: 'hidden' hides a field.",
151: "100:29: 'prop' hides a field.",
152: "106:29: 'prop' hides a field.",
153: "112:29: 'prop' hides a field.",
154: "124:28: 'prop' hides a field.",
155: "138:13: 'hidden' hides a field.",
156: "143:13: 'hidden' hides a field.",
157: "148:13: 'hidden' hides a field.",
158: "152:13: 'hidden' hides a field.",
159: "179:23: 'y' hides a field.",
160: "200:17: 'hidden' hides a field.",
161: "217:13: 'hidden' hides a field.",
162: "223:13: 'hiddenStatic' hides a field.",
163: "230:41: 'x' hides a field.", };
164: verify(checkConfig, getPath("InputHiddenField.java"), expected);
165: }
166:
167: /** Test against a class with field declarations in different order */
168: public void testReordered() throws Exception {
169: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
170: final String[] expected = { "18:13: 'hidden' hides a field.",
171: "21:40: 'hidden' hides a field.",
172: "27:13: 'hidden' hides a field.",
173: "32:18: 'hidden' hides a field.",
174: "36:33: 'hidden' hides a field.",
175: "46:17: 'innerHidden' hides a field.",
176: "49:26: 'innerHidden' hides a field.",
177: "55:17: 'innerHidden' hides a field.",
178: "56:17: 'hidden' hides a field.",
179: "61:22: 'innerHidden' hides a field.",
180: "64:22: 'hidden' hides a field.",
181: "69:17: 'innerHidden' hides a field.",
182: "70:17: 'hidden' hides a field.",
183: "76:17: 'innerHidden' hides a field.",
184: "77:17: 'hidden' hides a field.",
185: "83:13: 'hidden' hides a field.",
186: "105:17: 'hidden' hides a field.",
187: "118:21: 'hidden' hides a field.",
188: "125:13: 'hidden' hides a field.",
189: "131:13: 'hiddenStatic' hides a field.", };
190: verify(checkConfig, getPath("InputHiddenFieldReorder.java"),
191: expected);
192: }
193:
194: public void testIgnoreAbstractMethods() throws Exception {
195: final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);
196: checkConfig.addAttribute("ignoreAbstractMethods", "true");
197:
198: final String[] expected = { "18:13: 'hidden' hides a field.",
199: "21:33: 'hidden' hides a field.",
200: "27:13: 'hidden' hides a field.",
201: "32:18: 'hidden' hides a field.",
202: "36:33: 'hidden' hides a field.",
203: "46:17: 'innerHidden' hides a field.",
204: "49:26: 'innerHidden' hides a field.",
205: "55:17: 'innerHidden' hides a field.",
206: "56:17: 'hidden' hides a field.",
207: "61:22: 'innerHidden' hides a field.",
208: "64:22: 'hidden' hides a field.",
209: "69:17: 'innerHidden' hides a field.",
210: "70:17: 'hidden' hides a field.",
211: "76:17: 'innerHidden' hides a field.",
212: "77:17: 'hidden' hides a field.",
213: "82:13: 'hidden' hides a field.",
214: "100:29: 'prop' hides a field.",
215: "106:29: 'prop' hides a field.",
216: "112:29: 'prop' hides a field.",
217: "124:28: 'prop' hides a field.",
218: "138:13: 'hidden' hides a field.",
219: "143:13: 'hidden' hides a field.",
220: "148:13: 'hidden' hides a field.",
221: "152:13: 'hidden' hides a field.",
222: "179:23: 'y' hides a field.",
223: "200:17: 'hidden' hides a field.",
224: "210:20: 'hidden' hides a field.",
225: "217:13: 'hidden' hides a field.",
226: "223:13: 'hiddenStatic' hides a field.", };
227: verify(checkConfig, getPath("InputHiddenField.java"), expected);
228: }
229: }
|