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.core;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNull;
018import static org.junit.Assert.assertSame;
019import static org.junit.Assert.assertTrue;
020import static org.junit.Assert.fail;
021
022import org.junit.Test;
023
024import ch.qos.logback.core.spi.LifeCycle;
025
026import java.util.ArrayList;
027import java.util.concurrent.ExecutorService;
028
029public class ContextBaseTest {
030
031    private InstrumentedLifeCycleManager lifeCycleManager = new InstrumentedLifeCycleManager();
032
033    private InstrumentedContextBase context = new InstrumentedContextBase(lifeCycleManager);
034
035    @Test
036    public void renameDefault() {
037        context.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
038        context.setName("hello");
039    }
040
041    @Test
042    public void idempotentNameTest() {
043        context.setName("hello");
044        context.setName("hello");
045    }
046
047    @Test
048    public void renameTest() {
049        context.setName("hello");
050        try {
051            context.setName("x");
052            fail("renaming is not allowed");
053        } catch (IllegalStateException ise) {
054        }
055    }
056
057    @Test
058    public void resetTest() {
059        context.setName("hello");
060        context.putProperty("keyA", "valA");
061        context.putObject("keyA", "valA");
062        assertEquals("valA", context.getProperty("keyA"));
063        assertEquals("valA", context.getObject("keyA"));
064        MockLifeCycleComponent component = new MockLifeCycleComponent();
065        context.register(component);
066        assertSame(component, lifeCycleManager.getLastComponent());
067        context.reset();
068        assertNull(context.getProperty("keyA"));
069        assertNull(context.getObject("keyA"));
070        assertTrue(lifeCycleManager.isReset());
071    }
072
073    @Test
074    public void contextNameProperty() {
075        assertNull(context.getProperty(CoreConstants.CONTEXT_NAME_KEY));
076        String HELLO = "hello";
077        context.setName(HELLO);
078        assertEquals(HELLO, context.getProperty(CoreConstants.CONTEXT_NAME_KEY));
079        // good to have a raw reference to the "CONTEXT_NAME" as most clients would
080        // not go through CoreConstants
081        assertEquals(HELLO, context.getProperty("CONTEXT_NAME"));
082    }
083
084    private static class InstrumentedContextBase extends ContextBase {
085
086        private final LifeCycleManager lifeCycleManager;
087
088        public InstrumentedContextBase(LifeCycleManager lifeCycleManager) {
089            this.lifeCycleManager = lifeCycleManager;
090        }
091
092        @Override
093        protected LifeCycleManager getLifeCycleManager() {
094            return lifeCycleManager;
095        }
096
097    }
098
099    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}