View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.TreeWalkerAuditEvent;
36  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
38  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
39  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
40  import nl.jqno.equalsverifier.EqualsVerifier;
41  import nl.jqno.equalsverifier.Warning;
42  
43  public class SuppressionXpathFilterTest extends AbstractModuleTestSupport {
44  
45      @Rule
46      public final TemporaryFolder temporaryFolder = new TemporaryFolder();
47  
48      @Override
49      protected String getPackageLocation() {
50          return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathfilter";
51      }
52  
53      @Test
54      public void testAcceptOne() throws Exception {
55          final boolean optional = false;
56          final SuppressionXpathFilter filter =
57                  createSuppressionXpathFilter(getPath("InputSuppressionXpathFilterNone.xml"),
58                          optional);
59  
60          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "ATest.java", null, null);
61  
62          assertTrue("TreeWalker audit event should be accepted when there are no suppressions",
63                  filter.accept(ev));
64      }
65  
66      @Test
67      public void testAcceptTwo() throws Exception {
68          final boolean optional = false;
69          final SuppressionXpathFilter filter = createSuppressionXpathFilter(
70                  getPath("InputSuppressionXpathFilterIdAndQuery.xml"), optional);
71          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "file1.java", null, null);
72  
73          assertTrue("TreeWalker audit event should be accepted",
74                  filter.accept(ev));
75      }
76  
77      @Test
78      public void testAcceptOnNullFile() throws Exception {
79          final String fileName = null;
80          final boolean optional = false;
81          final SuppressionXpathFilter filter = createSuppressionXpathFilter(fileName, optional);
82  
83          final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyJava.java", null, null);
84          assertTrue("TreeWalker audit event on null file should be accepted, but was not",
85                  filter.accept(ev));
86      }
87  
88      @Test
89      public void testNonExistentSuppressionFileWithFalseOptional() throws Exception {
90          final String fileName = getPath("non_existent_suppression_file.xml");
91          try {
92              final boolean optional = false;
93              createSuppressionXpathFilter(fileName, optional);
94              fail("Exception is expected");
95          }
96          catch (CheckstyleException ex) {
97              assertEquals("Invalid error message",
98                      "Unable to find: " + fileName, ex.getMessage());
99          }
100     }
101 
102     @Test
103     public void testExistingInvalidSuppressionFileWithTrueOptional() throws Exception {
104         final String fileName = getPath("InputSuppressionXpathFilterInvalidFile.xml");
105         try {
106             final boolean optional = true;
107             createSuppressionXpathFilter(fileName, optional);
108             fail("Exception is expected");
109         }
110         catch (CheckstyleException ex) {
111             assertEquals("Invalid error message", "Unable to parse " + fileName
112                     + " - invalid files or checks or message format for suppress-xpath",
113                     ex.getMessage());
114         }
115     }
116 
117     @Test
118     public void testExistingSuppressionFileWithTrueOptional() throws Exception {
119         final boolean optional = true;
120         final SuppressionXpathFilter filter =
121                 createSuppressionXpathFilter(getPath("InputSuppressionXpathFilterNone.xml"),
122                         optional);
123 
124         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyJava.java", null, null);
125 
126         assertTrue("Suppression file with true optional was not accepted",
127                 filter.accept(ev));
128     }
129 
130     @Test
131     public void testNonExistentSuppressionFileWithTrueOptional() throws Exception {
132         final String fileName = "src/test/resources/com/puppycrawl/tools/checkstyle/filters/"
133                 + "non_existent_suppression_file.xml";
134         final boolean optional = true;
135         final SuppressionXpathFilter filter = createSuppressionXpathFilter(fileName, optional);
136 
137         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyFile.java", null, null);
138 
139         assertTrue("Should except event when suppression file does not exist",
140                 filter.accept(ev));
141     }
142 
143     @Test
144     public void testReject() throws Exception {
145         final boolean optional = false;
146         final SuppressionXpathFilter filter = createSuppressionXpathFilter(
147                         getPath("InputSuppressionXpathFilterIdAndQuery.xml"), optional);
148         final File file = new File(getPath("InputSuppressionXpathFilter.java"));
149         final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "",
150                 null, null, "777", getClass(), null);
151         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "file1.java",
152                 message, TestUtil.parseFile(file));
153 
154         assertFalse("TreeWalker audit event should be rejected",
155                 filter.accept(ev));
156     }
157 
158     @Test
159     public void testEqualsAndHashCode() {
160         EqualsVerifier
161                 .forClass(SuppressionXpathFilter.class)
162                 .usingGetClass()
163                 .withIgnoredFields("file", "optional", "configuration")
164                 .suppress(Warning.NONFINAL_FIELDS)
165                 .verify();
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 }