View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  
25  import java.io.File;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30  
31  public class SuppressionsStringPrinterTest extends AbstractTreeTestSupport {
32  
33      private static final String EOL = System.getProperty("line.separator");
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/suppressionsstringprinter";
38      }
39  
40      @Test
41      public void testIsProperUtilsClass() throws ReflectiveOperationException {
42          assertWithMessage("Constructor is not private")
43                  .that(isUtilsClassHasPrivateConstructor(SuppressionsStringPrinter.class))
44                  .isTrue();
45      }
46  
47      @Test
48      public void testCorrect() throws Exception {
49          final String expected = "/COMPILATION_UNIT/CLASS_DEF"
50                  + "[./IDENT[@text='InputSuppressionsStringPrinter']]" + EOL
51                  + "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputSuppressionsStringPrinter']]"
52                  + "/MODIFIERS" + EOL
53                  + "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputSuppressionsStringPrinter']]"
54                  + "/MODIFIERS/LITERAL_PUBLIC" + EOL;
55  
56          final File input = new File(getPath("InputSuppressionsStringPrinter.java"));
57          final String lineAndColumnNumber = "3:1";
58          final int tabWidth = 2;
59          final String result = SuppressionsStringPrinter.printSuppressions(input,
60                  lineAndColumnNumber, tabWidth);
61  
62          assertWithMessage("Invalid xpath queries")
63              .that(result)
64              .isEqualTo(expected);
65      }
66  
67      @Test
68      public void testCustomTabWidth() throws Exception {
69          final String expected = "/COMPILATION_UNIT/CLASS_DEF"
70                  + "[./IDENT[@text='InputSuppressionsStringPrinter']]"
71                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='toString']]" + EOL
72                  + "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
73                  + "[@text='InputSuppressionsStringPrinter']]/OBJBLOCK"
74                  + "/METHOD_DEF[./IDENT[@text='toString']]/MODIFIERS" + EOL
75                  + "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
76                  + "[@text='InputSuppressionsStringPrinter']]/OBJBLOCK"
77                  + "/METHOD_DEF[./IDENT[@text='toString']]/MODIFIERS/LITERAL_PUBLIC" + EOL;
78  
79          final File input = new File(getPath("InputSuppressionsStringPrinter.java"));
80          final String lineAndColumnNumber = "5:13";
81          final int tabWidth = 4;
82          final String result = SuppressionsStringPrinter.printSuppressions(input,
83                  lineAndColumnNumber, tabWidth);
84  
85          assertWithMessage("Invalid xpath queries")
86              .that(result)
87              .isEqualTo(expected);
88      }
89  
90      @Test
91      public void testCustomTabWidthEmptyResult() throws Exception {
92          final File input = new File(getPath("InputSuppressionsStringPrinter.java"));
93          final String lineAndColumnNumber = "5:13";
94          final int tabWidth = 6;
95          final String result = SuppressionsStringPrinter.printSuppressions(input,
96                  lineAndColumnNumber, tabWidth);
97          assertWithMessage("Invalid xpath queries")
98              .that(result)
99              .isEqualTo(EOL);
100     }
101 
102     @Test
103     public void testInvalidLineAndColumnNumberParameter() throws Exception {
104         final File input = new File(getPath("InputSuppressionsStringPrinter.java"));
105         final String invalidLineAndColumnNumber = "abc-432";
106         final int tabWidth = 2;
107         try {
108             SuppressionsStringPrinter.printSuppressions(input,
109                     invalidLineAndColumnNumber, tabWidth);
110             assertWithMessage("exception expected").fail();
111         }
112         catch (IllegalStateException ex) {
113             assertWithMessage("Invalid exception message")
114                 .that(ex.getMessage())
115                 .isEqualTo("abc-432 does not match valid format 'line:column'.");
116         }
117     }
118 
119     @Test
120     public void testParseFileTextThrowable() throws Exception {
121         final File input = new File(getNonCompilablePath("InputSuppressionsStringPrinter.java"));
122         final String lineAndColumnNumber = "2:3";
123         final int tabWidth = 2;
124         try {
125             SuppressionsStringPrinter.printSuppressions(input,
126                     lineAndColumnNumber, tabWidth);
127             assertWithMessage("exception expected").fail();
128         }
129         catch (CheckstyleException ex) {
130             assertWithMessage("Invalid class")
131                 .that(ex.getCause())
132                 .isInstanceOf(IllegalStateException.class);
133             assertWithMessage("Invalid exception message")
134                 .that(ex.getCause().toString())
135                 .isEqualTo(IllegalStateException.class.getName()
136                             + ": 2:0: no viable alternative at input 'classD'");
137         }
138     }
139 
140 }