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  
17  import ch.qos.logback.core.spi.FilterReply;
18  import ch.qos.logback.core.testUtil.RandomUtil;
19  import org.junit.jupiter.api.AfterEach;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.slf4j.MDC;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  import static org.junit.jupiter.api.Assertions.assertFalse;
26  
27  public class MDCFilterTest {
28  
29      int diff = RandomUtil.getPositiveInt();
30      String key = "myKey" + diff;
31      String value = "val" + diff;
32  
33      private MDCFilter filter;
34  
35      @BeforeEach
36      public void setUp() {
37          filter = new MDCFilter();
38          filter.setOnMatch("ACCEPT");
39          filter.setOnMismatch("DENY");
40          filter.setMDCKey(key);
41          filter.setValue(value);
42  
43          MDC.clear();
44      }
45  
46      @AfterEach
47      public void tearDown() {
48          MDC.clear();
49      }
50  
51      @Test
52      public void smoke() {
53          filter.start();
54          MDC.put(key, "other" + diff);
55          assertEquals(FilterReply.DENY, filter.decide(null, null, null, null, null, null));
56          MDC.put(key, null);
57          assertEquals(FilterReply.DENY, filter.decide(null, null, null, null, null, null));
58          MDC.put(key, value);
59          assertEquals(FilterReply.ACCEPT, filter.decide(null, null, null, null, null, null));
60      }
61  
62      @Test
63      public void testNoValueOption() {
64  
65          filter.setValue(null);
66          filter.start();
67          assertFalse(filter.isStarted());
68          MDC.put(key, null);
69          assertEquals(FilterReply.NEUTRAL, filter.decide(null, null, null, null, null, null));
70          MDC.put(key, value);
71          assertEquals(FilterReply.NEUTRAL, filter.decide(null, null, null, null, null, null));
72      }
73  
74      @Test
75      public void testNoMDCKeyOption() {
76          filter.setMDCKey(null);
77          filter.start();
78          assertFalse(filter.isStarted());
79          MDC.put(key, null);
80          assertEquals(FilterReply.NEUTRAL, filter.decide(null, null, null, null, null, null));
81          MDC.put(key, value);
82          assertEquals(FilterReply.NEUTRAL, filter.decide(null, null, null, null, null, null));
83      }
84  
85  }