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.status;
015
016import java.util.Iterator;
017
018import junit.framework.TestCase;
019
020public class StatusBaseTest extends TestCase {
021
022    public void testAddStatus() {
023        {
024            InfoStatus status = new InfoStatus("testing", this);
025            status.add(new ErrorStatus("error", this));
026            Iterator<Status> it = status.iterator();
027            assertTrue("No status was added", it.hasNext());
028            assertTrue("hasChilden method reported wrong result", status.hasChildren());
029        }
030        {
031            InfoStatus status = new InfoStatus("testing", this);
032            try {
033                status.add(null);
034                fail("method should have thrown an Exception");
035            } catch (NullPointerException ex) {
036            }
037        }
038    }
039
040    public void testRemoveStatus() {
041        {
042            InfoStatus status = new InfoStatus("testing", this);
043            ErrorStatus error = new ErrorStatus("error", this);
044            status.add(error);
045            boolean result = status.remove(error);
046            Iterator<Status> it = status.iterator();
047            assertTrue("Remove failed", result);
048            assertFalse("No status was removed", it.hasNext());
049            assertFalse("hasChilden method reported wrong result", status.hasChildren());
050        }
051        {
052            InfoStatus status = new InfoStatus("testing", this);
053            ErrorStatus error = new ErrorStatus("error", this);
054            status.add(error);
055            boolean result = status.remove(null);
056            assertFalse("Remove result was not false", result);
057        }
058    }
059
060    public void testEffectiveLevel() {
061        {
062            // effective level = 0 level deep
063            ErrorStatus status = new ErrorStatus("error", this);
064            WarnStatus warn = new WarnStatus("warning", this);
065            status.add(warn);
066            assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.ERROR);
067        }
068
069        {
070            // effective level = 1 level deep
071            InfoStatus status = new InfoStatus("info", this);
072            WarnStatus warn = new WarnStatus("warning", this);
073            status.add(warn);
074            assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.WARN);
075        }
076
077        {
078            // effective level = 2 levels deep
079            InfoStatus status = new InfoStatus("info", this);
080            WarnStatus warn = new WarnStatus("warning", this);
081            ErrorStatus error = new ErrorStatus("error", this);
082            status.add(warn);
083            warn.add(error);
084            assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.ERROR);
085        }
086    }
087
088}