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.assertEquals;
017import static org.junit.Assert.assertFalse;
018import static org.junit.Assert.assertTrue;
019
020import org.junit.Test;
021import org.slf4j.Marker;
022import org.slf4j.MarkerFactory;
023
024import ch.qos.logback.core.spi.FilterReply;
025
026public class MarkerFilterTest {
027
028    static String TOTO = "TOTO";
029    static String COMPOSITE = "COMPOSITE";
030
031    Marker totoMarker = MarkerFactory.getMarker(TOTO);
032
033    @Test
034    public void testNoMarker() {
035        MarkerFilter mkt = new MarkerFilter();
036        mkt.start();
037        assertFalse(mkt.isStarted());
038        assertEquals(FilterReply.NEUTRAL, mkt.decide(totoMarker, null, null, null, null, null));
039        assertEquals(FilterReply.NEUTRAL, mkt.decide(null, null, null, null, null, null));
040
041    }
042
043    @Test
044    public void testBasic() {
045        MarkerFilter mkt = new MarkerFilter();
046        mkt.setMarker(TOTO);
047        mkt.setOnMatch("ACCEPT");
048        mkt.setOnMismatch("DENY");
049
050        mkt.start();
051        assertTrue(mkt.isStarted());
052        assertEquals(FilterReply.DENY, mkt.decide(null, null, null, null, null, null));
053        assertEquals(FilterReply.ACCEPT, mkt.decide(totoMarker, null, null, null, null, null));
054    }
055
056    @Test
057    public void testComposite() {
058        String compositeMarkerName = COMPOSITE;
059        Marker compositeMarker = MarkerFactory.getMarker(compositeMarkerName);
060        compositeMarker.add(totoMarker);
061
062        MarkerFilter mkt = new MarkerFilter();
063        mkt.setMarker(TOTO);
064        mkt.setOnMatch("ACCEPT");
065        mkt.setOnMismatch("DENY");
066
067        mkt.start();
068
069        assertTrue(mkt.isStarted());
070        assertEquals(FilterReply.DENY, mkt.decide(null, null, null, null, null, null));
071        assertEquals(FilterReply.ACCEPT, mkt.decide(totoMarker, null, null, null, null, null));
072        assertEquals(FilterReply.ACCEPT, mkt.decide(compositeMarker, null, null, null, null, null));
073    }
074
075}