diff options
Diffstat (limited to 'simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileBufferTest.java')
-rw-r--r-- | simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileBufferTest.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileBufferTest.java b/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileBufferTest.java new file mode 100644 index 0000000..54cb142 --- /dev/null +++ b/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileBufferTest.java @@ -0,0 +1,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()); + } + +} |