View Javadoc

1   // Copyright (C) 2005 - 2011 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.distribution;
23  
24  import static net.grinder.testutility.FileUtilities.createRandomFile;
25  
26  import java.io.File;
27  
28  import net.grinder.communication.Address;
29  import net.grinder.console.communication.DistributionControl;
30  import net.grinder.messages.agent.CacheHighWaterMark;
31  import net.grinder.testutility.AbstractFileTestCase;
32  import net.grinder.testutility.RandomStubFactory;
33  import net.grinder.util.FileContents;
34  
35  /**
36   * Unit test for {@link FileDistributionHandlerImplementation}.
37   *
38   * @author Philip Aston
39   */
40  public class TestFileDistributionHandlerImplementation
41    extends AbstractFileTestCase {
42  
43    private final RandomStubFactory<DistributionControl>
44      m_distributionControlStubFactory =
45        RandomStubFactory.create(DistributionControl.class);
46    private final DistributionControl m_distributionControl =
47      m_distributionControlStubFactory.getStub();
48  
49    private final RandomStubFactory<AgentSet> m_agentSetStubFactory =
50      RandomStubFactory.create(AgentSet.class);
51    private final AgentSet m_agentSet = m_agentSetStubFactory.getStub();
52  
53    private final CacheParameters m_cacheParameters =
54      new CacheParametersImplementation(null, null);
55  
56    final File[] m_files = {
57      new File("a"),
58      new File("b"),
59    };
60  
61    protected void setUp() throws Exception {
62      super.setUp();
63  
64      for (int i = 0; i < m_files.length; ++i) {
65        createRandomFile(new File(getDirectory(), m_files[i].getPath()));
66      }
67    }
68  
69    public void testFileDistributionHandlerImplementation() throws Exception {
70      final FileDistributionHandlerImplementation fileDistributionHandler =
71        new FileDistributionHandlerImplementation(
72          m_cacheParameters,
73          getDirectory(),
74          m_files,
75          m_distributionControl,
76          m_agentSet);
77  
78      m_distributionControlStubFactory.assertNoMoreCalls();
79  
80      final FileDistributionHandler.Result result0 =
81        fileDistributionHandler.sendNextFile();
82  
83      assertEquals(50, result0.getProgressInCents());
84      assertEquals("a", result0.getFileName());
85  
86      m_distributionControlStubFactory.assertSuccess(
87        "clearFileCaches", Address.class);
88  
89      m_agentSetStubFactory.assertSuccess(
90        "getAddressOfOutOfDateAgents", new Long(0));
91  
92      m_distributionControlStubFactory.assertSuccess("sendFile",
93                                                   Address.class,
94                                                   FileContents.class);
95  
96      m_agentSetStubFactory.assertSuccess(
97        "getAddressOfOutOfDateAgents",
98        new Long(new File(getDirectory(), m_files[0].getPath()).lastModified()));
99  
100     m_agentSetStubFactory.assertNoMoreCalls();
101 
102     final FileDistributionHandler.Result result1 =
103       fileDistributionHandler.sendNextFile();
104 
105     assertEquals(100, result1.getProgressInCents());
106     assertEquals("b", result1.getFileName());
107 
108     m_distributionControlStubFactory.assertSuccess("sendFile",
109                                                  Address.class,
110                                                  FileContents.class);
111 
112     m_agentSetStubFactory.assertSuccess(
113       "getAddressOfOutOfDateAgents",
114       new Long(new File(getDirectory(), m_files[1].getPath()).lastModified()));
115 
116     m_agentSetStubFactory.assertNoMoreCalls();
117 
118     final FileDistributionHandler.Result result2 =
119       fileDistributionHandler.sendNextFile();
120 
121     assertNull(result2);
122 
123     m_distributionControlStubFactory.assertSuccess(
124       "setHighWaterMark", Address.class, CacheHighWaterMark.class);
125     m_distributionControlStubFactory.assertNoMoreCalls();
126 
127     m_agentSetStubFactory.assertSuccess("getAddressOfAllAgents");
128 
129     m_agentSetStubFactory.assertNoMoreCalls();
130   }
131 
132   public void testOutOfDateHandler() throws Exception {
133 
134     final FileDistributionHandlerImplementation fileDistributionHandler =
135       new FileDistributionHandlerImplementation(
136         m_cacheParameters,
137         getDirectory(),
138         m_files,
139         m_distributionControl,
140         m_agentSet);
141 
142     assertNotNull(fileDistributionHandler.sendNextFile());
143 
144     m_agentSetStubFactory.setThrows("getAddressOfOutOfDateAgents",
145                                     new AgentSet.OutOfDateException());
146 
147     assertNull(fileDistributionHandler.sendNextFile());
148   }
149 }