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_Handle.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.io.FileInputStream;
029: import java.io.FileOutputStream;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032:
033: import javax.ejb.EJBHome;
034: import javax.ejb.Handle;
035: import javax.ejb.HomeHandle;
036: import javax.rmi.PortableRemoteObject;
037:
038: import org.objectweb.jonas.jtests.beans.ebasic.Simple;
039: import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
040: import org.objectweb.jonas.jtests.util.JTestCase;
041:
042: /**
043: * test cases common to both suites CMP and BMP.
044: */
045: public abstract class A_Handle extends JTestCase {
046:
047: public A_Handle(String name) {
048: super (name);
049: }
050:
051: protected void setUp() {
052: super .setUp();
053: useBeans("ebasic", true);
054: }
055:
056: /**
057: * return SimpleHome, that can be either BMP or CMP bean.
058: */
059: abstract public SimpleHome getHome();
060:
061: /**
062: * Testing serialization of EJBObject handle
063: *
064: *
065: */
066: public void testHandle() throws Exception {
067: int val1 = 11;
068: int val2 = 12;
069: Simple s1 = getHome().create("pkforhandle", val1, val2);
070: Handle handle1 = s1.getHandle();
071: FileOutputStream fos = new FileOutputStream("handleSimple.ser");
072: ObjectOutputStream outStream = new ObjectOutputStream(fos);
073: //System.out.println("Writing handle to file...");
074: outStream.writeObject(handle1);
075: outStream.flush();
076: fos.close();
077:
078: // Deserialize the Handle handleSimple
079: FileInputStream fis = new FileInputStream("handleSimple.ser");
080: ObjectInputStream inStream = new ObjectInputStream(fis);
081: //System.out.println("Reading handle from file...");
082: Handle handle2 = (Handle) inStream.readObject();
083: fis.close();
084:
085: // Reobtain a remote reference .
086: //System.out.println("Acquiring reference using deserialized handle2...");
087: Object ref = handle2.getEJBObject();
088: Simple s2 = (Simple) PortableRemoteObject.narrow(ref,
089: Simple.class);
090:
091: try {
092: assertTrue(s1.isIdentical(s2));
093: } finally {
094: s1.remove();
095: }
096: }
097:
098: /**
099: * Testing serialization of EJBHome handle
100: *
101: *
102: */
103: public void testHomeHandle() throws Exception {
104: SimpleHome sh1 = getHome();
105: Simple s1 = sh1.create("pkforhomehandle", 100, 102);
106: HomeHandle hhandle1 = sh1.getHomeHandle();
107: FileOutputStream fos = new FileOutputStream(
108: "homehandleSimple.ser");
109: ObjectOutputStream outStream = new ObjectOutputStream(fos);
110: //System.out.println("Writing homehandle to file...");
111: outStream.writeObject(hhandle1);
112: outStream.flush();
113: fos.close();
114: // Deserialize the Handle handleSimple
115: FileInputStream fis = new FileInputStream(
116: "homehandleSimple.ser");
117: ObjectInputStream inStream = new ObjectInputStream(fis);
118: //System.out.println("Reading homehandle from file...");
119: HomeHandle hhandle2 = (HomeHandle) inStream.readObject();
120: fis.close();
121: EJBHome eh = hhandle2.getEJBHome();
122: SimpleHome sh2 = (SimpleHome) PortableRemoteObject.narrow(eh,
123: SimpleHome.class);
124: try {
125: Simple s2 = sh2.findByPrimaryKey("pkforhomehandle");
126: } finally {
127: s1.remove();
128: }
129:
130: }
131: }
|