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