mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-03-20 00:00:03 -04:00
moving files around
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
import com.twelvemonkeys.util.CollectionUtil;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* CompoundReaderTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/CompoundReaderTestCase.java#2 $
|
||||
*/
|
||||
public class CompoundReaderTestCase extends ReaderAbstractTestCase {
|
||||
|
||||
protected Reader makeReader(String pInput) {
|
||||
// Split
|
||||
String[] input = StringUtil.toStringArray(pInput, " ");
|
||||
List<Reader> readers = new ArrayList<Reader>(input.length);
|
||||
|
||||
// Reappend spaces...
|
||||
// TODO: Add other readers
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (i != 0) {
|
||||
input[i] = " " + input[i];
|
||||
}
|
||||
readers.add(new StringReader(input[i]));
|
||||
}
|
||||
|
||||
return new CompoundReader(readers.iterator());
|
||||
}
|
||||
|
||||
public void testNullConstructor() {
|
||||
try {
|
||||
new CompoundReader(null);
|
||||
fail("Should not allow null argument");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyIteratorConstructor() throws IOException {
|
||||
Reader reader = new CompoundReader(CollectionUtil.iterator(new Reader[0]));
|
||||
assertEquals(-1, reader.read());
|
||||
}
|
||||
|
||||
public void testIteratorWithNullConstructor() throws IOException {
|
||||
try {
|
||||
new CompoundReader(CollectionUtil.iterator(new Reader[] {null}));
|
||||
fail("Should not allow null in iterator argument");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* FastByteArrayOutputStreamTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/FastByteArrayOutputStreamTestCase.java#1 $
|
||||
*/
|
||||
public class FastByteArrayOutputStreamTestCase extends OutputStreamAbstractTestCase {
|
||||
protected FastByteArrayOutputStream makeObject() {
|
||||
return new FastByteArrayOutputStream(256);
|
||||
}
|
||||
|
||||
public void testCreateInputStream() throws IOException {
|
||||
FastByteArrayOutputStream out = makeObject();
|
||||
|
||||
String hello = "Hello World";
|
||||
out.write(hello.getBytes("UTF-8"));
|
||||
|
||||
InputStream in = out.createInputStream();
|
||||
|
||||
byte[] read = FileUtil.read(in);
|
||||
|
||||
assertEquals(hello, new String(read, "UTF-8"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* FileCacheSeekableStreamTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/FileCacheSeekableStreamTestCase.java#3 $
|
||||
*/
|
||||
public class FileCacheSeekableStreamTestCase extends SeekableInputStreamAbstractTestCase {
|
||||
public FileCacheSeekableStreamTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected SeekableInputStream makeInputStream(final InputStream pStream) {
|
||||
try {
|
||||
return new FileCacheSeekableStream(pStream);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* MemoryCacheSeekableStreamTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/FileSeekableStreamTestCase.java#3 $
|
||||
*/
|
||||
public class FileSeekableStreamTestCase extends SeekableInputStreamAbstractTestCase {
|
||||
public FileSeekableStreamTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected SeekableInputStream makeInputStream(final InputStream pStream) {
|
||||
try {
|
||||
return new FileSeekableStream(createFileWithContent(pStream));
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private File createFileWithContent(final InputStream pStream) throws IOException {
|
||||
File temp = File.createTempFile("tm-io-junit", null);
|
||||
temp.deleteOnExit();
|
||||
OutputStream os = new FileOutputStream(temp);
|
||||
try {
|
||||
FileUtil.copy(pStream, os);
|
||||
}
|
||||
finally {
|
||||
os.close();
|
||||
pStream.close();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testCloseUnderlyingStream() throws IOException {
|
||||
// There is no underlying stream here...
|
||||
}
|
||||
|
||||
public void testCloseUnderlyingFile() throws IOException {
|
||||
final boolean[] closed = new boolean[1];
|
||||
|
||||
File file = createFileWithContent(new ByteArrayInputStream(makeRandomArray(256)));
|
||||
|
||||
RandomAccessFile raf = new RandomAccessFile(file, "r") {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed[0] = true;
|
||||
super.close();
|
||||
}
|
||||
};
|
||||
|
||||
FileSeekableStream stream = new FileSeekableStream(raf);
|
||||
|
||||
try {
|
||||
FileUtil.read(stream); // Read until EOF
|
||||
|
||||
assertEquals("EOF not reached (test case broken)", -1, stream.read());
|
||||
assertFalse("Underlying stream closed before close", closed[0]);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
assertTrue("Underlying stream not closed", closed[0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import com.twelvemonkeys.lang.ObjectAbstractTestCase;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* InputStreamAbstractTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/InputStreamAbstractTestCase.java#1 $
|
||||
*/
|
||||
public abstract class InputStreamAbstractTestCase extends ObjectAbstractTestCase {
|
||||
// TODO: FixMe! THIS TEST IS (WAS) COMPLETELY BROKEN...
|
||||
// It relies on the contents of the stream being a certain order byte0 == 0, byte1 == 1 etc..
|
||||
// But the subclasses don't implement this.. Need to fix.
|
||||
|
||||
final static private long SEED = 29487982745l;
|
||||
final static Random sRandom = new Random(SEED);
|
||||
|
||||
public InputStreamAbstractTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected final Object makeObject() {
|
||||
return makeInputStream();
|
||||
}
|
||||
|
||||
protected InputStream makeInputStream() {
|
||||
return makeInputStream(16);
|
||||
}
|
||||
|
||||
protected InputStream makeInputStream(int pSize) {
|
||||
byte[] bytes = makeRandomArray(pSize);
|
||||
return makeInputStream(bytes);
|
||||
}
|
||||
|
||||
protected abstract InputStream makeInputStream(byte[] pBytes);
|
||||
|
||||
protected final byte[] makeRandomArray(final int pSize) {
|
||||
byte[] bytes = new byte[pSize];
|
||||
sRandom.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
protected final byte[] makeOrderedArray(final int pSize) {
|
||||
byte[] bytes = new byte[pSize];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) i;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public void testRead() throws Exception {
|
||||
int size = 5;
|
||||
InputStream input = makeInputStream(makeOrderedArray(size));
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertEquals("Check Size [" + i + "]", (size - i), input.available());
|
||||
assertEquals("Check Value [" + i + "]", i, input.read());
|
||||
}
|
||||
assertEquals("Available after contents all read", 0, input.available());
|
||||
|
||||
// Test reading after the end of file
|
||||
try {
|
||||
int result = input.read();
|
||||
assertEquals("Wrong value read after end of file", -1, result);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not have thrown an IOException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testAvailable() throws Exception {
|
||||
InputStream input = makeInputStream(1);
|
||||
assertFalse("Unexpected EOF", input.read() < 0);
|
||||
assertEquals("Available after contents all read", 0, input.available());
|
||||
|
||||
// Check availbale is zero after End of file
|
||||
assertEquals("End of File", -1, input.read());
|
||||
assertEquals("Available after End of File", 0, input.available());
|
||||
}
|
||||
|
||||
public void testReadByteArray() throws Exception {
|
||||
byte[] bytes = new byte[10];
|
||||
byte[] data = makeOrderedArray(15);
|
||||
InputStream input = makeInputStream(data);
|
||||
|
||||
// Read into array
|
||||
int count1 = input.read(bytes);
|
||||
assertEquals("Read 1", bytes.length, count1);
|
||||
for (int i = 0; i < count1; i++) {
|
||||
assertEquals("Check Bytes 1", i, bytes[i]);
|
||||
}
|
||||
|
||||
// Read into array
|
||||
int count2 = input.read(bytes);
|
||||
assertEquals("Read 2", 5, count2);
|
||||
for (int i = 0; i < count2; i++) {
|
||||
assertEquals("Check Bytes 2", count1 + i, bytes[i]);
|
||||
}
|
||||
|
||||
// End of File
|
||||
int count3 = input.read(bytes);
|
||||
assertEquals("Read 3 (EOF)", -1, count3);
|
||||
|
||||
// Test reading after the end of file
|
||||
try {
|
||||
int result = input.read(bytes);
|
||||
assertEquals("Wrong value read after end of file", -1, result);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not have thrown an IOException: " + e.getMessage());
|
||||
}
|
||||
|
||||
// Reset
|
||||
input = makeInputStream(data);
|
||||
|
||||
// Read into array using offset & length
|
||||
int offset = 2;
|
||||
int lth = 4;
|
||||
int count5 = input.read(bytes, offset, lth);
|
||||
assertEquals("Read 5", lth, count5);
|
||||
for (int i = offset; i < lth; i++) {
|
||||
assertEquals("Check Bytes 2", i - offset, bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testEOF() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(2));
|
||||
assertEquals("Read 1", 0, input.read());
|
||||
assertEquals("Read 2", 1, input.read());
|
||||
assertEquals("Read 3", -1, input.read());
|
||||
assertEquals("Read 4", -1, input.read());
|
||||
assertEquals("Read 5", -1, input.read());
|
||||
}
|
||||
|
||||
public void testMarkResetUnsupported() throws IOException {
|
||||
InputStream input = makeInputStream(10);
|
||||
if (input.markSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
input.mark(100); // Should be a no-op
|
||||
|
||||
int read = input.read();
|
||||
assertEquals(0, read);
|
||||
|
||||
// TODO: According to InputStream#reset, it is allowed to do some
|
||||
// implementation specific reset, and still be correct...
|
||||
try {
|
||||
input.reset();
|
||||
fail("Should throw IOException");
|
||||
}
|
||||
catch (IOException e) {
|
||||
assertTrue("Wrong messge: " + e.getMessage(), e.getMessage().contains("reset"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testResetNoMark() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(10));
|
||||
|
||||
if (!input.markSupported()) {
|
||||
return; // Not supported, skip test
|
||||
}
|
||||
|
||||
int read = input.read();
|
||||
assertEquals(0, read);
|
||||
|
||||
// No mark may either throw exception, or reset to beginning of stream.
|
||||
try {
|
||||
input.reset();
|
||||
assertEquals("Re-read of reset data should be same", 0, input.read());
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertTrue("Wrong no mark IOException message", e.getMessage().contains("mark"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testMarkReset() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(25));
|
||||
|
||||
if (!input.markSupported()) {
|
||||
return; // Not supported, skip test
|
||||
}
|
||||
|
||||
int read = input.read();
|
||||
assertEquals(0, read);
|
||||
|
||||
int position = 1;
|
||||
int readlimit = 10;
|
||||
|
||||
// Mark
|
||||
input.mark(readlimit);
|
||||
|
||||
// Read further
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertEquals("Read After Mark [" + i + "]", (position + i), input.read());
|
||||
}
|
||||
|
||||
// Reset
|
||||
input.reset();
|
||||
|
||||
// Read from marked position
|
||||
for (int i = 0; i < readlimit + 1; i++) {
|
||||
assertEquals("Read After Reset [" + i + "]", (position + i), input.read());
|
||||
}
|
||||
}
|
||||
|
||||
public void testResetAfterReadLimit() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(25));
|
||||
|
||||
if (!input.markSupported()) {
|
||||
return; // Not supported, skip test
|
||||
}
|
||||
|
||||
int read = input.read();
|
||||
assertEquals(0, read);
|
||||
|
||||
int position = 1;
|
||||
int readlimit = 5;
|
||||
|
||||
// Mark
|
||||
input.mark(readlimit);
|
||||
|
||||
// Read past marked position
|
||||
for (int i = 0; i < readlimit + 1; i++) {
|
||||
assertEquals("Read After Reset [" + i + "]", (position + i), input.read());
|
||||
}
|
||||
|
||||
// Reset after read limit passed, may either throw exception, or reset to last mark
|
||||
try {
|
||||
input.reset();
|
||||
assertEquals("Re-read of reset data should be same", 1, input.read());
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertTrue("Wrong read-limit IOException message", e.getMessage().contains("mark"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testResetAfterReset() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(25));
|
||||
|
||||
if (!input.markSupported()) {
|
||||
return; // Not supported, skip test
|
||||
}
|
||||
|
||||
assertTrue("Expected to read positive value", input.read() >= 0);
|
||||
|
||||
int readlimit = 5;
|
||||
|
||||
// Mark
|
||||
input.mark(readlimit);
|
||||
int read = input.read();
|
||||
assertTrue("Expected to read positive value", read >= 0);
|
||||
|
||||
input.reset();
|
||||
assertEquals("Expected value read differes from actual", read, input.read());
|
||||
|
||||
// Reset after read limit passed, may either throw exception, or reset to last mark
|
||||
try {
|
||||
input.reset();
|
||||
assertEquals("Re-read of reset data should be same", read, input.read());
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertTrue("Wrong read-limit IOException message", e.getMessage().contains("mark"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSkip() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(10));
|
||||
|
||||
assertEquals("Unexpected value read", 0, input.read());
|
||||
assertEquals("Unexpected value read", 1, input.read());
|
||||
assertEquals("Unexpected number of bytes skipped", 5, input.skip(5));
|
||||
assertEquals("Unexpected value read", 7, input.read());
|
||||
|
||||
assertEquals("Unexpected number of bytes skipped", 2, input.skip(5)); // only 2 left to skip
|
||||
assertEquals("Unexpected value read after EOF", -1, input.read());
|
||||
|
||||
// Spec says skip might return 0 or negative after EOF...
|
||||
assertTrue("Positive value skipped after EOF", input.skip(5) <= 0); // End of file
|
||||
assertEquals("Unexpected value read after EOF", -1, input.read());
|
||||
}
|
||||
|
||||
public void testSanityOrdered() throws IOException {
|
||||
// This is to sanity check that the test itself is correct...
|
||||
byte[] bytes = makeOrderedArray(25);
|
||||
InputStream expected = new ByteArrayInputStream(bytes);
|
||||
InputStream actual = makeInputStream(bytes);
|
||||
|
||||
for (byte b : bytes) {
|
||||
assertEquals((int) b, expected.read());
|
||||
assertEquals((int) b, actual.read());
|
||||
}
|
||||
}
|
||||
|
||||
public void testSanityOrdered2() throws IOException {
|
||||
// This is to sanity check that the test itself is correct...
|
||||
byte[] bytes = makeOrderedArray(25);
|
||||
InputStream expected = new ByteArrayInputStream(bytes);
|
||||
InputStream actual = makeInputStream(bytes);
|
||||
|
||||
byte[] e = new byte[bytes.length];
|
||||
byte[] a = new byte[bytes.length];
|
||||
|
||||
assertEquals(e.length, expected.read(e, 0, e.length));
|
||||
assertEquals(a.length, actual.read(a, 0, a.length));
|
||||
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
assertEquals(bytes[i], e[i]);
|
||||
assertEquals(bytes[i], a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSanityNegative() throws IOException {
|
||||
// This is to sanity check that the test itself is correct...
|
||||
byte[] bytes = new byte[25];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) (255 - i);
|
||||
}
|
||||
InputStream expected = new ByteArrayInputStream(bytes);
|
||||
InputStream actual = makeInputStream(bytes);
|
||||
|
||||
for (byte b : bytes) {
|
||||
assertEquals(b & 0xff, expected.read());
|
||||
assertEquals(b & 0xff, actual.read());
|
||||
}
|
||||
}
|
||||
|
||||
public void testSanityNegative2() throws IOException {
|
||||
// This is to sanity check that the test itself is correct...
|
||||
byte[] bytes = new byte[25];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) (255 - i);
|
||||
}
|
||||
InputStream expected = new ByteArrayInputStream(bytes);
|
||||
InputStream actual = makeInputStream(bytes);
|
||||
|
||||
byte[] e = new byte[bytes.length];
|
||||
byte[] a = new byte[bytes.length];
|
||||
|
||||
assertEquals(e.length, expected.read(e, 0, e.length));
|
||||
assertEquals(a.length, actual.read(a, 0, a.length));
|
||||
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
assertEquals(bytes[i], e[i]);
|
||||
assertEquals(bytes[i], a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSanityRandom() throws IOException {
|
||||
// This is to sanity check that the test itself is correct...
|
||||
byte[] bytes = makeRandomArray(25);
|
||||
InputStream expected = new ByteArrayInputStream(bytes);
|
||||
InputStream actual = makeInputStream(bytes);
|
||||
|
||||
for (byte b : bytes) {
|
||||
assertEquals(b & 0xff, expected.read());
|
||||
assertEquals(b & 0xff, actual.read());
|
||||
}
|
||||
}
|
||||
|
||||
public void testSanityRandom2() throws IOException {
|
||||
// This is to sanity check that the test itself is correct...
|
||||
byte[] bytes = makeRandomArray(25);
|
||||
InputStream expected = new ByteArrayInputStream(bytes);
|
||||
InputStream actual = makeInputStream(bytes);
|
||||
|
||||
byte[] e = new byte[bytes.length];
|
||||
byte[] a = new byte[bytes.length];
|
||||
|
||||
assertEquals(e.length, expected.read(e, 0, e.length));
|
||||
assertEquals(a.length, actual.read(a, 0, a.length));
|
||||
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
assertEquals(bytes[i], e[i]);
|
||||
assertEquals(bytes[i], a[i]);
|
||||
}
|
||||
}}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* MemoryCacheSeekableStreamTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/MemoryCacheSeekableStreamTestCase.java#2 $
|
||||
*/
|
||||
public class MemoryCacheSeekableStreamTestCase extends SeekableInputStreamAbstractTestCase {
|
||||
public MemoryCacheSeekableStreamTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected SeekableInputStream makeInputStream(final InputStream pStream) {
|
||||
return new MemoryCacheSeekableStream(pStream);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import com.twelvemonkeys.lang.ObjectAbstractTestCase;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* InputStreamAbstractTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/OutputStreamAbstractTestCase.java#1 $
|
||||
*/
|
||||
public abstract class OutputStreamAbstractTestCase extends ObjectAbstractTestCase {
|
||||
protected abstract OutputStream makeObject();
|
||||
|
||||
public void testWrite() throws IOException {
|
||||
OutputStream os = makeObject();
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
os.write((byte) i);
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArray() throws IOException {
|
||||
OutputStream os = makeObject();
|
||||
|
||||
os.write(new byte[256]);
|
||||
}
|
||||
|
||||
public void testWriteByteArrayNull() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(null);
|
||||
fail("Should not accept null-argument");
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not throw IOException of null-arguemnt: " + e.getMessage());
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
fail("Should only throw NullPointerException: " + e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayOffsetLenght() throws IOException {
|
||||
byte[] input = new byte[256];
|
||||
|
||||
OutputStream os = makeObject();
|
||||
|
||||
// TODO: How to test that data is actually written!?
|
||||
for (int i = 0; i < 256; i++) {
|
||||
input[i] = (byte) i;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
os.write(input, i, 256 - i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
os.write(input, i * 64, 64);
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayZeroLenght() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(new byte[1], 0, 0);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("Should not throw Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayOffsetLenghtNull() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(null, 5, 10);
|
||||
fail("Should not accept null-argument");
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not throw IOException of null-arguemnt: " + e.getMessage());
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
fail("Should only throw NullPointerException: " + e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayNegativeOffset() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(new byte[5], -3, 5);
|
||||
fail("Should not accept negative offset");
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not throw IOException negative offset: " + e.getMessage());
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
fail("Should only throw IndexOutOfBoundsException: " + e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayNegativeLength() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(new byte[5], 2, -5);
|
||||
fail("Should not accept negative length");
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not throw IOException negative length: " + e.getMessage());
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
fail("Should only throw IndexOutOfBoundsException: " + e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayOffsetOutOfBounds() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(new byte[5], 5, 1);
|
||||
fail("Should not accept offset out of bounds");
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not throw IOException offset out of bounds: " + e.getMessage());
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
fail("Should only throw IndexOutOfBoundsException: " + e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteByteArrayLengthOutOfBounds() {
|
||||
OutputStream os = makeObject();
|
||||
try {
|
||||
os.write(new byte[5], 1, 5);
|
||||
fail("Should not accept length out of bounds");
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Should not throw IOException length out of bounds: " + e.getMessage());
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
fail("Should only throw IndexOutOfBoundsException: " + e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testFlush() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
public void testClose() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
public void testWriteAfterClose() throws IOException {
|
||||
OutputStream os = makeObject();
|
||||
|
||||
os.close();
|
||||
|
||||
boolean success = false;
|
||||
try {
|
||||
os.write(0);
|
||||
success = true;
|
||||
// TODO: Not all streams throw exception! (ByteArrayOutputStream)
|
||||
//fail("Write after close");
|
||||
}
|
||||
catch (IOException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
os.write(new byte[16]);
|
||||
// TODO: Not all streams throw exception! (ByteArrayOutputStream)
|
||||
//fail("Write after close");
|
||||
if (!success) {
|
||||
fail("Inconsistent write(int)/write(byte[]) after close");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
if (success) {
|
||||
fail("Inconsistent write(int)/write(byte[]) after close");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testFlushAfterClose() throws IOException {
|
||||
OutputStream os = makeObject();
|
||||
|
||||
os.close();
|
||||
|
||||
try {
|
||||
os.flush();
|
||||
// TODO: Not all streams throw exception! (ByteArrayOutputStream)
|
||||
//fail("Flush after close");
|
||||
try {
|
||||
os.write(0);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Inconsistent write/flush after close");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testCloseAfterClose() throws IOException {
|
||||
OutputStream os = makeObject();
|
||||
|
||||
os.close();
|
||||
|
||||
try {
|
||||
os.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Close after close, failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
215
common/common-io/src/test/java/com/twelvemonkeys/io/ReaderAbstractTestCase.java
Executable file
215
common/common-io/src/test/java/com/twelvemonkeys/io/ReaderAbstractTestCase.java
Executable file
@@ -0,0 +1,215 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import com.twelvemonkeys.lang.ObjectAbstractTestCase;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* ReaderAbstractTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/ReaderAbstractTestCase.java#1 $
|
||||
*/
|
||||
public abstract class ReaderAbstractTestCase extends ObjectAbstractTestCase {
|
||||
|
||||
// Kindly provided by lipsum.org :-)
|
||||
protected final String mInput =
|
||||
"Cras tincidunt euismod tellus. Aenean a odio. " +
|
||||
"Aenean metus. Sed tristique est non purus. Class aptent " +
|
||||
"taciti sociosqu ad litora torquent per conubia nostra, per " +
|
||||
"inceptos hymenaeos. Fusce vulputate dolor non mauris. " +
|
||||
"Nullam nunc massa, pretium quis, ultricies a, varius quis, " +
|
||||
"neque. Nam id nulla eu ante malesuada fermentum. Sed " +
|
||||
"vulputate purus eget magna. Sed mollis. Curabitur enim " +
|
||||
"diam, faucibus ac, hendrerit eu, consequat nec, augue.";
|
||||
|
||||
protected final Object makeObject() {
|
||||
return makeReader();
|
||||
}
|
||||
|
||||
protected Reader makeReader() {
|
||||
return makeReader(mInput);
|
||||
}
|
||||
|
||||
protected abstract Reader makeReader(String pInput);
|
||||
|
||||
public void testRead() throws IOException {
|
||||
Reader reader = makeReader();
|
||||
|
||||
int count = 0;
|
||||
int ch;
|
||||
StringBuilder buffer = new StringBuilder(mInput.length());
|
||||
while ((ch = reader.read()) > 0) {
|
||||
count++;
|
||||
buffer.append((char) ch);
|
||||
}
|
||||
|
||||
assertEquals(mInput.length(), count);
|
||||
assertEquals(mInput, buffer.toString());
|
||||
}
|
||||
|
||||
public void testReadBuffer() throws IOException {
|
||||
Reader reader = makeReader();
|
||||
|
||||
char[] chars = new char[mInput.length()];
|
||||
StringBuilder buffer = new StringBuilder(mInput.length());
|
||||
|
||||
int count;
|
||||
int offset = 0;
|
||||
int lenght = chars.length;
|
||||
while ((count = reader.read(chars, offset, lenght)) > 0) {
|
||||
buffer.append(chars, offset, count);
|
||||
offset += count;
|
||||
lenght -= count;
|
||||
}
|
||||
|
||||
assertEquals(mInput, buffer.toString());
|
||||
assertEquals(mInput, new String(chars));
|
||||
}
|
||||
|
||||
public void testSkipToEnd() throws IOException {
|
||||
Reader reader = makeReader();
|
||||
|
||||
int toSkip = mInput.length();
|
||||
while (toSkip > 0) {
|
||||
long skipped = reader.skip(toSkip);
|
||||
assertFalse("Skipped < 0", skipped < 0);
|
||||
toSkip -= skipped;
|
||||
}
|
||||
|
||||
assertEquals(0, toSkip);
|
||||
}
|
||||
|
||||
public void testSkipToEndAndRead() throws IOException {
|
||||
Reader reader = makeReader();
|
||||
|
||||
int toSkip = mInput.length();
|
||||
while (toSkip > 0) {
|
||||
toSkip -= reader.skip(toSkip);
|
||||
}
|
||||
|
||||
assertEquals(reader.read(), -1);
|
||||
}
|
||||
|
||||
// TODO: It's possible to support reset and not mark (resets to beginning of stream, for example)
|
||||
public void testResetMarkSupported() throws IOException {
|
||||
Reader reader = makeReader();
|
||||
|
||||
if (reader.markSupported()) {
|
||||
// Mark at 0
|
||||
reader.mark(mInput.length() / 4);
|
||||
|
||||
// Read one char
|
||||
char ch = (char) reader.read();
|
||||
reader.reset();
|
||||
assertEquals(ch, (char) reader.read());
|
||||
reader.reset();
|
||||
|
||||
// Read from start
|
||||
StringBuilder first = new StringBuilder(mInput.length() / 4);
|
||||
for (int i = 0; i < mInput.length() / 4; i++) {
|
||||
first.append((char) reader.read());
|
||||
}
|
||||
|
||||
reader.reset(); // 0
|
||||
|
||||
StringBuilder second = new StringBuilder(mInput.length() / 4);
|
||||
for (int i = 0; i < mInput.length() / 4; i++) {
|
||||
second.append((char) reader.read());
|
||||
}
|
||||
|
||||
assertEquals(first.toString(), second.toString());
|
||||
|
||||
// Mark at 1/4
|
||||
reader.mark(mInput.length() / 4);
|
||||
|
||||
// Read from 1/4
|
||||
first = new StringBuilder(mInput.length() / 4);
|
||||
for (int i = 0; i < mInput.length() / 4; i++) {
|
||||
first.append((char) reader.read());
|
||||
}
|
||||
|
||||
reader.reset(); // 1/4
|
||||
|
||||
second = new StringBuilder(mInput.length() / 4);
|
||||
for (int i = 0; i < mInput.length() / 4; i++) {
|
||||
second.append((char) reader.read());
|
||||
}
|
||||
|
||||
assertEquals(first.toString(), second.toString());
|
||||
|
||||
// Read past limit
|
||||
reader.read();
|
||||
|
||||
// This may or may not fail, depending on the stream
|
||||
try {
|
||||
reader.reset();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
assertNotNull(ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testResetMarkNotSupported() throws IOException {
|
||||
Reader reader = makeReader();
|
||||
|
||||
if (!reader.markSupported()) {
|
||||
try {
|
||||
reader.mark(mInput.length());
|
||||
fail("Mark set, while markSupprted is false");
|
||||
}
|
||||
catch (IOException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
// Read one char
|
||||
char ch = (char) reader.read();
|
||||
try {
|
||||
reader.reset();
|
||||
assertEquals(ch, (char) reader.read());
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
assertNotNull(ioe.getMessage());
|
||||
}
|
||||
|
||||
// Read from start
|
||||
StringBuilder first = new StringBuilder(mInput.length() / 4);
|
||||
for (int i = 0; i < mInput.length() / 4; i++) {
|
||||
first.append((char) reader.read());
|
||||
}
|
||||
|
||||
try {
|
||||
reader.reset(); // 0
|
||||
|
||||
StringBuilder second = new StringBuilder(mInput.length() / 4);
|
||||
for (int i = 0; i < mInput.length() / 4; i++) {
|
||||
second.append((char) reader.read());
|
||||
}
|
||||
|
||||
assertEquals(first.toString(), second.toString());
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
assertNotNull(ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testReadAfterClose() throws IOException {
|
||||
Reader reader = makeReader("foo bar");
|
||||
|
||||
reader.close();
|
||||
|
||||
try {
|
||||
reader.read();
|
||||
fail("Should not allow read after close");
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
assertNotNull(ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* SeekableAbstractTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/SeekableAbstractTestCase.java#1 $
|
||||
*/
|
||||
public abstract class SeekableAbstractTestCase extends TestCase implements SeekableInterfaceTest {
|
||||
|
||||
protected abstract Seekable createSeekable();
|
||||
|
||||
public void testFail() {
|
||||
fail();
|
||||
}
|
||||
|
||||
public void testSeekable() {
|
||||
assertTrue(createSeekable() instanceof Seekable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,480 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* SeekableInputStreamAbstractTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/SeekableInputStreamAbstractTestCase.java#4 $
|
||||
*/
|
||||
public abstract class SeekableInputStreamAbstractTestCase extends InputStreamAbstractTestCase implements SeekableInterfaceTest {
|
||||
|
||||
public SeekableInputStreamAbstractTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
//// TODO: Figure out a better way of creating interface tests without duplicating code
|
||||
final SeekableAbstractTestCase mSeekableTestCase = new SeekableAbstractTestCase() {
|
||||
protected Seekable createSeekable() {
|
||||
return makeInputStream();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected SeekableInputStream makeInputStream() {
|
||||
return (SeekableInputStream) super.makeInputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SeekableInputStream makeInputStream(final int pSize) {
|
||||
return (SeekableInputStream) super.makeInputStream(pSize);
|
||||
}
|
||||
|
||||
protected SeekableInputStream makeInputStream(byte[] pBytes) {
|
||||
return makeInputStream(new ByteArrayInputStream(pBytes));
|
||||
}
|
||||
|
||||
protected abstract SeekableInputStream makeInputStream(InputStream pStream);
|
||||
|
||||
@Override
|
||||
public void testResetAfterReset() throws Exception {
|
||||
InputStream input = makeInputStream(makeOrderedArray(25));
|
||||
|
||||
if (!input.markSupported()) {
|
||||
return; // Not supported, skip test
|
||||
}
|
||||
|
||||
assertTrue("Expected to read positive value", input.read() >= 0);
|
||||
|
||||
int readlimit = 5;
|
||||
|
||||
// Mark
|
||||
input.mark(readlimit);
|
||||
int read = input.read();
|
||||
assertTrue("Expected to read positive value", read >= 0);
|
||||
|
||||
input.reset();
|
||||
assertEquals("Expected value read differes from actual", read, input.read());
|
||||
|
||||
// Reset after read limit passed, may either throw exception, or reset to last mark
|
||||
try {
|
||||
input.reset();
|
||||
assertEquals("Re-read of reset data should be first", 0, input.read());
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertTrue("Wrong read-limit IOException message", e.getMessage().contains("mark"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSeekable() {
|
||||
mSeekableTestCase.testSeekable();
|
||||
}
|
||||
|
||||
public void testFlushBeyondCurrentPos() throws Exception {
|
||||
SeekableInputStream seekable = makeInputStream(20);
|
||||
|
||||
int pos = 10;
|
||||
try {
|
||||
seekable.flushBefore(pos);
|
||||
fail("Flush beyond current position should throw IndexOutOfBoundsException");
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
public void testSeek() throws Exception {
|
||||
SeekableInputStream seekable = makeInputStream(55);
|
||||
int pos = 37;
|
||||
|
||||
seekable.seek(pos);
|
||||
long streamPos = seekable.getStreamPosition();
|
||||
assertEquals("Stream positon should match seeked position", pos, streamPos);
|
||||
}
|
||||
|
||||
public void testSeekFlush() throws Exception {
|
||||
SeekableInputStream seekable = makeInputStream(133);
|
||||
int pos = 45;
|
||||
seekable.seek(pos);
|
||||
seekable.flushBefore(pos);
|
||||
long flushedPos = seekable.getFlushedPosition();
|
||||
assertEquals("Flushed positon should match position", pos, flushedPos);
|
||||
|
||||
try {
|
||||
seekable.seek(pos - 1);
|
||||
fail("Read before flushed position succeeded");
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
public void testMarkFlushReset() throws Exception {
|
||||
SeekableInputStream seekable = makeInputStream(77);
|
||||
|
||||
seekable.mark();
|
||||
|
||||
int position = 55;
|
||||
seekable.seek(position);
|
||||
seekable.flushBefore(position);
|
||||
|
||||
try {
|
||||
seekable.reset();
|
||||
fail("Reset before flushed position succeeded");
|
||||
}
|
||||
catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
assertEquals(position, seekable.getStreamPosition());
|
||||
}
|
||||
|
||||
public void testSeekSkipRead() throws Exception {
|
||||
SeekableInputStream seekable = makeInputStream(133);
|
||||
int pos = 45;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
seekable.seek(pos);
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
seekable.skip(i);
|
||||
byte[] bytes = FileUtil.read(seekable);
|
||||
assertEquals(133, seekable.getStreamPosition());
|
||||
assertEquals(133 - 45- i, bytes.length);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSeekSkip(SeekableInputStream pSeekable, String pStr) throws IOException {
|
||||
System.out.println();
|
||||
pSeekable.seek(pStr.length());
|
||||
FileUtil.read(pSeekable);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
byte[] bytes = FileUtil.read(pSeekable);
|
||||
int len = bytes.length;
|
||||
if (len != 0) {
|
||||
System.err.println("Error in buffer length after full read...");
|
||||
System.err.println("len: " + len);
|
||||
System.err.println("bytes: \"" + new String(bytes) + "\"");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pSeekable.seek(0);
|
||||
int skip = i * 3;
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
pSeekable.skip(skip);
|
||||
String str = new String(FileUtil.read(pSeekable));
|
||||
System.out.println(str);
|
||||
if (str.length() != pStr.length() - skip) {
|
||||
throw new Error("Error in buffer length after skip");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("seek/skip ok!");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
protected static void markReset(SeekableInputStream pSeekable) throws IOException {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pSeekable.mark();
|
||||
System.out.println(new String(FileUtil.read(pSeekable)));
|
||||
pSeekable.reset();
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("mark/reset ok!");
|
||||
}
|
||||
|
||||
protected static void timeRead(SeekableInputStream pSeekable) throws IOException {
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
pSeekable.mark();
|
||||
FileUtil.read(pSeekable);
|
||||
pSeekable.reset();
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
final int times = 200000;
|
||||
for (int i = 0; i < times; i++) {
|
||||
pSeekable.mark();
|
||||
FileUtil.read(pSeekable);
|
||||
pSeekable.reset();
|
||||
}
|
||||
long time = System.currentTimeMillis() - start;
|
||||
|
||||
System.out.println("Time; " + time + "ms (" + (time / (float) times) + "ms/inv)");
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// Test code below...
|
||||
protected final static String STR = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce massa orci, adipiscing vel, dapibus et, vulputate tristique, tortor. Quisque sodales. Mauris varius turpis et pede. Nam ac dolor vel diam condimentum elementum. Pellentesque eget tellus. Praesent magna. Sed fringilla. Proin ullamcorper tincidunt ante. Fusce dapibus nibh nec dolor. Etiam erat. Nullam dignissim laoreet nibh. Maecenas scelerisque. Pellentesque in quam. Maecenas sollicitudin, magna nec imperdiet facilisis, metus quam tristique ipsum, vitae consequat massa purus eget leo. Nulla ipsum. Proin non purus eget tellus lobortis iaculis. In lorem justo, posuere id, vulputate at, adipiscing ut, nisl. Nunc dui erat, tincidunt ac, interdum quis, rutrum et, libero. Etiam lectus dui, viverra sit amet, elementum ut, malesuada sed, massa. Vestibulum mi nulla, sodales vel, vestibulum sed, congue blandit, velit.";
|
||||
|
||||
protected static void flushSeek(SeekableInputStream pSeekable, String pStr) throws IOException {
|
||||
pSeekable.seek(0);
|
||||
pSeekable.mark();
|
||||
int pos = pStr.length() / 2;
|
||||
try {
|
||||
pSeekable.flushBefore(pos);
|
||||
System.err.println("Error in flush/seek");
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
// Ignore
|
||||
}
|
||||
pSeekable.seek(pos);
|
||||
long streamPos = pSeekable.getStreamPosition();
|
||||
if (streamPos != pos) {
|
||||
System.err.println("Streampos not equal seeked pos");
|
||||
}
|
||||
|
||||
pSeekable.flushBefore(pos);
|
||||
long flushedPos = pSeekable.getFlushedPosition();
|
||||
if (flushedPos != pos) {
|
||||
System.err.println("flushedpos not equal set flushed pos");
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pSeekable.seek(pos);
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
pSeekable.skip(i);
|
||||
System.out.println(new String(FileUtil.read(pSeekable)));
|
||||
}
|
||||
|
||||
try {
|
||||
pSeekable.seek(pos - 1);
|
||||
System.err.println("Error in flush/seek");
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
// Ignore
|
||||
}
|
||||
try {
|
||||
pSeekable.reset();
|
||||
System.err.println("Error in flush/seek");
|
||||
}
|
||||
catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("flush/seek ok!");
|
||||
}
|
||||
|
||||
protected static void seekSkip(SeekableInputStream pSeekable, String pStr) throws IOException {
|
||||
System.out.println();
|
||||
pSeekable.seek(pStr.length());
|
||||
FileUtil.read(pSeekable);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
byte[] bytes = FileUtil.read(pSeekable);
|
||||
int len = bytes.length;
|
||||
if (len != 0) {
|
||||
System.err.println("Error in buffer length after full read...");
|
||||
System.err.println("len: " + len);
|
||||
System.err.println("bytes: \"" + new String(bytes) + "\"");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pSeekable.seek(0);
|
||||
int skip = i * 3;
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
pSeekable.skip(skip);
|
||||
String str = new String(FileUtil.read(pSeekable));
|
||||
System.out.println(str);
|
||||
if (str.length() != pStr.length() - skip) {
|
||||
throw new Error("Error in buffer length after skip");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("seek/skip ok!");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
protected static void markReset(SeekableInputStream pSeekable) throws IOException {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pSeekable.mark();
|
||||
System.out.println(new String(FileUtil.read(pSeekable)));
|
||||
pSeekable.reset();
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("mark/reset ok!");
|
||||
}
|
||||
|
||||
protected static void timeRead(SeekableInputStream pSeekable) throws IOException {
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
pSeekable.mark();
|
||||
FileUtil.read(pSeekable);
|
||||
pSeekable.reset();
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
final int times = 200000;
|
||||
for (int i = 0; i < times; i++) {
|
||||
pSeekable.mark();
|
||||
FileUtil.read(pSeekable);
|
||||
pSeekable.reset();
|
||||
}
|
||||
long time = System.currentTimeMillis() - start;
|
||||
|
||||
System.out.println("Time; " + time + "ms (" + (time / (float) times) + "ms/inv)");
|
||||
}
|
||||
*/
|
||||
|
||||
public void testReadResetReadDirectBufferBug() throws IOException {
|
||||
// Make sure we use the exact size of the buffer
|
||||
final int size = 1024;
|
||||
|
||||
// Fill bytes
|
||||
byte[] bytes = new byte[size * 2];
|
||||
sRandom.nextBytes(bytes);
|
||||
|
||||
// Create wrapper stream
|
||||
SeekableInputStream stream = makeInputStream(bytes);
|
||||
|
||||
// Read to fill the buffer, then reset
|
||||
int val;
|
||||
|
||||
val = stream.read();
|
||||
assertFalse("Unexepected EOF", val == -1);
|
||||
val = stream.read();
|
||||
assertFalse("Unexepected EOF", val == -1);
|
||||
val = stream.read();
|
||||
assertFalse("Unexepected EOF", val == -1);
|
||||
val = stream.read();
|
||||
assertFalse("Unexepected EOF", val == -1);
|
||||
|
||||
stream.seek(0);
|
||||
|
||||
// Read fully and compare
|
||||
byte[] result = new byte[size];
|
||||
|
||||
readFully(stream, result);
|
||||
assertTrue(rangeEquals(bytes, 0, result, 0, size));
|
||||
|
||||
readFully(stream, result);
|
||||
assertTrue(rangeEquals(bytes, size, result, 0, size));
|
||||
}
|
||||
|
||||
public void testReadAllByteValuesRegression() throws IOException {
|
||||
final int size = 128;
|
||||
|
||||
// Fill bytes
|
||||
byte[] bytes = new byte[256];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) i;
|
||||
}
|
||||
|
||||
// Create wrapper stream
|
||||
SeekableInputStream stream = makeInputStream(bytes);
|
||||
|
||||
// Fill buffer
|
||||
byte[] buffer = new byte[size];
|
||||
while (stream.read(buffer) >= 0) {
|
||||
}
|
||||
|
||||
stream.seek(0);
|
||||
for (int i = 0; i < bytes.length; i += 2) {
|
||||
assertEquals("Wrong stream position", i, stream.getStreamPosition());
|
||||
int count = stream.read(buffer, 0, 2);
|
||||
assertEquals(2, count);
|
||||
assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], buffer[0]);
|
||||
assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i + 1], buffer[1]);
|
||||
}
|
||||
|
||||
stream.seek(0);
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
assertEquals("Wrong stream position", i, stream.getStreamPosition());
|
||||
int actual = stream.read();
|
||||
assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i] & 0xff, actual);
|
||||
assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], (byte) actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testCloseUnderlyingStream() throws IOException {
|
||||
final boolean[] closed = new boolean[1];
|
||||
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed[0] = true;
|
||||
super.close();
|
||||
}
|
||||
};
|
||||
|
||||
SeekableInputStream stream = makeInputStream(input);
|
||||
|
||||
try {
|
||||
FileUtil.read(stream); // Read until EOF
|
||||
|
||||
assertEquals("EOF not reached (test case broken)", -1, stream.read());
|
||||
assertFalse("Underlying stream closed before close", closed[0]);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
assertTrue("Underlying stream not closed", closed[0]);
|
||||
|
||||
}
|
||||
|
||||
private void readFully(InputStream pStream, byte[] pResult) throws IOException {
|
||||
int pos = 0;
|
||||
while (pos < pResult.length) {
|
||||
int read = pStream.read(pResult, pos, pResult.length - pos);
|
||||
if (read == -1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
pos += read;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test two arrays for range equality. That is, they contain the same elements for some specified range.
|
||||
*
|
||||
* @param pFirst one array to test for equality
|
||||
* @param pFirstOffset the offset into the first array to start testing for equality
|
||||
* @param pSecond the other array to test for equality
|
||||
* @param pSecondOffset the offset into the second array to start testing for equality
|
||||
* @param pLength the length of the range to check for equality
|
||||
*
|
||||
* @return {@code true} if both arrays are non-{@code null}
|
||||
* and have at least {@code offset + pLength} elements
|
||||
* and all elements in the range from the first array is equal to the elements from the second array,
|
||||
* or if {@code pFirst == pSecond} (including both arrays being {@code null})
|
||||
* and {@code pFirstOffset == pSecondOffset}.
|
||||
* Otherwise {@code false}.
|
||||
*/
|
||||
static boolean rangeEquals(byte[] pFirst, int pFirstOffset, byte[] pSecond, int pSecondOffset, int pLength) {
|
||||
if (pFirst == pSecond && pFirstOffset == pSecondOffset) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pFirst == null || pSecond == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pFirst.length < pFirstOffset + pLength || pSecond.length < pSecondOffset + pLength) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < pLength; i++) {
|
||||
if (pFirst[pFirstOffset + i] != pSecond[pSecondOffset + i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
/**
|
||||
* SeekableInterfaceTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/SeekableInterfaceTest.java#1 $
|
||||
*/
|
||||
public interface SeekableInterfaceTest {
|
||||
void testSeekable();
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.twelvemonkeys.io;
|
||||
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* StringArrayReaderTestCase
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/StringArrayReaderTestCase.java#1 $
|
||||
*/
|
||||
public class StringArrayReaderTestCase extends ReaderAbstractTestCase {
|
||||
|
||||
protected Reader makeReader(String pInput) {
|
||||
// Split
|
||||
String[] input = StringUtil.toStringArray(pInput, " ");
|
||||
// Reappend spaces...
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (i != 0) {
|
||||
input[i] = " " + input[i];
|
||||
}
|
||||
}
|
||||
|
||||
return new StringArrayReader(input);
|
||||
}
|
||||
|
||||
public void testNullConstructor() {
|
||||
try {
|
||||
new StringArrayReader(null);
|
||||
fail("Should not allow null argument");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyArrayConstructor() throws IOException {
|
||||
Reader reader = new StringArrayReader(new String[0]);
|
||||
assertEquals(-1, reader.read());
|
||||
}
|
||||
|
||||
public void testEmptyStringConstructor() throws IOException {
|
||||
Reader reader = new StringArrayReader(new String[] {""});
|
||||
assertEquals(-1, reader.read());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.twelvemonkeys.io.enc;
|
||||
|
||||
|
||||
import com.twelvemonkeys.io.FileUtil;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Base64DecoderTest
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/enc/Base64DecoderTestCase.java#1 $
|
||||
*/
|
||||
public class Base64DecoderTestCase extends DecoderAbstractTestCase {
|
||||
|
||||
public Decoder createDecoder() {
|
||||
return new Base64Decoder();
|
||||
}
|
||||
|
||||
public Encoder createCompatibleEncoder() {
|
||||
return new Base64Encoder();
|
||||
}
|
||||
|
||||
public void testEmptyDecode2() throws IOException {
|
||||
String data = "";
|
||||
|
||||
InputStream in = new DecoderStream(new ByteArrayInputStream(data.getBytes()), createDecoder());
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
FileUtil.copy(in, bytes);
|
||||
|
||||
assertEquals("Strings does not match", "", new String(bytes.toByteArray()));
|
||||
}
|
||||
|
||||
public void testShortDecode() throws IOException {
|
||||
String data = "dGVzdA==";
|
||||
|
||||
InputStream in = new DecoderStream(new ByteArrayInputStream(data.getBytes()), createDecoder());
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
FileUtil.copy(in, bytes);
|
||||
|
||||
assertEquals("Strings does not match", "test", new String(bytes.toByteArray()));
|
||||
}
|
||||
|
||||
public void testLongDecode() throws IOException {
|
||||
String data = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVlciBhZGlwaXNjaW5nIGVsaXQuIEZ1" +
|
||||
"c2NlIGVzdC4gTW9yYmkgbHVjdHVzIGNvbnNlY3RldHVlciBqdXN0by4gVml2YW11cyBkYXBpYnVzIGxh" +
|
||||
"b3JlZXQgcHVydXMuIE51bmMgdml2ZXJyYSBkaWN0dW0gbmlzbC4gSW50ZWdlciB1bGxhbWNvcnBlciwg" +
|
||||
"bmlzaSBpbiBkaWN0dW0gYW1ldC4=";
|
||||
|
||||
InputStream in = new DecoderStream(new ByteArrayInputStream(data.getBytes()), createDecoder());
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
FileUtil.copy(in, bytes);
|
||||
|
||||
assertEquals("Strings does not match",
|
||||
"Lorem ipsum dolor sit amet, consectetuer adipiscing " +
|
||||
"elit. Fusce est. Morbi luctus consectetuer justo. Vivamus " +
|
||||
"dapibus laoreet purus. Nunc viverra dictum nisl. Integer " +
|
||||
"ullamcorper, nisi in dictum amet.",
|
||||
new String(bytes.toByteArray()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.twelvemonkeys.io.enc;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Base64EncoderTest
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/enc/Base64EncoderTestCase.java#1 $
|
||||
*/
|
||||
public class Base64EncoderTestCase extends EncoderAbstractTestCase {
|
||||
|
||||
protected Encoder createEncoder() {
|
||||
return new Base64Encoder();
|
||||
}
|
||||
|
||||
protected Decoder createCompatibleDecoder() {
|
||||
return new Base64Decoder();
|
||||
}
|
||||
|
||||
public void testNegativeEncode() throws IOException {
|
||||
Encoder encoder = createEncoder();
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
try {
|
||||
encoder.encode(bytes, new byte[1], 2, 1);
|
||||
fail("wrong index should throw IndexOutOfBoundsException");
|
||||
}
|
||||
catch (IndexOutOfBoundsException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyEncode() throws IOException {
|
||||
String data = "";
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
OutputStream out = new EncoderStream(bytes, createEncoder(), true);
|
||||
out.write(data.getBytes());
|
||||
|
||||
assertEquals("Strings does not match", "", new String(bytes.toByteArray()));
|
||||
}
|
||||
|
||||
public void testShortEncode() throws IOException {
|
||||
String data = "test";
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
OutputStream out = new EncoderStream(bytes, createEncoder(), true);
|
||||
out.write(data.getBytes());
|
||||
|
||||
assertEquals("Strings does not match", "dGVzdA==", new String(bytes.toByteArray()));
|
||||
}
|
||||
|
||||
public void testLongEncode() throws IOException {
|
||||
String data = "Lorem ipsum dolor sit amet, consectetuer adipiscing " +
|
||||
"elit. Fusce est. Morbi luctus consectetuer justo. Vivamus " +
|
||||
"dapibus laoreet purus. Nunc viverra dictum nisl. Integer " +
|
||||
"ullamcorper, nisi in dictum amet.";
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
OutputStream out = new EncoderStream(bytes, createEncoder(), true);
|
||||
out.write(data.getBytes());
|
||||
|
||||
assertEquals("Strings does not match",
|
||||
"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVlciBhZGlwaXNjaW5nIGVsaXQuIEZ1" +
|
||||
"c2NlIGVzdC4gTW9yYmkgbHVjdHVzIGNvbnNlY3RldHVlciBqdXN0by4gVml2YW11cyBkYXBpYnVzIGxh" +
|
||||
"b3JlZXQgcHVydXMuIE51bmMgdml2ZXJyYSBkaWN0dW0gbmlzbC4gSW50ZWdlciB1bGxhbWNvcnBlciwg" +
|
||||
"bmlzaSBpbiBkaWN0dW0gYW1ldC4=",
|
||||
new String(bytes.toByteArray()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.twelvemonkeys.io.enc;
|
||||
|
||||
import com.twelvemonkeys.io.FileUtil;
|
||||
import com.twelvemonkeys.lang.ObjectAbstractTestCase;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* AbstractDecoderTest
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/enc/DecoderAbstractTestCase.java#1 $
|
||||
*/
|
||||
public abstract class DecoderAbstractTestCase extends ObjectAbstractTestCase {
|
||||
|
||||
public abstract Decoder createDecoder();
|
||||
public abstract Encoder createCompatibleEncoder();
|
||||
|
||||
protected Object makeObject() {
|
||||
return createDecoder();
|
||||
}
|
||||
|
||||
public final void testNullDecode() throws IOException {
|
||||
Decoder decoder = createDecoder();
|
||||
ByteArrayInputStream bytes = new ByteArrayInputStream(new byte[20]);
|
||||
|
||||
try {
|
||||
decoder.decode(bytes, null);
|
||||
fail("null should throw NullPointerException");
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public final void testEmptyDecode() throws IOException {
|
||||
Decoder decoder = createDecoder();
|
||||
ByteArrayInputStream bytes = new ByteArrayInputStream(new byte[0]);
|
||||
|
||||
try {
|
||||
int count = decoder.decode(bytes, new byte[2]);
|
||||
assertEquals("Should not be able to read any bytes", 0, count);
|
||||
}
|
||||
catch (EOFException allowed) {
|
||||
// Okay
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] createData(int pLength) throws Exception {
|
||||
byte[] bytes = new byte[pLength];
|
||||
EncoderAbstractTestCase.RANDOM.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private void runStreamTest(int pLength) throws Exception {
|
||||
byte[] data = createData(pLength);
|
||||
|
||||
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
|
||||
OutputStream out = new EncoderStream(outBytes, createCompatibleEncoder(), true);
|
||||
out.write(data);
|
||||
out.close();
|
||||
byte[] encoded = outBytes.toByteArray();
|
||||
|
||||
byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()));
|
||||
assertTrue(Arrays.equals(data, decoded));
|
||||
|
||||
InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder());
|
||||
outBytes = new ByteArrayOutputStream();
|
||||
/*
|
||||
byte[] buffer = new byte[3];
|
||||
for (int n = in.read(buffer); n > 0; n = in.read(buffer)) {
|
||||
outBytes.write(buffer, 0, n);
|
||||
}
|
||||
*/
|
||||
FileUtil.copy(in, outBytes);
|
||||
|
||||
outBytes.close();
|
||||
in.close();
|
||||
decoded = outBytes.toByteArray();
|
||||
assertTrue(Arrays.equals(data, decoded));
|
||||
}
|
||||
|
||||
public final void testStreams() throws Exception {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
try {
|
||||
runStreamTest(i);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 100; i < 2000; i += 250) {
|
||||
try {
|
||||
runStreamTest(i);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 2000; i < 80000; i += 1000) {
|
||||
try {
|
||||
runStreamTest(i);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.twelvemonkeys.io.enc;
|
||||
|
||||
import com.twelvemonkeys.io.FileUtil;
|
||||
import com.twelvemonkeys.lang.ObjectAbstractTestCase;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* AbstractEncoderTest
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/enc/EncoderAbstractTestCase.java#1 $
|
||||
*/
|
||||
public abstract class EncoderAbstractTestCase extends ObjectAbstractTestCase {
|
||||
// Use seed to make sure we create same number all the time
|
||||
static final long SEED = 12345678;
|
||||
static final Random RANDOM = new Random(SEED);
|
||||
|
||||
protected abstract Encoder createEncoder();
|
||||
protected abstract Decoder createCompatibleDecoder();
|
||||
|
||||
protected Object makeObject() {
|
||||
return createEncoder();
|
||||
}
|
||||
|
||||
public final void testNullEncode() throws IOException {
|
||||
Encoder encoder = createEncoder();
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
try {
|
||||
encoder.encode(bytes, null, 0, 1);
|
||||
fail("null should throw NullPointerException");
|
||||
}
|
||||
catch (NullPointerException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] createData(final int pLength) throws Exception {
|
||||
byte[] bytes = new byte[pLength];
|
||||
RANDOM.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private void runStreamTest(final int pLength) throws Exception {
|
||||
byte[] data = createData(pLength);
|
||||
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
|
||||
OutputStream out = new EncoderStream(outBytes, createEncoder(), true);
|
||||
|
||||
try {
|
||||
out.write(data);
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
|
||||
byte[] encoded = outBytes.toByteArray();
|
||||
|
||||
// System.err.println("encoded.length: " + encoded.length);
|
||||
// System.err.println("encoded: " + Arrays.toString(encoded));
|
||||
|
||||
byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder()));
|
||||
assertTrue(Arrays.equals(data, decoded));
|
||||
|
||||
InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder());
|
||||
outBytes = new ByteArrayOutputStream();
|
||||
|
||||
try {
|
||||
FileUtil.copy(in, outBytes);
|
||||
}
|
||||
finally {
|
||||
outBytes.close();
|
||||
in.close();
|
||||
}
|
||||
|
||||
decoded = outBytes.toByteArray();
|
||||
assertTrue(Arrays.equals(data, decoded));
|
||||
}
|
||||
|
||||
public final void testStreams() throws Exception {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
try {
|
||||
runStreamTest(i);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 100; i < 2000; i += 250) {
|
||||
try {
|
||||
runStreamTest(i);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 2000; i < 80000; i += 1000) {
|
||||
try {
|
||||
runStreamTest(i);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage() + ": " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.twelvemonkeys.io.enc;
|
||||
|
||||
import com.twelvemonkeys.io.enc.Decoder;
|
||||
import com.twelvemonkeys.io.enc.Encoder;
|
||||
import com.twelvemonkeys.io.enc.PackBitsDecoder;
|
||||
import com.twelvemonkeys.io.enc.PackBitsEncoder;
|
||||
|
||||
/**
|
||||
* PackBitsDecoderTest
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/enc/PackBitsDecoderTestCase.java#1 $
|
||||
*/
|
||||
public class PackBitsDecoderTestCase extends DecoderAbstractTestCase {
|
||||
public Decoder createDecoder() {
|
||||
return new PackBitsDecoder();
|
||||
}
|
||||
|
||||
public Encoder createCompatibleEncoder() {
|
||||
return new PackBitsEncoder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.twelvemonkeys.io.enc;
|
||||
|
||||
import com.twelvemonkeys.io.enc.Decoder;
|
||||
import com.twelvemonkeys.io.enc.Encoder;
|
||||
import com.twelvemonkeys.io.enc.PackBitsDecoder;
|
||||
import com.twelvemonkeys.io.enc.PackBitsEncoder;
|
||||
|
||||
/**
|
||||
* PackBitsEncoderTest
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/enc/PackBitsEncoderTestCase.java#1 $
|
||||
*/
|
||||
public class PackBitsEncoderTestCase extends EncoderAbstractTestCase {
|
||||
protected Encoder createEncoder() {
|
||||
return new PackBitsEncoder();
|
||||
}
|
||||
|
||||
protected Decoder createCompatibleDecoder() {
|
||||
return new PackBitsDecoder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.twelvemonkeys.io.ole2;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import javax.imageio.stream.MemoryCacheImageInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
/**
|
||||
* CompoundDocumentTestCase
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/io/ole2/CompoundDocumentTestCase.java#1 $
|
||||
*/
|
||||
public class CompoundDocumentTestCase extends TestCase {
|
||||
public void testReadCatalogInputStream() throws IOException {
|
||||
InputStream input = getClass().getResourceAsStream("/Thumbs-camera.db");
|
||||
|
||||
assertNotNull("Missing test resource!", input);
|
||||
|
||||
CompoundDocument document = new CompoundDocument(input);
|
||||
Entry root = document.getRootEntry();
|
||||
assertNotNull(root);
|
||||
assertEquals(25, root.getChildEntries().size());
|
||||
|
||||
Entry catalog = root.getChildEntry("Catalog");
|
||||
assertNotNull(catalog);
|
||||
assertNotNull("Input stream may not be null", catalog.getInputStream());
|
||||
}
|
||||
|
||||
public void testReadCatalogImageInputStream() throws IOException {
|
||||
InputStream input = getClass().getResourceAsStream("/Thumbs-camera.db");
|
||||
|
||||
assertNotNull("Missing test resource!", input);
|
||||
|
||||
MemoryCacheImageInputStream stream = new MemoryCacheImageInputStream(input);
|
||||
stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
CompoundDocument document = new CompoundDocument(stream);
|
||||
|
||||
Entry root = document.getRootEntry();
|
||||
|
||||
assertNotNull(root);
|
||||
assertEquals(25, root.getChildEntries().size());
|
||||
|
||||
Entry catalog = root.getChildEntry("Catalog");
|
||||
|
||||
assertNotNull(catalog);
|
||||
assertNotNull("Input stream may not be null", catalog.getInputStream());
|
||||
}
|
||||
|
||||
public void testReadThumbsCatalogFile() throws IOException, URISyntaxException {
|
||||
URL input = getClass().getResource("/Thumbs-camera.db");
|
||||
|
||||
assertNotNull("Missing test resource!", input);
|
||||
assertEquals("Test resource not a file:// resource", "file", input.getProtocol());
|
||||
|
||||
File file = new File(input.toURI());
|
||||
|
||||
CompoundDocument document = new CompoundDocument(file);
|
||||
|
||||
Entry root = document.getRootEntry();
|
||||
|
||||
assertNotNull(root);
|
||||
assertEquals(25, root.getChildEntries().size());
|
||||
|
||||
Entry catalog = root.getChildEntry("Catalog");
|
||||
|
||||
assertNotNull(catalog);
|
||||
assertNotNull("Input stream may not be null", catalog.getInputStream());
|
||||
}
|
||||
}
|
||||
59
common/common-io/src/test/java/com/twelvemonkeys/net/NetUtilTestCase.java
Executable file
59
common/common-io/src/test/java/com/twelvemonkeys/net/NetUtilTestCase.java
Executable file
@@ -0,0 +1,59 @@
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* NetUtilTestCase
|
||||
* <p/>
|
||||
* <!-- To change this template use Options | File Templates. -->
|
||||
* <!-- Created by IntelliJ IDEA. -->
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/net/NetUtilTestCase.java#1 $
|
||||
*/
|
||||
public class NetUtilTestCase extends TestCase {
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testParseHTTPDateRFC1123() {
|
||||
long time = NetUtil.parseHTTPDate("Sun, 06 Nov 1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sunday, 06 Nov 1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
public void testParseHTTPDateRFC850() {
|
||||
long time = NetUtil.parseHTTPDate("Sunday, 06-Nov-1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sun, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
// NOTE: This test will fail some time, around 2044,
|
||||
// as the 50 year window will slide...
|
||||
time = NetUtil.parseHTTPDate("Sunday, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sun, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
public void testParseHTTPDateAsctime() {
|
||||
long time = NetUtil.parseHTTPDate("Sun Nov 6 08:49:37 1994");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sun Nov 6 08:49:37 94");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
public void testFormatHTTPDateRFC1123() {
|
||||
long time = 784111777000l;
|
||||
assertEquals("Sun, 06 Nov 1994 08:49:37 GMT", NetUtil.formatHTTPDate(time));
|
||||
}
|
||||
}
|
||||
BIN
common/common-io/src/test/resources/Thumbs-camera.db
Executable file
BIN
common/common-io/src/test/resources/Thumbs-camera.db
Executable file
Binary file not shown.
Reference in New Issue
Block a user