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 org.checkstyle.suppressionxpathfilter;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.StringLiteralEqualityCheck;
31  
32  public class XpathRegressionStringLiteralEqualityTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = StringLiteralEqualityCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Test
42      public void testEqualityTrue() throws Exception {
43          final File fileToProcess =
44                  new File(getPath("InputXpathStringLiteralEqualityTrue.java"));
45          final DefaultConfiguration moduleConfig =
46                  createModuleConfig(StringLiteralEqualityCheck.class);
47          final String[] expectedViolation = {
48              "6:17: " + getCheckMessage(StringLiteralEqualityCheck.class,
49                      StringLiteralEqualityCheck.MSG_KEY, "=="),
50          };
51          final List<String> expectedXpathQueries = Arrays.asList(
52              "/COMPILATION_UNIT"
53                  + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityTrue']]"
54                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
55                  + "/SLIST/LITERAL_IF/EXPR",
56              "/COMPILATION_UNIT"
57                  + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityTrue']]"
58                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
59                  + "/SLIST/LITERAL_IF/EXPR/EQUAL[./IDENT[@text='foo']]"
60  
61          );
62  
63          runVerifications(moduleConfig, fileToProcess, expectedViolation,
64                  expectedXpathQueries);
65      }
66  
67      @Test
68      public void testEqualityFalse() throws Exception {
69          final File fileToProcess =
70                  new File(getPath("InputXpathStringLiteralEqualityFalse.java"));
71          final DefaultConfiguration moduleConfig =
72                  createModuleConfig(StringLiteralEqualityCheck.class);
73          final String[] expectedViolation = {
74              "6:20: " + getCheckMessage(StringLiteralEqualityCheck.class,
75                      StringLiteralEqualityCheck.MSG_KEY, "!="),
76          };
77          final List<String> expectedXpathQueries = Arrays.asList(
78              "/COMPILATION_UNIT"
79                  + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityFalse']]"
80                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
81                  + "/SLIST/LITERAL_WHILE/EXPR",
82              "/COMPILATION_UNIT"
83                  + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityFalse']]"
84                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
85                  + "/SLIST/LITERAL_WHILE/EXPR/NOT_EQUAL[./IDENT[@text='foo']]"
86  
87          );
88  
89          runVerifications(moduleConfig, fileToProcess, expectedViolation,
90                  expectedXpathQueries);
91      }
92  
93      @Test
94      public void testEqualityExp() throws Exception {
95          final File fileToProcess =
96                  new File(getPath("InputXpathStringLiteralEqualityExp.java"));
97          final DefaultConfiguration moduleConfig =
98                  createModuleConfig(StringLiteralEqualityCheck.class);
99          final String[] expectedViolation = {
100             "6:29: " + getCheckMessage(StringLiteralEqualityCheck.class,
101                     StringLiteralEqualityCheck.MSG_KEY, "=="),
102         };
103         final List<String> expectedXpathQueries = Collections.singletonList(
104             "/COMPILATION_UNIT"
105                 + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityExp']]"
106                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
107                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='flag']]"
108                 + "/ASSIGN/EXPR/EQUAL[./IDENT[@text='foo']]"
109 
110         );
111 
112         runVerifications(moduleConfig, fileToProcess, expectedViolation,
113                 expectedXpathQueries);
114     }
115 }