View Javadoc

1   // Copyright (C) 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.synchronisation.messages;
23  
24  import static java.util.Arrays.asList;
25  import static net.grinder.testutility.Serializer.serialize;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertSame;
28  import static org.junit.Assert.fail;
29  import static org.mockito.Mockito.mock;
30  
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  import net.grinder.common.processidentity.AgentIdentity;
35  import net.grinder.common.processidentity.WorkerIdentity;
36  import net.grinder.communication.CommunicationException;
37  import net.grinder.messages.console.AgentAddress;
38  import net.grinder.messages.console.WorkerAddress;
39  import net.grinder.synchronisation.BarrierIdentityGenerator;
40  
41  import org.junit.Test;
42  
43  
44  /**
45   * Unit tests for barrier group messages.
46   *
47   * @author Philip Aston
48   */
49  public class TestBarrierGroupMessages {
50  
51    private final BarrierIdentity.Factory m_identityFactory =
52      new BarrierIdentityGenerator(new Integer(1));
53  
54    @Test public void testOpenBarrierMessage() throws Exception {
55  
56      final Set<BarrierIdentity> waiters =
57        new HashSet<BarrierIdentity>(asList(m_identityFactory.next(),
58                                            m_identityFactory.next()));
59  
60      final OpenBarrierMessage message = new OpenBarrierMessage("abc",
61                                                                waiters);
62  
63      final OpenBarrierMessage serialized = serialize(message);
64  
65      assertEquals("abc", serialized.getName());
66      assertEquals(waiters, message.getWaiters());
67    }
68  
69    @Test public void testAddWaiterMessage() throws Exception {
70      final BarrierIdentity identity = m_identityFactory.next();
71  
72      final AddWaiterMessage message = new AddWaiterMessage("abc", identity);
73  
74      final AddWaiterMessage serialized = serialize(message);
75  
76      assertEquals("abc", serialized.getName());
77      assertEquals(identity, serialized.getBarrierIdentity());
78    }
79  
80    @Test public void testAddressAwareMessage() throws Exception {
81  
82      final WorkerIdentity identity = mock(WorkerIdentity.class);
83  
84      final AddWaiterMessage message =
85        new AddWaiterMessage("abc", m_identityFactory.next());
86  
87      message.setAddress(new WorkerAddress(identity));
88  
89      assertSame(identity, message.getProcessIdentity());
90    }
91  
92    @Test public void testAddressAwareMessageBadAddress() throws Exception {
93  
94      final AddWaiterMessage message =
95        new AddWaiterMessage("abc", m_identityFactory.next());
96  
97      try {
98        message.setAddress(new AgentAddress(mock(AgentIdentity.class)));
99        fail("Expected CommunicationException");
100     }
101     catch (CommunicationException e) {
102     }
103   }
104 }