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.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      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/whitespace/filetabcharacter";
39      }
40  
41      @Test
42      public void testDefault() throws Exception {
43          final DefaultConfiguration checkConfig =
44              createModuleConfig(FileTabCharacterCheck.class);
45          checkConfig.addAttribute("eachLine", "false");
46          final String[] expected = {
47              "19:25: " + getCheckMessage(MSG_FILE_CONTAINS_TAB),
48          };
49          final File[] files = {
50              new File(getPath("InputFileTabCharacterSimple.java")),
51          };
52          verify(createChecker(checkConfig), files, getPath("InputFileTabCharacterSimple.java"),
53              expected);
54      }
55  
56      @Test
57      public void testVerbose() throws Exception {
58          final DefaultConfiguration checkConfig =
59                  createModuleConfig(FileTabCharacterCheck.class);
60          checkConfig.addAttribute("eachLine", "true");
61          final String[] expected = {
62              "19:25: " + getCheckMessage(MSG_CONTAINS_TAB),
63              "145:35: " + getCheckMessage(MSG_CONTAINS_TAB),
64              "146:64: " + getCheckMessage(MSG_CONTAINS_TAB),
65              "154:9: " + getCheckMessage(MSG_CONTAINS_TAB),
66              "155:10: " + getCheckMessage(MSG_CONTAINS_TAB),
67              "156:1: " + getCheckMessage(MSG_CONTAINS_TAB),
68              "157:3: " + getCheckMessage(MSG_CONTAINS_TAB),
69              "158:3: " + getCheckMessage(MSG_CONTAINS_TAB),
70          };
71          final File[] files = {
72              new File(getPath("InputFileTabCharacterSimple.java")),
73          };
74          verify(createChecker(checkConfig), files, getPath("InputFileTabCharacterSimple.java"),
75              expected);
76      }
77  
78      @Test
79      public void testBadFile() throws Exception {
80          final DefaultConfiguration checkConfig =
81                  createModuleConfig(FileTabCharacterCheck.class);
82          checkConfig.addAttribute("eachLine", "false");
83          final String path = getPath("Claira");
84          final String exceptionMessage = " (No such file or directory)";
85          final LocalizedMessage localizedMessage = new LocalizedMessage(0,
86                  Definitions.CHECKSTYLE_BUNDLE, "general.exception",
87                  new String[] {path + exceptionMessage}, null, getClass(), null);
88  
89          final String[] expected = {
90              "0: " + localizedMessage.getMessage(),
91          };
92          final File[] files = {
93              new File(path),
94          };
95          verify(createChecker(checkConfig), files, path, expected);
96      }
97  }