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.api;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.fail;
24  
25  import org.junit.Test;
26  
27  public class SeverityLevelCounterTest {
28  
29      @Test
30      public void testCtorException() {
31          try {
32              final Object test = new SeverityLevelCounter(null);
33              fail("exception expected but got " + test);
34          }
35          catch (IllegalArgumentException ex) {
36              assertEquals("Invalid exception message",
37                      "'level' cannot be null", ex.getMessage());
38          }
39      }
40  
41      @Test
42      public void testAddException() {
43          final SeverityLevelCounter counter = new SeverityLevelCounter(SeverityLevel.ERROR);
44          final AuditEvent event = new AuditEvent(this, "ATest.java", null);
45          assertEquals("Invalid severity level count", 0, counter.getCount());
46          counter.addException(event, new IllegalStateException("Test IllegalStateException"));
47          assertEquals("Invalid severity level count", 1, counter.getCount());
48      }
49  
50      @Test
51      public void testAddExceptionWarning() {
52          final SeverityLevelCounter counter = new SeverityLevelCounter(SeverityLevel.WARNING);
53          final AuditEvent event = new AuditEvent(this, "ATest.java", null);
54          assertEquals("Invalid severity level count", 0, counter.getCount());
55          counter.addException(event, new IllegalStateException("Test IllegalStateException"));
56          assertEquals("Invalid severity level count", 0, counter.getCount());
57      }
58  
59      @Test
60      public void testAuditStartedClearsState() {
61          final SeverityLevelCounter counter = new SeverityLevelCounter(SeverityLevel.ERROR);
62          final AuditEvent event = new AuditEvent(this, "ATest.java", null);
63          final AuditEvent secondEvent = new AuditEvent(this, "BTest.java", null);
64  
65          counter.auditStarted(event);
66          assertEquals("Invalid severity level count", 0, counter.getCount());
67  
68          counter.addException(event, new IllegalStateException("Test IllegalStateException"));
69          assertEquals("Invalid severity level count", 1, counter.getCount());
70  
71          counter.auditStarted(secondEvent);
72          assertEquals("Invalid severity level count", 0, counter.getCount());
73      }
74  }