1   package com.atlassian.core.spool;
2   
3   import com.mockobjects.dynamic.Mock;
4   
5   import java.io.ByteArrayInputStream;
6   import java.io.InputStream;
7   
8   public class TestSpools extends AbstractSpoolTest
9   {
10      public void testByteArraySpool() throws Exception
11      {
12          Spool byteArraySpool = new ByteArraySpool();
13          byte[] data = getTestData(1024);
14          verifySpool(byteArraySpool, data);
15      }
16  
17      public void testBufferedFileSpool() throws Exception
18      {
19          Spool bufferedArraySpool = new BufferedFileSpool();
20          byte[] data = getTestData(1024 * 10);
21          verifySpool(bufferedArraySpool, data);
22      }
23  
24      public void testSmartSpoolImmediateOverflow() throws Exception
25      {
26          Mock mockImmediateOverflowSpool = new Mock(Spool.class);
27          Mock mockThresholdingSpool = new Mock(ThresholdingSpool.class);
28  
29          Spool immediateOverflowSpool = (Spool) mockImmediateOverflowSpool.proxy();
30          ThresholdingSpool thresholdingSpool = (ThresholdingSpool) mockThresholdingSpool.proxy();
31  
32          SmartSpool smartSpool = new SmartSpool();
33          smartSpool.setOverThresholdSpool(immediateOverflowSpool);
34          smartSpool.setThresholdingSpool(thresholdingSpool);
35  
36          byte[] data = getTestData(2049); // One byte more than we are prepared to spool in RAM
37          InputStream dataStream = new ByteArrayInputStream(data);
38  
39          mockThresholdingSpool.expectAndReturn("getThresholdBytes", new Integer(2048));
40          mockImmediateOverflowSpool.expectAndReturn("spool", dataStream, new ByteArrayInputStream(data));
41          mockThresholdingSpool.expectNotCalled("spool");
42  
43          verifySpool(smartSpool, dataStream);
44  
45          mockThresholdingSpool.verify();
46          mockImmediateOverflowSpool.verify();
47      }
48  
49      public void testSmartSpoolNoImmediateOverflow() throws Exception
50      {
51          Mock mockImmediateOverflowSpool = new Mock(Spool.class);
52          Mock mockThresholdingSpool = new Mock(ThresholdingSpool.class);
53  
54          Spool immediateOverflowSpool = (Spool) mockImmediateOverflowSpool.proxy();
55          ThresholdingSpool thresholdingSpool = (ThresholdingSpool) mockThresholdingSpool.proxy();
56  
57          SmartSpool smartSpool = new SmartSpool();
58          smartSpool.setOverThresholdSpool(immediateOverflowSpool);
59          smartSpool.setThresholdingSpool(thresholdingSpool);
60  
61          byte[] data = getTestData(2047); // One byte under RAM maximum
62          InputStream dataStream = new ByteArrayInputStream(data);
63  
64          mockThresholdingSpool.expectAndReturn("getThresholdBytes", new Integer(2048));
65          mockThresholdingSpool.expectAndReturn("spool", dataStream, new ByteArrayInputStream(data));
66          mockImmediateOverflowSpool.expectNotCalled("spool");
67  
68          verifySpool(smartSpool, dataStream);
69  
70          mockThresholdingSpool.verify();
71          mockImmediateOverflowSpool.verify();
72      }
73  
74  }