View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.turbo;
15  
16  import static org.junit.Assert.*;
17  
18  import org.junit.Test;
19  
20  import ch.qos.logback.core.spi.FilterReply;
21  
22  public class DuplicateMessageFilterTest {
23  
24      @Test
25      public void smoke() {
26          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
27          dmf.setAllowedRepetitions(0);
28          dmf.start();
29          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "x", null, null));
30          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "y", null, null));
31          assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "x", null, null));
32          assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "y", null, null));
33      }
34  
35      @Test
36      public void memoryLoss() {
37          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
38          dmf.setAllowedRepetitions(1);
39          dmf.setCacheSize(1);
40          dmf.start();
41          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a", null, null));
42          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "b", null, null));
43          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a", null, null));
44      }
45  
46      @Test
47      public void many() {
48          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
49          dmf.setAllowedRepetitions(0);
50          int cacheSize = 10;
51          int margin = 2;
52          dmf.setCacheSize(cacheSize);
53          dmf.start();
54          for (int i = 0; i < cacheSize + margin; i++) {
55              assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a" + i, null, null));
56          }
57          for (int i = cacheSize - 1; i >= margin; i--) {
58              assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "a" + i, null, null));
59          }
60          for (int i = margin - 1; i >= 0; i--) {
61              assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a" + i, null, null));
62          }
63      }
64  
65      @Test
66      // isXXXEnabled invokes decide with a null format
67      // http://jira.qos.ch/browse/LBCLASSIC-134
68      public void nullFormat() {
69          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
70          dmf.setAllowedRepetitions(0);
71          dmf.setCacheSize(10);
72          dmf.start();
73          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, null, null, null));
74          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, null, null, null));
75      }
76  
77  }