View Javadoc

1   // Copyright (C) 2008 - 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.console.synchronisation;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.verifyNoMoreInteractions;
28  import static org.mockito.Mockito.when;
29  import net.grinder.communication.Message;
30  import net.grinder.communication.MessageDispatchRegistry;
31  import net.grinder.console.communication.ConsoleCommunication;
32  import net.grinder.synchronisation.BarrierGroup;
33  import net.grinder.synchronisation.messages.BarrierIdentity;
34  import net.grinder.synchronisation.messages.OpenBarrierMessage;
35  
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.mockito.ArgumentCaptor;
39  import org.mockito.Captor;
40  import org.mockito.Mock;
41  import org.mockito.MockitoAnnotations;
42  
43  
44  /**
45   * Unit tests for {@link ConsoleBarrierGroups}.
46   *
47   * @author Philip Aston
48   */
49  public class TestConsoleBarrierGroups {
50  
51    @Mock private MessageDispatchRegistry m_messageDispatchRegistry;
52    @Mock private ConsoleCommunication m_consoleCommunication;
53  
54    @Captor private ArgumentCaptor<Message> m_messageCaptor;
55  
56    @Before public void setUp() {
57      MockitoAnnotations.initMocks(this);
58  
59      when(m_consoleCommunication.getMessageDispatchRegistry())
60        .thenReturn(m_messageDispatchRegistry);
61    }
62  
63    @Test public void testConsoleBarrierGroups() throws Exception {
64  
65      final ConsoleBarrierGroups barrierGroups =
66        new ConsoleBarrierGroups(m_consoleCommunication);
67  
68      final BarrierGroup bg = barrierGroups.createBarrierGroup("foo");
69  
70      bg.addBarrier();
71      verifyNoMoreInteractions(m_consoleCommunication);
72  
73      bg.addWaiter(mock(BarrierIdentity.class));
74      verify(m_consoleCommunication).sendToAgents(m_messageCaptor.capture());
75  
76      assertEquals("foo",
77                   ((OpenBarrierMessage)m_messageCaptor.getValue()).getName());
78    }
79  }