1   /*
2    * $Id: EventGroupTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing.inbound;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.util.UUID;
18  
19  import java.util.Arrays;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Set;
23  
24  import org.apache.commons.collections.IteratorUtils;
25  
26  public class EventGroupTestCase extends AbstractMuleTestCase
27  {
28  
29       public void testConcurrentIteration() throws Exception
30      {
31          EventGroup eg = new EventGroup(UUID.getUUID());
32          assertFalse(eg.iterator().hasNext());
33  
34          // add to events to start with
35          eg.addEvent(getTestEvent("foo1"));
36          eg.addEvent(getTestEvent("foo2"));
37          assertTrue(eg.iterator().hasNext());
38  
39          // now add events while we iterate over the group
40          Iterator i = eg.iterator();
41          assertNotNull(i.next());
42          eg.addEvent(getTestEvent("foo3"));
43          assertNotNull(i.next());
44          eg.addEvent(getTestEvent("foo4"));
45          assertFalse(i.hasNext());
46  
47          // the added events should be in there though
48          assertEquals(4, eg.size());
49      }
50  
51      public void testEquals()
52      {
53          EventGroup g1 = new EventGroup("foo");
54          EventGroup g2 = new EventGroup("foo");
55          EventGroup g3 = new EventGroup("bar");
56  
57          assertEquals(g1, g2);
58          assertFalse(g1.equals(g3));
59  
60          MyEventGroup mg = new MyEventGroup("foo");
61          assertEquals(g1, mg);
62          assertEquals(mg, g1);
63  
64          mg = new MyEventGroup("bar");
65          assertFalse(g1.equals(mg));
66          assertFalse(mg.equals(g1));
67      }
68  
69      public void testHashCode()
70      {
71          String uuid = UUID.getUUID();
72          EventGroup g1 = new EventGroup(uuid);
73          EventGroup g2 = new EventGroup(uuid);
74          EventGroup g3 = new EventGroup(UUID.getUUID());
75  
76          assertEquals(g1.hashCode(), g2.hashCode());
77          assertEquals(g1, g2);
78  
79          assertFalse(g1.hashCode() == g3.hashCode());
80          assertFalse(g1.equals(g3));
81          assertFalse(g3.equals(g1));
82  
83          // now test Set compatibility
84          Set s = new HashSet();
85          s.add(g1);
86  
87          // make sure g1 is in the set
88          assertTrue(s.contains(g1));
89          assertEquals(1, s.size());
90  
91          // g2 has the same hash, so it should match
92          assertTrue(s.contains(g2));
93          // even though there is only one object in the set
94          assertEquals(1, s.size());
95  
96          // make sure g3 cannot be found
97          assertFalse(s.contains(g3));
98          // now add it
99          assertTrue(s.add(g3));
100         // make sure it is in there
101         assertTrue(s.contains(g3));
102         // make sure it is really in there
103         assertEquals(2, s.size());
104     }
105 
106     public void testCompareTo()
107     {
108         String uuid = UUID.getUUID();
109         EventGroup g1 = new EventGroup(uuid);
110         EventGroup g2 = new EventGroup(uuid);
111         EventGroup g3 = new EventGroup(UUID.getUUID());
112 
113         // test comparison against null
114         try
115         {
116             g1.compareTo(null);
117             fail("expected NullPointerException");
118         }
119         catch (NullPointerException npe)
120         {
121             // expected
122         }
123 
124         // test comparison against incompatible object
125         try
126         {
127             g1.compareTo("foo");
128             fail("expected ClassCastException");
129         }
130         catch (ClassCastException cce)
131         {
132             // expected
133         }
134 
135         // these tests use the groupId
136         assertEquals(0, g1.compareTo(g2));
137         assertTrue(g1.compareTo(g3) < 0);
138         assertTrue(g3.compareTo(g1) > 0);
139         assertTrue(g3.compareTo(g2) > 0);
140 
141         // when the groupId is not Comparable, the creation time is used as fallback
142         g1 = new EventGroup(new Object());
143         g2 = new EventGroup(new Object());
144         // g1 is older (smaller) than g2
145         assertTrue(g1.compareTo(g2) < 0);
146         assertTrue(g2.compareTo(g1) > 0);
147     }
148 
149     public void testToArray() throws Exception
150     {
151         EventGroup eg = new EventGroup(UUID.getUUID());
152         eg.addEvent(getTestEvent("foo1"));
153         eg.addEvent(getTestEvent("foo2"));
154 
155         Object[] array1 = IteratorUtils.toArray(eg.iterator());
156         MuleEvent[] array2 = eg.toArray();
157         assertTrue(Arrays.equals(array1, array2));
158     }
159 
160     public void testToString() throws Exception
161     {
162         EventGroup eg = new EventGroup(UUID.getUUID());
163         String es = eg.toString();
164         assertTrue(es.endsWith("events=0}"));
165 
166         MuleEvent e = getTestEvent("foo");
167         eg.addEvent(e);
168         es = eg.toString();
169         assertTrue(es.indexOf("events=1") != -1);
170         assertTrue(es.endsWith("[" + e.getMessage().getUniqueId() + "]}"));
171 
172         MuleEvent e2 = new DefaultMuleEvent(new DefaultMuleMessage("foo2"), e);
173         eg.addEvent(e2);
174         es = eg.toString();
175         assertTrue(es.indexOf("events=2") != -1);
176         assertTrue(es.endsWith(e.getMessage().getUniqueId() + ", " + e2.getMessage().getUniqueId() + "]}"));
177     }
178 
179     private static class MyEventGroup extends EventGroup
180     {
181         private static final long serialVersionUID = 1L;
182 
183         public MyEventGroup(Object groupId)
184         {
185             super(groupId);
186         }
187 
188         public MyEventGroup(Object groupId, int expectedSize)
189         {
190             super(groupId, expectedSize);
191         }
192     }
193 
194 }