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.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
30  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
32  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
33  
34  public class SeverityMatchFilterTest {
35      private final SeverityMatchFilter filter = new SeverityMatchFilter();
36  
37      @Test
38      public void testDefault() {
39          final AuditEvent ev = new AuditEvent(this, "Test.java");
40          assertFalse("no message", filter.accept(ev));
41          final SeverityLevel errorLevel = SeverityLevel.ERROR;
42          final LocalizedMessage errorMessage =
43              new LocalizedMessage(0, 0, "", "", null,
44                  errorLevel, null, getClass(), null);
45          final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
46          assertTrue("level:" + errorLevel, filter.accept(ev2));
47          final SeverityLevel infoLevel = SeverityLevel.INFO;
48          final LocalizedMessage infoMessage =
49                  new LocalizedMessage(0, 0, "", "", null, infoLevel, null, getClass(), null);
50          final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoMessage);
51          assertFalse("level:" + infoLevel, filter.accept(ev3));
52      }
53  
54      @Test
55      public void testSeverity() {
56          filter.setSeverity(SeverityLevel.INFO);
57          final AuditEvent ev = new AuditEvent(this, "Test.java");
58          // event with no message has severity level INFO
59          assertTrue("no message", filter.accept(ev));
60          final SeverityLevel errorLevel = SeverityLevel.ERROR;
61          final LocalizedMessage errorMessage =
62              new LocalizedMessage(0, 0, "", "", null,
63                  errorLevel, null, getClass(), null);
64          final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
65          assertFalse("level:" + errorLevel, filter.accept(ev2));
66          final SeverityLevel infoLevel = SeverityLevel.INFO;
67          final LocalizedMessage infoMessage =
68                  new LocalizedMessage(0, 0, "", "", null, infoLevel, null, getClass(), null);
69          final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoMessage);
70          assertTrue("level:" + infoLevel, filter.accept(ev3));
71      }
72  
73      @Test
74      public void testAcceptOnMatch() {
75          filter.setSeverity(SeverityLevel.INFO);
76          filter.setAcceptOnMatch(false);
77          final AuditEvent ev = new AuditEvent(this, "Test.java");
78          // event with no message has severity level INFO
79          assertFalse("no message", filter.accept(ev));
80          final SeverityLevel errorLevel = SeverityLevel.ERROR;
81          final LocalizedMessage errorMessage =
82              new LocalizedMessage(0, 0, "", "", null,
83                  errorLevel, null, getClass(), null);
84          final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
85          assertTrue("level:" + errorLevel, filter.accept(ev2));
86          final SeverityLevel infoLevel = SeverityLevel.INFO;
87          final LocalizedMessage infoMessage = new LocalizedMessage(0, 0, "", "", null, infoLevel,
88              null, getClass(), null);
89          final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoMessage);
90          assertFalse("level:" + infoLevel, filter.accept(ev3));
91      }
92  
93      @Test
94      public void testConfigure() throws CheckstyleException {
95          filter.configure(new DefaultConfiguration("test"));
96          assertNotNull("object exists", filter);
97      }
98  }