View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.filters;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.File;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
31  import net.sf.saxon.Configuration;
32  import net.sf.saxon.sxpath.XPathEvaluator;
33  import net.sf.saxon.sxpath.XPathExpression;
34  import nl.jqno.equalsverifier.EqualsVerifier;
35  import nl.jqno.equalsverifier.EqualsVerifierReport;
36  
37  public class XpathFilterElementTest extends AbstractModuleTestSupport {
38  
39      private File file;
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43          file = new File(getPath("InputXpathFilterElementSuppressByXpath.java"));
44      }
45  
46      @Override
47      public String getPackageLocation() {
48          return "com/puppycrawl/tools/checkstyle/filters/xpathfilterelement";
49      }
50  
51      /**
52       * This test should remain as a low level pure unit test, purely for coverage.
53       * It tests defensive null-guard in {@link XpathFilterElement#accept} that can never
54       * be reached through the pipeline, as it always produces non-null filenames and violations.
55       */
56      @Test
57      public void testNullFileName() {
58          final XpathFilterElement filter = new XpathFilterElement(
59                  "InputXpathFilterElementSuppressByXpath", "Test", null, null, null);
60          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null,
61                  null, null, null);
62          assertWithMessage("Event should be accepted")
63                  .that(filter.accept(ev))
64                  .isTrue();
65      }
66  
67      /**
68       * This test should remain as a low level pure unit test, purely for coverage.
69       * It tests defensive null-guard in {@link XpathFilterElement#accept} that can never
70       * be reached through the pipeline, as it always produces non-null filenames and violations.
71       */
72      @Test
73      public void testNullViolation() {
74          final XpathFilterElement filter = new XpathFilterElement(
75                  "InputXpathFilterElementSuppressByXpath", "Test", null, null, null);
76          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null,
77                  file.getName(), null, null);
78          assertWithMessage("Event should be accepted")
79                  .that(filter.accept(ev))
80                  .isTrue();
81      }
82  
83      /**
84       * This test should remain as a low level pure unit test.
85       * It uses {@link EqualsVerifier} library to validate {@code equals()}
86       * and {@code hashCode()} methods on all edge cases. This is a very
87       * technical method with implementation by strict rules, not related
88       * to checkstyle's target of validation.
89       */
90      @Test
91      public void testEqualsAndHashCode() throws Exception {
92          final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
93          final EqualsVerifierReport ev = EqualsVerifier.forClass(XpathFilterElement.class)
94              .withPrefabValues(XPathExpression.class,
95                  xpathEvaluator.createExpression("//METHOD_DEF"),
96                  xpathEvaluator.createExpression("//VARIABLE_DEF"))
97                  .usingGetClass()
98                  .withIgnoredFields("xpathExpression", "isEmptyConfig")
99                  .report();
100         assertWithMessage("Error: %s", ev.getMessage())
101                 .that(ev.isSuccessful())
102                 .isTrue();
103     }
104 
105 }