001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.io;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.io.*;
027:
028: import db4ounit.*;
029:
030: public class IoAdapterTest implements TestLifeCycle {
031:
032: public static void main(String[] args) {
033: new TestRunner(IoAdapterTest.class).run();
034: }
035:
036: private String _cachedIoAdapterFile = "CachedIoAdapter.dat";
037:
038: private String _randomAccessFileAdapterFile = "_randomAccessFileAdapter.dat";
039:
040: private IoAdapter[] _adapters;
041:
042: public void setUp() throws Exception {
043: deleteAllTestFiles();
044: initAdapters(false);
045: }
046:
047: private void initAdapters(boolean readOnly) throws Exception {
048: _adapters = new IoAdapter[] {
049: initRandomAccessAdapter(readOnly),
050: initCachedRandomAccessAdapter(readOnly), };
051: }
052:
053: public void tearDown() throws Exception {
054: closeAdapters();
055: deleteAllTestFiles();
056: }
057:
058: public void testReadWrite() throws Exception {
059: for (int i = 0; i < _adapters.length; ++i) {
060: assertReadWrite(_adapters[i]);
061: }
062: }
063:
064: private void assertReadWrite(IoAdapter adapter) throws IOException {
065: adapter.seek(0);
066: int count = 1024 * 8 + 10;
067: byte[] data = new byte[count];
068: for (int i = 0; i < count; ++i) {
069: data[i] = (byte) (i % 256);
070: }
071: adapter.write(data);
072: adapter.seek(0);
073: byte[] readBytes = new byte[count];
074: adapter.read(readBytes);
075: for (int i = 0; i < count; i++) {
076: Assert.areEqual(data[i], readBytes[i]);
077: }
078: }
079:
080: public void testSeek() throws Exception {
081: for (int i = 0; i < _adapters.length; ++i) {
082: assertSeek(_adapters[i]);
083: }
084: }
085:
086: public void testReadWriteBytes() throws Exception {
087: String[] strs = {
088: "short string",
089: "this is a really long string, just to make sure that all IoAdapters work correctly. " };
090: for (int i = 0; i < _adapters.length; i++) {
091: for (int j = 0; j < strs.length; j++)
092: assertReadWriteString(_adapters[i], strs[j]);
093: }
094: }
095:
096: private void assertReadWriteString(IoAdapter adapter, String str)
097: throws Exception {
098: byte[] data = str.getBytes();
099: byte[] read = new byte[2048];
100: adapter.seek(0);
101: adapter.write(data);
102: adapter.seek(0);
103: adapter.read(read);
104: Assert.areEqual(str, new String(read, 0, data.length));
105: }
106:
107: public void testReadOnly() throws Exception {
108: closeAdapters();
109: initAdapters(true);
110: for (int i = 0; i < _adapters.length; i++) {
111: assertReadOnly(_adapters[i]);
112: }
113: }
114:
115: private void assertReadOnly(final IoAdapter adapter) {
116: Assert.expect(Db4oIOException.class, new CodeBlock() {
117: public void run() throws Throwable {
118: adapter.write(new byte[] { 0 });
119: }
120: });
121: }
122:
123: /*
124: * This test is disabled because the API difference between java & .net.
125: */
126: public void _testReadWriteAheadFileEnd() throws Exception {
127: String str = "this is a really long string, just to make sure that all IoAdapters work correctly. ";
128: for (int i = 0; i < _adapters.length; i++) {
129: assertReadWriteAheadFileEnd(_adapters[i], str);
130: }
131: }
132:
133: private void assertReadWriteAheadFileEnd(IoAdapter adapter,
134: String str) throws Exception {
135: byte[] data = str.getBytes();
136: byte[] read = new byte[2048];
137: adapter.seek(10);
138: int readBytes = adapter.read(data);
139: Assert.areEqual(-1, readBytes);
140: Assert.areEqual(0, adapter.getLength());
141: adapter.seek(0);
142: readBytes = adapter.read(data);
143: Assert.areEqual(-1, readBytes);
144: Assert.areEqual(0, adapter.getLength());
145:
146: adapter.seek(10);
147: adapter.write(data);
148: Assert.areEqual(10 + data.length, adapter.getLength());
149:
150: adapter.seek(0);
151: readBytes = adapter.read(read);
152: Assert.areEqual(10 + data.length, readBytes);
153:
154: adapter.seek(20 + data.length);
155: readBytes = adapter.read(read);
156: Assert.areEqual(-1, readBytes);
157:
158: adapter.seek(1024 + data.length);
159: readBytes = adapter.read(read);
160: Assert.areEqual(-1, readBytes);
161:
162: adapter.seek(1200);
163: adapter.write(data);
164: adapter.seek(0);
165: readBytes = adapter.read(read);
166: Assert.areEqual(1200 + data.length, readBytes);
167: }
168:
169: public void testReopen() throws Exception {
170: testReadWrite();
171: closeAdapters();
172: initAdapters(false);
173: testReadWrite();
174: }
175:
176: private void assertSeek(IoAdapter adapter) throws Exception {
177: int count = 1024 * 2 + 10;
178: byte[] data = new byte[count];
179: for (int i = 0; i < data.length; ++i) {
180: data[i] = (byte) (i % 256);
181: }
182: adapter.write(data);
183: byte[] readBytes = new byte[count];
184: adapter.seek(0);
185: adapter.read(readBytes);
186: for (int i = 0; i < count; i++) {
187: Assert.areEqual(data[i], readBytes[i]);
188: }
189: adapter.seek(20);
190: adapter.read(readBytes);
191: for (int i = 0; i < count - 20; i++) {
192: Assert.areEqual(data[i + 20], readBytes[i]);
193: }
194:
195: byte[] writtenData = new byte[10];
196: for (int i = 0; i < writtenData.length; ++i) {
197: writtenData[i] = (byte) i;
198: }
199: adapter.seek(1000);
200: adapter.write(writtenData);
201: adapter.seek(1000);
202: int readCount = adapter.read(readBytes, 10);
203: Assert.areEqual(10, readCount);
204: for (int i = 0; i < readCount; ++i) {
205: Assert.areEqual(i, readBytes[i]);
206: }
207: }
208:
209: private IoAdapter initCachedRandomAccessAdapter(boolean readOnly)
210: throws Exception {
211: IoAdapter adapter = new CachedIoAdapter(
212: new RandomAccessFileAdapter());
213: adapter = adapter
214: .open(_cachedIoAdapterFile, false, 0, readOnly);
215: return adapter;
216: }
217:
218: private IoAdapter initRandomAccessAdapter(boolean readOnly)
219: throws Exception {
220: IoAdapter adapter = new RandomAccessFileAdapter();
221: adapter = adapter.open(_randomAccessFileAdapterFile, false, 0,
222: readOnly);
223: return adapter;
224: }
225:
226: private void deleteAllTestFiles() throws Exception {
227: new File(_cachedIoAdapterFile).delete();
228: new File(_randomAccessFileAdapterFile).delete();
229: }
230:
231: private void closeAdapters() {
232: for (int i = 0; i < _adapters.length; ++i) {
233: try {
234: _adapters[i].close();
235: } catch (Db4oIOException e) {
236: // ignore
237: }
238: }
239: }
240:
241: }
|