1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.client.connector.http;
22
23 import javax.jms.QueueSession;
24
25 import junit.framework.Test;
26
27 import org.apache.cactus.JmsRequest;
28 import org.apache.cactus.Request;
29 import org.apache.cactus.ServiceDefinition;
30 import org.apache.cactus.internal.client.jms.JmsClient;
31 import org.apache.cactus.internal.client.jms.JmsClientHelper;
32 import org.apache.cactus.internal.util.JUnitVersionHelper;
33 import org.apache.cactus.spi.client.ResponseObjectFactory;
34 import org.apache.cactus.spi.client.connector.ProtocolHandler;
35 import org.apache.cactus.spi.client.connector.ProtocolState;
36 import org.apache.cactus.util.JmsConfiguration;
37
38 /**
39 * The JMS protocol handler.
40 * @author ptahchiev
41 */
42 public class JmsProtocolHandler implements ProtocolHandler
43 {
44 /**
45 * Cactus configuration data to use. In particular contains useful
46 * configuration data for the HTTP connector (e.g. redirector URL).
47 */
48 private JmsConfiguration configuration;
49
50 private QueueSession session;
51
52 /**
53 * @param theConfiguration configuration data
54 */
55 public JmsProtocolHandler(JmsConfiguration theConfiguration)
56 {
57 this.configuration = theConfiguration;
58 }
59
60 /**
61 * A method that gets executed after the test execution.
62 * @param theProtocolState the state of the protocol after the execution.
63 */
64 public void afterTest(ProtocolState theState) throws Exception
65 {
66 // We simply do nothing here.
67 }
68
69 /**
70 * A method to create a request object.
71 * @return Request
72 */
73 public Request createRequest() {
74 return new JmsRequest(session);
75 }
76
77 /**
78 * TODO implement this method.
79 */
80 public ResponseObjectFactory createResponseObjectFactory(
81 ProtocolState theState) {
82 return null;
83 }
84
85
86 /**
87 * The method that actually runs the tests.
88 *
89 * @param theDelegatedTest
90 * @param theWrappedTest
91 * @param theRequest
92 */
93 public ProtocolState runTest(Test theDelegatedTest, Test theWrappedTest,
94 Request theRequest) throws Throwable
95 {
96 // Create the JMS Request object and creates necessary JMS objects
97 // so that the user can get them in his beginXXX method, so that he
98 // can create the message to send.
99 JmsRequest request = new JmsRequest(
100 JmsClientHelper.getQueueSession());
101
102 // Add Cactus information to the JMS Message
103 request.getMessage().setStringProperty(
104 ServiceDefinition.CLASS_NAME_PARAM, theDelegatedTest.getClass().getName());
105 request.getMessage().setStringProperty(
106 ServiceDefinition.METHOD_NAME_PARAM, (getCurrentTestName(theDelegatedTest)));
107
108 // Start the test
109 new JmsClient().doTest(request);
110
111 return new JmsProtocolState();
112 }
113
114
115 /**
116 * Returns the configuration data.
117 * @return configuration data
118 */
119 private JmsConfiguration getConfiguration()
120 {
121 return this.configuration;
122 }
123
124 /**
125 * @param theDelegatedTest the Cactus test to execute
126 * @return the name of the current test case being executed (it corresponds
127 * to the name of the test method with the "test" prefix removed.
128 * For example, for "testSomeTestOk" would return "someTestOk".
129 */
130 private String getCurrentTestName(Test theDelegatedTest)
131 {
132 return JUnitVersionHelper.getTestCaseName(theDelegatedTest);
133 }
134 }