001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_EtypeByteArray.java 7533 2005-10-19 15:55:05Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.util.Collection;
029: import java.util.Iterator;
030: import javax.ejb.FinderException;
031: import junit.framework.Assert;
032:
033: import org.objectweb.jonas.jtests.beans.etype.bytearray.ByteArray;
034: import org.objectweb.jonas.jtests.beans.etype.bytearray.ByteArrayHome;
035: import org.objectweb.jonas.jtests.util.JTestCase;
036:
037: /**
038: * This set of test are common to CMP version 1 and CMP version 2
039: * These are tests about byte array field of entity bean .
040: * Beans used: etype/bytearray
041: * @author Helene Joanin
042: */
043: public abstract class A_EtypeByteArray extends JTestCase {
044:
045: public A_EtypeByteArray(String name) {
046: super (name);
047: }
048:
049: protected boolean isInit = false;
050:
051: protected void setUp() {
052: super .setUp();
053: useBeans("bytearray", true);
054: if (!isInit) {
055: ByteArrayHome home = getHome();
056: // check if tables have been initialized
057: try {
058: home.findByPrimaryKey("pk1");
059: } catch (Exception e) {
060: // init here tables
061: try {
062: home.create("pk1", new byte[] { 1 });
063: home.create("pk2", new byte[] { 2, 2 });
064: home.create("pk3", new byte[] { 3, 3, 3 });
065: home.create("pk4", new byte[] { 4, 4, 4, 4 });
066: home.create("pk5", new byte[] { 5, 5, 5, 5, 5 });
067: home.create("pk5bis", new byte[] { 5, 5, 5, 5, 5 });
068: home.create("pktoremove", new byte[] { 100 });
069: } catch (Exception i) {
070: fail("InitialState creation problem: " + i);
071: }
072: }
073: isInit = true;
074: }
075: }
076:
077: /**
078: * Return ByteArrayHome, that can be either CMP version 1 or CMP version 2 bean.
079: */
080: abstract public ByteArrayHome getHome();
081:
082: /**
083: * findByPrimaryKey() test
084: */
085: public void testByteArrayFindByPk() throws Exception {
086: String pk = "pk1";
087: ByteArray bean = getHome().findByPrimaryKey(pk);
088: Assert.assertEquals("Pk", pk, bean.getPk());
089: }
090:
091: /**
092: * findByF1() test
093: */
094: public void testByteArrayFindByF1() throws Exception {
095: byte[] f1 = new byte[] { 5, 5, 5, 5, 5 };
096: Collection cBeans = getHome().findByF1(f1);
097: int nb = 0;
098: Iterator iBeans = cBeans.iterator();
099: while (iBeans.hasNext()) {
100: ByteArray bean = (ByteArray) javax.rmi.PortableRemoteObject
101: .narrow(iBeans.next(), ByteArray.class);
102: Assert.assertEquals("F1", 5, bean.getF1().length);
103: nb++;
104: }
105: assertEquals("Beans number: ", 2, nb);
106: }
107:
108: /**
109: * getF1() test
110: */
111: public void testByteArrayGetF1() throws Exception {
112: String pk = "pk2";
113: ByteArray bean = getHome().findByPrimaryKey(pk);
114: byte[] f1 = bean.getF1();
115: Assert.assertEquals("Pk", pk, bean.getPk());
116: Assert.assertEquals("F1", 2, f1.length);
117: }
118:
119: /**
120: * setF1() test
121: */
122: public void testByteArraySetF1() throws Exception {
123: String pk = "pk3";
124: ByteArray bean = getHome().findByPrimaryKey(pk);
125: int nb = 30;
126: byte[] f1 = new byte[nb];
127: for (int i = 0; i < nb; i++) {
128: f1[i] = 3;
129: }
130: bean.setF1(f1);
131: Assert.assertEquals("Pk", pk, bean.getPk());
132: Assert.assertEquals("F1", nb, bean.getF1().length);
133: }
134:
135: /**
136: * create() test
137: */
138: public void testByteArrayCreate() throws Exception {
139: String pk = "pkcreated";
140: byte[] f1 = new byte[] { 9, 9, 9, 9, 9, 9, 9, 9, 9 };
141: ByteArray bean = getHome().create(pk, f1);
142: Assert.assertEquals("Pk", pk, bean.getPk());
143: Assert.assertEquals("F1", 9, bean.getF1().length);
144: bean = getHome().findByPrimaryKey(pk);
145: // cleaning
146: bean.remove();
147: }
148:
149: /**
150: * remove() test
151: */
152: public void testByteArrayRemove() throws Exception {
153: String pk = "pktoremove";
154: ByteArray bean = getHome().findByPrimaryKey(pk);
155: byte[] f1 = bean.getF1();
156: bean.remove();
157: try {
158: getHome().findByPrimaryKey(pk);
159: fail("not removed");
160: } catch (FinderException e) {
161: // ok
162: }
163: // cleaning
164: getHome().create(pk, f1);
165: }
166:
167: }
|