View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j;
26  
27  import static org.junit.Assert.*;
28  
29  import java.util.Iterator;
30  
31  import org.junit.Test;
32  import org.slf4j.helpers.BasicMarkerFactory;
33  
34  /**
35   * Unit test BasicMarker
36   * 
37   * @author Ceki Gülcü
38   * @author Joern Huxhorn
39   */
40  public class BasicMarkerTest {
41      static final String BLUE_STR = "BLUE";
42      static final String RED_STR = "RED";
43      static final String GREEN_STR = "GREEN";
44      static final String COMP_STR = "COMP";
45      static final String MULTI_COMP_STR = "MULTI_COMP";
46      static final String PARENT_MARKER_STR = "PARENT_MARKER";
47      static final String CHILD_MARKER_STR = "CHILD_MARKER";
48      static final String NOT_CONTAINED_MARKER_STR = "NOT_CONTAINED";
49  
50      final IMarkerFactory factory;
51      final Marker blue;
52      final Marker red;
53      final Marker green;
54      final Marker comp;
55      final Marker multiComp;
56  
57      short diff = Differentiator.getDiffentiator();
58  
59      public BasicMarkerTest() {
60          factory = new BasicMarkerFactory();
61  
62          blue = factory.getMarker(BLUE_STR);
63          red = factory.getMarker(RED_STR);
64          green = factory.getMarker(GREEN_STR);
65          comp = factory.getMarker(COMP_STR);
66          comp.add(blue);
67  
68          multiComp = factory.getMarker(MULTI_COMP_STR);
69          multiComp.add(green);
70          multiComp.add(comp);
71      }
72  
73      @Test
74      public void testPrimitive() {
75          assertEquals(BLUE_STR, blue.getName());
76          assertTrue(blue.contains(blue));
77  
78          Marker blue2 = factory.getMarker(BLUE_STR);
79          assertEquals(BLUE_STR, blue2.getName());
80          assertEquals(blue, blue2);
81          assertTrue(blue.contains(blue2));
82          assertTrue(blue2.contains(blue));
83      }
84  
85      @Test
86      public void testPrimitiveByName() {
87          assertTrue(blue.contains(BLUE_STR));
88      }
89  
90      @Test
91      public void testComposite() {
92          assertTrue(comp.contains(comp));
93          assertTrue(comp.contains(blue));
94      }
95  
96      @Test
97      public void testCompositeByName() {
98          assertTrue(comp.contains(COMP_STR));
99          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 }