View Javadoc

1   // Copyright (C) 2008 Pawel Lacinski
2   // Copyright (C) 2008 - 2011 Philip Aston
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.engine.agent;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertSame;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  import static org.mockito.Matchers.contains;
30  import static org.mockito.Mockito.doThrow;
31  import static org.mockito.Mockito.verify;
32  import static org.mockito.Mockito.verifyNoMoreInteractions;
33  
34  import java.io.File;
35  
36  import net.grinder.common.GrinderException;
37  import net.grinder.engine.agent.TestAgentDaemon.ActionListSleeperStubFactory.SleepAction;
38  import net.grinder.testutility.AbstractJUnit4FileTestCase;
39  import net.grinder.util.Sleeper;
40  import net.grinder.util.Sleeper.ShutdownException;
41  
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.mockito.Mock;
45  import org.mockito.MockitoAnnotations;
46  import org.slf4j.Logger;
47  
48  
49  /**
50   * Unit tests for {@link AgentDaemon}
51   * TestAgentDaemon.
52   *
53   * @author
54   * @author Philip Aston
55   */
56  public class TestAgentDaemon extends AbstractJUnit4FileTestCase {
57  
58    @Mock private Logger m_logger;
59    @Mock private Agent m_agent;
60  
61    @Before public void setUp() throws Exception {
62      MockitoAnnotations.initMocks(this);
63    }
64  
65    @Test public void testConstruction() throws Exception {
66      final File propertyFile = new File(getDirectory(), "properties");
67      final Agent agent = new AgentImplementation(m_logger, propertyFile, false);
68      final AgentDaemon daemon = new AgentDaemon(m_logger, 1000, agent);
69      daemon.shutdown();
70  
71      verify(m_logger).info(contains("finished"));
72      verifyNoMoreInteractions(m_logger);
73    }
74  
75    @Test public void testRun() throws Exception {
76      final ActionListSleeperStubFactory sleeperStubFactory =
77        new ActionListSleeperStubFactory(
78          new SleepAction[] {
79            new SleepAction() {
80              public void sleep(long time) throws ShutdownException {
81                assertEquals(1000, time);
82              }
83            },
84            new SleepAction() {
85              public void sleep(long time) throws ShutdownException {
86                assertEquals(1000, time);
87                throw new ShutdownException("");
88              }
89            }
90        } );
91  
92      final AgentDaemon agentDaemon =
93        new AgentDaemon(m_logger, 1000, m_agent, sleeperStubFactory.getStub());
94  
95      agentDaemon.run();
96  
97      sleeperStubFactory.assertFinished();
98    }
99  
100   @Test public void testShutdownHook() throws Exception {
101     final AgentDaemon agentDaemon = new AgentDaemon(m_logger, 0, m_agent);
102 
103     final Thread shutdownHook = agentDaemon.getShutdownHook();
104 
105     assertFalse(Runtime.getRuntime().removeShutdownHook(shutdownHook));
106     verifyNoMoreInteractions(m_agent);
107 
108     final GrinderException runException = new GrinderException("") {};
109     doThrow(runException).when(m_agent).run();
110 
111     try {
112       agentDaemon.run();
113       fail("Expected GrinderException");
114     }
115     catch (GrinderException e) {
116       assertSame(runException, e);
117     }
118     assertTrue(Runtime.getRuntime().removeShutdownHook(shutdownHook));
119 
120     shutdownHook.run();
121     verifyNoMoreInteractions(m_logger);
122     verify(m_agent).shutdown();
123   }
124 
125   public static class ActionListSleeperStubFactory
126     // Good grief. Some horrible javac issue means we need to fully qualify
127     // this.
128     extends net.grinder.testutility.RandomStubFactory<
129             net.grinder.util.Sleeper> {
130 
131     public interface SleepAction {
132       void sleep(long time) throws ShutdownException;
133     }
134 
135     private final SleepAction[] m_actions;
136     private int m_nextRunnable = 0;
137 
138     public ActionListSleeperStubFactory(SleepAction[] actions) {
139       super(Sleeper.class);
140       m_actions = actions;
141     }
142 
143     public void override_sleepNormal(Object proxy, long time)
144       throws ShutdownException {
145       m_actions[m_nextRunnable++].sleep(time);
146     }
147 
148     public void assertFinished() {
149       assertTrue("All actions complete", m_nextRunnable == m_actions.length);
150     }
151   }
152 }