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  
26  import org.junit.Test;
27  
28  import nl.jqno.equalsverifier.EqualsVerifier;
29  
30  public class CsvFilterTest {
31      @Test
32      public void testDecideSingle() {
33          final IntFilter filter = new CsvFilter("0");
34          assertFalse("less than", filter.accept(-1));
35          assertTrue("equal", filter.accept(0));
36          assertFalse("greater than", filter.accept(1));
37      }
38  
39      @Test
40      public void testDecidePair() {
41          final IntFilter filter = new CsvFilter("0, 2");
42          assertFalse("less than", filter.accept(-1));
43          assertTrue("equal 0", filter.accept(0));
44          assertFalse("greater than", filter.accept(1));
45          assertTrue("equal 2", filter.accept(2));
46      }
47  
48      @Test
49      public void testDecideRange() {
50          final IntFilter filter = new CsvFilter("0-2");
51          assertFalse("less than", filter.accept(-1));
52          assertTrue("equal 0", filter.accept(0));
53          assertTrue("equal 1", filter.accept(1));
54          assertTrue("equal 2", filter.accept(2));
55          assertFalse("greater than", filter.accept(3));
56      }
57  
58      @Test
59      public void testDecideEmptyRange() {
60          final IntFilter filter = new CsvFilter("2-0");
61          assertFalse("less than", filter.accept(-1));
62          assertFalse("equal 0", filter.accept(0));
63          assertFalse("equal 1", filter.accept(1));
64          assertFalse("equal 2", filter.accept(2));
65          assertFalse("greater than", filter.accept(3));
66      }
67  
68      @Test
69      public void testDecideRangePlusValue() {
70          final IntFilter filter = new CsvFilter("0-2, 10");
71          assertFalse("less than", filter.accept(-1));
72          assertTrue("equal 0", filter.accept(0));
73          assertTrue("equal 1", filter.accept(1));
74          assertTrue("equal 2", filter.accept(2));
75          assertFalse("greater than", filter.accept(3));
76          assertTrue("equal 10", filter.accept(10));
77      }
78  
79      @Test
80      public void testEmptyChain() {
81          final CsvFilter filter = new CsvFilter("");
82          assertFalse("0", filter.accept(0));
83      }
84  
85      @Test
86      public void testOneFilter() {
87          final CsvFilter filter = new CsvFilter("");
88          filter.addFilter(new IntMatchFilter(0));
89          assertTrue("0", filter.accept(0));
90          assertFalse("1", filter.accept(1));
91      }
92  
93      @Test
94      public void testMultipleFilter() {
95          final CsvFilter filter = new CsvFilter("");
96          filter.addFilter(new IntMatchFilter(0));
97          filter.addFilter(new IntRangeFilter(0, 2));
98          assertTrue("0", filter.accept(0));
99          assertTrue("1", filter.accept(1));
100         filter.addFilter(new IntRangeFilter(3, 4));
101         assertTrue("0 is in [3,4]", filter.accept(0));
102     }
103 
104     @Test
105     public void testGetFilters() {
106         final CsvFilter filter = new CsvFilter("");
107         filter.addFilter(new IntMatchFilter(0));
108         assertEquals("size is the same", 1, filter.getFilters().size());
109     }
110 
111     @Test
112     public void testEqualsAndHashCode() {
113         EqualsVerifier.forClass(CsvFilter.class).usingGetClass().verify();
114     }
115 }