View Javadoc

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