View Javadoc
1   package uk.org.lidalia.slf4jtest;
2   
3   import java.util.Collections;
4   import java.util.List;
5   
6   import org.junit.Before;
7   import org.junit.Test;
8   import org.junit.runner.Description;
9   import org.junit.runners.model.Statement;
10  
11  import uk.org.lidalia.lang.Task;
12  import uk.org.lidalia.slf4jext.Level;
13  
14  import static java.util.Arrays.asList;
15  import static org.hamcrest.MatcherAssert.assertThat;
16  import static org.hamcrest.core.Is.is;
17  import static uk.org.lidalia.lang.Exceptions.throwUnchecked;
18  import static uk.org.lidalia.slf4jext.Level.DEBUG;
19  import static uk.org.lidalia.slf4jext.Level.INFO;
20  import static uk.org.lidalia.slf4jtest.LoggingEvent.info;
21  import static uk.org.lidalia.test.ShouldThrow.shouldThrow;
22  
23  public class TestLoggerFactoryResetRuleUnitTests {
24  
25      TestLoggerFactoryResetRule resetRule = new TestLoggerFactoryResetRule();
26  
27      @Test
28      public void resetsThreadLocalData() throws Throwable {
29  
30          final TestLogger logger = TestLoggerFactory.getTestLogger("logger_name");
31          logger.setEnabledLevels(INFO, DEBUG);
32          logger.info("a message");
33  
34          resetRule.apply(new Statement() {
35              @Override
36              public void evaluate() throws Throwable {
37              }
38          }, Description.EMPTY).evaluate();
39  
40  
41          assertThat(TestLoggerFactory.getLoggingEvents(), is(Collections.<LoggingEvent>emptyList()));
42          assertThat(logger.getLoggingEvents(), is(Collections.<LoggingEvent>emptyList()));
43          assertThat(logger.getEnabledLevels(), is(Level.enablableValueSet()));
44      }
45  
46      @Test
47      public void resetsThreadLocalDataOnException() throws Throwable {
48  
49          final TestLogger logger = TestLoggerFactory.getTestLogger("logger_name");
50          logger.setEnabledLevels(INFO, DEBUG);
51          logger.info("a message");
52  
53          final Exception toThrow = new Exception();
54          Exception thrown = shouldThrow(Exception.class, new Task() {
55              @Override
56              public void perform() throws Exception {
57                  try {
58                      resetRule.apply(new Statement() {
59                          @Override
60                          public void evaluate() throws Throwable {
61                              throw toThrow;
62                          }
63                      }, Description.EMPTY).evaluate();
64                  } catch (Throwable throwable) {
65                      throwUnchecked(throwable);
66                  }
67              }
68          });
69  
70          assertThat(thrown, is(toThrow));
71          assertThat(TestLoggerFactory.getLoggingEvents(), is(Collections.<LoggingEvent>emptyList()));
72          assertThat(logger.getLoggingEvents(), is(Collections.<LoggingEvent>emptyList()));
73          assertThat(logger.getEnabledLevels(), is(Level.enablableValueSet()));
74      }
75  
76      @Test
77      public void doesNotResetNonThreadLocalData() throws Throwable {
78  
79          final TestLogger logger = TestLoggerFactory.getTestLogger("logger_name");
80          logger.info("a message");
81  
82          resetRule.apply(new Statement() {
83              @Override
84              public void evaluate() throws Throwable {
85              }
86          }, Description.EMPTY).evaluate();
87  
88          final List<LoggingEvent> loggedEvents = asList(info("a message"));
89  
90          assertThat(TestLoggerFactory.getAllLoggingEvents(), is(loggedEvents));
91          assertThat(logger.getAllLoggingEvents(), is(loggedEvents));
92      }
93  
94      @Before
95      public void resetTestLoggerFactory() {
96          TestLoggerFactory.reset();
97      }
98  }