KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > lob > LOBUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.cmp2.lob;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.naming.InitialContext JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
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 /**
43  * A test suite to check JBoss data mapping to/from Large Binary Objects (LOBs).
44  *
45  * @see net.sourceforge.junitejb.EJBTestCase
46  *
47  * @version <tt>$Revision: 56709 $</tt>
48  * @author <a HREF="mailto:steve@resolvesw.com">Steve Coy</a>.
49  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
50  */

51 public class LOBUnitTestCase extends EJBTestCase
52 {
53    private static final Integer JavaDoc LOB_PK0 = new Integer JavaDoc(0);
54    private static final Integer JavaDoc LOB_PK1 = new Integer JavaDoc(1);
55    private static final Integer JavaDoc LOB_PK2 = new Integer JavaDoc(2);
56    private static final Integer JavaDoc LOB_PK3 = new Integer JavaDoc(3);
57    private static final Integer JavaDoc LOB_PK4 = new Integer JavaDoc(4);
58    private String JavaDoc SMALL_TEXT_FILE_PATH = "data/style.xsl";
59    private String JavaDoc BIG_TEXT_FILE_PATH = "data/page.html";
60    private String JavaDoc SMALL_BINARY_FILE_PATH = "data/smallimage.png";
61    private String JavaDoc BIG_BINARY_FILE_PATH = "data/image.png";
62    static final String JavaDoc PI = "3.14159265358979323846264338327950288419716939937510";
63
64    // Attributes ----------------------------------------------------
65
private LOBHome lobHome;
66    private FacadeHome facadeHome;
67    private boolean resourcesLoaded;
68    private String JavaDoc smallString;
69    private String JavaDoc bigString;
70    private byte[] smallBlob;
71    private byte[] bigBlob;
72
73    // Static --------------------------------------------------------
74

75    private static final Logger log = Logger.getLogger(LOBUnitTestCase.class);
76
77    public static Test suite() throws Exception JavaDoc
78    {
79       // JBAS-3497, the execution order of tests in this test case is important
80
// so it must be defined explicitly when running under some JVMs
81
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    // Constructors --------------------------------------------------
106

107    public LOBUnitTestCase(String JavaDoc name)
108       throws java.io.IOException JavaDoc
109    {
110       super(name);
111    }
112
113    // Public --------------------------------------------------------
114

115    /**
116     * Attempt to create a LOB entity with NULL attributes.
117     */

118    public void testCreate0()
119       throws Exception JavaDoc
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    /**
129     * Attempt to load the entity created above and ensure that we can recover
130     * the null attributes.
131     */

132    public void testNullLoad()
133       throws Exception JavaDoc
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    /**
143     * Attempt to create a LOB entity with the small dataset.
144     */

145    public void testCreate1()
146       throws Exception JavaDoc
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    /**
159     * Verify the data set created by {@link #testCreate1}.
160     */

161    public void testLoad1()
162       throws Exception JavaDoc
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    /**
174     * Attempt to create a LOB entity with a large text object
175     * and a small binary object.
176     */

177    public void testCreate2()
178       throws Exception JavaDoc
179    {
180       log.debug("testCreate2");
181       LOB aLob = lobHome.create(LOB_PK2);
182       aLob.setBigString(bigString);
183       aLob.setBinaryData(smallBlob);
184    }
185
186    /**
187     * Verify the data set created by {@link#testCreate2}.
188     */

189    public void testLoad2()
190       throws Exception JavaDoc
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    /**
199     * Attempt to create a LOB entity with the small text object
200     * and a large binary object.
201     */

202    public void testCreate3()
203       throws Exception JavaDoc
204    {
205       log.debug("testCreate3");
206       LOB aLob = lobHome.create(LOB_PK3);
207       aLob.setBigString(smallString);
208       aLob.setBinaryData(bigBlob);
209    }
210
211
212    /**
213     * Verify the data set created by {@link#testCreate3}.
214     */

215    public void testLoad3()
216       throws Exception JavaDoc
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    /**
225     * Attempt to create a LOB entity with the large dataset.
226     */

227    public void testCreate4()
228       throws Exception JavaDoc
229    {
230       log.debug("testCreate4");
231       LOB aLob = lobHome.create(LOB_PK4);
232       aLob.setBigString(bigString);
233       aLob.setBinaryData(bigBlob);
234    }
235
236    /**
237     * Verify the data set created by {@link#testCreate4}.
238     */

239    public void testLoad4()
240       throws Exception JavaDoc
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    /**
249     * Attempt to load each entity in turn and verify that they contain the
250     * text data with which they were created.
251     */

252    public void testTextLoad()
253       throws Exception JavaDoc
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    /**
270     * Attempt to load each entity in turn and verify that they contain the
271     * binary data with which they were created.
272     */

273    public void testBinaryLoad()
274       throws Exception JavaDoc
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    //
291
// Map, Set, List as a CMP field types
292
//
293

294    public void testMapCMPField() throws Exception JavaDoc
295    {
296       log.debug("testMapCMPField> start");
297       Facade facade = facadeHome.create();
298       Integer JavaDoc id = new Integer JavaDoc(111);
299       try
300       {
301          facade.createLOB(id);
302
303          // populate the map
304
Map JavaDoc oldMap = facade.getMapField(id);
305          facade.addMapEntry(id, "key", "value");
306          Map JavaDoc curMap = facade.getMapField(id);
307          assertTrue("!oldMap.equals(curMap)", !oldMap.equals(curMap));
308
309          // try to put the same values
310
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 JavaDoc e) {}
318       }
319    }
320
321    public void testSetCMPField() throws Exception JavaDoc
322    {
323       log.debug("testSetCMPField> start");
324       Facade facade = facadeHome.create();
325       Integer JavaDoc id = new Integer JavaDoc(111);
326       try
327       {
328          facade.createLOB(id);
329
330          // populate the set
331
Set JavaDoc oldSet = facade.getSetField(id);
332          facade.addSetElement(id, "value");
333          Set JavaDoc curSet = facade.getSetField(id);
334          assertTrue("!oldSet.equals(curSet)", !oldSet.equals(curSet));
335
336          // try to put the same values
337
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 JavaDoc e) {}
345       }
346    }
347
348    public void testListCMPField() throws Exception JavaDoc
349    {
350       log.debug("testListCMPField> start");
351       Facade facade = facadeHome.create();
352       Integer JavaDoc id = new Integer JavaDoc(111);
353       try
354       {
355          facade.createLOB(id);
356
357          // populate the list
358
List JavaDoc oldList = facade.getListField(id);
359          facade.addListElement(id, "value");
360          List JavaDoc curList = facade.getListField(id);
361          assertTrue("!oldList.equals(curList)", !oldList.equals(curList));
362
363          // try to put the same values
364
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 JavaDoc e) {}
372       }
373    }
374
375    public void testBinaryDataField() throws Exception JavaDoc
376    {
377       log.debug("testBinaryDataField> start");
378       Facade facade = facadeHome.create();
379       Integer JavaDoc id = new Integer JavaDoc(111);
380       try
381       {
382          facade.createLOB(id);
383
384          // populate the list
385
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 JavaDoc e) {}
396       }
397    }
398
399    public void testValueHolder() throws Exception JavaDoc
400    {
401       log.debug("testValueHolder> start");
402       Facade facade = facadeHome.create();
403       Integer JavaDoc id = new Integer JavaDoc(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 JavaDoc e) {}
416       }
417    }
418
419    public void testCleanGetValueHolder() throws Exception JavaDoc
420    {
421       log.debug("testCleanGetValueHolder> start");
422       Facade facade = facadeHome.create();
423       Integer JavaDoc id = new Integer JavaDoc(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 JavaDoc e) {}
441       }
442    }
443
444    public void testStateFactoryValueHolder() throws Exception JavaDoc
445    {
446       log.debug("testStateFactoryValueHolder> start");
447       Facade facade = facadeHome.create();
448       Integer JavaDoc id = new Integer JavaDoc(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 JavaDoc e) {}
467       }
468    }
469
470    /**
471     * Lookup the LOB lobHome and cache it.
472     * Load the test data.
473     */

474    public void setUpEJB()
475       throws Exception JavaDoc
476    {
477       log.debug("setupEJB");
478
479       if(!resourcesLoaded)
480       {
481          InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
482          Object JavaDoc 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    /**
496     * Remove data references so that they can be garbage collected if needed.
497     */

498    public void tearDownEJB()
499       throws Exception JavaDoc
500    {
501       log.debug("tearDownEJB");
502    }
503
504    // Protected -------------------------------------------------------
505

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    // Private -------------------------------------------------------
514

515    /**
516     * Return the content of the input stream provided as a byte array.
517     * @param resourceName resource to read
518     * @return content as a byte array
519     */

520    private static final byte[] loadBinaryData(String JavaDoc resourceName)
521    {
522       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
523       InputStream JavaDoc input = classLoader.getResourceAsStream(resourceName);
524       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
525       try
526       {
527          int byteRead;
528          while((byteRead = input.read()) != -1)
529             baos.write(byteRead);
530          return baos.toByteArray();
531       }
532       catch(Exception JavaDoc e)
533       {
534          throw new IllegalStateException JavaDoc(e.getMessage());
535       }
536       finally
537       {
538          try
539          {
540             baos.close();
541          }
542          catch(Exception JavaDoc e)
543          {
544          }
545          try
546          {
547             input.close();
548          }
549          catch(Exception JavaDoc e)
550          {
551          }
552       }
553    }
554
555
556    /**
557     * Return the content of the input stream provided as a String.
558     * @param resourceName resource to read
559     * @return content as a string
560     */

561    private static final String JavaDoc loadTextData(String JavaDoc resourceName)
562    {
563       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
564       InputStream JavaDoc input = classLoader.getResourceAsStream(resourceName);
565       StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
566       try
567       {
568          int byteRead;
569          while((byteRead = input.read()) != -1)
570             stringWriter.write(byteRead);
571          return stringWriter.toString();
572       }
573       catch(Exception JavaDoc e)
574       {
575          throw new IllegalStateException JavaDoc(e.getMessage());
576       }
577       finally
578       {
579          try
580          {
581             stringWriter.close();
582          }
583          catch(Exception JavaDoc e)
584          {
585          }
586          try
587          {
588             input.close();
589          }
590          catch(Exception JavaDoc e)
591          {
592          }
593       }
594    }
595 }
596
Popular Tags