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.common;
23  
24  import java.util.HashMap;
25  
26  import junit.framework.TestCase;
27  import net.grinder.common.processidentity.AgentProcessReport;
28  import net.grinder.common.processidentity.ProcessReport;
29  import net.grinder.common.processidentity.WorkerIdentity;
30  import net.grinder.common.processidentity.WorkerProcessReport;
31  import net.grinder.common.processidentity.ProcessReport.State;
32  import net.grinder.console.common.ProcessReportDescriptionFactory.ProcessDescription;
33  import net.grinder.engine.agent.StubAgentIdentity;
34  import net.grinder.testutility.RandomStubFactory;
35  
36  
37  /**
38   * Unit tests for {@link ProcessReportDescriptionFactory}.
39   *
40   * @author Philip Aston
41   */
42  public class TestProcessReportDescriptionFactory extends TestCase {
43  
44    private Resources m_resources =
45      new StubResources<String>(
46        new HashMap<String, String>() { {
47          put("processTable.threads.label", "strings");
48          put("processTable.agentProcess.label", "AG");
49          put("processTable.workerProcess.label", "WK");
50          put("processState.started.label", "hot to trot");
51          put("processState.running.label", "rolling");
52          put("processState.connected.label", "plugged in");
53          put("processState.finished.label", "fini");
54          put("processState.disconnected.label", "that's all folks");
55          put("processState.unknown.label", "huh");
56        } }
57      );
58  
59    public void testWithAgentProcessReport() throws Exception {
60      final StubAgentIdentity agentIdentity =
61        new StubAgentIdentity("my agent");
62  
63      final RandomStubFactory<AgentProcessReport> agentProcessReportStubFactory =
64        RandomStubFactory.create(AgentProcessReport.class);
65      final AgentProcessReport agentProcessReport =
66        agentProcessReportStubFactory.getStub();
67      agentProcessReportStubFactory.setResult("getAgentIdentity", agentIdentity);
68      agentProcessReportStubFactory.setResult(
69        "getState", ProcessReport.State.UNKNOWN);
70  
71      final ProcessReportDescriptionFactory processReportDescriptionFactory =
72        new ProcessReportDescriptionFactory(m_resources);
73  
74      final ProcessDescription description1 =
75        processReportDescriptionFactory.create(agentProcessReport);
76  
77      assertEquals("my agent", description1.getName());
78      assertEquals("AG", description1.getProcessType());
79      assertEquals("huh", description1.getState());
80      assertEquals("AG my agent [huh]", description1.toString());
81  
82      agentProcessReportStubFactory.setResult(
83        "getState", ProcessReport.State.RUNNING);
84  
85      final ProcessDescription description2 =
86        processReportDescriptionFactory.create(agentProcessReport);
87  
88      assertEquals("plugged in", description2.getState());
89  
90      // Both started and running report "connected".
91      agentProcessReportStubFactory.setResult(
92        "getState", ProcessReport.State.STARTED);
93  
94      final ProcessDescription description3 =
95        processReportDescriptionFactory.create(agentProcessReport);
96  
97      assertEquals("plugged in", description3.getState());
98  
99      agentProcessReportStubFactory.setResult(
100       "getState", ProcessReport.State.FINISHED);
101 
102     final ProcessDescription description4 =
103       processReportDescriptionFactory.create(agentProcessReport);
104 
105     assertEquals("that's all folks", description4.getState());
106 
107     agentProcessReportStubFactory.setResult("getState", State.UNKNOWN);
108 
109     final ProcessDescription description5 =
110       processReportDescriptionFactory.create(agentProcessReport);
111 
112     assertEquals("huh", description5.getState());
113 
114     agentIdentity.setNumber(10);
115     final ProcessDescription description6 =
116       processReportDescriptionFactory.create(agentProcessReport);
117     assertEquals("my agent (AG 10)", description6.getName());
118   }
119 
120   public void testWithWorkerProcessReport() throws Exception {
121     final StubAgentIdentity agentIdentity =
122       new StubAgentIdentity("agent");
123 
124     final WorkerIdentity workerIdentity =
125       agentIdentity.createWorkerIdentity();
126 
127     final RandomStubFactory<WorkerProcessReport>
128       workerProcessReportStubFactory =
129         RandomStubFactory.create(WorkerProcessReport.class);
130 
131     final WorkerProcessReport workerProcessReport =
132       workerProcessReportStubFactory.getStub();
133     workerProcessReportStubFactory.setResult("getWorkerIdentity",
134                                              workerIdentity);
135     workerProcessReportStubFactory.setResult(
136       "getState", ProcessReport.State.UNKNOWN);
137 
138     final ProcessReportDescriptionFactory processReportDescriptionFactory =
139       new ProcessReportDescriptionFactory(m_resources);
140 
141     final ProcessDescription description1 =
142       processReportDescriptionFactory.create(workerProcessReport);
143 
144     assertEquals("agent-0", description1.getName());
145     assertEquals("WK", description1.getProcessType());
146     assertEquals("huh", description1.getState());
147     assertEquals("WK agent-0 [huh]", description1.toString());
148 
149     workerProcessReportStubFactory.setResult(
150       "getState", ProcessReport.State.RUNNING);
151     workerProcessReportStubFactory.setResult(
152       "getNumberOfRunningThreads", new Short((short) 10));
153     workerProcessReportStubFactory.setResult(
154       "getMaximumNumberOfThreads", new Short((short) 21));
155 
156     final ProcessDescription description2 =
157       processReportDescriptionFactory.create(workerProcessReport);
158 
159     assertEquals("rolling (10/21 strings)", description2.getState());
160 
161     workerProcessReportStubFactory.setResult(
162       "getState", ProcessReport.State.STARTED);
163 
164     final ProcessDescription description3 =
165       processReportDescriptionFactory.create(workerProcessReport);
166 
167     assertEquals("hot to trot", description3.getState());
168 
169     workerProcessReportStubFactory.setResult(
170       "getState", ProcessReport.State.FINISHED);
171 
172     final ProcessDescription description4 =
173       processReportDescriptionFactory.create(workerProcessReport);
174 
175     assertEquals("fini", description4.getState());
176 
177     workerProcessReportStubFactory.setResult("getState", State.UNKNOWN);
178 
179     final ProcessDescription description5 =
180       processReportDescriptionFactory.create(workerProcessReport);
181 
182     assertEquals("huh", description5.getState());
183   }
184 }