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