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:
020: package org.apache.axis2.engine;
021:
022: import junit.framework.TestCase;
023: import org.apache.axiom.om.OMAbstractFactory;
024: import org.apache.axiom.om.util.UUIDGenerator;
025: import org.apache.axiom.soap.SOAPFactory;
026: import org.apache.axis2.AxisFault;
027: import org.apache.axis2.addressing.EndpointReference;
028: import org.apache.axis2.context.ConfigurationContext;
029: import org.apache.axis2.context.MessageContext;
030: import org.apache.axis2.context.OperationContext;
031: import org.apache.axis2.context.ServiceContext;
032: import org.apache.axis2.context.ServiceGroupContext;
033: import org.apache.axis2.description.AxisOperation;
034: import org.apache.axis2.description.AxisService;
035: import org.apache.axis2.description.AxisServiceGroup;
036: import org.apache.axis2.description.HandlerDescription;
037: import org.apache.axis2.description.InOutAxisOperation;
038: import org.apache.axis2.description.TransportInDescription;
039: import org.apache.axis2.description.TransportOutDescription;
040: import org.apache.axis2.dispatchers.AddressingBasedDispatcher;
041: import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
042: import org.apache.axis2.dispatchers.SOAPActionBasedDispatcher;
043: import org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher;
044: import org.apache.axis2.handlers.AbstractHandler;
045: import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
046: import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
047: import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050:
051: import javax.xml.namespace.QName;
052: import java.io.File;
053: import java.io.FileInputStream;
054: import java.io.FileOutputStream;
055: import java.io.ObjectInputStream;
056: import java.io.ObjectOutputStream;
057: import java.util.ArrayList;
058:
059: public class OperationContextSaveTest extends TestCase {
060: protected static final Log log = LogFactory
061: .getLog(OperationContextSaveTest.class);
062:
063: private QName serviceName = new QName("TestService");
064: private QName operationName = new QName("Operation_1");
065:
066: private ConfigurationContext configurationContext = null;
067: private ServiceGroupContext serviceGroupContext = null;
068: private ServiceContext serviceContext = null;
069: private OperationContext operationContext = null;
070:
071: private AxisConfiguration axisConfiguration = null;
072: private AxisServiceGroup axisServiceGroup = null;
073: private AxisService axisService = null;
074: private AxisOperation axisOperation = null;
075:
076: private TransportOutDescription transportOut = null;
077: private TransportInDescription transportIn = null;
078:
079: private MessageContext mc = null;
080:
081: private ArrayList executedHandlers = null;
082:
083: private String testArg = null;
084:
085: public OperationContextSaveTest(String arg0) {
086: super (arg0);
087: testArg = new String(arg0);
088:
089: try {
090: prepare();
091: } catch (Exception e) {
092: log
093: .debug("OperationContextSaveTest:constructor: error in setting up object graph ["
094: + e.getClass().getName()
095: + " : "
096: + e.getMessage() + "]");
097: }
098: }
099:
100: //
101: // prepare the object hierarchy for testing
102: //
103: private void prepare() throws Exception {
104: //-----------------------------------------------------------------
105:
106: axisConfiguration = new AxisConfiguration();
107:
108: configurationContext = new ConfigurationContext(
109: axisConfiguration);
110:
111: configurationContext.getAxisConfiguration().addMessageReceiver(
112: "http://www.w3.org/2004/08/wsdl/in-only",
113: new RawXMLINOnlyMessageReceiver());
114: configurationContext.getAxisConfiguration().addMessageReceiver(
115: "http://www.w3.org/2004/08/wsdl/in-out",
116: new RawXMLINOutMessageReceiver());
117:
118: DispatchPhase dispatchPhase = new DispatchPhase();
119: dispatchPhase.setName("Dispatch");
120:
121: AddressingBasedDispatcher abd = new AddressingBasedDispatcher();
122: abd.initDispatcher();
123:
124: RequestURIBasedDispatcher rud = new RequestURIBasedDispatcher();
125: rud.initDispatcher();
126:
127: SOAPActionBasedDispatcher sabd = new SOAPActionBasedDispatcher();
128: sabd.initDispatcher();
129:
130: SOAPMessageBodyBasedDispatcher smbd = new SOAPMessageBodyBasedDispatcher();
131: smbd.initDispatcher();
132:
133: dispatchPhase.addHandler(abd);
134: dispatchPhase.addHandler(rud);
135: dispatchPhase.addHandler(sabd);
136: dispatchPhase.addHandler(smbd);
137:
138: configurationContext.getAxisConfiguration().getInFlowPhases()
139: .add(dispatchPhase);
140:
141: //-----------------------------------------------------------------
142:
143: axisServiceGroup = new AxisServiceGroup(axisConfiguration);
144: axisServiceGroup.setServiceGroupName("ServiceGroupTest");
145:
146: axisService = new AxisService(serviceName.getLocalPart());
147: axisServiceGroup.addService(axisService);
148:
149: axisOperation = new InOutAxisOperation(operationName);
150: axisOperation.setMessageReceiver(new MessageReceiver() {
151: public void receive(MessageContext messageCtx) {
152:
153: }
154: });
155:
156: axisService.addOperation(axisOperation);
157: axisService.mapActionToOperation(operationName.getLocalPart(),
158: axisOperation);
159:
160: configurationContext.getAxisConfiguration().addService(
161: axisService);
162:
163: //-----------------------------------------------------------------
164:
165: serviceGroupContext = configurationContext
166: .createServiceGroupContext(axisService
167: .getAxisServiceGroup());
168: serviceGroupContext.setId("ServiceGroupContextTest");
169:
170: serviceContext = serviceGroupContext
171: .getServiceContext(axisService);
172:
173: operationContext = serviceContext
174: .createOperationContext(operationName);
175:
176: //-----------------------------------------------------------------
177:
178: transportOut = new TransportOutDescription("null");
179: transportOut.setSender(new CommonsHTTPTransportSender());
180:
181: transportIn = new TransportInDescription("null");
182:
183: //-----------------------------------------------------------------
184:
185: mc = configurationContext.createMessageContext();
186: mc.setTransportIn(transportIn);
187: mc.setTransportOut(transportOut);
188: mc.setTransportOut(transportOut);
189:
190: mc.setServerSide(true);
191: mc.setProperty(MessageContext.TRANSPORT_OUT, System.out);
192: SOAPFactory omFac = OMAbstractFactory.getSOAP11Factory();
193: mc.setEnvelope(omFac.getDefaultEnvelope());
194:
195: Phase phase1 = new Phase("beginPhase1");
196: phase1.addHandler(new TempHandler(1));
197: phase1.addHandler(new TempHandler(2));
198: phase1.addHandler(new TempHandler(3));
199:
200: Phase phase2 = new Phase("middlePhase2");
201: phase2.addHandler(new TempHandler(4));
202: phase2.addHandler(new TempHandler(5));
203: phase2.addHandler(new TempHandler(6));
204: phase2.addHandler(new TempHandler(7));
205: phase2.addHandler(new TempHandler(8));
206:
207: Phase phase3 = new Phase("lastPhase3");
208: phase3.addHandler(new TempHandler(9));
209: phase3.addHandler(new TempHandler(10));
210:
211: axisOperation.getRemainingPhasesInFlow().add(phase1);
212: axisOperation.getRemainingPhasesInFlow().add(phase2);
213: axisOperation.getRemainingPhasesInFlow().add(phase3);
214:
215: mc.setWSAAction(operationName.getLocalPart());
216: mc.setSoapAction(operationName.getLocalPart());
217: System.out.flush();
218:
219: mc.setMessageID(UUIDGenerator.getUUID());
220:
221: //operationContext.addMessageContext(mc); gets done via the register
222: axisOperation.registerOperationContext(mc, operationContext);
223: mc.setOperationContext(operationContext);
224: mc.setServiceContext(serviceContext);
225:
226: mc.setTo(new EndpointReference("axis2/services/NullService"));
227: mc.setWSAAction("DummyOp");
228:
229: //-----------------------------------------------------------------
230:
231: executedHandlers = new ArrayList();
232:
233: }
234:
235: protected void setUp() throws Exception {
236: //org.apache.log4j.BasicConfigurator.configure();
237: }
238:
239: public void testSaveAndRestore() throws Exception {
240: File theFile = null;
241: String theFilename = null;
242: boolean saved = false;
243: boolean restored = false;
244: boolean done = false;
245: boolean comparesOk = false;
246:
247: log
248: .debug("OperationContextSaveTest:testSaveAndRestore(): BEGIN ---------------");
249:
250: // ---------------------------------------------------------
251: // setup a temporary file to use
252: // ---------------------------------------------------------
253: try {
254: theFile = File.createTempFile("OpCtxSave", null);
255: theFilename = theFile.getName();
256: log
257: .debug("OperationContextSaveTest:testSaveAndRestore(): temp file = ["
258: + theFilename + "]");
259: } catch (Exception ex) {
260: log
261: .debug("OperationContextSaveTest:testSaveAndRestore(): error creating temp file = ["
262: + ex.getMessage() + "]");
263: theFile = null;
264: }
265:
266: if (theFile != null) {
267: // ---------------------------------------------------------
268: // save to the temporary file
269: // ---------------------------------------------------------
270: try {
271: // setup an output stream to a physical file
272: FileOutputStream outStream = new FileOutputStream(
273: theFile);
274:
275: // attach a stream capable of writing objects to the
276: // stream connected to the file
277: ObjectOutputStream outObjStream = new ObjectOutputStream(
278: outStream);
279:
280: // try to save the message context
281: log
282: .debug("OperationContextSaveTest:testSaveAndRestore(): saving .....");
283: saved = false;
284: outObjStream.writeObject(operationContext);
285:
286: // close out the streams
287: outObjStream.flush();
288: outObjStream.close();
289: outStream.flush();
290: outStream.close();
291:
292: saved = true;
293: log
294: .debug("OperationContextSaveTest:testSaveAndRestore(): ....save operation completed.....");
295:
296: long filesize = theFile.length();
297: log
298: .debug("OperationContextSaveTest:testSaveAndRestore(): file size after save ["
299: + filesize
300: + "] temp file = ["
301: + theFilename + "]");
302:
303: } catch (Exception ex2) {
304: log
305: .debug("OperationContextSaveTest:testSaveAndRestore(): error during save ["
306: + ex2.getClass().getName()
307: + " : "
308: + ex2.getMessage() + "]");
309: ex2.printStackTrace();
310: }
311:
312: assertTrue(saved);
313:
314: // ---------------------------------------------------------
315: // restore from the temporary file
316: // ---------------------------------------------------------
317: try {
318: // setup an input stream to the file
319: FileInputStream inStream = new FileInputStream(theFile);
320:
321: // attach a stream capable of reading objects from the
322: // stream connected to the file
323: ObjectInputStream inObjStream = new ObjectInputStream(
324: inStream);
325:
326: // try to restore the context
327: log
328: .debug("OperationContextSaveTest:testSaveAndRestore(): restoring .....");
329: restored = false;
330: OperationContext opctx_restored = (OperationContext) inObjStream
331: .readObject();
332: inObjStream.close();
333: inStream.close();
334:
335: opctx_restored.activate(configurationContext);
336:
337: restored = true;
338: log
339: .debug("OperationContextSaveTest:testSaveAndRestore(): ....restored operation completed.....");
340:
341: // compare to original
342: comparesOk = opctx_restored
343: .isEquivalent(operationContext);
344: log
345: .debug("OperationContextSaveTest:testSaveAndRestore(): OperationContext equivalency ["
346: + comparesOk + "]");
347: assertTrue(comparesOk);
348:
349: ServiceContext restored_srvCtx = opctx_restored
350: .getServiceContext();
351: comparesOk = restored_srvCtx
352: .isEquivalent(serviceContext);
353: log
354: .debug("OperationContextSaveTest:testSaveAndRestore(): ServiceContext equivalency ["
355: + comparesOk + "]");
356: assertTrue(comparesOk);
357:
358: ServiceGroupContext restored_sgCtx = restored_srvCtx
359: .getServiceGroupContext();
360: comparesOk = restored_sgCtx
361: .isEquivalent(serviceGroupContext);
362: log
363: .debug("OperationContextSaveTest:testSaveAndRestore(): ServiceGroupContext equivalency ["
364: + comparesOk + "]");
365: assertTrue(comparesOk);
366:
367: } catch (Exception ex2) {
368: log
369: .debug("OperationContextSaveTest:testSaveAndRestore(): error during restore ["
370: + ex2.getClass().getName()
371: + " : "
372: + ex2.getMessage() + "]");
373: ex2.printStackTrace();
374: }
375:
376: assertTrue(restored);
377:
378: // if the save/restore of the operation context succeeded,
379: // then don't keep the temporary file around
380: boolean removeTmpFile = saved && restored && comparesOk;
381: if (removeTmpFile) {
382: try {
383: theFile.delete();
384: } catch (Exception e) {
385: // just absorb it
386: }
387: }
388:
389: // indicate that the temp file was created ok
390: done = true;
391: }
392:
393: // this is false when there are problems with the temporary file
394: assertTrue(done);
395:
396: log
397: .debug("OperationContextSaveTest:testSaveAndRestore(): END ---------------");
398: }
399:
400: public class TempHandler extends AbstractHandler {
401: private Integer index;
402:
403: //-----------------------------------------------------------------
404: // constructors
405: //-----------------------------------------------------------------
406:
407: public TempHandler(int index) {
408: this .index = new Integer(index);
409: init(new HandlerDescription(new String("handler" + index)));
410: }
411:
412: //-----------------------------------------------------------------
413: // methods
414: //-----------------------------------------------------------------
415:
416: public InvocationResponse invoke(MessageContext msgContext)
417: throws AxisFault {
418: log.debug("TempHandler:invoke(): index = [" + index + "]");
419: executedHandlers.add(index);
420: return InvocationResponse.CONTINUE;
421: }
422:
423: }
424:
425: }
|