01: /*
02: * $Id: TestBlockIo.java,v 1.1 2000/05/06 00:00:53 boisvert Exp $
03: *
04: * Unit tests for BlockIo class
05: *
06: * Simple db toolkit
07: * Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
08: *
09: * This library is free software; you can redistribute it and/or
10: * modify it under the terms of the GNU Library General Public License
11: * as published by the Free Software Foundation; either version 2
12: * of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: * Library General Public License for more details.
18: *
19: * You should have received a copy of the GNU Library General Public License
20: * along with this library; if not, write to the Free Software Foundation,
21: * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22: */
23: package jdbm.recman;
24:
25: import junit.framework.*;
26: import java.io.*;
27:
28: /**
29: * This class contains all Unit tests for {@link BlockIo}.
30: */
31: public class TestBlockIo extends TestCase {
32:
33: private static final short SHORT_VALUE = 0x1234;
34: private static final int INT_VALUE = 0xe7b3c8a1;
35: private static final long LONG_VALUE = 0xfdebca9876543210L;
36:
37: public TestBlockIo(String name) {
38: super (name);
39: }
40:
41: /**
42: * Test writing
43: */
44: public void testWrite() throws Exception {
45: byte[] data = new byte[100];
46: BlockIo test = new BlockIo(0, data);
47: test.writeShort(0, SHORT_VALUE);
48: test.writeLong(2, LONG_VALUE);
49: test.writeInt(10, INT_VALUE);
50:
51: DataInputStream is = new DataInputStream(
52: new ByteArrayInputStream(data));
53: assertEquals("short", SHORT_VALUE, is.readShort());
54: assertEquals("long", LONG_VALUE, is.readLong());
55: assertEquals("int", INT_VALUE, is.readInt());
56: }
57:
58: /**
59: * Test reading
60: */
61: public void testRead() throws Exception {
62: ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
63: DataOutputStream os = new DataOutputStream(bos);
64: os.writeShort(SHORT_VALUE);
65: os.writeLong(LONG_VALUE);
66: os.writeInt(INT_VALUE);
67:
68: byte[] data = bos.toByteArray();
69: BlockIo test = new BlockIo(0, data);
70: assertEquals("short", SHORT_VALUE, test.readShort(0));
71: assertEquals("long", LONG_VALUE, test.readLong(2));
72: assertEquals("int", INT_VALUE, test.readInt(10));
73: }
74:
75: /**
76: * Runs all tests in this class
77: */
78: public static void main(String[] args) {
79: junit.textui.TestRunner.run(new TestSuite(TestBlockIo.class));
80: }
81: }
|