Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Low-level InputStream test w/mock

Erik Hatcher7eb95be70573
diff --git a/src/test/org/apache/lucene/index/MockInputStream.java b/src/test/org/apache/lucene/index/MockInputStream.java
new file mode 100644
index 00000000000..ba4c377324b
--- /dev/null
+++ b/src/test/org/apache/lucene/index/MockInputStream.java
@@ -0,0 +1,40 @@
+package org.apache.lucene.index;
+
+import org.apache.lucene.store.InputStream;
+
+import java.io.IOException;
+
+public class MockInputStream extends InputStream {
+ private byte[] buffer;
+ private int pointer = 0;
+
+ public MockInputStream(byte[] bytes) {
+ buffer = bytes;
+ length = bytes.length;
+ }
+
+ protected void readInternal(byte[] dest, int destOffset, int len)
+ throws IOException {
+ int remainder = len;
+ int start = pointer;
+ while (remainder != 0) {
+// int bufferNumber = start / buffer.length;
+ int bufferOffset = start % buffer.length;
+ int bytesInBuffer = buffer.length - bufferOffset;
+ int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
+ System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
+ destOffset += bytesToCopy;
+ start += bytesToCopy;
+ remainder -= bytesToCopy;
+ }
+ pointer += len;
+ }
+
+ public void close() throws IOException {
+ // ignore
+ }
+
+ protected void seekInternal(long pos) throws IOException {
+ pointer = (int) pos;
+ }
+}
diff --git a/src/test/org/apache/lucene/index/TestInputStream.java b/src/test/org/apache/lucene/index/TestInputStream.java
new file mode 100644
index 00000000000..8225b58f17a
--- /dev/null
+++ b/src/test/org/apache/lucene/index/TestInputStream.java
@@ -0,0 +1,21 @@
+package org.apache.lucene.index;
+
+import junit.framework.TestCase;
+import org.apache.lucene.store.InputStream;
+
+import java.io.IOException;
+
+public class TestInputStream extends TestCase {
+ public void testRead() throws IOException {
+ InputStream is = new MockInputStream(new byte[] { (byte) 0x80, 0x01,
+ (byte) 0xFF, 0x7F,
+ (byte) 0x80, (byte) 0x80, 0x01,
+ (byte) 0x81, (byte) 0x80, 0x01,
+ 0x06, 'L', 'u', 'c', 'e', 'n', 'e'});
+ assertEquals(128,is.readVInt());
+ assertEquals(16383,is.readVInt());
+ assertEquals(16384,is.readVInt());
+ assertEquals(16385,is.readVInt());
+ assertEquals("Lucene",is.readString());
+ }
+}