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.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.modifier.InterfaceMemberImpliedModifierCheck;
30  
31  public class XpathRegressionInterfaceMemberImpliedModifierTest extends AbstractXpathTestSupport {
32  
33      @Override
34      protected String getCheckName() {
35          return InterfaceMemberImpliedModifierCheck.class.getSimpleName();
36      }
37  
38      @Test
39      public void testField() throws Exception {
40          final File fileToProcess = new File(
41              getPath("InputXpathInterfaceMemberImpliedModifierField.java")
42          );
43  
44          final DefaultConfiguration moduleConfig =
45              createModuleConfig(InterfaceMemberImpliedModifierCheck.class);
46  
47          final String[] expectedViolation = {
48              "4:5: " + getCheckMessage(InterfaceMemberImpliedModifierCheck.class,
49                  InterfaceMemberImpliedModifierCheck.MSG_KEY, "final"),
50          };
51  
52          final List<String> expectedXpathQueries = Arrays.asList(
53              "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
54                  + "[@text='InputXpathInterfaceMemberImpliedModifierField']]"
55                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='str']]",
56  
57              "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
58                  + "[@text='InputXpathInterfaceMemberImpliedModifierField']]"
59                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='str']]"
60                  + "/MODIFIERS",
61  
62              "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
63                  + "[@text='InputXpathInterfaceMemberImpliedModifierField']]"
64                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='str']]"
65                  + "/MODIFIERS/LITERAL_PUBLIC"
66          );
67  
68          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
69      }
70  
71      @Test
72      public void testMethod() throws Exception {
73          final File fileToProcess = new File(
74              getPath("InputXpathInterfaceMemberImpliedModifierMethod.java")
75          );
76  
77          final DefaultConfiguration moduleConfig =
78              createModuleConfig(InterfaceMemberImpliedModifierCheck.class);
79  
80          final String[] expectedViolation = {
81              "4:5: " + getCheckMessage(InterfaceMemberImpliedModifierCheck.class,
82                  InterfaceMemberImpliedModifierCheck.MSG_KEY, "public"),
83          };
84  
85          final List<String> expectedXpathQueries = Arrays.asList(
86              "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
87                  + "[@text='InputXpathInterfaceMemberImpliedModifierMethod']]"
88                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='setData']]",
89  
90              "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
91                  + "[@text='InputXpathInterfaceMemberImpliedModifierMethod']]"
92                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='setData']]"
93                  + "/MODIFIERS",
94  
95              "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
96                  + "[@text='InputXpathInterfaceMemberImpliedModifierMethod']]"
97                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='setData']]"
98                  + "/MODIFIERS/ABSTRACT"
99          );
100 
101         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
102     }
103 
104     @Test
105     public void testInner() throws Exception {
106         final File fileToProcess = new File(
107             getPath("InputXpathInterfaceMemberImpliedModifierInner.java")
108         );
109 
110         final DefaultConfiguration moduleConfig =
111             createModuleConfig(InterfaceMemberImpliedModifierCheck.class);
112 
113         final String[] expectedViolation = {
114             "4:5: " + getCheckMessage(InterfaceMemberImpliedModifierCheck.class,
115                 InterfaceMemberImpliedModifierCheck.MSG_KEY, "static"),
116         };
117 
118         final List<String> expectedXpathQueries = Arrays.asList(
119             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
120                 + "[@text='InputXpathInterfaceMemberImpliedModifierInner']]"
121                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Data']]",
122 
123             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
124                 + "[@text='InputXpathInterfaceMemberImpliedModifierInner']]"
125                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Data']]"
126                 + "/MODIFIERS",
127 
128             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
129                 + "[@text='InputXpathInterfaceMemberImpliedModifierInner']]"
130                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Data']]"
131                 + "/MODIFIERS/LITERAL_PUBLIC"
132         );
133 
134         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
135     }
136 }