001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.test.mdb;
018:
019: import javax.jms.Connection;
020: import javax.jms.Destination;
021: import javax.jms.Message;
022: import javax.jms.MessageConsumer;
023: import javax.jms.MessageProducer;
024: import javax.jms.ObjectMessage;
025: import javax.jms.Session;
026: import java.io.Serializable;
027: import java.util.Map;
028: import java.util.TreeMap;
029:
030: public class MdbConnectionFactoryTests extends MdbTestClient {
031: public MdbConnectionFactoryTests() {
032: super ("ConnectionFactory.");
033: }
034:
035: public void test01_createConnection() throws Exception {
036: Connection connection = createConnection();
037: try {
038: assertNotNull("Jms connection is null.", connection);
039: } finally {
040: MdbUtil.close(connection);
041: }
042: }
043:
044: public void test02_directRpc() throws Exception {
045: Connection connection = createConnection();
046: Session session = null;
047: MessageProducer producer = null;
048: MessageConsumer consumer = null;
049: try {
050:
051: // create request
052: Map<String, Object> request = new TreeMap<String, Object>();
053: request.put("method", "businessMethod(java.lang.String)");
054: request.put("args", new Object[] { "cheese" });
055:
056: // initialize session
057: session = connection.createSession(false,
058: Session.AUTO_ACKNOWLEDGE);
059: Destination requestQueue = session.createQueue("BasicMdb");
060: Destination responseQueue = session.createTemporaryQueue();
061:
062: // Create a request messages
063: ObjectMessage requestMessage = session
064: .createObjectMessage();
065: requestMessage.setJMSReplyTo(responseQueue);
066: requestMessage.setObject((Serializable) request);
067:
068: // Send the request message
069: producer = session.createProducer(requestQueue);
070: producer.send(requestMessage);
071:
072: // System.out.println("\n" + "***************************************\n" +
073: // "Sent request message: " + requestMessage + "\n" +
074: // " request map: " + request + "\n" +
075: // " to queue: " + requestQueue + "\n" +
076: // "***************************************\n\n");
077:
078: // create consumer
079: consumer = session.createConsumer(responseQueue);
080: // System.out.println("\n" + "***************************************\n" +
081: // "Listening for response at : " + responseQueue + "\n" +
082: // "***************************************\n\n");
083:
084: // wait for response mesage
085: Message message = consumer.receive(1000);
086:
087: // verify message
088: assertNotNull("Did not get a response message", message);
089: assertTrue("Response message is not an ObjectMessage",
090: message instanceof ObjectMessage);
091: ObjectMessage responseMessage = (ObjectMessage) message;
092: Serializable object = responseMessage.getObject();
093: assertNotNull("Response ObjectMessage contains a null object");
094: assertTrue(
095: "Response ObjectMessage does not contain an instance of Map",
096: object instanceof Map);
097: Map response = (Map) object;
098:
099: // process results
100: if (response.containsKey("exception")) {
101: throw (Exception) response.get("return");
102: }
103: String returnValue = (String) response.get("return");
104: assertEquals("eseehc", returnValue);
105: } finally {
106: MdbUtil.close(producer);
107: MdbUtil.close(session);
108: MdbUtil.close(connection);
109: }
110: }
111:
112: public void test03_proxy() throws Exception {
113: BasicMdbObject basicMdbObject = MdbProxy.newProxyInstance(
114: BasicMdbObject.class, connectionFactory, "BasicMdb");
115: try {
116: String returnValue = basicMdbObject.businessMethod("blah");
117: assertEquals("halb", returnValue);
118: } finally {
119: MdbProxy.destroyProxy(basicMdbObject);
120: }
121: }
122: }
|