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 com.puppycrawl.tools.checkstyle;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.File;
25  import java.nio.charset.StandardCharsets;
26  import java.util.Map;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.FileContents;
33  import com.puppycrawl.tools.checkstyle.api.FileText;
34  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import com.puppycrawl.tools.checkstyle.api.Violation;
37  import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck;
38  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
39  
40  public class XpathFileGeneratorAstFilterTest {
41  
42      @Test
43      public void testAcceptNoToken() {
44          final Violation violation = new Violation(0, 0, 0, null, null, null, null,
45                  null, XpathFileGeneratorAstFilterTest.class, null);
46          final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(null, null, violation, null);
47          final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
48          filter.finishLocalSetup();
49  
50          assertWithMessage("filter accepted")
51                  .that(filter.accept(event))
52                  .isTrue();
53  
54          final AuditEvent auditEvent = new AuditEvent(this, "Test.java", violation);
55  
56          assertWithMessage("filter has no queries")
57              .that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
58              .isNull();
59      }
60  
61      @Test
62      public void test() throws Exception {
63          final Violation violation = new Violation(3, 47, TokenTypes.LCURLY,
64                  "messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class,
65                  null);
66          final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
67                  "InputXpathFileGeneratorAstFilter.java", violation);
68          final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
69          filter.finishLocalSetup();
70  
71          assertWithMessage("filter accepted")
72                  .that(filter.accept(event))
73                  .isTrue();
74  
75          final AuditEvent auditEvent = new AuditEvent(this,
76                  getPath("InputXpathFileGeneratorAstFilter.java"), violation);
77  
78          assertWithMessage("expected xpath")
79              .that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
80              .isEqualTo("/COMPILATION_UNIT/CLASS_DEF[./IDENT"
81                      + "[@text='InputXpathFileGeneratorAstFilter']]/OBJBLOCK/LCURLY");
82      }
83  
84      @Test
85      public void testNoXpathQuery() throws Exception {
86          final Violation violation = new Violation(10, 10, TokenTypes.LCURLY,
87                  "messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class,
88                  null);
89          final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
90                  "InputXpathFileGeneratorAstFilter.java", violation);
91          final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
92          filter.finishLocalSetup();
93  
94          assertWithMessage("filter accepted")
95                  .that(filter.accept(event))
96                  .isTrue();
97  
98          final AuditEvent auditEvent = new AuditEvent(this,
99                  getPath("InputXpathFileGeneratorAstFilter.java"), violation);
100 
101         assertWithMessage("expected null")
102             .that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
103             .isNull();
104     }
105 
106     @Test
107     public void testTabWidth() throws Exception {
108         final Violation violation = new Violation(6, 7, TokenTypes.LITERAL_RETURN,
109                 "messages.properties", null, null, SeverityLevel.ERROR, null,
110                 XpathFileGeneratorAstFilterTest.class, null);
111         final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
112                 "InputXpathFileGeneratorAstFilter.java", violation);
113         final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
114         filter.finishLocalSetup();
115         filter.setTabWidth(6);
116 
117         assertWithMessage("filter accepted")
118                 .that(filter.accept(event))
119                 .isTrue();
120 
121         final AuditEvent auditEvent = new AuditEvent(this,
122                 getPath("InputXpathFileGeneratorAstFilter.java"), violation);
123 
124         assertWithMessage("expected xpath")
125             .that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
126             .isEqualTo("/COMPILATION_UNIT"
127                         + "/CLASS_DEF[./IDENT[@text='InputXpathFileGeneratorAstFilter']]"
128                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='tabMethod']]/SLIST/LITERAL_RETURN");
129     }
130 
131     /**
132      * We cannot reproduce situation when {@code finishLocalSetup} is called
133      * twice. So, we have to use reflection to be sure that even in such
134      * situation state of the field will be cleared.
135      *
136      * @throws Exception when code tested throws exception
137      */
138     @Test
139     @SuppressWarnings("unchecked")
140     public void testClearState() throws Exception {
141         final Violation violation = new Violation(3, 47, TokenTypes.LCURLY,
142                 "messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class,
143                 null);
144         final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
145                 "InputXpathFileGeneratorAstFilter.java", violation);
146 
147         final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
148         filter.finishLocalSetup();
149 
150         assertWithMessage("State is not cleared on finishLocalSetup")
151                 .that(TestUtil.isStatefulFieldClearedDuringLocalSetup(filter, event,
152                         "MESSAGE_QUERY_MAP",
153                         variableStack -> ((Map<Violation, String>) variableStack).isEmpty()))
154                 .isTrue();
155     }
156 
157     private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName,
158             Violation violation) throws Exception {
159         final File file = new File(getPath(fileName));
160         final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty(
161                 "file.encoding", StandardCharsets.UTF_8.name()));
162         final FileContents fileContents = new FileContents(fileText);
163         final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
164 
165         return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
166     }
167 
168     private static String getPath(String filename) {
169         return "src/test/resources/com/puppycrawl/tools/checkstyle/xpathfilegeneratorastfilter/"
170                 + filename;
171     }
172 
173 }