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.annotation.AnnotationOnSameLineCheck;
30  
31  public class XpathRegressionAnnotationOnSameLineTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = AnnotationOnSameLineCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void testOne() throws Exception {
42          final File fileToProcess =
43                  new File(getPath(
44                          "InputXpathAnnotationOnSameLineMethod.java"));
45  
46          final DefaultConfiguration moduleConfig =
47                  createModuleConfig(AnnotationOnSameLineCheck.class);
48  
49          moduleConfig.addProperty("tokens",
50                  "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
51                  + "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
52                  + "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
53                  + "ANNOTATION_FIELD_DEF");
54  
55          final String[] expectedViolation = {
56              "6:5: " + getCheckMessage(AnnotationOnSameLineCheck.class,
57                       AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE,
58                       "Deprecated"),
59          };
60  
61          final List<String> expectedXpathQueries = Arrays.asList(
62                  "/COMPILATION_UNIT/CLASS_DEF"
63                          + "[./IDENT[@text='InputXpathAnnotationOnSameLineMethod']]"
64                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]",
65                  "/COMPILATION_UNIT/CLASS_DEF"
66                          + "[./IDENT[@text='InputXpathAnnotationOnSameLineMethod']]"
67                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]/MODIFIERS",
68                  "/COMPILATION_UNIT/CLASS_DEF"
69                          + "[./IDENT[@text='InputXpathAnnotationOnSameLineMethod']]"
70                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]/MODIFIERS"
71                          + "/ANNOTATION[./IDENT[@text='Deprecated']]",
72                  "/COMPILATION_UNIT/CLASS_DEF"
73                          + "[./IDENT[@text='InputXpathAnnotationOnSameLineMethod']]"
74                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]"
75                          + "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
76          );
77  
78          runVerifications(moduleConfig, fileToProcess, expectedViolation,
79                  expectedXpathQueries);
80      }
81  
82      @Test
83      public void testTwo() throws Exception {
84          final File fileToProcess =
85                  new File(getPath(
86                          "InputXpathAnnotationOnSameLineField.java"));
87  
88          final DefaultConfiguration moduleConfig =
89                  createModuleConfig(AnnotationOnSameLineCheck.class);
90  
91          final String[] expectedViolation = {
92              "7:5: " + getCheckMessage(AnnotationOnSameLineCheck.class,
93                      AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE,
94                      "Deprecated"),
95          };
96  
97          final List<String> expectedXpathQueries = Arrays.asList(
98                  "/COMPILATION_UNIT/CLASS_DEF"
99                          + "[./IDENT[@text='InputXpathAnnotationOnSameLineField']]"
100                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]",
101                 "/COMPILATION_UNIT/CLASS_DEF"
102                         + "[./IDENT[@text='InputXpathAnnotationOnSameLineField']]"
103                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]/MODIFIERS",
104                 "/COMPILATION_UNIT/CLASS_DEF"
105                         + "[./IDENT[@text='InputXpathAnnotationOnSameLineField']]"
106                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]/MODIFIERS"
107                         + "/ANNOTATION[./IDENT[@text='Deprecated']]",
108                 "/COMPILATION_UNIT/CLASS_DEF"
109                         + "[./IDENT[@text='InputXpathAnnotationOnSameLineField']]"
110                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]/MODIFIERS"
111                         + "/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 
118     @Test
119     public void testThree() throws Exception {
120         final File fileToProcess =
121                 new File(getPath(
122                         "InputXpathAnnotationOnSameLineInterface.java"));
123 
124         final DefaultConfiguration moduleConfig =
125                 createModuleConfig(AnnotationOnSameLineCheck.class);
126         moduleConfig.addProperty("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
127                 + "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
128                 + "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
129                 + "ANNOTATION_FIELD_DEF");
130 
131         final String[] expectedViolation = {
132             "3:1: " + getCheckMessage(AnnotationOnSameLineCheck.class,
133                     AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE,
134                     "Deprecated"),
135         };
136 
137         final List<String> expectedXpathQueries = Arrays.asList(
138                 "/COMPILATION_UNIT/INTERFACE_DEF["
139                         + "./IDENT[@text='InputXpathAnnotationOnSameLineInterface']]",
140                 "/COMPILATION_UNIT/INTERFACE_DEF["
141                         + "./IDENT[@text='InputXpathAnnotationOnSameLineInterface']]"
142                         + "/MODIFIERS",
143                 "/COMPILATION_UNIT/INTERFACE_DEF["
144                         + "./IDENT[@text='InputXpathAnnotationOnSameLineInterface']]"
145                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]",
146                 "/COMPILATION_UNIT/INTERFACE_DEF["
147                         + "./IDENT[@text='InputXpathAnnotationOnSameLineInterface']]"
148                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
149         );
150 
151         runVerifications(moduleConfig, fileToProcess, expectedViolation,
152                 expectedXpathQueries);
153     }
154 }