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;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018import static org.junit.Assert.assertNotNull;
019import static org.junit.Assert.assertTrue;
020
021import org.junit.Before;
022import org.junit.Test;
023import org.slf4j.Marker;
024import org.slf4j.MarkerFactory;
025
026import ch.qos.logback.classic.turbo.MarkerFilter;
027import ch.qos.logback.classic.turbo.TurboFilter;
028import ch.qos.logback.core.spi.FilterReply;
029
030public class TurboFilteringInLoggerTest {
031
032    static final String BLUE = "BLUE";
033    LoggerContext context;
034    Logger logger;
035    Marker blueMarker = MarkerFactory.getMarker(BLUE);
036
037    @Before
038    public void setUp() throws Exception {
039        context = new LoggerContext();
040        context.setName("test");
041        context.start();
042        logger = context.getLogger(TurboFilteringInLoggerTest.class);
043    }
044
045    private void addYesFilter() {
046        YesFilter filter = new YesFilter();
047        filter.start();
048        context.addTurboFilter(filter);
049    }
050
051    private void addNoFilter() {
052        NoFilter filter = new NoFilter();
053        filter.start();
054        context.addTurboFilter(filter);
055    }
056
057    private void addAcceptBLUEFilter() {
058        MarkerFilter filter = new MarkerFilter();
059        filter.setMarker(BLUE);
060        filter.setOnMatch("ACCEPT");
061        filter.start();
062        context.addTurboFilter(filter);
063    }
064
065    private void addDenyBLUEFilter() {
066        MarkerFilter filter = new MarkerFilter();
067        filter.setMarker(BLUE);
068        filter.setOnMatch("DENY");
069        filter.start();
070        context.addTurboFilter(filter);
071    }
072
073    @Test
074    public void testIsDebugEnabledWithYesFilter() {
075        addYesFilter();
076        logger.setLevel(Level.INFO);
077        assertTrue(logger.isDebugEnabled());
078    }
079
080    @Test
081    public void testIsInfoEnabledWithYesFilter() {
082        addYesFilter();
083        logger.setLevel(Level.WARN);
084        assertTrue(logger.isInfoEnabled());
085    }
086
087    @Test
088    public void testIsWarnEnabledWithYesFilter() {
089        addYesFilter();
090        logger.setLevel(Level.ERROR);
091        assertTrue(logger.isWarnEnabled());
092    }
093
094    @Test
095    public void testIsErrorEnabledWithYesFilter() {
096        addYesFilter();
097        logger.setLevel(Level.OFF);
098        assertTrue(logger.isErrorEnabled());
099    }
100
101    @Test
102    public void testIsEnabledForWithYesFilter() {
103        addYesFilter();
104        logger.setLevel(Level.ERROR);
105        assertTrue(logger.isEnabledFor(Level.INFO));
106    }
107
108    @Test
109    public void testIsEnabledForWithNoFilter() {
110        addNoFilter();
111        logger.setLevel(Level.DEBUG);
112        assertFalse(logger.isEnabledFor(Level.INFO));
113    }
114
115    @Test
116    public void testIsDebugEnabledWithNoFilter() {
117        addNoFilter();
118        logger.setLevel(Level.DEBUG);
119        assertFalse(logger.isDebugEnabled());
120    }
121
122    @Test
123    public void testIsInfoEnabledWithNoFilter() {
124        addNoFilter();
125        logger.setLevel(Level.DEBUG);
126        assertFalse(logger.isInfoEnabled());
127    }
128
129    @Test
130    public void testIsWarnEnabledWithNoFilter() {
131        addNoFilter();
132        logger.setLevel(Level.DEBUG);
133        assertFalse(logger.isWarnEnabled());
134    }
135
136    @Test
137    public void testIsErrorEnabledWithNoFilter() {
138        addNoFilter();
139        logger.setLevel(Level.DEBUG);
140        assertFalse(logger.isErrorEnabled());
141    }
142
143    @Test
144    public void testIsErrorEnabledWithAcceptBlueFilter() {
145        addAcceptBLUEFilter();
146        logger.setLevel(Level.ERROR);
147        assertTrue(logger.isDebugEnabled(blueMarker));
148    }
149
150    @Test
151    public void testIsErrorEnabledWithDenyBlueFilter() {
152        addDenyBLUEFilter();
153        logger.setLevel(Level.ALL);
154        assertFalse(logger.isDebugEnabled(blueMarker));
155    }
156
157    @Test
158    public void testLoggingContextReset() {
159        addYesFilter();
160        assertNotNull(context.getTurboFilterList().get(0));
161        context.reset();
162        assertEquals(0, context.getTurboFilterList().size());
163    }
164
165}
166
167class YesFilter extends TurboFilter {
168    @Override
169    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
170        return FilterReply.ACCEPT;
171    }
172}
173
174class NoFilter extends TurboFilter {
175    @Override
176    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
177        return FilterReply.DENY;
178    }
179}