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.filters;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.io.File;
28  import java.util.Collections;
29  
30  import org.junit.Rule;
31  import org.junit.Test;
32  import org.junit.rules.TemporaryFolder;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.JavaParser;
36  import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
37  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
38  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
39  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
40  import nl.jqno.equalsverifier.EqualsVerifier;
41  import nl.jqno.equalsverifier.EqualsVerifierReport;
42  import nl.jqno.equalsverifier.Warning;
43  
44  public class SuppressionXpathFilterTest extends AbstractModuleTestSupport {
45  
46      @Rule
47      public final TemporaryFolder temporaryFolder = new TemporaryFolder();
48  
49      @Override
50      protected String getPackageLocation() {
51          return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathfilter";
52      }
53  
54      @Test
55      public void testAcceptOne() throws Exception {
56          final boolean optional = false;
57          final SuppressionXpathFilter filter =
58                  createSuppressionXpathFilter(getPath("InputSuppressionXpathFilterNone.xml"),
59                          optional);
60  
61          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "ATest.java", null, null);
62  
63          assertTrue("TreeWalker audit event should be accepted when there are no suppressions",
64                  filter.accept(ev));
65      }
66  
67      @Test
68      public void testAcceptTwo() throws Exception {
69          final boolean optional = false;
70          final SuppressionXpathFilter filter = createSuppressionXpathFilter(
71                  getPath("InputSuppressionXpathFilterIdAndQuery.xml"), optional);
72          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "file1.java", null, null);
73  
74          assertTrue("TreeWalker audit event should be accepted",
75                  filter.accept(ev));
76      }
77  
78      @Test
79      public void testAcceptOnNullFile() throws Exception {
80          final String fileName = null;
81          final boolean optional = false;
82          final SuppressionXpathFilter filter = createSuppressionXpathFilter(fileName, optional);
83  
84          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyJava.java", null, null);
85          assertTrue("TreeWalker audit event on null file should be accepted, but was not",
86                  filter.accept(ev));
87      }
88  
89      @Test
90      public void testNonExistentSuppressionFileWithFalseOptional() throws Exception {
91          final String fileName = getPath("non_existent_suppression_file.xml");
92          try {
93              final boolean optional = false;
94              createSuppressionXpathFilter(fileName, optional);
95              fail("Exception is expected");
96          }
97          catch (CheckstyleException ex) {
98              assertEquals("Invalid error message",
99                      "Unable to find: " + fileName, ex.getMessage());
100         }
101     }
102 
103     @Test
104     public void testExistingInvalidSuppressionFileWithTrueOptional() throws Exception {
105         final String fileName = getPath("InputSuppressionXpathFilterInvalidFile.xml");
106         try {
107             final boolean optional = true;
108             createSuppressionXpathFilter(fileName, optional);
109             fail("Exception is expected");
110         }
111         catch (CheckstyleException ex) {
112             assertEquals("Invalid error message", "Unable to parse " + fileName
113                     + " - invalid files or checks or message format for suppress-xpath",
114                     ex.getMessage());
115         }
116     }
117 
118     @Test
119     public void testExistingSuppressionFileWithTrueOptional() throws Exception {
120         final boolean optional = true;
121         final SuppressionXpathFilter filter =
122                 createSuppressionXpathFilter(getPath("InputSuppressionXpathFilterNone.xml"),
123                         optional);
124 
125         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyJava.java", null, null);
126 
127         assertTrue("Suppression file with true optional was not accepted",
128                 filter.accept(ev));
129     }
130 
131     @Test
132     public void testNonExistentSuppressionFileWithTrueOptional() throws Exception {
133         final String fileName = "src/test/resources/com/puppycrawl/tools/checkstyle/filters/"
134                 + "non_existent_suppression_file.xml";
135         final boolean optional = true;
136         final SuppressionXpathFilter filter = createSuppressionXpathFilter(fileName, optional);
137 
138         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyFile.java", null, null);
139 
140         assertTrue("Should except event when suppression file does not exist",
141                 filter.accept(ev));
142     }
143 
144     @Test
145     public void testReject() throws Exception {
146         final boolean optional = false;
147         final SuppressionXpathFilter filter = createSuppressionXpathFilter(
148                         getPath("InputSuppressionXpathFilterIdAndQuery.xml"), optional);
149         final File file = new File(getPath("InputSuppressionXpathFilter.java"));
150         final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "",
151                 null, null, "777", getClass(), null);
152         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "file1.java",
153                 message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
154 
155         assertFalse("TreeWalker audit event should be rejected",
156                 filter.accept(ev));
157     }
158 
159     @Test
160     public void testEqualsAndHashCode() {
161         final EqualsVerifierReport ev = EqualsVerifier.forClass(SuppressionXpathFilter.class)
162                 .usingGetClass()
163                 .withIgnoredFields("file", "optional", "configuration")
164                 .suppress(Warning.NONFINAL_FIELDS).report();
165         assertEquals("Error: " + ev.getMessage(), EqualsVerifierReport.SUCCESS, ev);
166     }
167 
168     @Test
169     public void testExternalResource() throws Exception {
170         final boolean optional = false;
171         final String fileName = getPath("InputSuppressionXpathFilterIdAndQuery.xml");
172         final SuppressionXpathFilter filter = createSuppressionXpathFilter(fileName, optional);
173         assertEquals("Invalid external resource",
174                 Collections.singleton(fileName),
175                 filter.getExternalResourceLocations());
176     }
177 
178     private static SuppressionXpathFilter createSuppressionXpathFilter(String fileName,
179                                                                        boolean optional)
180             throws CheckstyleException {
181         final SuppressionXpathFilter suppressionXpathFilter = new SuppressionXpathFilter();
182         suppressionXpathFilter.setFile(fileName);
183         suppressionXpathFilter.setOptional(optional);
184         suppressionXpathFilter.finishLocalSetup();
185         return suppressionXpathFilter;
186     }
187 
188 }