001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j;
026
027import static org.junit.Assert.*;
028
029import java.util.Iterator;
030
031import org.junit.Test;
032import org.slf4j.helpers.BasicMarkerFactory;
033
034/**
035 * Unit test BasicMarker
036 * 
037 * @author Ceki Gülcü
038 * @author Joern Huxhorn
039 */
040public class BasicMarkerTest {
041    static final String BLUE_STR = "BLUE";
042    static final String RED_STR = "RED";
043    static final String GREEN_STR = "GREEN";
044    static final String COMP_STR = "COMP";
045    static final String MULTI_COMP_STR = "MULTI_COMP";
046    static final String PARENT_MARKER_STR = "PARENT_MARKER";
047    static final String CHILD_MARKER_STR = "CHILD_MARKER";
048    static final String NOT_CONTAINED_MARKER_STR = "NOT_CONTAINED";
049
050    final IMarkerFactory factory;
051    final Marker blue;
052    final Marker red;
053    final Marker green;
054    final Marker comp;
055    final Marker multiComp;
056
057    short diff = Differentiator.getDiffentiator();
058
059    public BasicMarkerTest() {
060        factory = new BasicMarkerFactory();
061
062        blue = factory.getMarker(BLUE_STR);
063        red = factory.getMarker(RED_STR);
064        green = factory.getMarker(GREEN_STR);
065        comp = factory.getMarker(COMP_STR);
066        comp.add(blue);
067
068        multiComp = factory.getMarker(MULTI_COMP_STR);
069        multiComp.add(green);
070        multiComp.add(comp);
071    }
072
073    @Test
074    public void testPrimitive() {
075        assertEquals(BLUE_STR, blue.getName());
076        assertTrue(blue.contains(blue));
077
078        Marker blue2 = factory.getMarker(BLUE_STR);
079        assertEquals(BLUE_STR, blue2.getName());
080        assertEquals(blue, blue2);
081        assertTrue(blue.contains(blue2));
082        assertTrue(blue2.contains(blue));
083    }
084
085    @Test
086    public void testPrimitiveByName() {
087        assertTrue(blue.contains(BLUE_STR));
088    }
089
090    @Test
091    public void testComposite() {
092        assertTrue(comp.contains(comp));
093        assertTrue(comp.contains(blue));
094    }
095
096    @Test
097    public void testCompositeByName() {
098        assertTrue(comp.contains(COMP_STR));
099        assertTrue(comp.contains(BLUE_STR));
100    }
101
102    @Test
103    public void testMultiComposite() {
104        assertTrue(multiComp.contains(comp));
105        assertTrue(multiComp.contains(blue));
106        assertTrue(multiComp.contains(green));
107        assertFalse(multiComp.contains(red));
108    }
109
110    @Test
111    public void testMultiCompositeByName() {
112        assertTrue(multiComp.contains(COMP_STR));
113        assertTrue(multiComp.contains(BLUE_STR));
114        assertTrue(multiComp.contains(GREEN_STR));
115        assertFalse(multiComp.contains(RED_STR));
116    }
117
118    @Test
119    public void testMultiAdd() {
120        Marker parent = factory.getMarker(PARENT_MARKER_STR);
121        Marker child = factory.getMarker(CHILD_MARKER_STR);
122        for (int i = 0; i < 10; i++) {
123            parent.add(child);
124        }
125
126        // check that the child was added once and only once
127        Iterator<Marker> iterator = parent.iterator();
128        assertTrue(iterator.hasNext());
129        assertEquals(CHILD_MARKER_STR, iterator.next().toString());
130        assertFalse(iterator.hasNext());
131    }
132
133    @Test
134    public void testAddRemove() {
135        final String NEW_PREFIX = "NEW_";
136        Marker parent = factory.getMarker(NEW_PREFIX + PARENT_MARKER_STR);
137        Marker child = factory.getMarker(NEW_PREFIX + CHILD_MARKER_STR);
138        assertFalse(parent.contains(child));
139        assertFalse(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
140        assertFalse(parent.remove(child));
141
142        parent.add(child);
143
144        assertTrue(parent.contains(child));
145        assertTrue(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
146
147        assertTrue(parent.remove(child));
148
149        assertFalse(parent.contains(child));
150        assertFalse(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
151        assertFalse(parent.remove(child));
152    }
153
154    @Test
155    public void testSelfRecursion() {
156        final String diffPrefix = "NEW_" + diff;
157        final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
158        final String NOT_CONTAINED_NAME = diffPrefix + NOT_CONTAINED_MARKER_STR;
159        Marker parent = factory.getMarker(PARENT_NAME);
160        Marker notContained = factory.getMarker(NOT_CONTAINED_NAME);
161        parent.add(parent);
162        assertTrue(parent.contains(parent));
163        assertTrue(parent.contains(PARENT_NAME));
164        assertFalse(parent.contains(notContained));
165        assertFalse(parent.contains(NOT_CONTAINED_MARKER_STR));
166    }
167
168    @Test
169    public void testIndirectRecursion() {
170        final String diffPrefix = "NEW_" + diff;
171        final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
172        final String CHILD_NAME = diffPrefix + CHILD_MARKER_STR;
173        final String NOT_CONTAINED_NAME = diffPrefix + NOT_CONTAINED_MARKER_STR;
174
175        Marker parent = factory.getMarker(PARENT_NAME);
176        Marker child = factory.getMarker(CHILD_NAME);
177        Marker notContained = factory.getMarker(NOT_CONTAINED_NAME);
178
179        parent.add(child);
180        child.add(parent);
181        assertTrue(parent.contains(parent));
182        assertTrue(parent.contains(child));
183        assertTrue(parent.contains(PARENT_NAME));
184        assertTrue(parent.contains(CHILD_NAME));
185        assertFalse(parent.contains(notContained));
186        assertFalse(parent.contains(NOT_CONTAINED_MARKER_STR));
187    }
188
189    @Test
190    public void testHomonyms() {
191        final String diffPrefix = "homonym" + diff;
192        final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
193        final String CHILD_NAME = diffPrefix + CHILD_MARKER_STR;
194        Marker parent = factory.getMarker(PARENT_NAME);
195        Marker child = factory.getMarker(CHILD_NAME);
196        parent.add(child);
197
198        IMarkerFactory otherFactory = new BasicMarkerFactory();
199        Marker otherParent = otherFactory.getMarker(PARENT_NAME);
200        Marker otherChild = otherFactory.getMarker(CHILD_NAME);
201
202        assertTrue(parent.contains(otherParent));
203        assertTrue(parent.contains(otherChild));
204
205        assertTrue(parent.remove(otherChild));
206    }
207
208}