summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileBufferTest.java
blob: 54cb142ba1ce6693ab321377dc8ecca040d2f3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.simpleframework.common.buffer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.simpleframework.common.buffer.Buffer;
import org.simpleframework.common.buffer.FileBuffer;

import junit.framework.TestCase;

public class FileBufferTest extends TestCase {
   
   public void  testFileBuffer() throws Exception {
      File tempFile = File.createTempFile(FileBufferTest.class.getSimpleName(), null);
      Buffer buffer = new FileBuffer(tempFile);
      buffer.append("abcdefghijklmnopqrstuvwxyz".getBytes());
      
      Buffer alphabet = buffer.allocate();      
      alphabet.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes());
      
      Buffer digits = buffer.allocate();
      digits.append("0123456789".getBytes());
      
      expect(buffer, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".getBytes());
      expect(alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes());
      expect(digits, "0123456789".getBytes());
   }
   
   private void expect(Buffer buffer, byte[] expect) throws IOException {
      InputStream result = buffer.open();
      
      for(int i  =0; i < expect.length; i++) {
         byte octet = expect[i];
         int value = result.read();
         
         if(value < 0) {
            throw new IOException("Buffer exhausted too early");
         }
         assertEquals(octet, (byte)value);
      }
      assertEquals(-1, result.read());
   }

}