001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.lob;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.io.InputStream;
026: import java.io.StringWriter;
027: import java.util.Arrays;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import javax.naming.InitialContext;
033: import javax.rmi.PortableRemoteObject;
034:
035: import junit.framework.Test;
036: import junit.framework.TestSuite;
037: import org.jboss.test.util.ejb.EJBTestCase;
038:
039: import org.jboss.logging.Logger;
040: import org.jboss.test.JBossTestCase;
041:
042: /**
043: * A test suite to check JBoss data mapping to/from Large Binary Objects (LOBs).
044: *
045: * @see org.jboss.test.util.ejb.EJBTestCase
046: *
047: * @version <tt>$Revision: 61780 $</tt>
048: * @author <a href="mailto:steve@resolvesw.com">Steve Coy</a>.
049: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
050: */
051: public class LOBUnitTestCase extends EJBTestCase {
052: private static final Integer LOB_PK0 = new Integer(0);
053: private static final Integer LOB_PK1 = new Integer(1);
054: private static final Integer LOB_PK2 = new Integer(2);
055: private static final Integer LOB_PK3 = new Integer(3);
056: private static final Integer LOB_PK4 = new Integer(4);
057: private String SMALL_TEXT_FILE_PATH = "data/style.xsl";
058: private String BIG_TEXT_FILE_PATH = "data/page.html";
059: private String SMALL_BINARY_FILE_PATH = "data/smallimage.png";
060: private String BIG_BINARY_FILE_PATH = "data/image.png";
061: static final String PI = "3.14159265358979323846264338327950288419716939937510";
062:
063: // Attributes ----------------------------------------------------
064: private LOBHome lobHome;
065: private FacadeHome facadeHome;
066: private boolean resourcesLoaded;
067: private String smallString;
068: private String bigString;
069: private byte[] smallBlob;
070: private byte[] bigBlob;
071:
072: // Static --------------------------------------------------------
073:
074: private static final Logger log = Logger
075: .getLogger(LOBUnitTestCase.class);
076:
077: public static Test suite() throws Exception {
078: // JBAS-3497, the execution order of tests in this test case is important
079: // so it must be defined explicitly when running under some JVMs
080: TestSuite suite = new TestSuite();
081: suite.addTest(new LOBUnitTestCase("testCreate0"));
082: suite.addTest(new LOBUnitTestCase("testNullLoad"));
083: suite.addTest(new LOBUnitTestCase("testCreate1"));
084: suite.addTest(new LOBUnitTestCase("testLoad1"));
085: suite.addTest(new LOBUnitTestCase("testCreate2"));
086: suite.addTest(new LOBUnitTestCase("testLoad2"));
087: suite.addTest(new LOBUnitTestCase("testCreate3"));
088: suite.addTest(new LOBUnitTestCase("testLoad3"));
089: suite.addTest(new LOBUnitTestCase("testCreate4"));
090: suite.addTest(new LOBUnitTestCase("testLoad4"));
091: suite.addTest(new LOBUnitTestCase("testTextLoad"));
092: suite.addTest(new LOBUnitTestCase("testBinaryLoad"));
093: suite.addTest(new LOBUnitTestCase("testMapCMPField"));
094: suite.addTest(new LOBUnitTestCase("testSetCMPField"));
095: suite.addTest(new LOBUnitTestCase("testListCMPField"));
096: suite.addTest(new LOBUnitTestCase("testBinaryDataField"));
097: suite.addTest(new LOBUnitTestCase("testValueHolder"));
098: suite.addTest(new LOBUnitTestCase("testCleanGetValueHolder"));
099: suite
100: .addTest(new LOBUnitTestCase(
101: "testStateFactoryValueHolder"));
102:
103: return JBossTestCase.getDeploySetup(suite, "cmp2-lob.jar");
104: }
105:
106: // Constructors --------------------------------------------------
107:
108: public LOBUnitTestCase(String name) throws java.io.IOException {
109: super (name);
110: }
111:
112: // Public --------------------------------------------------------
113:
114: /**
115: * Attempt to create a LOB entity with NULL attributes.
116: */
117: public void testCreate0() throws Exception {
118: log.debug("testCreate1");
119: LOB aLob = lobHome.create(LOB_PK0);
120: aLob.setBigString(null);
121: aLob.setBinaryData(null);
122: aLob.setObjectField(null);
123: }
124:
125: /**
126: * Attempt to load the entity created above and ensure that we can recover
127: * the null attributes.
128: */
129: public void testNullLoad() throws Exception {
130: log.debug("testNullLoad");
131: LOB aLob = lobHome.findByPrimaryKey(LOB_PK0);
132: assertNull(aLob.getBigString());
133: assertNull(aLob.getBinaryData());
134: assertNull(aLob.getObjectField());
135: }
136:
137: /**
138: * Attempt to create a LOB entity with the small dataset.
139: */
140: public void testCreate1() throws Exception {
141: log.debug("testCreate1");
142: LOB aLob = lobHome.create(LOB_PK1);
143: aLob.setBigString(smallString);
144: aLob.setBinaryData(smallBlob);
145: BlobInfo info = new BlobInfo();
146: info.setBytes(PI.getBytes());
147: info.setNumber(314);
148: aLob.setObjectField(info);
149: }
150:
151: /**
152: * Verify the data set created by {@link #testCreate1}.
153: */
154: public void testLoad1() throws Exception {
155: log.debug("testLoad1");
156: LOB aLob = lobHome.findByPrimaryKey(LOB_PK1);
157: assertEquals(smallString, aLob.getBigString());
158: assertEquals(smallBlob, aLob.getBinaryData());
159: BlobInfo info = (BlobInfo) aLob.getObjectField();
160: assertTrue(Arrays.equals(info.getBytes(), PI.getBytes()));
161: assertTrue(info.getNumber() == 314);
162: }
163:
164: /**
165: * Attempt to create a LOB entity with a large text object
166: * and a small binary object.
167: */
168: public void testCreate2() throws Exception {
169: log.debug("testCreate2");
170: LOB aLob = lobHome.create(LOB_PK2);
171: aLob.setBigString(bigString);
172: aLob.setBinaryData(smallBlob);
173: }
174:
175: /**
176: * Verify the data set created by {@link#testCreate2}.
177: */
178: public void testLoad2() throws Exception {
179: log.debug("testLoad2");
180: LOB aLob = lobHome.findByPrimaryKey(LOB_PK2);
181: assertEquals(bigString, aLob.getBigString());
182: assertEquals(smallBlob, aLob.getBinaryData());
183: }
184:
185: /**
186: * Attempt to create a LOB entity with the small text object
187: * and a large binary object.
188: */
189: public void testCreate3() throws Exception {
190: log.debug("testCreate3");
191: LOB aLob = lobHome.create(LOB_PK3);
192: aLob.setBigString(smallString);
193: aLob.setBinaryData(bigBlob);
194: }
195:
196: /**
197: * Verify the data set created by {@link#testCreate3}.
198: */
199: public void testLoad3() throws Exception {
200: log.debug("testLoad3");
201: LOB aLob = lobHome.findByPrimaryKey(LOB_PK3);
202: assertEquals(smallString, aLob.getBigString());
203: assertEquals(bigBlob, aLob.getBinaryData());
204: }
205:
206: /**
207: * Attempt to create a LOB entity with the large dataset.
208: */
209: public void testCreate4() throws Exception {
210: log.debug("testCreate4");
211: LOB aLob = lobHome.create(LOB_PK4);
212: aLob.setBigString(bigString);
213: aLob.setBinaryData(bigBlob);
214: }
215:
216: /**
217: * Verify the data set created by {@link#testCreate4}.
218: */
219: public void testLoad4() throws Exception {
220: log.debug("testLoad4");
221: LOB aLob = lobHome.findByPrimaryKey(LOB_PK4);
222: assertEquals(bigString, aLob.getBigString());
223: assertEquals(bigBlob, aLob.getBinaryData());
224: }
225:
226: /**
227: * Attempt to load each entity in turn and verify that they contain the
228: * text data with which they were created.
229: */
230: public void testTextLoad() throws Exception {
231: log.debug("testTextLoad");
232: LOB aLob = lobHome.findByPrimaryKey(LOB_PK1);
233: assertEquals(smallString, aLob.getBigString());
234:
235: aLob = lobHome.findByPrimaryKey(LOB_PK2);
236: assertEquals(bigString, aLob.getBigString());
237:
238: aLob = lobHome.findByPrimaryKey(LOB_PK3);
239: assertEquals(smallString, aLob.getBigString());
240:
241: aLob = lobHome.findByPrimaryKey(LOB_PK4);
242: assertEquals(bigString, aLob.getBigString());
243: }
244:
245: /**
246: * Attempt to load each entity in turn and verify that they contain the
247: * binary data with which they were created.
248: */
249: public void testBinaryLoad() throws Exception {
250: log.debug("testBinaryLoad");
251: LOB aLob = lobHome.findByPrimaryKey(LOB_PK1);
252: assertEquals(smallBlob, aLob.getBinaryData());
253:
254: aLob = lobHome.findByPrimaryKey(LOB_PK2);
255: assertEquals(smallBlob, aLob.getBinaryData());
256:
257: aLob = lobHome.findByPrimaryKey(LOB_PK3);
258: assertEquals(bigBlob, aLob.getBinaryData());
259:
260: aLob = lobHome.findByPrimaryKey(LOB_PK4);
261: assertEquals(bigBlob, aLob.getBinaryData());
262: }
263:
264: //
265: // Map, Set, List as a CMP field types
266: //
267:
268: public void testMapCMPField() throws Exception {
269: log.debug("testMapCMPField> start");
270: Facade facade = facadeHome.create();
271: Integer id = new Integer(111);
272: try {
273: facade.createLOB(id);
274:
275: // populate the map
276: Map oldMap = facade.getMapField(id);
277: facade.addMapEntry(id, "key", "value");
278: Map curMap = facade.getMapField(id);
279: assertTrue("!oldMap.equals(curMap)", !oldMap.equals(curMap));
280:
281: // try to put the same values
282: oldMap = curMap;
283: facade.addMapEntry(id, "key", "value");
284: curMap = facade.getMapField(id);
285: assertTrue("oldMap.equals(curMap)", oldMap.equals(curMap));
286: } finally {
287: try {
288: facade.removeLOB(id);
289: } catch (Exception e) {
290: }
291: }
292: }
293:
294: public void testSetCMPField() throws Exception {
295: log.debug("testSetCMPField> start");
296: Facade facade = facadeHome.create();
297: Integer id = new Integer(111);
298: try {
299: facade.createLOB(id);
300:
301: // populate the set
302: Set oldSet = facade.getSetField(id);
303: facade.addSetElement(id, "value");
304: Set curSet = facade.getSetField(id);
305: assertTrue("!oldSet.equals(curSet)", !oldSet.equals(curSet));
306:
307: // try to put the same values
308: oldSet = curSet;
309: facade.addSetElement(id, "value");
310: curSet = facade.getSetField(id);
311: assertTrue("oldSet.equals(curSet)", oldSet.equals(curSet));
312: } finally {
313: try {
314: facade.removeLOB(id);
315: } catch (Exception e) {
316: }
317: }
318: }
319:
320: public void testListCMPField() throws Exception {
321: log.debug("testListCMPField> start");
322: Facade facade = facadeHome.create();
323: Integer id = new Integer(111);
324: try {
325: facade.createLOB(id);
326:
327: // populate the list
328: List oldList = facade.getListField(id);
329: facade.addListElement(id, "value");
330: List curList = facade.getListField(id);
331: assertTrue("!oldList.equals(curList)", !oldList
332: .equals(curList));
333:
334: // try to put the same values
335: oldList = curList;
336: facade.addListElement(id, "value");
337: curList = facade.getListField(id);
338: assertTrue("curList.size() - oldList.size() == 1", curList
339: .size()
340: - oldList.size() == 1);
341: } finally {
342: try {
343: facade.removeLOB(id);
344: } catch (Exception e) {
345: }
346: }
347: }
348:
349: public void testBinaryDataField() throws Exception {
350: log.debug("testBinaryDataField> start");
351: Facade facade = facadeHome.create();
352: Integer id = new Integer(111);
353: try {
354: facade.createLOB(id);
355:
356: // populate the list
357: facade.setBinaryData(id, new byte[] { 1, 2, 3 });
358: assertTrue("facade.getBinaryDataElement(id, 1) == 2",
359: facade.getBinaryDataElement(id, 1) == 2);
360:
361: facade.setBinaryDataElement(id, 1, (byte) 5);
362: assertTrue("facade.getBinaryDataElement(id, 1) == 5",
363: facade.getBinaryDataElement(id, 1) == 5);
364: } finally {
365: try {
366: facade.removeLOB(id);
367: } catch (Exception e) {
368: }
369: }
370: }
371:
372: public void testValueHolder() throws Exception {
373: log.debug("testValueHolder> start");
374: Facade facade = facadeHome.create();
375: Integer id = new Integer(555);
376: try {
377: facade.createLOB(id);
378:
379: assertTrue("facade.getValueHolderValue(id) == null", facade
380: .getValueHolderValue(id) == null);
381:
382: facade.setValueHolderValue(id, "Avoka");
383: assertTrue(
384: "facade.getValueHolderValue(id).equals(\"Avoka\")",
385: facade.getValueHolderValue(id).equals("Avoka"));
386: } finally {
387: try {
388: facade.removeLOB(id);
389: } catch (Exception e) {
390: }
391: }
392: }
393:
394: public void testCleanGetValueHolder() throws Exception {
395: log.debug("testCleanGetValueHolder> start");
396: Facade facade = facadeHome.create();
397: Integer id = new Integer(777);
398: try {
399: facade.createLOB(id);
400:
401: assertTrue(
402: "facade.getCleanGetValueHolderValue(id) == null",
403: facade.getCleanGetValueHolderValue(id) == null);
404:
405: facade.setCleanGetValueHolderValue(id, "Avoka");
406: assertTrue(
407: "facade.getCleanGetValueHolderValue(id).equals(\"Avoka\")",
408: facade.getCleanGetValueHolderValue(id).equals(
409: "Avoka"));
410:
411: facade.modifyCleanGetValueHolderValue(id, "Ataka");
412: assertTrue(
413: "facade.getCleanGetValueHolderValue(id).equals(\"Avoka\")",
414: facade.getCleanGetValueHolderValue(id).equals(
415: "Avoka"));
416: } finally {
417: try {
418: facade.removeLOB(id);
419: } catch (Exception e) {
420: }
421: }
422: }
423:
424: public void testStateFactoryValueHolder() throws Exception {
425: log.debug("testStateFactoryValueHolder> start");
426: Facade facade = facadeHome.create();
427: Integer id = new Integer(777);
428: try {
429: facade.createLOB(id);
430:
431: assertTrue(
432: "facade.getStateFactoryValueHolderValue(id) == null",
433: facade.getStateFactoryValueHolderValue(id) == null);
434:
435: facade.modifyStateFactoryValueHolderValue(id, "Avoka");
436: assertTrue(
437: "facade.getStateFactoryValueHolderValue(id) == null",
438: facade.getStateFactoryValueHolderValue(id) == null);
439:
440: facade.setStateFactoryValueHolderValue(id, "Avoka");
441: assertTrue(
442: "facade.getStateFactoryValueHolderValue(id).equals(\"Avoka\")",
443: facade.getStateFactoryValueHolderValue(id).equals(
444: "Avoka"));
445: } finally {
446: try {
447: facade.removeLOB(id);
448: } catch (Exception e) {
449: }
450: }
451: }
452:
453: /**
454: * Lookup the LOB lobHome and cache it.
455: * Load the test data.
456: */
457: public void setUpEJB() throws Exception {
458: log.debug("setupEJB");
459:
460: if (!resourcesLoaded) {
461: InitialContext initialContext = new InitialContext();
462: Object home = initialContext
463: .lookup(LOBHome.LOB_HOME_CONTEXT);
464: lobHome = (LOBHome) PortableRemoteObject.narrow(home,
465: LOBHome.class);
466: home = initialContext.lookup(FacadeHome.JNDI_NAME);
467: facadeHome = (FacadeHome) PortableRemoteObject.narrow(home,
468: FacadeHome.class);
469:
470: smallString = loadTextData(SMALL_TEXT_FILE_PATH);
471: bigString = loadTextData(BIG_TEXT_FILE_PATH);
472: smallBlob = loadBinaryData(SMALL_BINARY_FILE_PATH);
473: bigBlob = loadBinaryData(BIG_BINARY_FILE_PATH);
474: resourcesLoaded = true;
475: }
476: }
477:
478: /**
479: * Remove data references so that they can be garbage collected if needed.
480: */
481: public void tearDownEJB() throws Exception {
482: log.debug("tearDownEJB");
483: }
484:
485: // Protected -------------------------------------------------------
486:
487: static void assertEquals(byte[] expected, byte[] actual) {
488: assertEquals(expected.length, actual.length);
489: for (int i = 0; i < expected.length; ++i)
490: assertEquals(expected[i], actual[i]);
491: }
492:
493: // Private -------------------------------------------------------
494:
495: /**
496: * Return the content of the input stream provided as a byte array.
497: * @param resourceName resource to read
498: * @return content as a byte array
499: */
500: private static final byte[] loadBinaryData(String resourceName) {
501: ClassLoader classLoader = Thread.currentThread()
502: .getContextClassLoader();
503: InputStream input = classLoader
504: .getResourceAsStream(resourceName);
505: ByteArrayOutputStream baos = new ByteArrayOutputStream();
506: try {
507: int byteRead;
508: while ((byteRead = input.read()) != -1)
509: baos.write(byteRead);
510: return baos.toByteArray();
511: } catch (Exception e) {
512: throw new IllegalStateException(e.getMessage());
513: } finally {
514: try {
515: baos.close();
516: } catch (Exception e) {
517: }
518: try {
519: input.close();
520: } catch (Exception e) {
521: }
522: }
523: }
524:
525: /**
526: * Return the content of the input stream provided as a String.
527: * @param resourceName resource to read
528: * @return content as a string
529: */
530: private static final String loadTextData(String resourceName) {
531: ClassLoader classLoader = Thread.currentThread()
532: .getContextClassLoader();
533: InputStream input = classLoader
534: .getResourceAsStream(resourceName);
535: StringWriter stringWriter = new StringWriter();
536: try {
537: int byteRead;
538: while ((byteRead = input.read()) != -1)
539: stringWriter.write(byteRead);
540: return stringWriter.toString();
541: } catch (Exception e) {
542: throw new IllegalStateException(e.getMessage());
543: } finally {
544: try {
545: stringWriter.close();
546: } catch (Exception e) {
547: }
548: try {
549: input.close();
550: } catch (Exception e) {
551: }
552: }
553: }
554: }
|