1 64 package com.jcorporate.expresso.core.dataobjects.jdbc.tests; 65 66 import com.jcorporate.expresso.core.dataobjects.jdbc.LobField; 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.db.TableCreator; 69 import com.jcorporate.expresso.core.dbobj.tests.Test1; 70 import com.jcorporate.expresso.core.dbobj.tests.Test2; 71 import com.jcorporate.expresso.core.dbobj.tests.TestSchema; 72 import com.jcorporate.expresso.core.misc.ConfigManager; 73 import com.jcorporate.expresso.core.misc.ConfigurationException; 74 import com.jcorporate.expresso.services.test.TestSystemInitializer; 75 import junit.framework.Test; 76 import junit.framework.TestCase; 77 import junit.framework.TestSuite; 78 import org.apache.log4j.Logger; 79 80 import java.sql.Blob ; 81 import java.util.Enumeration ; 82 import java.util.Vector ; 83 84 91 92 public class LobFieldTest extends TestCase { 93 94 private static Logger log = Logger.getLogger(LobFieldTest.class); 95 96 101 public LobFieldTest(String name) { 102 super(name); 103 } 104 105 106 public static void main(String [] args) 107 throws Exception { 108 109 junit.textui.TestRunner.run(suite()); 111 ConfigManager.destroy(); 112 } 113 114 118 public void setUp() 119 throws Exception { 120 TestSystemInitializer.setUp(); 121 122 try { 123 ConfigManager.getContext(TestSystemInitializer.getTestContext()); 124 } catch (ConfigurationException ce) { 125 fail( 126 "Specified context to test:" + TestSystemInitializer.getTestContext() + " but couldn't find that context"); 127 } 128 129 TestSchema ts = new TestSchema(); 130 Vector v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext()); 131 132 for (Enumeration ev = v.elements(); ev.hasMoreElements();) { 133 log.info((String ) ev.nextElement()); 134 } 135 136 Test1 test1List = new Test1(); 137 test1List.setDataContext(TestSystemInitializer.getTestContext()); 138 test1List.deleteAll(); 139 140 } 141 142 143 147 public void tearDown() 148 throws Exception { 149 try { 150 151 152 Test1 test1List = new Test1(); 153 test1List.setDataContext(TestSystemInitializer.getTestContext()); 154 test1List.deleteAll(); 155 156 Test2 test2List = new Test2(); 157 test2List.setDataContext(TestSystemInitializer.getTestContext()); 158 test2List.deleteAll(); 159 160 } catch (DBException de) { 161 de.printStackTrace(); 162 log.error(de); 163 } 164 } 165 166 167 172 public static Test suite() { 173 TestSuite suite = new TestSuite(LobFieldTest.class); 174 175 return suite; 176 } 177 178 179 184 public void testBLOBSupport() { 185 LobField lf = new LobField(); 186 final Integer testInt = new Integer (-3544); 187 final String testKey = "1"; 188 try { 189 java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream (128); 190 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (bos); 191 oos.writeObject(testInt); 192 byte buffer[] = bos.toByteArray(); 193 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (buffer); 194 195 196 Test1 oneTest = new Test1(); 197 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 198 oneTest.setField("TestKey", testKey); 199 oneTest.add(); 200 201 lf.setCriteria(oneTest); 202 lf.saveBlob("BlobTest", bis, buffer.length); 203 204 java.io.InputStream is = lf.getBlobStream("BlobTest"); 205 206 assertTrue("Blob Input stream data must not be null", is != null); 207 208 java.io.ObjectInputStream ois = new java.io.ObjectInputStream (is); 209 Object o = ois.readObject(); 210 assertTrue("Read object must not be null", o != null); 211 assertTrue("Read object must be of type java.lang.Integer", o instanceof Integer ); 212 Integer i = (Integer ) o; 213 assertTrue("Binary field data must be equal to original data. Got " + i.intValue() + 214 " instead of " + testInt.intValue(), i.intValue() == testInt.intValue()); 215 216 217 } catch (Throwable t) { 218 t.printStackTrace(); 219 fail("Caught exception testing BLOB Support: " + t.getMessage()); 220 } finally { 221 lf.close(); 222 } 223 } 224 225 231 public void testBLOBSupportLargeData() { 232 LobField lf = new LobField(); 233 final Integer testInt = new Integer (-3544); 234 Integer [] testIntArray = new Integer [1000]; 235 final String testKey = "1"; 236 try { 237 for (int i = 0; i < 1000; i++) { 238 testIntArray[i] = testInt; 239 } 240 241 java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream (128000); 242 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (bos); 243 oos.writeObject(testIntArray); 244 byte buffer[] = bos.toByteArray(); 245 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (buffer); 246 247 248 Test1 oneTest = new Test1(); 249 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 250 oneTest.setField("TestKey", testKey); 251 oneTest.add(); 252 253 lf.setCriteria(oneTest); 254 lf.saveBlob("BlobTest", bis, buffer.length); 255 256 java.io.InputStream is = lf.getBlobStream("BlobTest"); 257 258 assertTrue("Blob Input stream data must not be null", is != null); 259 260 java.io.ObjectInputStream ois = new java.io.ObjectInputStream (is); 261 Object o = ois.readObject(); 262 assertTrue("Read object must not be null", o != null); 263 assertTrue("Read object must be of type java.lang.Integer[]", o instanceof Integer []); 264 Integer [] i = (Integer []) o; 265 266 boolean objectsEqual = true; 267 for (int j = 0; j < i.length; j++) { 268 if (i[j].intValue() != testIntArray[j].intValue()) { 269 objectsEqual = false; 270 break; 271 } 272 } 273 assertTrue("Binary field data must be equal to original data.", objectsEqual); 274 275 276 } catch (Throwable t) { 277 t.printStackTrace(); 278 fail("Caught exception testing BLOB Support: " + t.getMessage()); 279 } finally { 280 lf.close(); 281 } 282 } 283 284 289 public void testBLOBSupportLargeData2() { 290 LobField lf = new LobField(); 291 LobField lf2 = new LobField(); 292 final Integer testInt = new Integer (-3544); 293 Integer [] testIntArray = new Integer [1000]; 294 final String testKey = "1"; 295 final String testKey2 = "2"; 296 try { 297 for (int i = 0; i < 1000; i++) { 298 testIntArray[i] = testInt; 299 } 300 301 java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream (100000); 302 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (bos); 303 oos.writeObject(testIntArray); 304 byte buffer[] = bos.toByteArray(); 305 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (buffer); 306 307 Test1 oneTest = new Test1(); 308 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 309 oneTest.setField("TestKey", testKey); 310 oneTest.add(); 311 312 lf.setCriteria(oneTest); 313 lf.saveBlob("BlobTest", bis, buffer.length); 314 315 Blob blob = lf.getBlob("BlobTest"); 316 Test1 oneTest2 = new Test1(); 317 oneTest2.setDataContext(TestSystemInitializer.getTestContext()); 318 oneTest2.setField("TestKey", testKey2); 319 oneTest2.add(); 320 lf2.setCriteria(oneTest2); 321 lf2.saveBlob("BlobTest", blob); 322 323 java.io.InputStream is = lf2.getBlobStream("BlobTest"); 324 325 assertTrue("Blob Input stream data must not be null", is != null); 326 327 java.io.ObjectInputStream ois = new java.io.ObjectInputStream (is); 328 Object o = ois.readObject(); 329 assertTrue("Read object must not be null", o != null); 330 assertTrue("Read object must be of type java.lang.Integer[]", o instanceof Integer []); 331 Integer [] i = (Integer []) o; 332 333 boolean objectsEqual = true; 334 for (int j = 0; j < i.length; j++) { 335 if (i[j].intValue() != testIntArray[j].intValue()) { 336 objectsEqual = false; 337 break; 338 } 339 } 340 assertTrue("Binary field data must be equal to original data.", objectsEqual); 341 342 343 } catch (Throwable t) { 344 t.printStackTrace(); 345 fail("Caught exception testing BLOB Support: " + t.getMessage()); 346 } finally { 347 lf.close(); 348 } 349 } 350 351 356 public void testCLOBSupport() { 357 final String testString = "LONG Character Update Test"; 358 final String testKey = "1"; 359 LobField lf = new LobField(); 360 try { 361 Test1 oneTest = new Test1(); 362 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 363 oneTest.setField("TestKey", testKey); 364 oneTest.add(); 365 366 lf.setCriteria(oneTest); 367 java.io.InputStream is = lf.getBlobStream("BlobTest"); 368 assertTrue("BLOB Stream should be null", is == null); 369 370 lf.saveClob("ClobTest", testString); 371 String fieldData = lf.getClobString("ClobTest"); 372 373 assertTrue("CLOB Field data must not be null", fieldData != null); 374 375 assertTrue("CLOB field data must be equal to the original data. Got " 376 + fieldData + " instead", testString.equals(fieldData)); 377 378 } catch (Throwable t) { 379 t.printStackTrace(); 380 fail("Caught exception testing CLOB Support: " + t.getMessage()); 381 } finally { 382 lf.close(); 383 } 384 } 385 386 390 public void testNullSupport() { 391 final String testString = "LONG Character Update Test"; 392 final String testKey = "1"; 393 LobField lf = new LobField(); 394 try { 395 Test1 oneTest = new Test1(); 396 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 397 oneTest.setField("TestKey", testKey); 398 oneTest.add(); 399 400 lf.setCriteria(oneTest); 401 lf.saveClob("ClobTest", (String ) null); 402 String fieldData = lf.getClobString("ClobTest"); 403 assertTrue("Field data should be null", fieldData == null); 404 405 } catch (Throwable t) { 406 t.printStackTrace(); 407 fail("Caught exception testing Null Support: " + t.getMessage()); 408 } finally { 409 lf.close(); 410 } 411 412 } 413 414 415 } | Popular Tags |