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