001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.turbo;
015
016import static org.junit.Assert.*;
017
018import org.junit.Test;
019
020import ch.qos.logback.core.spi.FilterReply;
021
022public class DuplicateMessageFilterTest {
023
024    @Test
025    public void smoke() {
026        DuplicateMessageFilter dmf = new DuplicateMessageFilter();
027        dmf.setAllowedRepetitions(0);
028        dmf.start();
029        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "x", null, null));
030        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "y", null, null));
031        assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "x", null, null));
032        assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "y", null, null));
033    }
034
035    @Test
036    public void memoryLoss() {
037        DuplicateMessageFilter dmf = new DuplicateMessageFilter();
038        dmf.setAllowedRepetitions(1);
039        dmf.setCacheSize(1);
040        dmf.start();
041        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a", null, null));
042        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "b", null, null));
043        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a", null, null));
044    }
045
046    @Test
047    public void many() {
048        DuplicateMessageFilter dmf = new DuplicateMessageFilter();
049        dmf.setAllowedRepetitions(0);
050        int cacheSize = 10;
051        int margin = 2;
052        dmf.setCacheSize(cacheSize);
053        dmf.start();
054        for (int i = 0; i < cacheSize + margin; i++) {
055            assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a" + i, null, null));
056        }
057        for (int i = cacheSize - 1; i >= margin; i--) {
058            assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "a" + i, null, null));
059        }
060        for (int i = margin - 1; i >= 0; i--) {
061            assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a" + i, null, null));
062        }
063    }
064
065    @Test
066    // isXXXEnabled invokes decide with a null format
067    // http://jira.qos.ch/browse/LBCLASSIC-134
068    public void nullFormat() {
069        DuplicateMessageFilter dmf = new DuplicateMessageFilter();
070        dmf.setAllowedRepetitions(0);
071        dmf.setCacheSize(10);
072        dmf.start();
073        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, null, null, null));
074        assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, null, null, null));
075    }
076
077}