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.basicTests;
026
027import static org.junit.Assert.*;
028
029import java.util.Iterator;
030
031import org.junit.Test;
032import org.slf4j.IMarkerFactory;
033import org.slf4j.Marker;
034import org.slf4j.helpers.BasicMarkerFactory;
035
036/**
037 * Unit test BasicMarker
038 * 
039 * @author Ceki Gülcü
040 * @author Joern Huxhorn
041 */
042public class BasicMarkerTest {
043    static final String BLUE_STR = "BLUE";
044    static final String RED_STR = "RED";
045    static final String GREEN_STR = "GREEN";
046    static final String COMP_STR = "COMP";
047    static final String MULTI_COMP_STR = "MULTI_COMP";
048    static final String PARENT_MARKER_STR = "PARENT_MARKER";
049    static final String CHILD_MARKER_STR = "CHILD_MARKER";
050    static final String NOT_CONTAINED_MARKER_STR = "NOT_CONTAINED";
051
052    final IMarkerFactory factory;
053    final Marker blue;
054    final Marker red;
055    final Marker green;
056    final Marker comp;
057    final Marker multiComp;
058
059    short diff = Differentiator.getDiffentiator();
060
061    public BasicMarkerTest() {
062        factory = new BasicMarkerFactory();
063
064        blue = factory.getMarker(BLUE_STR);
065        red = factory.getMarker(RED_STR);
066        green = factory.getMarker(GREEN_STR);
067        comp = factory.getMarker(COMP_STR);
068        comp.add(blue);
069
070        multiComp = factory.getMarker(MULTI_COMP_STR);
071        multiComp.add(green);
072        multiComp.add(comp);
073    }
074
075    @Test
076    public void testPrimitive() {
077        assertEquals(BLUE_STR, blue.getName());
078        assertTrue(blue.contains(blue));
079
080        Marker blue2 = factory.getMarker(BLUE_STR);
081        assertEquals(BLUE_STR, blue2.getName());
082        assertEquals(blue, blue2);
083        assertTrue(blue.contains(blue2));
084        assertTrue(blue2.contains(blue));
085    }
086
087    @Test
088    public void testPrimitiveByName() {
089        assertTrue(blue.contains(BLUE_STR));
090    }
091
092    @Test
093    public void testComposite() {
094        assertTrue(comp.contains(comp));
095        assertTrue(comp.contains(blue));
096    }
097
098    @Test
099    public void testCompositeByName() {
100        assertTrue(comp.contains(COMP_STR));
101        assertTrue(comp.contains(BLUE_STR));
102    }
103
104    @Test
105    public void testMultiComposite() {
106        assertTrue(multiComp.contains(comp));
107        assertTrue(multiComp.contains(blue));
108        assertTrue(multiComp.contains(green));
109        assertFalse(multiComp.contains(red));
110    }
111
112    @Test
113    public void testMultiCompositeByName() {
114        assertTrue(multiComp.contains(COMP_STR));
115        assertTrue(multiComp.contains(BLUE_STR));
116        assertTrue(multiComp.contains(GREEN_STR));
117        assertFalse(multiComp.contains(RED_STR));
118    }
119
120    @Test
121    public void testMultiAdd() {
122        Marker parent = factory.getMarker(PARENT_MARKER_STR);
123        Marker child = factory.getMarker(CHILD_MARKER_STR);
124        for (int i = 0; i < 10; i++) {
125            parent.add(child);
126        }
127
128        // check that the child was added once and only once
129        Iterator<Marker> iterator = parent.iterator();
130        assertTrue(iterator.hasNext());
131        assertEquals(CHILD_MARKER_STR, iterator.next().toString());
132        assertFalse(iterator.hasNext());
133    }
134
135    @Test
136    public void testAddRemove() {
137        final String NEW_PREFIX = "NEW_";
138        Marker parent = factory.getMarker(NEW_PREFIX + PARENT_MARKER_STR);
139        Marker child = factory.getMarker(NEW_PREFIX + CHILD_MARKER_STR);
140        assertFalse(parent.contains(child));
141        assertFalse(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
142        assertFalse(parent.remove(child));
143
144        parent.add(child);
145
146        assertTrue(parent.contains(child));
147        assertTrue(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
148
149        assertTrue(parent.remove(child));
150
151        assertFalse(parent.contains(child));
152        assertFalse(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
153        assertFalse(parent.remove(child));
154    }
155
156    @Test
157    public void testSelfRecursion() {
158        final String diffPrefix = "NEW_" + diff;
159        final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
160        final String NOT_CONTAINED_NAME = diffPrefix + NOT_CONTAINED_MARKER_STR;
161        Marker parent = factory.getMarker(PARENT_NAME);
162        Marker notContained = factory.getMarker(NOT_CONTAINED_NAME);
163        parent.add(parent);
164        assertTrue(parent.contains(parent));
165        assertTrue(parent.contains(PARENT_NAME));
166        assertFalse(parent.contains(notContained));
167        assertFalse(parent.contains(NOT_CONTAINED_MARKER_STR));
168    }
169
170    @Test
171    public void testIndirectRecursion() {
172        final String diffPrefix = "NEW_" + diff;
173        final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
174        final String CHILD_NAME = diffPrefix + CHILD_MARKER_STR;
175        final String NOT_CONTAINED_NAME = diffPrefix + NOT_CONTAINED_MARKER_STR;
176
177        Marker parent = factory.getMarker(PARENT_NAME);
178        Marker child = factory.getMarker(CHILD_NAME);
179        Marker notContained = factory.getMarker(NOT_CONTAINED_NAME);
180
181        parent.add(child);
182        child.add(parent);
183        assertTrue(parent.contains(parent));
184        assertTrue(parent.contains(child));
185        assertTrue(parent.contains(PARENT_NAME));
186        assertTrue(parent.contains(CHILD_NAME));
187        assertFalse(parent.contains(notContained));
188        assertFalse(parent.contains(NOT_CONTAINED_MARKER_STR));
189    }
190
191    @Test
192    public void testHomonyms() {
193        final String diffPrefix = "homonym" + diff;
194        final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
195        final String CHILD_NAME = diffPrefix + CHILD_MARKER_STR;
196        Marker parent = factory.getMarker(PARENT_NAME);
197        Marker child = factory.getMarker(CHILD_NAME);
198        parent.add(child);
199
200        IMarkerFactory otherFactory = new BasicMarkerFactory();
201        Marker otherParent = otherFactory.getMarker(PARENT_NAME);
202        Marker otherChild = otherFactory.getMarker(CHILD_NAME);
203
204        assertTrue(parent.contains(otherParent));
205        assertTrue(parent.contains(otherChild));
206
207        assertTrue(parent.remove(otherChild));
208    }
209
210}