View Javadoc

1   // Copyright (C) 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.mockito.Matchers.eq;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.util.Timer;
31  
32  import net.grinder.common.GrinderProperties;
33  import net.grinder.common.processidentity.AgentIdentity;
34  import net.grinder.common.processidentity.ProcessReport.State;
35  import net.grinder.communication.MessageDispatchRegistry;
36  import net.grinder.communication.MessageDispatchRegistry.Handler;
37  import net.grinder.console.common.DisplayMessageConsoleException;
38  import net.grinder.console.common.Resources;
39  import net.grinder.console.communication.ProcessControl.ProcessReports;
40  import net.grinder.engine.agent.StubAgentIdentity;
41  import net.grinder.messages.agent.StartGrinderMessage;
42  import net.grinder.messages.agent.StubCacheHighWaterMark;
43  import net.grinder.messages.console.AgentAddress;
44  import net.grinder.messages.console.AgentProcessReportMessage;
45  import net.grinder.util.Directory;
46  
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.mockito.ArgumentCaptor;
50  import org.mockito.Captor;
51  import org.mockito.Mock;
52  import org.mockito.MockitoAnnotations;
53  
54  /**
55   * Unit tests for {@link ProcessControlImplementation}.
56   *
57   * @author Philip Aston
58   */
59  public class TestProcessControlImplementation {
60  
61    private final AgentIdentity m_agentIdentity =
62        new StubAgentIdentity("my agent");
63  
64    private final AgentProcessReportMessage m_agentMessage =
65        new AgentProcessReportMessage(State.RUNNING,
66                                      new StubCacheHighWaterMark(null, 0));
67  
68    @Mock private ProcessReports m_processReports1;
69    @Mock private ProcessReports m_processReports2;
70    @Mock private Timer m_timer;
71    @Mock private MessageDispatchRegistry m_messageDispatchRegistry;
72    @Mock private ConsoleCommunication m_consoleCommunication;
73    @Mock private Resources m_resources;
74    @Captor ArgumentCaptor<Handler<AgentProcessReportMessage>>
75      m_agentReportMessageHandlerCaptor;
76    @Captor ArgumentCaptor<StartGrinderMessage> m_startGrinderMessageCaptor;
77  
78    private ProcessControl m_processControl;
79  
80    @Before public void setUp() throws Exception {
81      MockitoAnnotations.initMocks(this);
82  
83      when(m_consoleCommunication.getMessageDispatchRegistry())
84        .thenReturn(m_messageDispatchRegistry);
85  
86      m_agentMessage.setAddress(new AgentAddress(m_agentIdentity));
87  
88      m_processControl = new ProcessControlImplementation(m_timer,
89                                       m_consoleCommunication,
90                                       m_resources);
91  
92      verify(m_messageDispatchRegistry)
93        .set(eq(AgentProcessReportMessage.class),
94            m_agentReportMessageHandlerCaptor.capture());
95  
96      m_agentReportMessageHandlerCaptor.getValue().handle(m_agentMessage);
97    }
98  
99    @Test public void testStartWorkerProcessesWithDistributedFiles()
100       throws Exception {
101 
102     final GrinderProperties properties = new GrinderProperties();
103     m_processControl.startWorkerProcessesWithDistributedFiles(new Directory(),
104                                                               properties);
105 
106     verify(m_consoleCommunication).sendToAddressedAgents(
107       eq(m_agentMessage.getProcessAddress()),
108       m_startGrinderMessageCaptor.capture());
109 
110     final StartGrinderMessage startMessage =
111         m_startGrinderMessageCaptor.getValue();
112     assertEquals(0, startMessage.getAgentNumber());
113     final GrinderProperties sentProperties = startMessage.getProperties();
114 
115     assertEquals(properties, sentProperties);
116   }
117 
118   @Test(expected=DisplayMessageConsoleException.class)
119   public void
120     testStartWorkerProcessesWithDistributedFilesInvalidRelativePath()
121       throws Exception {
122 
123     final GrinderProperties properties = new GrinderProperties();
124     properties.setFile("grinder.script", new File("../outside"));
125     m_processControl.startWorkerProcessesWithDistributedFiles(new Directory(),
126                                                               properties);
127   }
128 
129   @Test(expected=DisplayMessageConsoleException.class)
130   public void
131     testStartWorkerProcessesWidthDistributedFilesInvalidRelativePath2()
132       throws Exception {
133 
134     final GrinderProperties properties = new GrinderProperties();
135     properties.setAssociatedFile(new File("foo/bah").getAbsoluteFile());
136     properties.setFile("grinder.script", new File("../../outside"));
137     m_processControl.startWorkerProcessesWithDistributedFiles(new Directory(),
138                                                               properties);
139   }
140 
141   @Test public void
142     testStartWorkerProcessesWithDistributedFilesKeepRelativePath()
143       throws Exception {
144 
145     final GrinderProperties properties = new GrinderProperties();
146     properties.setAssociatedFile(new File("one"));
147     properties.setFile("grinder.script", new File("one/two"));
148 
149     m_processControl.startWorkerProcessesWithDistributedFiles(
150       new Directory(new File("three").getAbsoluteFile()),
151       properties);
152 
153     verify(m_consoleCommunication).sendToAddressedAgents(
154       eq(m_agentMessage.getProcessAddress()),
155       m_startGrinderMessageCaptor.capture());
156 
157     final StartGrinderMessage startMessage =
158         m_startGrinderMessageCaptor.getValue();
159     assertEquals(0, startMessage.getAgentNumber());
160     final GrinderProperties sentProperties = startMessage.getProperties();
161 
162     assertEquals(properties, sentProperties);
163     assertEquals(properties.getAssociatedFile(),
164                  sentProperties.getAssociatedFile());
165   }
166 
167   @Test public void
168     testStartWorkerProcessesWithDistributedFilesAdjustRelativePath()
169       throws Exception {
170 
171     final GrinderProperties properties = new GrinderProperties();
172     properties.setAssociatedFile(
173       new File("three/four/my.props").getAbsoluteFile());
174     properties.setFile("grinder.script", new File("one/two"));
175 
176     m_processControl.startWorkerProcessesWithDistributedFiles(
177       new Directory(new File("three").getAbsoluteFile()),
178       properties);
179 
180     verify(m_consoleCommunication).sendToAddressedAgents(
181       eq(m_agentMessage.getProcessAddress()),
182       m_startGrinderMessageCaptor.capture());
183 
184     final StartGrinderMessage startMessage =
185         m_startGrinderMessageCaptor.getValue();
186     assertEquals(0, startMessage.getAgentNumber());
187     final GrinderProperties sentProperties = startMessage.getProperties();
188 
189     assertEquals(properties, sentProperties);
190     assertEquals(new File("four/my.props"), sentProperties.getAssociatedFile());
191   }
192 }