1 package com.atlassian.core.spool;
2
3 import com.mockobjects.dynamic.Mock;
4 import org.apache.commons.io.IOUtils;
5
6 import java.io.BufferedOutputStream;
7 import java.io.ByteArrayInputStream;
8 import java.io.File;
9 import java.io.FileOutputStream;
10
11 public class TestSpoolStreams extends AbstractSpoolTest
12 {
13 public void testDeferredSpoolOutputStreamInMemory() throws Exception
14 {
15 Mock mockFileFactory = new Mock(FileFactory.class);
16 mockFileFactory.expectNotCalled("createNewFile");
17
18 FileFactory fileFactory = (FileFactory) mockFileFactory.proxy();
19 DeferredSpoolFileOutputStream out = new DeferredSpoolFileOutputStream(1024, fileFactory);
20 byte[] data = getTestData(1024);
21
22 IOUtils.copy(new ByteArrayInputStream(data), out);
23
24 assertTrue(out.isInMemory());
25 if (out.getFile() != null)
26 assertFalse(out.getFile().exists());
27
28 mockFileFactory.verify();
29 }
30
31 public void testDeferredSpoolOutputStreamToFile() throws Exception
32 {
33 Mock mockFileFactory = new Mock(FileFactory.class);
34 File testSpoolFile = getTestFile();
35 mockFileFactory.expectAndReturn("createNewFile", testSpoolFile);
36
37 FileFactory fileFactory = (FileFactory) mockFileFactory.proxy();
38 DeferredSpoolFileOutputStream out = new DeferredSpoolFileOutputStream(1024, fileFactory);
39 byte[] data = getTestData(2048);
40
41 IOUtils.copy(new ByteArrayInputStream(data), out);
42 out.close();
43
44 File spoolFile = out.getFile();
45
46 assertFalse(out.isInMemory());
47 assertNotNull(spoolFile);
48 assertEquals(spoolFile, testSpoolFile);
49 assertTrue(spoolFile.exists());
50
51 IOUtils.contentEquals(new ByteArrayInputStream(data), out.getInputStream());
52
53 mockFileFactory.verify();
54 }
55
56 public void testDeferredSpoolInputStreamRemovesSpoolOnClose() throws Exception
57 {
58 byte[] data = getTestData(2048);
59 File testFile = getTestFile();
60
61 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(testFile));
62 IOUtils.write(data, bos);
63 bos.close();
64
65 SpoolFileInputStream dfis = new SpoolFileInputStream(testFile);
66 dfis.close();
67
68 assertFalse(testFile.exists());
69 }
70 }