KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > lib > TestRdbBlob


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.rdb.lib;
25
26 import junit.framework.TestCase;
27
28 import java.io.InputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.sql.Blob JavaDoc;
31
32 /**
33  * Basic code for testing BLOBs.
34  * @author S. Chassande-Barrioz
35  */

36 public abstract class TestRdbBlob extends TestCase {
37
38     public TestRdbBlob(String JavaDoc testName) {
39         super(testName);
40     }
41
42     protected Blob JavaDoc blob = null;
43
44     protected BlobVerifier verifier = null;
45
46     protected abstract void setUp();
47
48     protected abstract void tearDown();
49
50     // Test the access by the blob interface
51
public void testGetBytes() {
52         try {
53             assertTrue("blob.length", verifier.isSize(blob.length()));
54             byte[] data = blob.getBytes(0, (int) blob.length());
55             for (int i = 0; i < data.length; i++) {
56                 println("data[" + i + "]=" + data[i]);
57                 assertTrue("invalid value at " + i + " position",
58                     verifier.valid(data[i], i));
59             }
60         }
61         catch (Exception JavaDoc e) {
62             fail(e.getMessage());
63
64         }
65     }
66
67     public void testRead() {
68         try {
69             // Test the access by an InputStream
70
InputStream JavaDoc is = blob.getBinaryStream();
71             assertTrue("blob.getBinaryStream() is null", is != null);
72             for (long i = 0; i < blob.length(); i++) {
73                 byte aByte = (byte) is.read();
74                 println("read [" + i + "]=" + aByte);
75                 assertTrue("invalid value at " + i + " position",
76                     verifier.valid(aByte, i));
77             }
78         }
79         catch (Exception JavaDoc e) {
80             fail(e.getMessage());
81         }
82     }
83
84     // Test the access by an InputStream
85
public void testSkip() {
86         try {
87             InputStream JavaDoc is = blob.getBinaryStream();
88             assertTrue("blob.getBinaryStream() is null", is != null);
89             is.skip(blob.length() / 3);
90             for (long i = blob.length() / 3; i < blob.length(); i++) {
91                 byte aByte = (byte) is.read();
92                 println("sk+read [" + i + "]=" + aByte);
93                 assertTrue("invalid value at the position " + i,
94                     verifier.valid(aByte, i));
95             }
96         }
97         catch (Exception JavaDoc e) {
98             fail(e.getMessage());
99         }
100     }
101
102     // Test the access by an ObjectInputStream
103
public void testRebuildObject() {
104         try {
105             ObjectInputStream JavaDoc ois =
106                 new ObjectInputStream JavaDoc(blob.getBinaryStream());
107             assertTrue("ObjectInputStream is null", ois != null);
108             Object JavaDoc o = ois.readObject();
109             assertTrue("Deserialized object does not match",
110                 verifier.isEquals(o));
111         }
112         catch (Exception JavaDoc e) {
113             fail(e.getMessage());
114         }
115     }
116
117     private void println(String JavaDoc msg) {
118         //System.out.println(msg);
119
}
120 }
121
Popular Tags