View Javadoc

1   // Copyright (C) 2008 - 2012 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.communication;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.mockito.Mockito.when;
27  
28  import java.util.Comparator;
29  
30  import net.grinder.common.processidentity.AgentIdentity;
31  import net.grinder.common.processidentity.ProcessReport;
32  import net.grinder.console.common.processidentity.StubAgentProcessReport;
33  import net.grinder.console.communication.ProcessControl.ProcessReports;
34  import net.grinder.engine.agent.StubAgentIdentity;
35  import net.grinder.messages.console.AgentAndCacheReport;
36  
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.mockito.Mock;
40  import org.mockito.MockitoAnnotations;
41  
42  /**
43   * Unit tests for {@link ProcessControl.ProcessReportsComparator}.
44   *
45   * @author Philip Aston
46   */
47  public class TestProcessControl {
48  
49    private final AgentIdentity m_agentIdentity =
50        new StubAgentIdentity("my agent");
51  
52    private final AgentAndCacheReport m_agentProcessReport1 =
53      new StubAgentProcessReport(m_agentIdentity, ProcessReport.State.RUNNING);
54    private final AgentAndCacheReport agentProcessReport2 =
55      new StubAgentProcessReport(m_agentIdentity, ProcessReport.State.FINISHED);
56  
57    @Mock private ProcessReports m_processReports1;
58    @Mock private ProcessReports m_processReports2;
59  
60    @Before public void setUp() throws Exception {
61      MockitoAnnotations.initMocks(this);
62  
63      when(m_processReports1.getAgentProcessReport())
64        .thenReturn(m_agentProcessReport1);
65  
66      when(m_processReports2.getAgentProcessReport())
67        .thenReturn(agentProcessReport2);
68    }
69  
70    @Test public void testProcessReportsComparator() throws Exception {
71      final Comparator<ProcessReports> comparator =
72        new ProcessControl.ProcessReportsComparator();
73  
74      assertEquals(0, comparator.compare(m_processReports1, m_processReports1));
75      assertEquals(0, comparator.compare(m_processReports2, m_processReports2));
76      assertTrue(comparator.compare(m_processReports1, m_processReports2) < 0);
77      assertTrue(comparator.compare(m_processReports2, m_processReports1) > 0);
78    }
79  }