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