View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.whitespace;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck.MSG_CONTAINS_TAB;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck.MSG_FILE_CONTAINS_TAB;
24  
25  import java.io.File;
26  
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.Definitions;
32  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
33  
34  public class FileTabCharacterCheckTest
35      extends AbstractModuleTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/whitespace/filetabcharacter";
40      }
41  
42      @Test
43      public void testDefault() throws Exception {
44          final DefaultConfiguration checkConfig =
45              createModuleConfig(FileTabCharacterCheck.class);
46          checkConfig.addAttribute("eachLine", "false");
47          final String[] expected = {
48              "19:25: " + getCheckMessage(MSG_FILE_CONTAINS_TAB),
49          };
50          final File[] files = {
51              new File(getPath("InputFileTabCharacterSimple.java")),
52          };
53          verify(createChecker(checkConfig), files, getPath("InputFileTabCharacterSimple.java"),
54              expected);
55      }
56  
57      @Test
58      public void testVerbose() throws Exception {
59          final DefaultConfiguration checkConfig =
60                  createModuleConfig(FileTabCharacterCheck.class);
61          checkConfig.addAttribute("eachLine", "true");
62          final String[] expected = {
63              "19:25: " + getCheckMessage(MSG_CONTAINS_TAB),
64              "145:35: " + getCheckMessage(MSG_CONTAINS_TAB),
65              "146:64: " + getCheckMessage(MSG_CONTAINS_TAB),
66              "154:9: " + getCheckMessage(MSG_CONTAINS_TAB),
67              "155:10: " + getCheckMessage(MSG_CONTAINS_TAB),
68              "156:1: " + getCheckMessage(MSG_CONTAINS_TAB),
69              "157:3: " + getCheckMessage(MSG_CONTAINS_TAB),
70              "158:3: " + getCheckMessage(MSG_CONTAINS_TAB),
71          };
72          final File[] files = {
73              new File(getPath("InputFileTabCharacterSimple.java")),
74          };
75          verify(createChecker(checkConfig), files, getPath("InputFileTabCharacterSimple.java"),
76              expected);
77      }
78  
79      @Test
80      public void testBadFile() throws Exception {
81          final DefaultConfiguration checkConfig =
82                  createModuleConfig(FileTabCharacterCheck.class);
83          checkConfig.addAttribute("eachLine", "false");
84          final String path = getPath("Claira");
85          final String exceptionMessage = " (No such file or directory)";
86          final LocalizedMessage localizedMessage = new LocalizedMessage(0,
87                  Definitions.CHECKSTYLE_BUNDLE, "general.exception",
88                  new String[] {path + exceptionMessage}, null, getClass(), null);
89  
90          final String[] expected = {
91              "0: " + localizedMessage.getMessage(),
92          };
93          final File[] files = {
94              new File(path),
95          };
96          verify(createChecker(checkConfig), files, path, expected);
97      }
98  
99  }