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.checks.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class DesignForExtensionCheckTest
33      extends AbstractModuleTestSupport {
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          assertArrayEquals("Default required tokens are invalid",
44              expected, checkObj.getRequiredTokens());
45      }
46  
47      @Test
48      public void testIt() throws Exception {
49          final DefaultConfiguration checkConfig =
50              createModuleConfig(DesignForExtensionCheck.class);
51          final String[] expected = {
52              "46:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtension", "doh"),
53              "100:9: " + getCheckMessage(MSG_KEY, "anotherNonFinalClass", "someMethod"),
54          };
55          verify(checkConfig, getPath("InputDesignForExtension.java"), expected);
56  
57      }
58  
59      @Test
60      public void testGetAcceptableTokens() {
61          final DesignForExtensionCheck obj = new DesignForExtensionCheck();
62          final int[] expected = {TokenTypes.METHOD_DEF};
63          assertArrayEquals("Default acceptable tokens are invalid",
64              expected, obj.getAcceptableTokens());
65      }
66  
67      @Test
68      public void testOverridableMethods() throws Exception {
69          final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
70          final String[] expected = {
71              "6:9: " + getCheckMessage(MSG_KEY, "A", "foo1"),
72              "30:9: " + getCheckMessage(MSG_KEY, "A", "foo8"),
73              "35:9: " + getCheckMessage(MSG_KEY, "A", "foo9"),
74              "42:9: " + getCheckMessage(MSG_KEY, "A", "foo10"),
75              "49:9: " + getCheckMessage(MSG_KEY, "A", "foo11"),
76              "54:9: " + getCheckMessage(MSG_KEY, "A", "foo12"),
77              "61:9: " + getCheckMessage(MSG_KEY, "A", "foo13"),
78              "68:9: " + getCheckMessage(MSG_KEY, "A", "foo14"),
79              "90:9: " + getCheckMessage(MSG_KEY, "A", "foo22"),
80              "96:9: " + getCheckMessage(MSG_KEY, "A", "foo23"),
81              "110:9: " + getCheckMessage(MSG_KEY, "A", "foo26"),
82              "117:9: " + getCheckMessage(MSG_KEY, "A", "foo27"),
83          };
84          verify(checkConfig, getPath("InputDesignForExtensionOverridableMethods.java"), expected);
85      }
86  
87      @Test
88      public void testIgnoredAnnotationsOption() throws Exception {
89          final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
90          checkConfig.addAttribute("ignoredAnnotations", "Override, Deprecated, MyAnnotation");
91          final String[] expected = {
92              "31:5: "
93                  + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo1"),
94              "141:5: "
95                  + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo21"),
96              "146:5: "
97                  + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "setAge"),
98              "161:5: "
99                  + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo24"),
100         };
101         verify(checkConfig, getPath("InputDesignForExtensionIgnoredAnnotations.java"), expected);
102     }
103 
104     @Test
105     public void testIgnoreAnnotationsOptionWithMultipleAnnotations() throws Exception {
106         final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
107         checkConfig.addAttribute("ignoredAnnotations",
108             "Override, Deprecated, Before, After, BeforeClass, AfterClass");
109         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
110         verify(checkConfig, getPath("InputDesignForExtensionMultipleAnnotations.java"), expected);
111     }
112 
113     @Test
114     public void testNativeMethods() throws Exception {
115         final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
116         checkConfig.addAttribute("ignoredAnnotations", "Deprecated");
117         final String[] expected = {
118             "8:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo1"),
119         };
120         verify(checkConfig, getPath("InputDesignForExtensionNativeMethods.java"), expected);
121     }
122 }