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