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