View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 org.checkstyle.suppressionxpathfilter;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  import com.puppycrawl.tools.checkstyle.checks.coding.TextBlockGoogleStyleFormattingCheck;
29  
30  public class XpathRegressionTextBlockGoogleStyleFormattingTest extends AbstractXpathTestSupport {
31  
32      private static final Class<TextBlockGoogleStyleFormattingCheck> CLASS =
33              TextBlockGoogleStyleFormattingCheck.class;
34  
35      @Override
36      protected String getCheckName() {
37          return CLASS.getSimpleName();
38      }
39  
40      @Test
41      public void testTextBlocksFormatNotVerticallyAligned() throws Exception {
42          final File fileToProcess = new File(
43              getPath("InputXpathTextBlockGoogleStyleFormatting.java"));
44  
45          final DefaultConfiguration moduleConfig =
46              createModuleConfig(CLASS);
47  
48          final String[] expectedViolation = {
49              "8:13: " + getCheckMessage(CLASS,
50                  TextBlockGoogleStyleFormattingCheck.MSG_VERTICALLY_UNALIGNED),
51          };
52  
53          final List<String> expectedXpathQueries = List.of(
54                 "/COMPILATION_UNIT/CLASS_DEF"
55                         + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting']]"
56                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun']]"
57                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='simpleScript']]/ASSIGN/EXPR"
58                         + "/TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
59                         + "[@text='\\n            s\\n            ']]/TEXT_BLOCK_LITERAL_END"
60          );
61  
62          runVerifications(moduleConfig, fileToProcess, expectedViolation,
63                  expectedXpathQueries);
64      }
65  
66      @Test
67      public void testTextBlocksFormatNotVerticallyAlignedInMethodCall() throws Exception {
68          final File fileToProcess = new File(
69              getPath("InputXpathTextBlockGoogleStyleFormatting2.java"));
70  
71          final DefaultConfiguration moduleConfig =
72                  createModuleConfig(CLASS);
73  
74          final String[] expectedViolation = {
75              "15:13: " + getCheckMessage(CLASS,
76                  TextBlockGoogleStyleFormattingCheck.MSG_VERTICALLY_UNALIGNED),
77          };
78  
79          final List<String> expectedXpathQueries = List.of(
80              "/COMPILATION_UNIT/CLASS_DEF"
81                      + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting2']]"
82                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun1']]/SLIST/EXPR"
83                      + "/METHOD_CALL[./IDENT[@text='getData']]/ELIST/EXPR/"
84                      + "TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
85                      + "[@text='\\n            Hello,\\n            This is a multi-line message"
86                      + ".\\n            ']]/TEXT_BLOCK_LITERAL_END"
87          );
88  
89          runVerifications(moduleConfig, fileToProcess, expectedViolation,
90                  expectedXpathQueries);
91      }
92  
93      @Test
94      public void testTextBlocksFormatClosingQuotesNotOnNewLine() throws Exception {
95          final File fileToProcess = new File(
96              getPath("InputXpathTextBlockGoogleStyleFormatting3.java"));
97  
98          final DefaultConfiguration moduleConfig =
99                  createModuleConfig(CLASS);
100 
101         final String[] expectedViolation = {
102             "8:21: " + getCheckMessage(CLASS,
103                 TextBlockGoogleStyleFormattingCheck.MSG_CLOSE_QUOTES_ERROR),
104         };
105 
106         final List<String> expectedXpathQueries = List.of(
107             "/COMPILATION_UNIT/CLASS_DEF"
108                       + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting3']]"
109                       + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun1']]/SLIST/VARIABLE_DEF"
110                       + "[./IDENT[@text='simpleScriptViolation']]/ASSIGN/EXPR/"
111                       + "TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
112                       + "[@text='\\n this is sample text']]/TEXT_BLOCK_LITERAL_END"
113         );
114 
115         runVerifications(moduleConfig, fileToProcess, expectedViolation,
116                 expectedXpathQueries);
117     }
118 }