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.swingui;
23  
24  import java.util.HashMap;
25  
26  import junit.framework.TestCase;
27  import net.grinder.common.processidentity.ProcessReport;
28  import net.grinder.common.processidentity.WorkerProcessReport;
29  import net.grinder.console.common.Resources;
30  import net.grinder.console.common.StubResources;
31  import net.grinder.console.common.processidentity.StubAgentProcessReport;
32  import net.grinder.console.common.processidentity.StubWorkerProcessReport;
33  import net.grinder.console.communication.ProcessControl;
34  import net.grinder.console.communication.StubProcessReports;
35  import net.grinder.console.communication.ProcessControl.ProcessReports;
36  import net.grinder.engine.agent.StubAgentIdentity;
37  import net.grinder.testutility.RandomStubFactory;
38  
39  
40  /**
41   * Unit tests for {@link ProcessStatusTableModel}.
42   *
43   * @author Philip Aston
44   */
45  public class TestProcessStatusTableModel extends TestCase {
46  
47    private Resources m_resources =
48      new StubResources<String>(
49        new HashMap<String, String>() { {
50          put("processTable.nameColumn.label", "NaMe");
51          put("processTable.processTypeColumn.label", "type");
52          put("processTable.stateColumn.label", "STATE");
53          put("processTable.processes.label", "workers");
54          put("processTable.threads.label", "threads");
55          put("processState.connected.label", "connected");
56          put("processState.disconnected.label", "disconnected");
57          put("processState.finished.label", "finished");
58          put("processTable.agentProcess.label", "Agent");
59          put("processTable.workerProcess.label", "Worker");
60        } }
61      );
62  
63    final RandomStubFactory<ProcessControl> m_processControlStubFactory =
64      RandomStubFactory.create(ProcessControl.class);
65    final ProcessControl m_processControl = m_processControlStubFactory.getStub();
66  
67    public void testConstruction() throws Exception {
68  
69      final int[] swingDispatcherCallCount = new int[1];
70  
71      final SwingDispatcherFactory swingDispatcherFactory =
72        new SwingDispatcherFactory() {
73          public <T> T create(Class<T> clazz, T delegate) {
74            ++swingDispatcherCallCount[0];
75            return delegate;
76          }
77        };
78  
79      final ProcessStatusTableModel processStatusTableModel =
80        new ProcessStatusTableModel(m_resources,
81                                    m_processControl,
82                                    swingDispatcherFactory);
83  
84      m_processControlStubFactory.assertSuccess(
85        "addProcessStatusListener", ProcessControl.Listener.class);
86      m_processControlStubFactory.assertNoMoreCalls();
87  
88      assertEquals(3, processStatusTableModel.getColumnCount());
89      assertEquals(0, processStatusTableModel.getRowCount());
90      assertEquals("", processStatusTableModel.getValueAt(0, 0));
91      assertEquals("NaMe", processStatusTableModel.getColumnName(0));
92      assertEquals("type", processStatusTableModel.getColumnName(1));
93      assertEquals("STATE", processStatusTableModel.getColumnName(2));
94  
95      assertFalse(processStatusTableModel.isBold(0, 0));
96      assertNull(processStatusTableModel.getForeground(0, 0));
97      assertNull(processStatusTableModel.getBackground(0, 0));
98  
99      assertEquals(1, swingDispatcherCallCount[0]);
100   }
101 
102   public void testWithData() throws Exception {
103     final SwingDispatcherFactory swingDispatcherFactory =
104       new SwingDispatcherFactory() {
105         public <T> T create(Class<T> clazz, T delegate) { return delegate; }
106       };
107 
108     final ProcessStatusTableModel processStatusTableModel =
109       new ProcessStatusTableModel(m_resources,
110                                   m_processControl,
111                                   swingDispatcherFactory);
112 
113     final ProcessControl.Listener listener =
114       (ProcessControl.Listener)
115       m_processControlStubFactory.assertSuccess("addProcessStatusListener",
116                                                 ProcessControl.Listener.class)
117      .getParameters()[0];
118 
119     assertEquals(0, processStatusTableModel.getRowCount());
120 
121     final ProcessReports[] nullReport = new ProcessReports[0];
122     listener.update(nullReport);
123 
124     assertEquals(1, processStatusTableModel.getRowCount());
125     assertEquals("", processStatusTableModel.getValueAt(0, 0));
126     assertEquals("0 workers", processStatusTableModel.getValueAt(0, 1));
127     assertEquals("0/0 threads", processStatusTableModel.getValueAt(0, 2));
128     assertEquals("?", processStatusTableModel.getValueAt(0, 3));
129     assertEquals("", processStatusTableModel.getValueAt(1, 3));
130 
131     final StubAgentIdentity agentIdentity1 = new StubAgentIdentity("agent1");
132     final StubAgentProcessReport agentReport1 =
133       new StubAgentProcessReport(agentIdentity1, ProcessReport.State.RUNNING);
134 
135     final WorkerProcessReport workerProcessReport1 =
136       new StubWorkerProcessReport(agentIdentity1.createWorkerIdentity(),
137                                   ProcessReport.State.RUNNING, 3, 6);
138 
139     final WorkerProcessReport workerProcessReport2 =
140       new StubWorkerProcessReport(agentIdentity1.createWorkerIdentity(),
141                                   ProcessReport.State.FINISHED, 0, 6);
142 
143     final ProcessReports[] someData = new ProcessReports[] {
144         new StubProcessReports(agentReport1,
145           new WorkerProcessReport[] {
146             workerProcessReport1,
147             workerProcessReport2,
148           }),
149     };
150 
151     listener.update(someData);
152 
153     assertEquals(4, processStatusTableModel.getRowCount());
154     assertEquals("agent1", processStatusTableModel.getValueAt(0, 0));
155     assertEquals("Agent", processStatusTableModel.getValueAt(0, 1));
156     assertEquals("connected", processStatusTableModel.getValueAt(0, 2));
157     assertEquals("?", processStatusTableModel.getValueAt(0, 3));
158     assertEquals("?", processStatusTableModel.getValueAt(1, 3));
159     assertEquals("  agent1-1", processStatusTableModel.getValueAt(2, 0));
160     assertEquals("Worker", processStatusTableModel.getValueAt(2, 1));
161     assertEquals("finished", processStatusTableModel.getValueAt(2, 2));
162     assertEquals("?", processStatusTableModel.getValueAt(2, 3));
163     assertEquals("2 workers", processStatusTableModel.getValueAt(3, 1));
164     assertEquals("3/12 threads", processStatusTableModel.getValueAt(3, 2));
165 
166     m_processControlStubFactory.assertNoMoreCalls();
167   }
168 }