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.client;
017:
018: import java.io.*;
019: import java.lang.reflect.Method;
020: import javax.ejb.EJBHome;
021: import javax.ejb.EJBObject;
022: import javax.ejb.Handle;
023:
024: import junit.framework.TestCase;
025: import org.omg.CORBA.UserException;
026:
027: public class EJBRequestTest extends TestCase {
028: private EJBMetaDataImpl ejb;
029:
030: static interface FooHome extends EJBHome {
031: FooObject create();
032:
033: FooObject findByPrimaryKey(Integer key);
034: }
035:
036: static interface FooObject extends EJBObject {
037: String businessMethod(String param) throws UserException;
038: }
039:
040: protected void setUp() throws Exception {
041: ejb = new EJBMetaDataImpl(FooHome.class, FooObject.class,
042: Integer.class, "BMP_ENTITY", "FooBeanID", null);
043: }
044:
045: public void testEJBHomeCreate() throws Exception {
046: int requestMethod = RequestMethodConstants.EJB_HOME_CREATE;
047: Method method = FooHome.class.getMethod("create",
048: new Class[] {});
049: Object[] args = new Object[] {};
050:
051: invoke(requestMethod, method, args);
052: }
053:
054: public void testEJBHomeFind() throws Exception {
055: int requestMethod = RequestMethodConstants.EJB_HOME_FIND;
056: Method method = FooHome.class.getMethod("findByPrimaryKey",
057: new Class[] { Integer.class });
058: Object[] args = new Object[] { new Integer(4) };
059:
060: invoke(requestMethod, method, args);
061: }
062:
063: public void testEJBHomeRemove1() throws Exception {
064: int requestMethod = RequestMethodConstants.EJB_HOME_REMOVE_BY_HANDLE;
065: Method method = FooHome.class.getMethod("remove",
066: new Class[] { Handle.class });
067: Object[] args = new Object[] { null };
068:
069: invoke(requestMethod, method, args);
070: }
071:
072: public void testEJBHomeRemove2() throws Exception {
073: int requestMethod = RequestMethodConstants.EJB_HOME_REMOVE_BY_PKEY;
074: Method method = FooHome.class.getMethod("remove",
075: new Class[] { Object.class });
076: Object[] args = new Object[] { new Integer(4) };
077:
078: invoke(requestMethod, method, args);
079: }
080:
081: public void testGetMetaData() throws Exception {
082: int requestMethod = RequestMethodConstants.EJB_HOME_GET_EJB_META_DATA;
083: Method method = FooHome.class.getMethod("getEJBMetaData",
084: new Class[] {});
085: Object[] args = new Object[] {};
086:
087: invoke(requestMethod, method, args);
088: }
089:
090: public void testGetHomeHandle() throws Exception {
091: int requestMethod = RequestMethodConstants.EJB_HOME_GET_HOME_HANDLE;
092: Method method = FooHome.class.getMethod("getHomeHandle",
093: new Class[] {});
094: Object[] args = new Object[] {};
095:
096: invoke(requestMethod, method, args);
097: }
098:
099: public void testBusinessMethod() throws Exception {
100: int requestMethod = RequestMethodConstants.EJB_OBJECT_BUSINESS_METHOD;
101: Method method = FooObject.class.getMethod("businessMethod",
102: new Class[] { String.class });
103: Object[] args = new Object[] { "hola mundo" };
104:
105: invoke(requestMethod, method, args);
106: }
107:
108: public void testGetEJBHome() throws Exception {
109: int requestMethod = RequestMethodConstants.EJB_OBJECT_GET_EJB_HOME;
110: Method method = FooObject.class.getMethod("getEJBHome",
111: new Class[] {});
112: Object[] args = new Object[] {};
113:
114: invoke(requestMethod, method, args);
115: }
116:
117: public void testGetHandle() throws Exception {
118: int requestMethod = RequestMethodConstants.EJB_OBJECT_GET_HANDLE;
119: Method method = FooObject.class.getMethod("getHandle",
120: new Class[] {});
121: Object[] args = new Object[] {};
122:
123: invoke(requestMethod, method, args);
124: }
125:
126: public void testGetPrimaryKey() throws Exception {
127: int requestMethod = RequestMethodConstants.EJB_OBJECT_GET_PRIMARY_KEY;
128: Method method = FooObject.class.getMethod("getPrimaryKey",
129: new Class[] {});
130: Object[] args = new Object[] {};
131:
132: invoke(requestMethod, method, args);
133: }
134:
135: public void testIsIdentical() throws Exception {
136: int requestMethod = RequestMethodConstants.EJB_OBJECT_IS_IDENTICAL;
137: Method method = FooObject.class.getMethod("isIdentical",
138: new Class[] { EJBObject.class });
139: Object[] args = new Object[] { null };
140:
141: invoke(requestMethod, method, args);
142: }
143:
144: public void testEJBObjectRemove() throws Exception {
145: int requestMethod = RequestMethodConstants.EJB_OBJECT_REMOVE;
146: Method method = FooObject.class.getMethod("remove",
147: new Class[] {});
148: Object[] args = new Object[] {};
149:
150: invoke(requestMethod, method, args);
151: }
152:
153: private void invoke(int requestMethod, Method method, Object[] args)
154: throws IOException, ClassNotFoundException {
155: EJBRequest expected = new EJBRequest(requestMethod, ejb,
156: method, args, null);
157:
158: EJBRequest actual = new EJBRequest();
159:
160: ByteArrayOutputStream baos = new ByteArrayOutputStream();
161: ObjectOutputStream out = new ObjectOutputStream(baos);
162:
163: expected.writeExternal(out);
164: out.close();
165:
166: ByteArrayInputStream bais = new ByteArrayInputStream(baos
167: .toByteArray());
168: ObjectInputStream in = new ObjectInputStream(bais);
169:
170: actual.readExternal(in);
171: actual.getBody().readExternal(in);
172:
173: assertEquals("RequestType", expected.getRequestType(), actual
174: .getRequestType());
175: assertEquals("RequestMethod", expected.getRequestMethod(),
176: actual.getRequestMethod());
177:
178: assertEquals("DeploymentId", expected.getDeploymentId(), actual
179: .getDeploymentId());
180: assertEquals("DeploymentCode", expected.getDeploymentCode(),
181: actual.getDeploymentCode());
182:
183: assertEquals("PrimaryKey", expected.getPrimaryKey(), actual
184: .getPrimaryKey());
185:
186: assertEquals("ClientIdentity", expected.getClientIdentity(),
187: actual.getClientIdentity());
188:
189: assertEquals("InterfaceClass", expected.getInterfaceClass(),
190: actual.getInterfaceClass());
191:
192: assertEquals("MethodInstance", expected.getMethodInstance(),
193: actual.getMethodInstance());
194:
195: Object[] expectedParams = expected.getMethodParameters();
196: Object[] actualParams = actual.getMethodParameters();
197:
198: assertNotNull("MethodParameters", actualParams);
199: assertEquals("MethodParameters.length", expectedParams.length,
200: actualParams.length);
201: for (int i = 0; i < expectedParams.length; i++) {
202: assertEquals("MethodParameters." + i, expectedParams[i],
203: actualParams[i]);
204: }
205: }
206:
207: }
|