001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_MarkedLimitInputStream
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derbyTesting.unitTests.harness.T_Generic;
025: import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.io.DataInputStream;
032: import java.io.DataOutputStream;
033: import java.io.IOException;
034:
035: /**
036: A simple unit test for a MarkedLimitInputStream.
037: */
038: public class T_MarkedLimitInputStream extends T_Generic {
039:
040: private static final int TEST_SIZE = 10000;
041: private static final int BLOCK_SIZE = 256;
042:
043: private static MarkedLimitInputStream setup(byte[] data)
044: throws Exception {
045: // make an InputStream on top of an array
046: InputStream inputStream = new ByteArrayInputStream(data);
047:
048: // make an OutputStream on top of an empty array
049: ByteArrayOutputStream baos = new ByteArrayOutputStream(
050: TEST_SIZE + 200);
051: // make it into a DataOutputStream
052: DataOutputStream dos = new DataOutputStream(baos);
053: // fill it with data in the correct (block) format
054: writeDos(inputStream, dos);
055:
056: // make a MarkedLimitInputStream
057: return makeMLIS(baos.toByteArray());
058:
059: }
060:
061: private static void writeDos(InputStream x, DataOutputStream out)
062: throws Exception {
063: boolean isLastBlock = false;
064: byte[] b = new byte[BLOCK_SIZE];
065:
066: while (isLastBlock == false) {
067: int len = x.read(b);
068: if (len != BLOCK_SIZE) {
069: isLastBlock = true;
070: if (len < 0) {
071: len = 0;
072: }
073: }
074: out.writeBoolean(isLastBlock);
075: out.writeInt(len);
076: for (int i = 0; i < len; i++) {
077: out.writeByte(b[i]);
078: }
079: }
080: }
081:
082: private static MarkedLimitInputStream makeMLIS(byte[] b)
083: throws Exception {
084: // make an InputStream
085: InputStream inputStream = new ByteArrayInputStream(b);
086: // make a DataInputStream
087: DataInputStream dataInputStream = new DataInputStream(
088: inputStream);
089: // make a MarkedLimitInputStream
090: return new MarkedLimitInputStream(dataInputStream);
091: }
092:
093: private static boolean readAndCompare(MarkedLimitInputStream mlis,
094: byte[] x) throws Exception {
095: int b;
096: int i = 0;
097: while ((b = mlis.read()) != -1) {
098: if (x[i] != (byte) b) {
099: System.out
100: .println("Stream and array differ at position "
101: + i);
102: return false;
103: }
104: i++;
105: }
106: // read to end of stream, check array size
107: if (i != x.length) {
108: System.out.println("array size and stream size differ");
109: return false;
110: }
111: return true;
112:
113: }
114:
115: private static boolean readAndCompareChunks(
116: MarkedLimitInputStream mlis, byte[] x) throws Exception {
117: int chunkSize = 10;
118: byte[] chunk = new byte[chunkSize];
119: int c = 0;
120: int base = 0;
121: while ((c = mlis.read(chunk)) > 0) {
122: for (int offset = 0; offset < c; offset++) {
123: if (x[base + offset] != chunk[offset]) {
124: System.out
125: .println("Stream and array differ at position "
126: + (base + offset));
127: System.out.println("Array : x[" + (base + offset)
128: + "] = " + x[base + offset]);
129: System.out.println("Stream : chunk[" + offset
130: + "] = " + chunk[offset]);
131: return false;
132: }
133: }
134: base += c;
135: }
136:
137: // read to end of stream, check array size
138: if (base != x.length) {
139: System.out.println("array size ( " + x.length
140: + " ) and stream size ( " + base + " ) differ");
141: return false;
142: }
143: return true;
144:
145: }
146:
147: private static boolean skipAndCompare(MarkedLimitInputStream mlis,
148: byte[] x, long skipTo) throws Exception {
149: long c = mlis.skip(skipTo);
150: T_Fail.T_ASSERT(c == skipTo);
151: byte[] y = new byte[x.length - (int) c];
152: System.arraycopy(x, (int) skipTo, y, 0, x.length - (int) c);
153: return readAndCompare(mlis, y);
154: }
155:
156: /** Methods required by T_Generic
157: */
158: public String getModuleToTestProtocolName() {
159: return "internalUtils.MarkedLimitInputStream";
160: }
161:
162: protected void runTests() throws Exception {
163: boolean success = true;
164: // create and initialize array
165: byte[] data = new byte[TEST_SIZE];
166: for (int i = 0; i < data.length; i++) {
167: data[i] = (byte) (i & 0xFF);
168: }
169:
170: MarkedLimitInputStream mlis = setup(data);
171: // compare MarkedLimitInputStream with original byte array
172: if (readAndCompare(mlis, data)) {
173: PASS("test1");
174: } else {
175: FAIL("test1");
176: success = false;
177: }
178:
179: MarkedLimitInputStream mlis2 = setup(data);
180: // compare MarkedLimitInputStream with original byte array
181: // read in chunks
182: if (readAndCompareChunks(mlis2, data)) {
183: PASS("test2");
184: } else {
185: FAIL("test2");
186: success = false;
187: }
188:
189: MarkedLimitInputStream mlis3 = setup(data);
190: // skip and compare MarkedLimitInputStream with original byte array
191: if (skipAndCompare(mlis3, data, TEST_SIZE / 2)) {
192: PASS("test3");
193: } else {
194: FAIL("test3");
195: success = false;
196: }
197:
198: MarkedLimitInputStream mlis4 = setup(data);
199: // skip and compare MarkedLimitInputStream with original byte array
200: if (skipAndCompare(mlis4, data, TEST_SIZE - 1)) {
201: PASS("test4");
202: } else {
203: FAIL("test4");
204: success = false;
205: }
206:
207: if (!success) {
208: throw T_Fail.testFail();
209: }
210:
211: // create and initialize array with size BLOCK_SIZE
212: byte[] data2 = new byte[BLOCK_SIZE];
213: for (int i = 0; i < data.length; i++) {
214: data[i] = (byte) (i & 0xFF);
215: }
216: MarkedLimitInputStream mlis5 = setup(data2);
217: // skip and compare MarkedLimitInputStream with original byte array
218: if (readAndCompare(mlis5, data2)) {
219: PASS("test5");
220: } else {
221: FAIL("test5");
222: success = false;
223: }
224:
225: if (!success) {
226: throw T_Fail.testFail();
227: }
228:
229: }
230:
231: }
|