View Javadoc

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