KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > A_Handle


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_Handle.java,v 1.2 2004/03/19 11:57:15 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32
33 import javax.ejb.EJBHome JavaDoc;
34 import javax.ejb.Handle JavaDoc;
35 import javax.ejb.HomeHandle JavaDoc;
36 import javax.rmi.PortableRemoteObject JavaDoc;
37
38 import org.objectweb.jonas.jtests.beans.ebasic.Simple;
39 import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
40 import org.objectweb.jonas.jtests.util.JTestCase;
41
42 /**
43  * test cases common to both suites CMP and BMP.
44  */

45 public abstract class A_Handle extends JTestCase {
46
47     public A_Handle(String JavaDoc name) {
48         super(name);
49     }
50
51     protected void setUp() {
52         super.setUp();
53         useBeans("ebasic", true);
54     }
55
56     /**
57      * return SimpleHome, that can be either BMP or CMP bean.
58      */

59     abstract public SimpleHome getHome();
60
61     /**
62      * Testing serialization of EJBObject handle
63      *
64      *
65      */

66     public void testHandle() throws Exception JavaDoc {
67         int val1 = 11;
68         int val2 = 12;
69         Simple s1 = getHome().create("pkforhandle", val1, val2);
70         Handle JavaDoc handle1 = s1.getHandle();
71         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("handleSimple.ser");
72         ObjectOutputStream JavaDoc outStream = new ObjectOutputStream JavaDoc(fos);
73         //System.out.println("Writing handle to file...");
74
outStream.writeObject(handle1);
75         outStream.flush();
76         fos.close();
77
78         // Deserialize the Handle handleSimple
79
FileInputStream JavaDoc fis = new FileInputStream JavaDoc("handleSimple.ser");
80         ObjectInputStream JavaDoc inStream = new ObjectInputStream JavaDoc(fis);
81         //System.out.println("Reading handle from file...");
82
Handle JavaDoc handle2 = (Handle JavaDoc)inStream.readObject();
83         fis.close();
84
85         // Reobtain a remote reference .
86
//System.out.println("Acquiring reference using deserialized handle2...");
87
Object JavaDoc ref = handle2.getEJBObject();
88         Simple s2 = (Simple)PortableRemoteObject.narrow(ref, Simple.class);
89         
90         try {
91             assertTrue(s1.isIdentical(s2));
92         } finally {
93             s1.remove();
94         }
95     }
96
97     /**
98      * Testing serialization of EJBHome handle
99      *
100      *
101      */

102     public void testHomeHandle() throws Exception JavaDoc {
103         SimpleHome sh1 = getHome();
104         Simple s1 = sh1.create("pkforhomehandle",100, 102);
105         HomeHandle JavaDoc hhandle1 = sh1.getHomeHandle();
106         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("homehandleSimple.ser");
107         ObjectOutputStream JavaDoc outStream = new ObjectOutputStream JavaDoc(fos);
108         //System.out.println("Writing homehandle to file...");
109
outStream.writeObject(hhandle1);
110         outStream.flush();
111         fos.close();
112         // Deserialize the Handle handleSimple
113
FileInputStream JavaDoc fis = new FileInputStream JavaDoc("homehandleSimple.ser");
114         ObjectInputStream JavaDoc inStream = new ObjectInputStream JavaDoc(fis);
115         //System.out.println("Reading homehandle from file...");
116
HomeHandle JavaDoc hhandle2 = (HomeHandle JavaDoc)inStream.readObject();
117         fis.close();
118         EJBHome JavaDoc eh = hhandle2.getEJBHome();
119         SimpleHome sh2 = (SimpleHome)PortableRemoteObject.narrow(eh,SimpleHome.class) ;
120         try {
121             Simple s2 = sh2.findByPrimaryKey("pkforhomehandle");
122         } finally {
123            s1.remove();
124         }
125
126     }
127 }
128
Popular Tags