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