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