001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2;
020:
021: import junit.framework.TestCase;
022: import org.apache.axis2.engine.AxisConfiguration;
023: import org.apache.axis2.engine.Phase;
024: import org.apache.axis2.engine.DispatchPhase;
025: import org.apache.axis2.context.ConfigurationContext;
026: import org.apache.axis2.context.ConfigurationContextFactory;
027: import org.apache.axis2.transport.local.LocalTransportSender;
028: import org.apache.axis2.transport.local.LocalTransportReceiver;
029: import org.apache.axis2.description.TransportOutDescription;
030: import org.apache.axis2.description.WSDL2Constants;
031: import org.apache.axis2.description.AxisService;
032: import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
033: import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
034: import org.apache.axis2.deployment.util.Utils;
035: import org.apache.axis2.client.ServiceClient;
036: import org.apache.axis2.client.Options;
037: import org.apache.axis2.addressing.EndpointReference;
038: import org.apache.axiom.soap.SOAP12Constants;
039:
040: import java.util.ArrayList;
041:
042: /**
043: * LocalTestCase is an extendable base class which provides common functionality
044: * for building JUnit tests which exercise Axis2 using the (fast, in-process)
045: * "local" transport.
046: */
047: public class LocalTestCase extends TestCase {
048: /** Our server AxisConfiguration */
049: protected ConfigurationContext serverCtx;
050: protected AxisConfiguration serverConfig;
051:
052: /** Our client ConfigurationContext */
053: protected ConfigurationContext clientCtx;
054:
055: LocalTransportSender sender = new LocalTransportSender();
056:
057: protected void setUp() throws Exception {
058: // Configuration - server side
059: serverCtx = ConfigurationContextFactory
060: .createConfigurationContextFromFileSystem(null);
061: serverConfig = serverCtx.getAxisConfiguration();
062: LocalTransportReceiver.CONFIG_CONTEXT = new ConfigurationContext(
063: serverConfig);
064: LocalTransportReceiver.CONFIG_CONTEXT
065: .setServicePath("services");
066: LocalTransportReceiver.CONFIG_CONTEXT.setContextRoot("local:/");
067: TransportOutDescription tOut = new TransportOutDescription(
068: Constants.TRANSPORT_LOCAL);
069: tOut.setSender(new LocalTransportSender());
070: serverConfig.addTransportOut(tOut);
071:
072: // addInPhases(serverConfig.getInFlowPhases());
073: // DispatchPhase dp = (DispatchPhase)serverConfig.getInFlowPhases().get(1);
074: // dp.addHandler(new AddressingBasedDispatcher());
075: //
076: // addInPhases(serverConfig.getInFaultFlowPhases());
077: //
078: // addOutPhases(serverConfig.getOutFlowPhases());
079: // addOutPhases(serverConfig.getOutFaultFlowPhases());
080:
081: ///////////////////////////////////////////////////////////////////////
082: // Set up raw message receivers for OMElement based tests
083:
084: serverConfig.addMessageReceiver(WSDL2Constants.MEP_URI_IN_ONLY,
085: new RawXMLINOnlyMessageReceiver());
086: serverConfig.addMessageReceiver(WSDL2Constants.MEP_URI_IN_OUT,
087: new RawXMLINOutMessageReceiver());
088: serverConfig.addMessageReceiver(
089: WSDL2Constants.MEP_URI_ROBUST_IN_ONLY,
090: new RawXMLINOutMessageReceiver());
091:
092: ///////////////////////////////////////////////////////////////////////
093: // And client side
094: clientCtx = ConfigurationContextFactory
095: .createConfigurationContextFromFileSystem(null);
096: }
097:
098: /**
099: * Add well-known Phases on the in side
100: *
101: * @param flow the Flow in which to add these Phases
102: */
103: private void addInPhases(ArrayList flow) {
104: flow.add(new Phase("PreDispatch"));
105: Phase dispatchPhase = new DispatchPhase("Dispatch");
106: flow.add(dispatchPhase);
107: }
108:
109: /**
110: * Add well-known Phases on the out side
111: *
112: * @param flow the Flow in which to add these Phases
113: */
114: private void addOutPhases(ArrayList flow) {
115: flow.add(new Phase("MessageOut"));
116: }
117:
118: /**
119: * Deploy a class as a service.
120: *
121: * @param name the service name
122: * @param myClass the Java class to deploy (all methods exposed by default)
123: * @return a fully configured AxisService, already deployed into the server
124: * @throws Exception in case of problems
125: */
126: protected AxisService deployClassAsService(String name,
127: Class myClass) throws Exception {
128: return deployClassAsService(name, myClass, null);
129: }
130:
131: /**
132: * Deploy a class as a service.
133: *
134: * @param name the service name
135: * @param myClass the Java class to deploy (all methods exposed by default)
136: * @param scope the service scope
137: * @return a fully configured AxisService, already deployed into the server
138: * @throws Exception in case of problems
139: */
140: protected AxisService deployClassAsService(String name,
141: Class myClass, String scope) throws Exception {
142: AxisService service = new AxisService(name);
143: if (scope != null)
144: service.setScope(scope);
145:
146: service
147: .addParameter(Constants.SERVICE_CLASS, myClass
148: .getName());
149:
150: Utils.fillAxisService(service, serverConfig, null, null);
151:
152: serverCtx.deployService(service);
153: return service;
154: }
155:
156: /**
157: * Get a pre-initialized ServiceClient set up to talk to our local
158: * server. If you want to set options, call this and then use getOptions()
159: * on the return.
160: *
161: * @return a ServiceClient, pre-initialized to talk using our local sender
162: * @throws org.apache.axis2.AxisFault if there's a problem
163: */
164: protected ServiceClient getClient() throws AxisFault {
165: Options opts = getOptions();
166: ServiceClient client = new ServiceClient(clientCtx, null);
167: client.setOptions(opts);
168: return client;
169: }
170:
171: /**
172: * Get a pre-initialized ServiceClient set up to talk to our local
173: * server. If you want to set options, call this and then use getOptions()
174: * on the return. Clients created using this method have their To EPR
175: * preset to include the address for the service+operation.
176: *
177: * @return a ServiceClient, pre-initialized to talk using our local sender
178: * @throws org.apache.axis2.AxisFault if there's a problem
179: */
180: protected ServiceClient getClient(String serviceName,
181: String operationName) throws AxisFault {
182: String url = LocalTransportReceiver.CONFIG_CONTEXT
183: .getServiceContextPath()
184: + "/" + serviceName;
185:
186: Options opts = getOptions();
187: opts.setTo(new EndpointReference(url));
188: opts.setAction(operationName);
189: ServiceClient client = new ServiceClient(clientCtx, null);
190: client.setOptions(opts);
191: return client;
192: }
193:
194: /**
195: * Get an Options object initialized with the right transport info, defaulting to SOAP 1.2
196: *
197: * @return pre-initialized Options object
198: */
199: protected Options getOptions() {
200: TransportOutDescription td = new TransportOutDescription(
201: "local");
202: td.setSender(sender);
203:
204: Options opts = new Options();
205: opts.setTransportOut(td);
206: opts
207: .setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
208: return opts;
209: }
210: }
|