1   package com.atlassian.core.spool;
2   
3   import junit.framework.TestCase;
4   import org.apache.commons.io.IOUtils;
5   
6   import java.io.ByteArrayInputStream;
7   import java.io.File;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.util.Random;
11  
12  public abstract class AbstractSpoolTest extends TestCase
13  {
14      private static int counter = -1;
15  
16      public byte[] getTestData(int size)
17      {
18          byte[] data = new byte[size];
19          for (int x = 0; x < size; x++)
20              data[x] = (byte) (Math.random() * Byte.MAX_VALUE);
21          return data;
22      }
23  
24      public void verifySpool(Spool spool, byte[] data) throws IOException
25      {
26          ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
27          verifySpool(spool, dataStream);
28      }
29  
30      public void verifySpool(Spool spool, InputStream dataStream) throws IOException
31      {
32          InputStream spoolStream = spool.spool(dataStream);
33          dataStream.reset();
34          assertTrue("Stream duplicated exactly", IOUtils.contentEquals(dataStream, spoolStream));
35      }
36  
37      private static File generateFile(String prefix, String suffix, File dir)
38      {
39          if (counter == -1)
40          {
41              counter = new Random().nextInt() & 0xffff;
42          }
43          counter++;
44          return new File(dir, prefix + Integer.toString(counter) + suffix);
45      }
46  
47      public File getTestFile()
48      {
49          return generateFile("spool", "test", new File("target"));
50      }
51  
52  }