1 22 package org.jboss.test.cmp2.lob; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.InputStream ; 26 import java.io.StringWriter ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import javax.naming.InitialContext ; 33 import javax.rmi.PortableRemoteObject ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 import net.sourceforge.junitejb.EJBTestCase; 38 39 import org.jboss.logging.Logger; 40 import org.jboss.test.JBossTestCase; 41 42 51 public class LOBUnitTestCase extends EJBTestCase 52 { 53 private static final Integer LOB_PK0 = new Integer (0); 54 private static final Integer LOB_PK1 = new Integer (1); 55 private static final Integer LOB_PK2 = new Integer (2); 56 private static final Integer LOB_PK3 = new Integer (3); 57 private static final Integer LOB_PK4 = new Integer (4); 58 private String SMALL_TEXT_FILE_PATH = "data/style.xsl"; 59 private String BIG_TEXT_FILE_PATH = "data/page.html"; 60 private String SMALL_BINARY_FILE_PATH = "data/smallimage.png"; 61 private String BIG_BINARY_FILE_PATH = "data/image.png"; 62 static final String PI = "3.14159265358979323846264338327950288419716939937510"; 63 64 private LOBHome lobHome; 66 private FacadeHome facadeHome; 67 private boolean resourcesLoaded; 68 private String smallString; 69 private String bigString; 70 private byte[] smallBlob; 71 private byte[] bigBlob; 72 73 75 private static final Logger log = Logger.getLogger(LOBUnitTestCase.class); 76 77 public static Test suite() throws Exception 78 { 79 TestSuite suite = new TestSuite(); 82 suite.addTest(new LOBUnitTestCase("testCreate0")); 83 suite.addTest(new LOBUnitTestCase("testNullLoad")); 84 suite.addTest(new LOBUnitTestCase("testCreate1")); 85 suite.addTest(new LOBUnitTestCase("testLoad1")); 86 suite.addTest(new LOBUnitTestCase("testCreate2")); 87 suite.addTest(new LOBUnitTestCase("testLoad2")); 88 suite.addTest(new LOBUnitTestCase("testCreate3")); 89 suite.addTest(new LOBUnitTestCase("testLoad3")); 90 suite.addTest(new LOBUnitTestCase("testCreate4")); 91 suite.addTest(new LOBUnitTestCase("testLoad4")); 92 suite.addTest(new LOBUnitTestCase("testTextLoad")); 93 suite.addTest(new LOBUnitTestCase("testBinaryLoad")); 94 suite.addTest(new LOBUnitTestCase("testMapCMPField")); 95 suite.addTest(new LOBUnitTestCase("testSetCMPField")); 96 suite.addTest(new LOBUnitTestCase("testListCMPField")); 97 suite.addTest(new LOBUnitTestCase("testBinaryDataField")); 98 suite.addTest(new LOBUnitTestCase("testValueHolder")); 99 suite.addTest(new LOBUnitTestCase("testCleanGetValueHolder")); 100 suite.addTest(new LOBUnitTestCase("testStateFactoryValueHolder")); 101 102 return JBossTestCase.getDeploySetup(suite, "cmp2-lob.jar"); 103 } 104 105 107 public LOBUnitTestCase(String name) 108 throws java.io.IOException 109 { 110 super(name); 111 } 112 113 115 118 public void testCreate0() 119 throws Exception 120 { 121 log.debug("testCreate1"); 122 LOB aLob = lobHome.create(LOB_PK0); 123 aLob.setBigString(null); 124 aLob.setBinaryData(null); 125 aLob.setObjectField(null); 126 } 127 128 132 public void testNullLoad() 133 throws Exception 134 { 135 log.debug("testNullLoad"); 136 LOB aLob = lobHome.findByPrimaryKey(LOB_PK0); 137 assertNull(aLob.getBigString()); 138 assertNull(aLob.getBinaryData()); 139 assertNull(aLob.getObjectField()); 140 } 141 142 145 public void testCreate1() 146 throws Exception 147 { 148 log.debug("testCreate1"); 149 LOB aLob = lobHome.create(LOB_PK1); 150 aLob.setBigString(smallString); 151 aLob.setBinaryData(smallBlob); 152 BlobInfo info = new BlobInfo(); 153 info.setBytes(PI.getBytes()); 154 info.setNumber(314); 155 aLob.setObjectField(info); 156 } 157 158 161 public void testLoad1() 162 throws Exception 163 { 164 log.debug("testLoad1"); 165 LOB aLob = lobHome.findByPrimaryKey(LOB_PK1); 166 assertEquals(smallString, aLob.getBigString()); 167 assertEquals(smallBlob, aLob.getBinaryData()); 168 BlobInfo info = (BlobInfo) aLob.getObjectField(); 169 assertTrue(Arrays.equals(info.getBytes(), PI.getBytes())); 170 assertTrue(info.getNumber() == 314); 171 } 172 173 177 public void testCreate2() 178 throws Exception 179 { 180 log.debug("testCreate2"); 181 LOB aLob = lobHome.create(LOB_PK2); 182 aLob.setBigString(bigString); 183 aLob.setBinaryData(smallBlob); 184 } 185 186 189 public void testLoad2() 190 throws Exception 191 { 192 log.debug("testLoad2"); 193 LOB aLob = lobHome.findByPrimaryKey(LOB_PK2); 194 assertEquals(bigString, aLob.getBigString()); 195 assertEquals(smallBlob, aLob.getBinaryData()); 196 } 197 198 202 public void testCreate3() 203 throws Exception 204 { 205 log.debug("testCreate3"); 206 LOB aLob = lobHome.create(LOB_PK3); 207 aLob.setBigString(smallString); 208 aLob.setBinaryData(bigBlob); 209 } 210 211 212 215 public void testLoad3() 216 throws Exception 217 { 218 log.debug("testLoad3"); 219 LOB aLob = lobHome.findByPrimaryKey(LOB_PK3); 220 assertEquals(smallString, aLob.getBigString()); 221 assertEquals(bigBlob, aLob.getBinaryData()); 222 } 223 224 227 public void testCreate4() 228 throws Exception 229 { 230 log.debug("testCreate4"); 231 LOB aLob = lobHome.create(LOB_PK4); 232 aLob.setBigString(bigString); 233 aLob.setBinaryData(bigBlob); 234 } 235 236 239 public void testLoad4() 240 throws Exception 241 { 242 log.debug("testLoad4"); 243 LOB aLob = lobHome.findByPrimaryKey(LOB_PK4); 244 assertEquals(bigString, aLob.getBigString()); 245 assertEquals(bigBlob, aLob.getBinaryData()); 246 } 247 248 252 public void testTextLoad() 253 throws Exception 254 { 255 log.debug("testTextLoad"); 256 LOB aLob = lobHome.findByPrimaryKey(LOB_PK1); 257 assertEquals(smallString, aLob.getBigString()); 258 259 aLob = lobHome.findByPrimaryKey(LOB_PK2); 260 assertEquals(bigString, aLob.getBigString()); 261 262 aLob = lobHome.findByPrimaryKey(LOB_PK3); 263 assertEquals(smallString, aLob.getBigString()); 264 265 aLob = lobHome.findByPrimaryKey(LOB_PK4); 266 assertEquals(bigString, aLob.getBigString()); 267 } 268 269 273 public void testBinaryLoad() 274 throws Exception 275 { 276 log.debug("testBinaryLoad"); 277 LOB aLob = lobHome.findByPrimaryKey(LOB_PK1); 278 assertEquals(smallBlob, aLob.getBinaryData()); 279 280 aLob = lobHome.findByPrimaryKey(LOB_PK2); 281 assertEquals(smallBlob, aLob.getBinaryData()); 282 283 aLob = lobHome.findByPrimaryKey(LOB_PK3); 284 assertEquals(bigBlob, aLob.getBinaryData()); 285 286 aLob = lobHome.findByPrimaryKey(LOB_PK4); 287 assertEquals(bigBlob, aLob.getBinaryData()); 288 } 289 290 294 public void testMapCMPField() throws Exception 295 { 296 log.debug("testMapCMPField> start"); 297 Facade facade = facadeHome.create(); 298 Integer id = new Integer (111); 299 try 300 { 301 facade.createLOB(id); 302 303 Map oldMap = facade.getMapField(id); 305 facade.addMapEntry(id, "key", "value"); 306 Map curMap = facade.getMapField(id); 307 assertTrue("!oldMap.equals(curMap)", !oldMap.equals(curMap)); 308 309 oldMap = curMap; 311 facade.addMapEntry(id, "key", "value"); 312 curMap = facade.getMapField(id); 313 assertTrue("oldMap.equals(curMap)", oldMap.equals(curMap)); 314 } 315 finally 316 { 317 try { facade.removeLOB(id); } catch(Exception e) {} 318 } 319 } 320 321 public void testSetCMPField() throws Exception 322 { 323 log.debug("testSetCMPField> start"); 324 Facade facade = facadeHome.create(); 325 Integer id = new Integer (111); 326 try 327 { 328 facade.createLOB(id); 329 330 Set oldSet = facade.getSetField(id); 332 facade.addSetElement(id, "value"); 333 Set curSet = facade.getSetField(id); 334 assertTrue("!oldSet.equals(curSet)", !oldSet.equals(curSet)); 335 336 oldSet = curSet; 338 facade.addSetElement(id, "value"); 339 curSet = facade.getSetField(id); 340 assertTrue("oldSet.equals(curSet)", oldSet.equals(curSet)); 341 } 342 finally 343 { 344 try { facade.removeLOB(id); } catch(Exception e) {} 345 } 346 } 347 348 public void testListCMPField() throws Exception 349 { 350 log.debug("testListCMPField> start"); 351 Facade facade = facadeHome.create(); 352 Integer id = new Integer (111); 353 try 354 { 355 facade.createLOB(id); 356 357 List oldList = facade.getListField(id); 359 facade.addListElement(id, "value"); 360 List curList = facade.getListField(id); 361 assertTrue("!oldList.equals(curList)", !oldList.equals(curList)); 362 363 oldList = curList; 365 facade.addListElement(id, "value"); 366 curList = facade.getListField(id); 367 assertTrue("curList.size() - oldList.size() == 1", curList.size() - oldList.size() == 1); 368 } 369 finally 370 { 371 try { facade.removeLOB(id); } catch(Exception e) {} 372 } 373 } 374 375 public void testBinaryDataField() throws Exception 376 { 377 log.debug("testBinaryDataField> start"); 378 Facade facade = facadeHome.create(); 379 Integer id = new Integer (111); 380 try 381 { 382 facade.createLOB(id); 383 384 facade.setBinaryData(id, new byte[]{1, 2, 3}); 386 assertTrue("facade.getBinaryDataElement(id, 1) == 2", 387 facade.getBinaryDataElement(id, 1) == 2); 388 389 facade.setBinaryDataElement(id, 1, (byte)5); 390 assertTrue("facade.getBinaryDataElement(id, 1) == 5", 391 facade.getBinaryDataElement(id, 1) == 5); 392 } 393 finally 394 { 395 try { facade.removeLOB(id); } catch(Exception e) {} 396 } 397 } 398 399 public void testValueHolder() throws Exception 400 { 401 log.debug("testValueHolder> start"); 402 Facade facade = facadeHome.create(); 403 Integer id = new Integer (555); 404 try 405 { 406 facade.createLOB(id); 407 408 assertTrue("facade.getValueHolderValue(id) == null", facade.getValueHolderValue(id) == null); 409 410 facade.setValueHolderValue(id, "Avoka"); 411 assertTrue("facade.getValueHolderValue(id).equals(\"Avoka\")", facade.getValueHolderValue(id).equals("Avoka")); 412 } 413 finally 414 { 415 try { facade.removeLOB(id); } catch(Exception e) {} 416 } 417 } 418 419 public void testCleanGetValueHolder() throws Exception 420 { 421 log.debug("testCleanGetValueHolder> start"); 422 Facade facade = facadeHome.create(); 423 Integer id = new Integer (777); 424 try 425 { 426 facade.createLOB(id); 427 428 assertTrue("facade.getCleanGetValueHolderValue(id) == null", facade.getCleanGetValueHolderValue(id) == null); 429 430 facade.setCleanGetValueHolderValue(id, "Avoka"); 431 assertTrue("facade.getCleanGetValueHolderValue(id).equals(\"Avoka\")", 432 facade.getCleanGetValueHolderValue(id).equals("Avoka")); 433 434 facade.modifyCleanGetValueHolderValue(id, "Ataka"); 435 assertTrue("facade.getCleanGetValueHolderValue(id).equals(\"Avoka\")", 436 facade.getCleanGetValueHolderValue(id).equals("Avoka")); 437 } 438 finally 439 { 440 try { facade.removeLOB(id); } catch(Exception e) {} 441 } 442 } 443 444 public void testStateFactoryValueHolder() throws Exception 445 { 446 log.debug("testStateFactoryValueHolder> start"); 447 Facade facade = facadeHome.create(); 448 Integer id = new Integer (777); 449 try 450 { 451 facade.createLOB(id); 452 453 assertTrue("facade.getStateFactoryValueHolderValue(id) == null", 454 facade.getStateFactoryValueHolderValue(id) == null); 455 456 facade.modifyStateFactoryValueHolderValue(id, "Avoka"); 457 assertTrue("facade.getStateFactoryValueHolderValue(id) == null", 458 facade.getStateFactoryValueHolderValue(id) == null); 459 460 facade.setStateFactoryValueHolderValue(id, "Avoka"); 461 assertTrue("facade.getStateFactoryValueHolderValue(id).equals(\"Avoka\")", 462 facade.getStateFactoryValueHolderValue(id).equals("Avoka")); 463 } 464 finally 465 { 466 try { facade.removeLOB(id); } catch(Exception e) {} 467 } 468 } 469 470 474 public void setUpEJB() 475 throws Exception 476 { 477 log.debug("setupEJB"); 478 479 if(!resourcesLoaded) 480 { 481 InitialContext initialContext = new InitialContext (); 482 Object home = initialContext.lookup(LOBHome.LOB_HOME_CONTEXT); 483 lobHome = (LOBHome)PortableRemoteObject.narrow(home, LOBHome.class); 484 home = initialContext.lookup(FacadeHome.JNDI_NAME); 485 facadeHome = (FacadeHome)PortableRemoteObject.narrow(home, FacadeHome.class); 486 487 smallString = loadTextData(SMALL_TEXT_FILE_PATH); 488 bigString = loadTextData(BIG_TEXT_FILE_PATH); 489 smallBlob = loadBinaryData(SMALL_BINARY_FILE_PATH); 490 bigBlob = loadBinaryData(BIG_BINARY_FILE_PATH); 491 resourcesLoaded = true; 492 } 493 } 494 495 498 public void tearDownEJB() 499 throws Exception 500 { 501 log.debug("tearDownEJB"); 502 } 503 504 506 static void assertEquals(byte[] expected, byte[] actual) 507 { 508 assertEquals(expected.length, actual.length); 509 for(int i = 0; i < expected.length; ++i) 510 assertEquals(expected[i], actual[i]); 511 } 512 513 515 520 private static final byte[] loadBinaryData(String resourceName) 521 { 522 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 523 InputStream input = classLoader.getResourceAsStream(resourceName); 524 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 525 try 526 { 527 int byteRead; 528 while((byteRead = input.read()) != -1) 529 baos.write(byteRead); 530 return baos.toByteArray(); 531 } 532 catch(Exception e) 533 { 534 throw new IllegalStateException (e.getMessage()); 535 } 536 finally 537 { 538 try 539 { 540 baos.close(); 541 } 542 catch(Exception e) 543 { 544 } 545 try 546 { 547 input.close(); 548 } 549 catch(Exception e) 550 { 551 } 552 } 553 } 554 555 556 561 private static final String loadTextData(String resourceName) 562 { 563 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 564 InputStream input = classLoader.getResourceAsStream(resourceName); 565 StringWriter stringWriter = new StringWriter (); 566 try 567 { 568 int byteRead; 569 while((byteRead = input.read()) != -1) 570 stringWriter.write(byteRead); 571 return stringWriter.toString(); 572 } 573 catch(Exception e) 574 { 575 throw new IllegalStateException (e.getMessage()); 576 } 577 finally 578 { 579 try 580 { 581 stringWriter.close(); 582 } 583 catch(Exception e) 584 { 585 } 586 try 587 { 588 input.close(); 589 } 590 catch(Exception e) 591 { 592 } 593 } 594 } 595 } 596 | Popular Tags |