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.basicTests;
26  
27  import static org.junit.Assert.*;
28  
29  import java.util.Iterator;
30  
31  import org.junit.Test;
32  import org.slf4j.IMarkerFactory;
33  import org.slf4j.Marker;
34  import org.slf4j.helpers.BasicMarkerFactory;
35  
36  /**
37   * Unit test BasicMarker
38   * 
39   * @author Ceki Gülcü
40   * @author Joern Huxhorn
41   */
42  public class BasicMarkerTest {
43      static final String BLUE_STR = "BLUE";
44      static final String RED_STR = "RED";
45      static final String GREEN_STR = "GREEN";
46      static final String COMP_STR = "COMP";
47      static final String MULTI_COMP_STR = "MULTI_COMP";
48      static final String PARENT_MARKER_STR = "PARENT_MARKER";
49      static final String CHILD_MARKER_STR = "CHILD_MARKER";
50      static final String NOT_CONTAINED_MARKER_STR = "NOT_CONTAINED";
51  
52      final IMarkerFactory factory;
53      final Marker blue;
54      final Marker red;
55      final Marker green;
56      final Marker comp;
57      final Marker multiComp;
58  
59      short diff = Differentiator.getDiffentiator();
60  
61      public BasicMarkerTest() {
62          factory = new BasicMarkerFactory();
63  
64          blue = factory.getMarker(BLUE_STR);
65          red = factory.getMarker(RED_STR);
66          green = factory.getMarker(GREEN_STR);
67          comp = factory.getMarker(COMP_STR);
68          comp.add(blue);
69  
70          multiComp = factory.getMarker(MULTI_COMP_STR);
71          multiComp.add(green);
72          multiComp.add(comp);
73      }
74  
75      @Test
76      public void testPrimitive() {
77          assertEquals(BLUE_STR, blue.getName());
78          assertTrue(blue.contains(blue));
79  
80          Marker blue2 = factory.getMarker(BLUE_STR);
81          assertEquals(BLUE_STR, blue2.getName());
82          assertEquals(blue, blue2);
83          assertTrue(blue.contains(blue2));
84          assertTrue(blue2.contains(blue));
85      }
86  
87      @Test
88      public void testPrimitiveByName() {
89          assertTrue(blue.contains(BLUE_STR));
90      }
91  
92      @Test
93      public void testComposite() {
94          assertTrue(comp.contains(comp));
95          assertTrue(comp.contains(blue));
96      }
97  
98      @Test
99      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 }