Source code: com/puppycrawl/tools/checkstyle/checks/design/VisibilityModifierCheckTest.java
1 package com.puppycrawl.tools.checkstyle.checks.design;
2
3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
4 import com.puppycrawl.tools.checkstyle.Checker;
5 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
6
7 public class VisibilityModifierCheckTest
8 extends BaseCheckTestCase
9 {
10 private Checker getChecker() throws Exception
11 {
12 final DefaultConfiguration checkConfig =
13 createCheckConfig(VisibilityModifierCheck.class);
14 checkConfig.addAttribute("publicMemberPattern", "^f[A-Z][a-zA-Z0-9]*$");
15 return createChecker(checkConfig);
16 }
17
18 public void testInner()
19 throws Exception
20 {
21 final String[] expected = {
22 "30:24: Variable 'rData' must be private and have accessor methods.",
23 "33:27: Variable 'protectedVariable' must be private and have accessor methods.",
24 "36:17: Variable 'packageVariable' must be private and have accessor methods.",
25 "41:29: Variable 'sWeird' must be private and have accessor methods.",
26 "43:19: Variable 'sWeird2' must be private and have accessor methods."
27 };
28 verify(getChecker(), getPath("InputInner.java"), expected);
29 }
30
31 public void testIgnoreAccess()
32 throws Exception
33 {
34 final DefaultConfiguration checkConfig =
35 createCheckConfig(VisibilityModifierCheck.class);
36 checkConfig.addAttribute("publicMemberPattern", "^r[A-Z]");
37 checkConfig.addAttribute("protectedAllowed", "true");
38 checkConfig.addAttribute("packageAllowed", "true");
39 final String[] expected = {
40 "17:20: Variable 'fData' must be private and have accessor methods.",
41 };
42 verify(checkConfig, getPath("InputInner.java"), expected);
43 }
44
45 public void testSimple() throws Exception {
46 final String[] expected = {
47 "39:19: Variable 'mNumCreated2' must be private and have accessor methods.",
48 "49:23: Variable 'sTest1' must be private and have accessor methods.",
49 "51:26: Variable 'sTest3' must be private and have accessor methods.",
50 "53:16: Variable 'sTest2' must be private and have accessor methods.",
51 "56:9: Variable 'mTest1' must be private and have accessor methods.",
52 "58:16: Variable 'mTest2' must be private and have accessor methods.",
53 };
54 verify(getChecker(), getPath("InputSimple.java"), expected);
55 }
56
57 public void testStrictJavadoc()
58 throws Exception
59 {
60 final String[] expected = {
61 "44:9: Variable 'mLen' must be private and have accessor methods.",
62 "45:19: Variable 'mDeer' must be private and have accessor methods.",
63 "46:16: Variable 'aFreddo' must be private and have accessor methods.",
64 };
65 verify(getChecker(), getPath("InputPublicOnly.java"), expected);
66 }
67 }