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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck.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 OuterTypeFilenameCheckTest extends AbstractModuleTestSupport {
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/outertypefilename";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final OuterTypeFilenameCheck checkObj = new OuterTypeFilenameCheck();
41          final int[] expected = {
42              TokenTypes.CLASS_DEF,
43              TokenTypes.INTERFACE_DEF,
44              TokenTypes.ENUM_DEF,
45              TokenTypes.ANNOTATION_DEF,
46          };
47          assertArrayEquals("Required tokens array differs from expected",
48                  expected, checkObj.getRequiredTokens());
49      }
50  
51      @Test
52      public void testGood1() throws Exception {
53          final DefaultConfiguration checkConfig =
54              createModuleConfig(OuterTypeFilenameCheck.class);
55          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
56          verify(checkConfig, getPath("InputOuterTypeFilenameIllegalTokens.java"), expected);
57      }
58  
59      @Test
60      public void testGood2() throws Exception {
61          final DefaultConfiguration checkConfig =
62              createModuleConfig(OuterTypeFilenameCheck.class);
63          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
64          verify(checkConfig, getPath("InputOuterTypeFilename15Extensions.java"), expected);
65      }
66  
67      @Test
68      public void testGetAcceptableTokens() {
69          final OuterTypeFilenameCheck check = new OuterTypeFilenameCheck();
70          final int[] actual = check.getAcceptableTokens();
71          final int[] expected = {
72              TokenTypes.CLASS_DEF,
73              TokenTypes.INTERFACE_DEF,
74              TokenTypes.ENUM_DEF,
75              TokenTypes.ANNOTATION_DEF,
76          };
77          assertArrayEquals("Acceptable tokens array differs from expected",
78                  expected, actual);
79      }
80  
81      @Test
82      public void testNestedClass() throws Exception {
83          final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
84          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
85          verify(checkConfig, getPath("InputOuterTypeFilename1.java"), expected);
86      }
87  
88      @Test
89      public void testFinePublic() throws Exception {
90          final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
91          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
92          verify(checkConfig, getPath("InputOuterTypeFilename2.java"), expected);
93      }
94  
95      @Test
96      public void testPublicClassIsNotFirst() throws Exception {
97          final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
98          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
99          verify(checkConfig, getPath("InputOuterTypeFilenameCheckPublic.java"), expected);
100     }
101 
102     @Test
103     public void testFineDefault() throws Exception {
104         final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
105         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
106         verify(checkConfig, getPath("InputOuterTypeFilename3.java"), expected);
107     }
108 
109     @Test
110     public void testWrongDefault() throws Exception {
111         final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
112         final String[] expected = {
113             "4: " + getCheckMessage(MSG_KEY),
114         };
115         verify(checkConfig, getPath("InputOuterTypeFilename5.java"), expected);
116     }
117 
118     @Test
119     public void testPackageAnnotation() throws Exception {
120         final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
121 
122         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
123 
124         verify(checkConfig, getNonCompilablePath("package-info.java"), expected);
125     }
126 }