001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.entity.cmp2;
017:
018: import java.rmi.MarshalledObject;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.ByteArrayInputStream;
022: import java.io.ObjectInputStream;
023: import javax.ejb.EJBObject;
024: import javax.ejb.Handle;
025:
026: import org.apache.openejb.test.entity.cmp.BasicCmpHome;
027:
028: /**
029: * [7] Should be run as the seventh test suite of the BasicCmpTestClients
030: */
031: public class Cmp2HandleTests extends BasicCmp2TestClient {
032:
033: public Cmp2HandleTests() {
034: super ("Handle.");
035: }
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: Object obj = initialContext
040: .lookup("client/tests/entity/cmp2/BasicCmpHome");
041: ejbHome = (BasicCmpHome) javax.rmi.PortableRemoteObject.narrow(
042: obj, BasicCmpHome.class);
043: ejbObject = ejbHome.createObject("Fifth Bean");
044: ejbHandle = ejbObject.getHandle();
045: }
046:
047: protected void tearDown() throws Exception {
048: if (ejbObject != null) {
049: ejbObject.remove();
050: }
051: super .tearDown();
052: }
053:
054: //=================================
055: // Test handle methods
056: //
057: public void test01_getEJBObject() {
058:
059: try {
060: EJBObject object = ejbHandle.getEJBObject();
061: assertNotNull("The EJBObject is null", object);
062: // Wait until isIdentical is working.
063: //assertTrue("EJBObjects are not identical", object.isIdentical(ejbObject));
064: } catch (Exception e) {
065: fail("Received Exception " + e.getClass() + " : "
066: + e.getMessage());
067: }
068: }
069:
070: public void Xtest02_copyHandleByMarshalledObject() {
071: try {
072: MarshalledObject obj = new MarshalledObject(ejbHandle);
073: Handle copy = (Handle) obj.get();
074:
075: EJBObject object = copy.getEJBObject();
076: assertNotNull("The EJBObject is null", object);
077: assertTrue("EJBObjects are not identical", object
078: .isIdentical(ejbObject));
079: } catch (Exception e) {
080: fail("Received Exception " + e.getClass() + " : "
081: + e.getMessage());
082: }
083: }
084:
085: public void Xtest03_copyHandleBySerialize() {
086: try {
087: ByteArrayOutputStream baos = new ByteArrayOutputStream();
088: ObjectOutputStream oos = new ObjectOutputStream(baos);
089: oos.writeObject(ejbHandle);
090: oos.flush();
091: oos.close();
092: ByteArrayInputStream bais = new ByteArrayInputStream(baos
093: .toByteArray());
094: ObjectInputStream ois = new ObjectInputStream(bais);
095: Handle copy = (Handle) ois.readObject();
096:
097: EJBObject object = copy.getEJBObject();
098: assertNotNull("The EJBObject is null", object);
099: assertTrue("EJBObjects are not identical", object
100: .isIdentical(ejbObject));
101: } catch (Exception e) {
102: fail("Received Exception " + e.getClass() + " : "
103: + e.getMessage());
104: }
105: }
106:
107: /**
108: * This remove method of the EJBHome is placed hear as it
109: * is more a test on the handle then on the remove method
110: * itself.
111: */
112: public void test04_EJBHome_remove() {
113: try {
114: ejbHome.remove(ejbHandle);
115: try {
116: ejbObject.businessMethod("Should throw an exception");
117: assertTrue(
118: "Calling business method after removing the EJBObject does not throw an exception",
119: false);
120: } catch (Exception e) {
121: assertTrue(true);
122: return;
123: }
124: } catch (Exception e) {
125: fail("Received Exception " + e.getClass() + " : "
126: + e.getMessage());
127: } finally {
128: ejbObject = null;
129: }
130: }
131:
132: //
133: // Test handle methods
134: //=================================
135: }
|