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.assertNotEquals;
25  import static org.junit.Assert.assertTrue;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.TreeWalkerTest;
31  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
32  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
33  import nl.jqno.equalsverifier.EqualsVerifier;
34  import nl.jqno.equalsverifier.Warning;
35  
36  public class SuppressElementTest {
37      private SuppressElement filter;
38  
39      @Before
40      public void setUp() {
41          filter = new SuppressElement("Test", "Test", null, null, null, null);
42      }
43  
44      @Test
45      public void testDecideDefault() {
46          final AuditEvent ev = new AuditEvent(this, "Test.java");
47          assertTrue(ev.getFileName(), filter.accept(ev));
48      }
49  
50      @Test
51      public void testDecideLocalizedMessage() {
52          final LocalizedMessage message =
53              new LocalizedMessage(0, 0, "", "", null, null, getClass(), null);
54          final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
55          //deny because there are matches on file and check names
56          assertFalse("Names match", filter.accept(ev));
57      }
58  
59      @Test
60      public void testDecideByMessage() {
61          final LocalizedMessage message =
62              new LocalizedMessage(0, 0, "", "", null, null, getClass(), "Test");
63          final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
64          final SuppressElement filter1 =
65                  new SuppressElement(null, null, "Test", null, null, null);
66          final SuppressElement filter2 =
67                  new SuppressElement(null, null, "Bad", null, null, null);
68          assertFalse("Message match", filter1.accept(ev));
69          assertTrue("Message not match", filter2.accept(ev));
70      }
71  
72      @Test
73      public void testDecideByLine() {
74          final LocalizedMessage message =
75              new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
76          final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
77          final SuppressElement filter1 =
78                  new SuppressElement("Test", "Test", null, null, "1-10", null);
79          final SuppressElement filter2 =
80                  new SuppressElement("Test", "Test", null, null, "1-9, 11", null);
81          final SuppressElement filter3 =
82                  new SuppressElement("Test", "Test", null, null, null, null);
83          //deny because there are matches on file name, check name, and line
84          assertFalse("In range 1-10", filter1.accept(ev));
85          assertTrue("Not in 1-9, 11", filter2.accept(ev));
86          assertFalse("none", filter3.accept(ev));
87      }
88  
89      @Test
90      public void testDecideByColumn() {
91          final LocalizedMessage message =
92              new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
93          final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
94          final SuppressElement filter1 =
95                  new SuppressElement("Test", "Test", null, null, null, "1-10");
96          final SuppressElement filter2 =
97                  new SuppressElement("Test", "Test", null, null, null, "1-9, 11");
98  
99          //deny because there are matches on file name, check name, and column
100         assertFalse("In range 1-10", filter1.accept(ev));
101         assertTrue("Not in 1-9, 1)", filter2.accept(ev));
102     }
103 
104     @Test
105     public void testDecideByFileNameAndModuleMatchingFileNameNull() {
106         final LocalizedMessage message =
107                 new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
108         final AuditEvent ev = new AuditEvent(this, null, message);
109         assertTrue("Filter should accept valid event", filter.accept(ev));
110     }
111 
112     @Test
113     public void testDecideByFileNameAndModuleMatchingMessageNull() {
114         final AuditEvent ev = new AuditEvent(this, "ATest.java", null);
115         assertTrue("Filter should accept valid event", filter.accept(ev));
116     }
117 
118     @Test
119     public void testDecideByFileNameAndModuleMatchingModuleNull() {
120         final LocalizedMessage message =
121                 new LocalizedMessage(10, 10, "", "", null, "MyModule", getClass(), null);
122         final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
123         assertFalse("Filter should not accept invalid event", filter.accept(ev));
124     }
125 
126     @Test
127     public void testDecideByFileNameAndModuleMatchingModuleEqual() {
128         final LocalizedMessage message =
129                 new LocalizedMessage(10, 10, "", "", null, "MyModule", getClass(), null);
130         final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
131         final SuppressElement myFilter =
132                 new SuppressElement("Test", "Test", null, "MyModule", null, null);
133 
134         assertFalse("Filter should not accept invalid event", myFilter.accept(ev));
135     }
136 
137     @Test
138     public void testDecideByFileNameAndModuleMatchingModuleNotEqual() {
139         final LocalizedMessage message =
140                 new LocalizedMessage(10, 10, "", "", null, "TheirModule", getClass(), null);
141         final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
142         final SuppressElement myFilter =
143                 new SuppressElement("Test", "Test", null, "MyModule", null, null);
144 
145         assertTrue("Filter should accept valid event", myFilter.accept(ev));
146     }
147 
148     @Test
149     public void testDecideByFileNameAndModuleMatchingRegExpNotMatch() {
150         final LocalizedMessage message =
151                 new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
152         final AuditEvent ev = new AuditEvent(this, "T1est", message);
153         assertTrue("Filter should accept valid event", filter.accept(ev));
154     }
155 
156     @Test
157     public void testDecideByFileNameAndModuleMatchingRegExpMatch() {
158         final LocalizedMessage message =
159                 new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
160         final AuditEvent ev = new AuditEvent(this, "TestSUFFIX", message);
161         final SuppressElement myFilter =
162                 new SuppressElement("Test", null, null, null, null, null);
163         assertFalse("Filter should not accept invalid event", myFilter.accept(ev));
164     }
165 
166     @Test
167     public void testDecideByFileNameAndModuleMatchingCheckRegExpNotMatch() {
168         final LocalizedMessage message =
169                 new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
170         final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
171         final SuppressElement myFilter =
172                 new SuppressElement("Test", "NON_EXISTENT_CHECK", null, "MyModule", null, null);
173         assertTrue("Filter should accept valid event", myFilter.accept(ev));
174     }
175 
176     @Test
177     public void testDecideByFileNameAndModuleMatchingCheckRegExpMatch() {
178         final LocalizedMessage message =
179                 new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
180         final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
181         final SuppressElement myFilter =
182                 new SuppressElement("Test", getClass().getCanonicalName(), null, null, null, null);
183 
184         assertFalse("Filter should not accept invalid event", myFilter.accept(ev));
185     }
186 
187     @Test
188     public void testDecideByFileNameAndSourceNameCheckRegExpNotMatch() {
189         final LocalizedMessage message =
190                 new LocalizedMessage(10, 10, "", "", null, null, getClass(), null);
191         final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
192         final SuppressElement myFilter =
193                 new SuppressElement("Test", TreeWalkerTest.class.getCanonicalName(),
194                         null, null, null, null);
195 
196         assertTrue("Filter should not accept invalid event", myFilter.accept(ev));
197     }
198 
199     @Test
200     public void testEquals() {
201         // filterBased is used instead of filter field only to satisfy IntelliJ Idea Inspection
202         // Inspection "Arguments to assertEquals() in wrong order "
203         final SuppressElement filterBased =
204                 new SuppressElement("Test", "Test", null, null, null, null);
205 
206         final SuppressElement filter2 =
207                 new SuppressElement("Test", "Test", null, null, null, null);
208         assertEquals("filter, filter2", filterBased, filter2);
209         final SuppressElement filter3 =
210                 new SuppressElement("Test", "Test3", null, null, null, null);
211         assertNotEquals("filter, filter3", filterBased, filter3);
212         final SuppressElement filterBased1 =
213                 new SuppressElement("Test", "Test", null, null, null, "1-10");
214 
215         assertNotEquals("filter, filter2", filterBased1, filter2);
216         final SuppressElement filter22 =
217                 new SuppressElement("Test", "Test", null, null, null, "1-10");
218         assertEquals("filter, filter2", filterBased1, filter22);
219         assertNotEquals("filter, filter2", filterBased1, filter2);
220         final SuppressElement filterBased2 =
221                 new SuppressElement("Test", "Test", null, null, "3,4", null);
222         assertNotEquals("filter, filter2", filterBased2, filter2);
223         final SuppressElement filter23 =
224                 new SuppressElement("Test", "Test", null, null, "3,4", null);
225         assertEquals("filter, filter2", filterBased2, filter23);
226         assertNotEquals("filter, filter2", filterBased2, filter2);
227         assertEquals("filter, filter2", filterBased2, filter23);
228     }
229 
230     @Test
231     public void testEqualsAndHashCode() {
232         EqualsVerifier.forClass(SuppressElement.class)
233                 .usingGetClass()
234                 .withIgnoredFields("fileRegexp", "checkRegexp", "messageRegexp", "columnFilter",
235                         "lineFilter")
236                 .suppress(Warning.NONFINAL_FIELDS)
237                 .verify();
238     }
239 }