001: /*
002: * $Id: TestBufferedRandomAccessReadWrite.java,v 1.3 2005/05/02 22:32:01 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.util;
042:
043: import java.io.File;
044: import java.io.FileOutputStream;
045: import java.io.IOException;
046: import java.util.Random;
047:
048: import junit.framework.Test;
049: import junit.framework.TestCase;
050: import junit.framework.TestSuite;
051:
052: import org.axiondb.AxionException;
053: import org.axiondb.io.AxionFileSystem;
054: import org.axiondb.io.BufferedDataInputStream;
055: import org.axiondb.io.BufferedDataOutputStream;
056: import org.axiondb.types.CharacterVaryingType;
057: import org.axiondb.types.IntegerType;
058:
059: /**
060: * @version $Revision: 1.3 $ $Date: 2005/05/02 22:32:01 $
061: * @author Rodney Waldhoff
062: */
063: public class TestBufferedRandomAccessReadWrite extends TestCase {
064:
065: //-----------------------------------------------------b------- Conventional
066:
067: public TestBufferedRandomAccessReadWrite(String testName) {
068: super (testName);
069: }
070:
071: public static Test suite() {
072: return new TestSuite(TestBufferedRandomAccessReadWrite.class);
073: }
074:
075: //--------------------------------------------------------------- Lifecycle
076:
077: private Random _random = new Random();
078: private AxionFileSystem FS = new AxionFileSystem();
079:
080: //------------------------------------------------------------------- Tests
081:
082: public void testRead() throws Exception {
083: byte[] bytes = new byte[128];
084: for (int i = 0; i < bytes.length; i++) {
085: bytes[i] = (byte) i;
086: }
087: FileOutputStream out = new FileOutputStream("test.file");
088: out.write(bytes);
089: out.close();
090:
091: BufferedDataInputStream in = new BufferedDataInputStream(FS
092: .open(new File("test.file")), 12);
093: for (int i = 0; i < bytes.length; i++) {
094: assertEquals(bytes[i], (byte) in.read());
095: }
096: assertEquals(-1, in.read());
097: assertEquals(-1, in.read());
098: in.close();
099: File toDelete = new File("test.file");
100: toDelete.delete();
101: }
102:
103: public void testReadIntoBigBuffer() throws Exception {
104:
105: byte[] bytes = new byte[128];
106: for (int i = 0; i < bytes.length; i++) {
107: bytes[i] = (byte) i;
108: }
109: FileOutputStream out = new FileOutputStream("test.file");
110: out.write(bytes);
111: out.close();
112:
113: BufferedDataInputStream in = FS.openBufferedDIS(new File(
114: "test.file"));
115: byte[] data = new byte[256];
116: assertEquals(128, in.read(data));
117: for (int i = 0; i < bytes.length; i++) {
118: assertEquals(bytes[i], data[i]);
119: }
120: in.close();
121: File toDelete = new File("test.file");
122: toDelete.delete();
123: }
124:
125: public void testSeek() throws Exception {
126: byte[] bytes = new byte[128];
127: for (int i = 0; i < bytes.length; i++) {
128: bytes[i] = (byte) i;
129: }
130: FileOutputStream out = new FileOutputStream("test.file");
131: out.write(bytes);
132: out.close();
133:
134: BufferedDataInputStream in = new BufferedDataInputStream(FS
135: .open(new File("test.file")), 12);
136: for (int i = 0; i < bytes.length; i++) {
137: in.seek(i);
138: assertEquals("" + i, bytes[i], (byte) in.read());
139: }
140: for (int i = 0; i < bytes.length; i++) {
141: in.seek(bytes.length - i - 1);
142: assertEquals(bytes[bytes.length - i - 1], (byte) in.read());
143: }
144: in.close();
145: File toDelete = new File("test.file");
146: toDelete.delete();
147: }
148:
149: public void testShortBufferedRead() throws Exception {
150: for (int i = 0; i < 10; i++) {
151: byte[] bytes = new byte[i];
152: _random.nextBytes(bytes);
153: FileOutputStream out = new FileOutputStream("test.file");
154: out.write(bytes);
155: out.close();
156:
157: BufferedDataInputStream in = FS.openBufferedDIS(new File(
158: "test.file"));
159: byte[] bytesin = new byte[1];
160: for (int j = 0; j < i; j++) {
161: in.read(bytesin);
162: assertEquals("i=" + i + ";j=" + j, bytes[j], bytesin[0]);
163: }
164: in.close();
165: File toDelete = new File("test.file");
166: toDelete.delete();
167: }
168: }
169:
170: public void testLongBufferedRead() throws Exception {
171: for (int i = 2040; i < 2100; i++) {
172: byte[] bytes = new byte[i];
173: _random.nextBytes(bytes);
174: FileOutputStream out = new FileOutputStream("test.file");
175: out.write(bytes);
176: out.close();
177:
178: BufferedDataInputStream in = FS.openBufferedDIS(new File(
179: "test.file"));
180: byte[] bytesin = new byte[1];
181: for (int j = 0; j < i; j++) {
182: in.read(bytesin);
183: assertEquals("i=" + i + ";j=" + j, bytes[j], bytesin[0]);
184: }
185: in.close();
186: File toDelete = new File("test.file");
187: toDelete.delete();
188: }
189: }
190:
191: public void testLongBufferedRead2() throws Exception {
192: for (int i = 2040; i < 2100; i++) {
193: byte[] bytes = new byte[i];
194: _random.nextBytes(bytes);
195: FileOutputStream out = new FileOutputStream("test.file");
196: out.write(bytes);
197: out.close();
198:
199: BufferedDataInputStream in = FS.openBufferedDIS(new File(
200: "test.file"));
201: byte[] bytesin = new byte[i];
202: in.read(bytesin);
203: for (int j = 0; j < i; j++) {
204: assertEquals(bytes[j], bytesin[j]);
205: }
206: in.close();
207: File toDelete = new File("test.file");
208: toDelete.delete();
209: }
210: }
211:
212: public void testWriteAndRead() throws Exception {
213: BufferedDataOutputStream writefile = FS.openBufferedDOSAppend(
214: new File("test.file"), 20);
215: // create a long string
216: String text = null;
217: {
218: StringBuffer buf = new StringBuffer();
219: for (int i = 0; i < 10; i++) {
220: buf
221: .append("The quick brown fox jumped over the lazy dogs. ");
222: }
223: text = buf.toString();
224: }
225:
226: long offarray[] = new long[10];
227: CharacterVaryingType varcharType = new CharacterVaryingType(
228: 1000);
229: IntegerType intType = new IntegerType();
230: for (int i = 0; i < 10; i++) {
231: offarray[i] = writefile.getPos();
232: intType.write(new Integer(i), writefile);
233: varcharType.write(text, writefile);
234: }
235: writefile.close();
236:
237: BufferedDataInputStream readfile = FS.openBufferedDIS(new File(
238: "test.file"));
239: for (int i = 0; i < 10; i++) {
240: try {
241: readfile.seek(offarray[i]);
242: assertEquals(new Integer(i), intType.read(readfile));
243: assertEquals(text, varcharType.read(readfile));
244: } catch (IOException e) {
245: throw new AxionException(
246: "IOException while reading at position "
247: + offarray[i] + " in data file "
248: + readfile, e);
249: }
250: }
251:
252: readfile.close();
253: File toDelete = new File("test.file");
254: toDelete.delete();
255: }
256: }
|