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 ch.qos.logback.core.BasicStatusManager.MAX_HEADER_COUNT;
017import static ch.qos.logback.core.BasicStatusManager.TAIL_SIZE;
018import static org.junit.Assert.*;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import ch.qos.logback.core.status.OnConsoleStatusListener;
024import ch.qos.logback.core.status.StatusListener;
025
026import org.junit.Test;
027
028import ch.qos.logback.core.status.ErrorStatus;
029import ch.qos.logback.core.status.Status;
030
031public class BasicStatusManagerTest {
032
033    BasicStatusManager bsm = new BasicStatusManager();
034
035    @Test
036    public void smoke() {
037        bsm.add(new ErrorStatus("hello", this));
038        assertEquals(Status.ERROR, bsm.getLevel());
039
040        List<Status> statusList = bsm.getCopyOfStatusList();
041        assertNotNull(statusList);
042        assertEquals(1, statusList.size());
043        assertEquals("hello", statusList.get(0).getMessage());
044    }
045
046    @Test
047    public void many() {
048        int margin = 300;
049        int len = MAX_HEADER_COUNT + TAIL_SIZE + margin;
050        for (int i = 0; i < len; i++) {
051            bsm.add(new ErrorStatus("" + i, this));
052        }
053
054        List<Status> statusList = bsm.getCopyOfStatusList();
055        assertNotNull(statusList);
056        assertEquals(MAX_HEADER_COUNT + TAIL_SIZE, statusList.size());
057        List<Status> witness = new ArrayList<Status>();
058        for (int i = 0; i < MAX_HEADER_COUNT; i++) {
059            witness.add(new ErrorStatus("" + i, this));
060        }
061        for (int i = 0; i < TAIL_SIZE; i++) {
062            witness.add(new ErrorStatus("" + (MAX_HEADER_COUNT + margin + i), this));
063        }
064        assertEquals(witness, statusList);
065    }
066
067    @Test
068    public void duplicateInstallationsOfOnConsoleListener() {
069        OnConsoleStatusListener sl0 = new OnConsoleStatusListener();
070        sl0.start();
071        OnConsoleStatusListener sl1 = new OnConsoleStatusListener();
072        sl1.start();
073
074        assertTrue(bsm.add(sl0));
075
076        {
077            List<StatusListener> listeners = bsm.getCopyOfStatusListenerList();
078            assertEquals(1, listeners.size());
079        }
080
081        assertFalse(bsm.add(sl1));
082        {
083            List<StatusListener> listeners = bsm.getCopyOfStatusListenerList();
084            assertEquals(1, listeners.size());
085        }
086    }
087
088}