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.core;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNull;
18  import static org.junit.Assert.assertSame;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  import org.junit.Test;
23  
24  import ch.qos.logback.core.spi.LifeCycle;
25  
26  import java.util.ArrayList;
27  import java.util.concurrent.ExecutorService;
28  
29  public class ContextBaseTest {
30  
31      private InstrumentedLifeCycleManager lifeCycleManager = new InstrumentedLifeCycleManager();
32  
33      private InstrumentedContextBase context = new InstrumentedContextBase(lifeCycleManager);
34  
35      @Test
36      public void renameDefault() {
37          context.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
38          context.setName("hello");
39      }
40  
41      @Test
42      public void idempotentNameTest() {
43          context.setName("hello");
44          context.setName("hello");
45      }
46  
47      @Test
48      public void renameTest() {
49          context.setName("hello");
50          try {
51              context.setName("x");
52              fail("renaming is not allowed");
53          } catch (IllegalStateException ise) {
54          }
55      }
56  
57      @Test
58      public void resetTest() {
59          context.setName("hello");
60          context.putProperty("keyA", "valA");
61          context.putObject("keyA", "valA");
62          assertEquals("valA", context.getProperty("keyA"));
63          assertEquals("valA", context.getObject("keyA"));
64          MockLifeCycleComponent component = new MockLifeCycleComponent();
65          context.register(component);
66          assertSame(component, lifeCycleManager.getLastComponent());
67          context.reset();
68          assertNull(context.getProperty("keyA"));
69          assertNull(context.getObject("keyA"));
70          assertTrue(lifeCycleManager.isReset());
71      }
72  
73      @Test
74      public void contextNameProperty() {
75          assertNull(context.getProperty(CoreConstants.CONTEXT_NAME_KEY));
76          String HELLO = "hello";
77          context.setName(HELLO);
78          assertEquals(HELLO, context.getProperty(CoreConstants.CONTEXT_NAME_KEY));
79          // good to have a raw reference to the "CONTEXT_NAME" as most clients would
80          // not go through CoreConstants
81          assertEquals(HELLO, context.getProperty("CONTEXT_NAME"));
82      }
83  
84      private static class InstrumentedContextBase extends ContextBase {
85  
86          private final LifeCycleManager lifeCycleManager;
87  
88          public InstrumentedContextBase(LifeCycleManager lifeCycleManager) {
89              this.lifeCycleManager = lifeCycleManager;
90          }
91  
92          @Override
93          protected LifeCycleManager getLifeCycleManager() {
94              return lifeCycleManager;
95          }
96  
97      }
98  
99      private static class InstrumentedLifeCycleManager extends LifeCycleManager {
100 
101         private LifeCycle lastComponent;
102         private boolean reset;
103 
104         @Override
105         public void register(LifeCycle component) {
106             lastComponent = component;
107             super.register(component);
108         }
109 
110         @Override
111         public void reset() {
112             reset = true;
113             super.reset();
114         }
115 
116         public LifeCycle getLastComponent() {
117             return lastComponent;
118         }
119 
120         public boolean isReset() {
121             return reset;
122         }
123 
124     }
125 
126     @Test
127     public void contextThreadpoolIsDaemonized() throws InterruptedException {
128         ExecutorService execSvc = context.getExecutorService();
129         final ArrayList<Thread> executingThreads = new ArrayList<Thread>();
130         execSvc.execute(new Runnable() {
131             @Override
132             public void run() {
133                 synchronized (executingThreads) {
134                     executingThreads.add(Thread.currentThread());
135                     executingThreads.notifyAll();
136                 }
137             }
138         });
139         synchronized (executingThreads) {
140             while (executingThreads.isEmpty()) {
141                 executingThreads.wait();
142             }
143         }
144         assertTrue("executing thread should be a daemon thread.", executingThreads.get(0).isDaemon());
145     }
146 
147 }