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 java.util.Iterator;
28  
29  import junit.framework.TestCase;
30  
31  import org.slf4j.helpers.BasicMarkerFactory;
32  
33  /**
34   * Unit test BasicMarker
35   * 
36   * @author Ceki Gülcü
37   * @author Joern Huxhorn
38   */
39  public class BasicMarkerTest extends TestCase {
40      static final String BLUE_STR = "BLUE";
41      static final String RED_STR = "RED";
42      static final String GREEN_STR = "GREEN";
43      static final String COMP_STR = "COMP";
44      static final String MULTI_COMP_STR = "MULTI_COMP";
45      static final String PARENT_MARKER_STR = "PARENT_MARKER";
46      static final String CHILD_MARKER_STR = "CHILD_MARKER";
47      static final String NOT_CONTAINED_MARKER_STR = "NOT_CONTAINED";
48  
49      final IMarkerFactory factory;
50      final Marker blue;
51      final Marker red;
52      final Marker green;
53      final Marker comp;
54      final Marker multiComp;
55  
56      short diff = Differentiator.getDiffentiator();
57  
58      public BasicMarkerTest() {
59          factory = new BasicMarkerFactory();
60  
61          blue = factory.getMarker(BLUE_STR);
62          red = factory.getMarker(RED_STR);
63          green = factory.getMarker(GREEN_STR);
64          comp = factory.getMarker(COMP_STR);
65          comp.add(blue);
66  
67          multiComp = factory.getMarker(MULTI_COMP_STR);
68          multiComp.add(green);
69          multiComp.add(comp);
70      }
71  
72      public void testPrimitive() {
73          assertEquals(BLUE_STR, blue.getName());
74          assertTrue(blue.contains(blue));
75  
76          Marker blue2 = factory.getMarker(BLUE_STR);
77          assertEquals(BLUE_STR, blue2.getName());
78          assertEquals(blue, blue2);
79          assertTrue(blue.contains(blue2));
80          assertTrue(blue2.contains(blue));
81      }
82  
83      public void testPrimitiveByName() {
84          assertTrue(blue.contains(BLUE_STR));
85      }
86  
87      public void testComposite() {
88          assertTrue(comp.contains(comp));
89          assertTrue(comp.contains(blue));
90      }
91  
92      public void testCompositeByName() {
93          assertTrue(comp.contains(COMP_STR));
94          assertTrue(comp.contains(BLUE_STR));
95      }
96  
97      public void testMultiComposite() {
98          assertTrue(multiComp.contains(comp));
99          assertTrue(multiComp.contains(blue));
100         assertTrue(multiComp.contains(green));
101         assertFalse(multiComp.contains(red));
102     }
103 
104     public void testMultiCompositeByName() {
105         assertTrue(multiComp.contains(COMP_STR));
106         assertTrue(multiComp.contains(BLUE_STR));
107         assertTrue(multiComp.contains(GREEN_STR));
108         assertFalse(multiComp.contains(RED_STR));
109     }
110 
111     public void testMultiAdd() {
112         Marker parent = factory.getMarker(PARENT_MARKER_STR);
113         Marker child = factory.getMarker(CHILD_MARKER_STR);
114         for (int i = 0; i < 10; i++) {
115             parent.add(child);
116         }
117 
118         // check that the child was added once and only once
119         Iterator<Marker> iterator = parent.iterator();
120         assertTrue(iterator.hasNext());
121         assertEquals(CHILD_MARKER_STR, iterator.next().toString());
122         assertFalse(iterator.hasNext());
123     }
124 
125     public void testAddRemove() {
126         final String NEW_PREFIX = "NEW_";
127         Marker parent = factory.getMarker(NEW_PREFIX + PARENT_MARKER_STR);
128         Marker child = factory.getMarker(NEW_PREFIX + CHILD_MARKER_STR);
129         assertFalse(parent.contains(child));
130         assertFalse(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
131         assertFalse(parent.remove(child));
132 
133         parent.add(child);
134 
135         assertTrue(parent.contains(child));
136         assertTrue(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
137 
138         assertTrue(parent.remove(child));
139 
140         assertFalse(parent.contains(child));
141         assertFalse(parent.contains(NEW_PREFIX + CHILD_MARKER_STR));
142         assertFalse(parent.remove(child));
143     }
144 
145     public void testSelfRecursion() {
146         final String diffPrefix = "NEW_" + diff;
147         final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
148         final String NOT_CONTAINED_NAME = diffPrefix + NOT_CONTAINED_MARKER_STR;
149         Marker parent = factory.getMarker(PARENT_NAME);
150         Marker notContained = factory.getMarker(NOT_CONTAINED_NAME);
151         parent.add(parent);
152         assertTrue(parent.contains(parent));
153         assertTrue(parent.contains(PARENT_NAME));
154         assertFalse(parent.contains(notContained));
155         assertFalse(parent.contains(NOT_CONTAINED_MARKER_STR));
156     }
157 
158     public void testIndirectRecursion() {
159         final String diffPrefix = "NEW_" + diff;
160         final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
161         final String CHILD_NAME = diffPrefix + CHILD_MARKER_STR;
162         final String NOT_CONTAINED_NAME = diffPrefix + NOT_CONTAINED_MARKER_STR;
163 
164         Marker parent = factory.getMarker(PARENT_NAME);
165         Marker child = factory.getMarker(CHILD_NAME);
166         Marker notContained = factory.getMarker(NOT_CONTAINED_NAME);
167 
168         parent.add(child);
169         child.add(parent);
170         assertTrue(parent.contains(parent));
171         assertTrue(parent.contains(child));
172         assertTrue(parent.contains(PARENT_NAME));
173         assertTrue(parent.contains(CHILD_NAME));
174         assertFalse(parent.contains(notContained));
175         assertFalse(parent.contains(NOT_CONTAINED_MARKER_STR));
176     }
177 
178     public void testHomonyms() {
179         final String diffPrefix = "homonym" + diff;
180         final String PARENT_NAME = diffPrefix + PARENT_MARKER_STR;
181         final String CHILD_NAME = diffPrefix + CHILD_MARKER_STR;
182         Marker parent = factory.getMarker(PARENT_NAME);
183         Marker child = factory.getMarker(CHILD_NAME);
184         parent.add(child);
185 
186         IMarkerFactory otherFactory = new BasicMarkerFactory();
187         Marker otherParent = otherFactory.getMarker(PARENT_NAME);
188         Marker otherChild = otherFactory.getMarker(CHILD_NAME);
189 
190         assertTrue(parent.contains(otherParent));
191         assertTrue(parent.contains(otherChild));
192 
193         assertTrue(parent.remove(otherChild));
194     }
195 
196 }