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