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.AvoidInlineConditionalsCheck;
31  
32  public class XpathRegressionAvoidInlineConditionalsTest extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return AvoidInlineConditionalsCheck.class.getSimpleName();
37      }
38  
39      @Test
40      public void testInlineConditionalsVariableDef() throws Exception {
41          final File fileToProcess = new File(
42                  getPath("InputXpathAvoidInlineConditionalsVariableDef.java"));
43  
44          final DefaultConfiguration moduleConfig =
45                  createModuleConfig(AvoidInlineConditionalsCheck.class);
46  
47          final String[] expectedViolation = {
48              "5:50: " + getCheckMessage(AvoidInlineConditionalsCheck.class,
49                  AvoidInlineConditionalsCheck.MSG_KEY),
50          };
51  
52          final List<String> expectedXpathQueries = Arrays.asList(
53                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
54                          + "InputXpathAvoidInlineConditionalsVariableDef']]"
55                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='substring']]/SLIST"
56                          + "/VARIABLE_DEF[./IDENT[@text='b']]/ASSIGN/EXPR",
57                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
58                          + "InputXpathAvoidInlineConditionalsVariableDef']]"
59                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='substring']]/SLIST"
60                          + "/VARIABLE_DEF[./IDENT[@text='b']]/ASSIGN/EXPR/QUESTION"
61          );
62  
63          runVerifications(moduleConfig, fileToProcess, expectedViolation,
64                  expectedXpathQueries);
65      }
66  
67      @Test
68      public void testInlineConditionalsAssign() throws Exception {
69          final File fileToProcess = new File(
70                  getPath("InputXpathAvoidInlineConditionalsAssign.java"));
71  
72          final DefaultConfiguration moduleConfig =
73                  createModuleConfig(AvoidInlineConditionalsCheck.class);
74  
75          final String[] expectedViolation = {
76              "7:43: " + getCheckMessage(AvoidInlineConditionalsCheck.class,
77                  AvoidInlineConditionalsCheck.MSG_KEY),
78          };
79  
80          final List<String> expectedXpathQueries = Collections.singletonList(
81                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
82                          + "InputXpathAvoidInlineConditionalsAssign']]"
83                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='setB']]/SLIST"
84                          + "/EXPR/ASSIGN[./IDENT[@text='b']]/QUESTION"
85          );
86  
87          runVerifications(moduleConfig, fileToProcess, expectedViolation,
88                  expectedXpathQueries);
89      }
90  
91      @Test
92      public void testInlineConditionalsAssert() throws Exception {
93          final File fileToProcess = new File(
94                  getPath("InputXpathAvoidInlineConditionalsAssert.java"));
95  
96          final DefaultConfiguration moduleConfig =
97                  createModuleConfig(AvoidInlineConditionalsCheck.class);
98  
99          final String[] expectedViolation = {
100             "8:31: " + getCheckMessage(AvoidInlineConditionalsCheck.class,
101                 AvoidInlineConditionalsCheck.MSG_KEY),
102         };
103 
104         final List<String> expectedXpathQueries = Arrays.asList(
105                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
106                         + "InputXpathAvoidInlineConditionalsAssert']]"
107                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='assertA']]/SLIST"
108                         + "/LITERAL_ASSERT/EXPR[./QUESTION/METHOD_CALL/DOT/IDENT[@text='a']]",
109                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
110                         + "InputXpathAvoidInlineConditionalsAssert']]"
111                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='assertA']]/SLIST"
112                         + "/LITERAL_ASSERT/EXPR/QUESTION"
113         );
114 
115         runVerifications(moduleConfig, fileToProcess, expectedViolation,
116                 expectedXpathQueries);
117     }
118 
119 }