KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > BlobTest


1 package org.apache.ojb.broker;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.junit.PBTestCase;
19 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
20 import org.apache.ojb.broker.platforms.PlatformPostgreSQLImpl;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * This TestClass tests storing and retrieving Objects with BLOB/CLOB Attributes.
26  * @version $Id: BlobTest.java,v 1.9.2.6 2005/12/21 22:31:23 tomdz Exp $
27  */

28 public class BlobTest extends PBTestCase
29 {
30     private static final String JavaDoc SKIP_MESSAGE =
31             "# Skip "+BlobTest.class.getName()+", DB does not support Blob/Clob #";
32
33     /**
34      * Known issue (to be fixed, warn in setup)
35      * or DB lack of features (will never be fixed)?
36      */

37     private boolean knownIssue;
38     private boolean skipTest;
39
40     public BlobTest(String JavaDoc name)
41     {
42         super(name);
43     }
44
45     public static void main(String JavaDoc[] args)
46     {
47         String JavaDoc[] arr = {BlobTest.class.getName()};
48         junit.textui.TestRunner.main(arr);
49     }
50
51     public void setUp() throws Exception JavaDoc
52     {
53         super.setUp();
54
55         String JavaDoc platformClass = getPlatformClass();
56         /*
57         hsqldb<1.7.2 does not support Blob/Clob, so we skip test for this DB
58         */

59         knownIssue = false;
60         if (platformClass.equals(PlatformHsqldbImpl.class.getName()))
61         {
62             skipTest = true;
63         }
64         else if (platformClass.equals(PlatformPostgreSQLImpl.class.getName())
65             && ojbSkipKnownIssueProblem("LOB handling is not yet implemented for PostgreSQL"))
66         {
67             knownIssue = true;
68             skipTest = true;
69         }
70     }
71
72     public void testBlobInsertion() throws Exception JavaDoc
73     {
74         if (skipTest)
75         {
76             if (!knownIssue)
77             {
78                 System.out.println(SKIP_MESSAGE);
79             }
80             return;
81         }
82
83         int size = 5000;
84
85         ObjectWithBlob obj = new ObjectWithBlob();
86
87         byte[] barr = new byte[size];
88         char[] carr = new char[size];
89         for (int i = 0; i < size; i++)
90         {
91             barr[i] = (byte) 'x';
92             carr[i] = 'y';
93         }
94
95         // obj.setId(1); we use autoincrement
96
obj.setBlob(barr);
97         obj.setClob(new String JavaDoc(carr));
98         broker.beginTransaction();
99         broker.store(obj);
100         broker.commitTransaction();
101         broker.clearCache();
102
103         Identity oid = new Identity(obj, broker);
104         ObjectWithBlob obj1 = (ObjectWithBlob) broker.getObjectByIdentity(oid);
105         assertNotNull("BLOB was not stored", obj1.getBlob());
106         assertNotNull("CLOB was not stored", obj1.getClob());
107         assertEquals(obj.getBlob().length, obj1.getBlob().length);
108         assertEquals(obj.getClob().length(), obj1.getClob().length());
109
110     }
111
112     public void testReadNullBlob()
113     {
114         if (skipTest)
115         {
116             if (!knownIssue)
117             {
118                 System.out.println(SKIP_MESSAGE);
119             }
120             return;
121         }
122
123         ObjectWithBlob obj = new ObjectWithBlob();
124
125         // obj.setId(1); we use autoincrement
126
obj.setBlob(null);
127         obj.setClob(null);
128         broker.beginTransaction();
129         broker.store(obj);
130         broker.commitTransaction();
131         broker.clearCache();
132
133         Identity oid = new Identity(obj, broker);
134         ObjectWithBlob obj1 = (ObjectWithBlob) broker.getObjectByIdentity(oid);
135
136         assertEquals(null, obj1.getBlob());
137         assertEquals(null, obj1.getClob());
138     }
139
140
141     //*******************************************************
142
// inner class - test class
143
//*******************************************************
144
public static class ObjectWithBlob implements Serializable JavaDoc
145     {
146         private int id;
147
148         private byte[] blob;
149
150         private String JavaDoc clob;
151
152
153         /**
154          * Gets the blob.
155          * @return Returns a byte[]
156          */

157         public byte[] getBlob()
158         {
159             return blob;
160         }
161
162         /**
163          * Sets the blob.
164          * @param blob The blob to set
165          */

166         public void setBlob(byte[] blob)
167         {
168             this.blob = blob;
169         }
170
171         /**
172          * Gets the clob.
173          * @return Returns a char[]
174          */

175         public String JavaDoc getClob()
176         {
177             return clob;
178         }
179
180         /**
181          * Sets the clob.
182          * @param clob The clob to set
183          */

184         public void setClob(String JavaDoc clob)
185         {
186             this.clob = clob;
187         }
188
189         /**
190          * Gets the id.
191          * @return Returns a int
192          */

193         public int getId()
194         {
195             return id;
196         }
197
198         /**
199          * Sets the id.
200          * @param id The id to set
201          */

202         public void setId(int id)
203         {
204             this.id = id;
205         }
206     }
207
208 }
209
Popular Tags