0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: */
0019:
0020: package org.apache.axis2.engine;
0021:
0022: import junit.framework.TestCase;
0023: import org.apache.axiom.om.OMAbstractFactory;
0024: import org.apache.axiom.om.util.UUIDGenerator;
0025: import org.apache.axiom.soap.SOAPFactory;
0026: import org.apache.axis2.AxisFault;
0027: import org.apache.axis2.addressing.EndpointReference;
0028: import org.apache.axis2.context.ConfigurationContext;
0029: import org.apache.axis2.context.MessageContext;
0030: import org.apache.axis2.context.OperationContext;
0031: import org.apache.axis2.context.SelfManagedDataManager;
0032: import org.apache.axis2.context.ServiceContext;
0033: import org.apache.axis2.context.ServiceGroupContext;
0034: import org.apache.axis2.description.AxisOperation;
0035: import org.apache.axis2.description.AxisService;
0036: import org.apache.axis2.description.AxisServiceGroup;
0037: import org.apache.axis2.description.InOutAxisOperation;
0038: import org.apache.axis2.description.TransportInDescription;
0039: import org.apache.axis2.description.TransportOutDescription;
0040: import org.apache.axis2.dispatchers.AddressingBasedDispatcher;
0041: import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
0042: import org.apache.axis2.dispatchers.SOAPActionBasedDispatcher;
0043: import org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher;
0044: import org.apache.axis2.handlers.AbstractHandler;
0045: import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
0046: import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
0047: import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
0048: import org.apache.axis2.transport.http.SimpleHTTPServer;
0049: import org.apache.commons.logging.Log;
0050: import org.apache.commons.logging.LogFactory;
0051:
0052: import javax.xml.namespace.QName;
0053: import java.io.ByteArrayInputStream;
0054: import java.io.ByteArrayOutputStream;
0055: import java.io.File;
0056: import java.io.FileInputStream;
0057: import java.io.FileOutputStream;
0058: import java.io.IOException;
0059: import java.io.ObjectInputStream;
0060: import java.io.ObjectOutputStream;
0061: import java.util.ArrayList;
0062: import java.util.Iterator;
0063:
0064: public class MessageContextSelfManagedDataTest extends TestCase {
0065: protected static final Log log = LogFactory
0066: .getLog(MessageContextSelfManagedDataTest.class);
0067:
0068: private File theFile = null;
0069: boolean savedMessageContext = false;
0070: boolean restoredMessageContext = false;
0071:
0072: private String serviceGroupName = new String("NullServiceGroup");
0073: private QName serviceName = new QName("NullService");
0074: private QName operationName = new QName("DummyOp");
0075:
0076: private ConfigurationContext cfgContext = null;
0077: private ServiceGroupContext serviceGroupContext = null;
0078: private ServiceContext serviceContext = null;
0079: private OperationContext operationContext = null;
0080:
0081: private AxisConfiguration axisConfiguration = null;
0082: private AxisServiceGroup axisServiceGroup = null;
0083: private AxisService axisService = null;
0084: private AxisOperation axisOperation = null;
0085:
0086: private TransportOutDescription transportOut = null;
0087: private TransportOutDescription transportOut2 = null;
0088: private TransportOutDescription transportOut3 = null;
0089: private TransportInDescription transportIn = null;
0090: private TransportInDescription transportIn2 = null;
0091: private TransportInDescription transportIn3 = null;
0092:
0093: private MessageContext mc = null;
0094:
0095: private TempHandler01 handler01;
0096: private TempHandler02 handler02;
0097: private TempHandler02 subhandler; // this handler is intended to be a few levels down in executionChain
0098: private TempHandler03 handler03;
0099: private TempHandler04 handler04;
0100: private Phase phase1;
0101: private Phase phase2;
0102: private Phase phase3;
0103: private Phase subPhase;
0104:
0105: // use this to count how many times methods get called for a particular test
0106: private int invokecallcount = 0;
0107:
0108: // key-value pairs to be used for self managed data
0109:
0110: private String key01 = "key01";
0111: private String testData01 = "TempHandler01_01";
0112:
0113: private String key02 = "key02";
0114: private String testData02 = "TempHandler01_02";
0115:
0116: private String key03 = "key03";
0117: private byte[] testData03 = { 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1B,
0118: 0x1C, 0x1D, 0x1E, 0x1F, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0119: 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
0120:
0121: private String key04 = "key04";
0122: private long[] testData04 = { 0L, 1L, -6601664200673063531L,
0123: -7753637088257391858L };
0124:
0125: private String key05 = "key05";
0126: private int testData05 = 123456;
0127:
0128: public MessageContextSelfManagedDataTest(String arg0) {
0129: super (arg0);
0130: initAll();
0131: }
0132:
0133: protected void initAll() {
0134: try {
0135: prepare();
0136: } catch (Exception e) {
0137: log
0138: .debug("MessageContextSelfManagedDataTest:initAll: error in setting up object graph ["
0139: + e.getClass().getName()
0140: + " : "
0141: + e.getMessage() + "]");
0142: }
0143:
0144: if (handler01 == null) {
0145: handler01 = new TempHandler01(101);
0146: }
0147:
0148: if (handler02 == null) {
0149: handler02 = new TempHandler02(102);
0150: }
0151:
0152: if (handler03 == null) {
0153: handler03 = new TempHandler03(103);
0154: }
0155:
0156: if (handler04 == null) {
0157: handler04 = new TempHandler04(104);
0158: }
0159:
0160: if (subhandler == null) {
0161: subhandler = new TempHandler02(1000);
0162: }
0163:
0164: }
0165:
0166: //
0167: // prepare the object hierarchy for testing
0168: //
0169: private void prepare() throws Exception {
0170: //-----------------------------------------------------------------
0171:
0172: axisConfiguration = new AxisConfiguration();
0173:
0174: cfgContext = new ConfigurationContext(axisConfiguration);
0175:
0176: cfgContext.getAxisConfiguration().addMessageReceiver(
0177: "http://www.w3.org/2004/08/wsdl/in-only",
0178: new RawXMLINOnlyMessageReceiver());
0179: cfgContext.getAxisConfiguration().addMessageReceiver(
0180: "http://www.w3.org/2004/08/wsdl/in-out",
0181: new RawXMLINOutMessageReceiver());
0182:
0183: DispatchPhase dispatchPhase = new DispatchPhase();
0184: dispatchPhase.setName("Dispatch");
0185:
0186: AddressingBasedDispatcher abd = new AddressingBasedDispatcher();
0187: abd.initDispatcher();
0188:
0189: RequestURIBasedDispatcher rud = new RequestURIBasedDispatcher();
0190: rud.initDispatcher();
0191:
0192: SOAPActionBasedDispatcher sabd = new SOAPActionBasedDispatcher();
0193: sabd.initDispatcher();
0194:
0195: SOAPMessageBodyBasedDispatcher smbd = new SOAPMessageBodyBasedDispatcher();
0196: smbd.initDispatcher();
0197:
0198: dispatchPhase.addHandler(abd);
0199: dispatchPhase.addHandler(rud);
0200: dispatchPhase.addHandler(sabd);
0201: dispatchPhase.addHandler(smbd);
0202:
0203: cfgContext.getAxisConfiguration().getInFlowPhases().add(
0204: dispatchPhase);
0205:
0206: //-----------------------------------------------------------------
0207:
0208: axisServiceGroup = new AxisServiceGroup(axisConfiguration);
0209: axisServiceGroup.setServiceGroupName("ServiceGroupTest");
0210:
0211: axisService = new AxisService(serviceName.getLocalPart());
0212: axisServiceGroup.addService(axisService);
0213:
0214: axisOperation = new InOutAxisOperation(operationName);
0215: axisOperation.setMessageReceiver(new MessageReceiver() {
0216: public void receive(MessageContext messageCtx) {
0217:
0218: }
0219: });
0220:
0221: axisService.addOperation(axisOperation);
0222: axisService.mapActionToOperation(operationName.getLocalPart(),
0223: axisOperation);
0224:
0225: cfgContext.getAxisConfiguration().addService(axisService);
0226:
0227: //-----------------------------------------------------------------
0228:
0229: serviceGroupContext = cfgContext
0230: .createServiceGroupContext(axisService
0231: .getAxisServiceGroup());
0232: serviceGroupContext.setId("ServiceGroupContextTest");
0233:
0234: serviceContext = serviceGroupContext
0235: .getServiceContext(axisService);
0236:
0237: operationContext = serviceContext
0238: .createOperationContext(operationName);
0239:
0240: //-----------------------------------------------------------------
0241:
0242: transportOut = new TransportOutDescription("null");
0243: transportOut2 = new TransportOutDescription("happy");
0244: transportOut3 = new TransportOutDescription("golucky");
0245: transportOut.setSender(new CommonsHTTPTransportSender());
0246: transportOut2.setSender(new CommonsHTTPTransportSender());
0247: transportOut3.setSender(new CommonsHTTPTransportSender());
0248: axisConfiguration.addTransportOut(transportOut3);
0249: axisConfiguration.addTransportOut(transportOut2);
0250: axisConfiguration.addTransportOut(transportOut);
0251:
0252: transportIn = new TransportInDescription("null");
0253: transportIn2 = new TransportInDescription("always");
0254: transportIn3 = new TransportInDescription("thebest");
0255: transportIn.setReceiver(new SimpleHTTPServer());
0256: transportIn2.setReceiver(new SimpleHTTPServer());
0257: transportIn3.setReceiver(new SimpleHTTPServer());
0258: axisConfiguration.addTransportIn(transportIn2);
0259: axisConfiguration.addTransportIn(transportIn);
0260: axisConfiguration.addTransportIn(transportIn3);
0261:
0262: }
0263:
0264: /*
0265: * (non-Javadoc)
0266: * @see junit.framework.TestCase#setUp()
0267: *
0268: * setUp gets called before each test* method. In this class, a new
0269: * MessageContext object is created for each test* method. The test*
0270: * methods are responsible for adding whichever handler they may need
0271: * in order to run the desired test. See the handler class comments
0272: * for their respective functions.
0273: */
0274: protected void setUp() throws Exception {
0275: //org.apache.log4j.BasicConfigurator.configure();
0276:
0277: invokecallcount = 0;
0278:
0279: mc = cfgContext.createMessageContext();
0280: mc.setTransportIn(transportIn);
0281: mc.setTransportOut(transportOut);
0282:
0283: mc.setServerSide(true);
0284: mc.setProperty(MessageContext.TRANSPORT_OUT, System.out);
0285:
0286: SOAPFactory omFac = OMAbstractFactory.getSOAP11Factory();
0287: mc.setEnvelope(omFac.getDefaultEnvelope());
0288:
0289: phase1 = new Phase("1");
0290: phase1.addHandler(new TempHandler02(0));
0291: phase1.addHandler(new TempHandler02(1));
0292:
0293: phase2 = new Phase("2");
0294: phase2.addHandler(new TempHandler02(2));
0295: phase2.addHandler(handler02);
0296: phase2.addHandler(new TempHandler02(3));
0297:
0298: phase3 = new Phase("3");
0299: phase3.addHandler(new TempHandler02(4));
0300: phase3.addHandler(subhandler);
0301: phase3.addHandler(handler02); // same instance, second insertion
0302: phase3.addHandler(new TempHandler02(5));
0303:
0304: /*
0305: * TODO: WARNING WARNING WARNING
0306: * Ideally inserting subPhase here would make the axis2 engine call
0307: * the invoke of nested subhandler. It does not do this. Please see the
0308: * warning at bottom of testPause06 method.
0309: */
0310: subPhase = new Phase("sub");
0311: subPhase.addHandler(subhandler);
0312: phase3.addHandler(subPhase);
0313: phase3.addHandler(new TempHandler02(6));
0314: phase3.addHandler(new TempHandler02(7));
0315:
0316: axisOperation.getRemainingPhasesInFlow().add(phase1);
0317: axisOperation.getRemainingPhasesInFlow().add(phase2);
0318: axisOperation.getRemainingPhasesInFlow().add(phase3);
0319:
0320: mc.setMessageID(UUIDGenerator.getUUID());
0321:
0322: //operationContext.addMessageContext(mc); gets done via the register
0323: axisOperation.registerOperationContext(mc, operationContext);
0324: mc.setOperationContext(operationContext);
0325: mc.setServiceContext(serviceContext);
0326:
0327: mc.setTo(new EndpointReference("axis2/services/NullService"));
0328:
0329: mc.setWSAAction(operationName.getLocalPart());
0330: mc.setSoapAction(operationName.getLocalPart());
0331:
0332: }
0333:
0334: //-------------------------------------------------------------------------
0335: // test cases
0336: //-------------------------------------------------------------------------
0337:
0338: /**
0339: * Test case for setting and removing data from a message context
0340: */
0341: public void testSelfManagedData01() {
0342: log
0343: .debug("MessageContextSelfManagedDataTest::testSelfManagedData01()=======================================");
0344: try {
0345: ArrayList handlers = new ArrayList();
0346: handlers.add(handler01);
0347: cfgContext.getAxisConfiguration()
0348: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0349:
0350: mc
0351: .setTo(new EndpointReference(
0352: "axis2/services/NullService"));
0353: mc.setWSAAction("DummyOp");
0354:
0355: AxisEngine engine = new AxisEngine(cfgContext);
0356: engine.receive(mc);
0357:
0358: } catch (Exception e) {
0359: e.printStackTrace();
0360: }
0361:
0362: assertEquals(testData01, handler01
0363: .getTestData01FromMessageContext(mc));
0364: assertEquals(testData02, handler01
0365: .getTestData02FromMessageContext(mc));
0366:
0367: boolean isOk3 = isEquals(testData03, handler01
0368: .getTestData03FromMessageContext(mc));
0369: assertTrue(isOk3);
0370:
0371: boolean isOk4 = isEquals(testData04, handler01
0372: .getTestData04FromMessageContext(mc));
0373: assertTrue(isOk4);
0374:
0375: assertEquals(1, invokecallcount);
0376: }
0377:
0378: /**
0379: * Test for setting, saving, restoring self managed data with no exceptions
0380: */
0381: public void testPause01_noExceptions() {
0382: log
0383: .debug("MessageContextSelfManagedDataTest::testPause01_noExceptions()=======================================");
0384:
0385: try {
0386: ArrayList handlers = new ArrayList();
0387: handlers.add(handler02);
0388: cfgContext.getAxisConfiguration()
0389: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0390:
0391: mc
0392: .setTo(new EndpointReference(
0393: "axis2/services/NullService"));
0394: mc.setWSAAction("DummyOp");
0395:
0396: AxisEngine engine = new AxisEngine(cfgContext);
0397: engine.receive(mc);
0398: } catch (Exception e) {
0399: e.printStackTrace();
0400: }
0401:
0402: // if we didn't get any exceptions during save/restore, these will be true
0403: assertTrue(savedMessageContext);
0404: assertTrue(restoredMessageContext);
0405: assertEquals(1, invokecallcount);
0406: }
0407:
0408: /**
0409: * Test for verifying the self managed data used during the save and restore
0410: */
0411: public void testPause02_saveRestoreSelfManagedData() {
0412: log
0413: .debug("MessageContextSelfManagedDataTest::testPause02_saveRestoreSelfManagedData()=======================================");
0414:
0415: try {
0416: ArrayList handlers = new ArrayList();
0417: handlers.add(handler02);
0418: cfgContext.getAxisConfiguration()
0419: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0420:
0421: mc
0422: .setTo(new EndpointReference(
0423: "axis2/services/NullService"));
0424: mc.setWSAAction("DummyOp");
0425:
0426: AxisEngine engine = new AxisEngine(cfgContext);
0427: engine.receive(mc);
0428: } catch (Exception e) {
0429: e.printStackTrace();
0430: }
0431:
0432: // make sure the data in our handler got restored
0433: assertEquals(handler02.testData, handler02
0434: .getTestDataFromMessageContext(mc));
0435: assertEquals(1, invokecallcount);
0436: }
0437:
0438: /**
0439: * Test for save and restore of self managed data and the AxisOperation
0440: */
0441: public void testPause03_saveRestoreOperation() {
0442: log
0443: .debug("MessageContextSelfManagedDataTest::testPause03_saveRestoreOperation()=======================================");
0444:
0445: try {
0446: ArrayList handlers = new ArrayList();
0447: handlers.add(handler02);
0448: cfgContext.getAxisConfiguration()
0449: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0450:
0451: mc
0452: .setTo(new EndpointReference(
0453: "axis2/services/NullService"));
0454: mc.setWSAAction("DummyOp");
0455:
0456: AxisEngine engine = new AxisEngine(cfgContext);
0457: engine.receive(mc);
0458: } catch (Exception e) {
0459: e.printStackTrace();
0460: }
0461:
0462: // make sure the operation got restored in the MessageContext object
0463: assertEquals(operationName.toString(), mc.getAxisOperation()
0464: .getName().toString());
0465: assertEquals(1, invokecallcount);
0466:
0467: }
0468:
0469: /**
0470: * Test for save and restore of self managed data and the AxisService
0471: */
0472: public void testPause04_saveRestoreAxisService() {
0473: log
0474: .debug("MessageContextSelfManagedDataTest::testPause04_saveRestoreAxisService()=======================================");
0475:
0476: try {
0477: ArrayList handlers = new ArrayList();
0478: handlers.add(handler02);
0479: cfgContext.getAxisConfiguration()
0480: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0481:
0482: mc
0483: .setTo(new EndpointReference(
0484: "axis2/services/NullService"));
0485: mc.setWSAAction("DummyOp");
0486:
0487: AxisEngine engine = new AxisEngine(cfgContext);
0488: engine.receive(mc);
0489: } catch (Exception e) {
0490: e.printStackTrace();
0491: }
0492:
0493: // make sure the service got restored in the MessageContext object
0494: assertEquals(serviceName.toString(), mc.getAxisService()
0495: .getName().toString());
0496: assertEquals(1, invokecallcount);
0497: }
0498:
0499: /**
0500: * Test for save and restore of self managed data and the AxisServiceGroup
0501: */
0502: public void testPause05_saveRestoreAxisServiceGroup() {
0503: log
0504: .debug("MessageContextSelfManagedDataTest::testPause05_saveRestoreAxisServiceGroup()=======================================");
0505:
0506: try {
0507: ArrayList handlers = new ArrayList();
0508: handlers.add(handler02);
0509: cfgContext.getAxisConfiguration()
0510: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0511:
0512: mc
0513: .setTo(new EndpointReference(
0514: "axis2/services/NullService"));
0515: mc.setWSAAction("DummyOp");
0516:
0517: AxisEngine engine = new AxisEngine(cfgContext);
0518: engine.receive(mc);
0519:
0520: } catch (Exception e) {
0521: e.printStackTrace();
0522: }
0523:
0524: // make sure the serviceGroup got restored in the MessageContext object
0525: //assertEquals(serviceGroupName.toString(), mc.getAxisServiceGroup().getServiceGroupName());
0526: assertEquals(1, invokecallcount);
0527: }
0528:
0529: /**
0530: * Test for phases
0531: */
0532: public void testPause06_saveRestorePhases() {
0533: log
0534: .debug("MessageContextSelfManagedDataTest::testPause06_saveRestorePhases()=======================================");
0535:
0536: TempHandler02 handlerA = new TempHandler02(666);
0537:
0538: Phase phase601 = new Phase("01");
0539: phase601.addHandler(new TempHandler02(61)); // slot 1
0540: phase601.addHandler(new TempHandler03(62)); // slot 2
0541:
0542: Phase phase602 = new Phase("02");
0543: phase602.addHandler(new TempHandler02(63)); // slot 3
0544: phase602.addHandler(handlerA); // slot 4
0545: phase602.addHandler(new TempHandler03(64)); // slot 5
0546:
0547: Phase phase603 = new Phase("03");
0548: phase603.addHandler(new TempHandler02(65)); // slot 6
0549: phase603.addHandler(subhandler); // slot 7
0550: phase603.addHandler(handlerA); // slot 8 - same instance, second insertion
0551: phase603.addHandler(new TempHandler03(66)); // slot 9
0552:
0553: /*
0554: * TODO: WARNING WARNING WARNING
0555: * Ideally inserting subPhase here would make the axis2 engine call
0556: * the invoke of nested subhandler. It does not do this.
0557: * Please see the warning later in this method.
0558: */
0559: Phase subPhase601 = new Phase("sub6");
0560: subPhase601.addHandler(subhandler);
0561: phase603.addHandler(subPhase601); // slot 10
0562: phase603.addHandler(new TempHandler02(67)); // slot 11
0563: phase603.addHandler(new TempHandler03(68)); // slot 12
0564:
0565: try {
0566: ArrayList phases = new ArrayList();
0567: phases.add(phase601);
0568: phases.add(phase602);
0569: phases.add(phase603);
0570:
0571: cfgContext.getAxisConfiguration()
0572: .setInPhasesUptoAndIncludingPostDispatch(phases);
0573:
0574: mc
0575: .setTo(new EndpointReference(
0576: "axis2/services/NullService"));
0577: mc.setWSAAction("DummyOp");
0578:
0579: AxisEngine engine = new AxisEngine(cfgContext);
0580: engine.receive(mc);
0581: } catch (Exception e) {
0582: e.printStackTrace();
0583: }
0584:
0585: // get the phase lists and see if they match up
0586: ArrayList restoredPhases = mc.getExecutionChain();
0587: int it_count = 0;
0588:
0589: Iterator it = restoredPhases.iterator();
0590: while (it.hasNext()) {
0591: // we know everything at this level is a Phase.
0592: // if you change it, you might get a ClassCastException
0593: Phase restored_phase = (Phase) it.next();
0594:
0595: Phase original_phase = null;
0596:
0597: it_count++;
0598:
0599: if (it_count == 1) {
0600: original_phase = phase601;
0601: } else if (it_count == 2) {
0602: original_phase = phase602;
0603: } else if (it_count == 3) {
0604: original_phase = phase603;
0605: }
0606:
0607: //comparePhases(restored_phase.getHandlers().iterator(), original_phase.getHandlers());
0608:
0609: boolean isOk = comparePhases(restored_phase, original_phase);
0610: assertTrue(isOk);
0611: }
0612:
0613: // TODO WARNING WARNING WARNING
0614: // The axis2 engine is not calling the invoke on nested handlers!
0615: // The way this testcase works is that the handler's invoke() method is
0616: // what sets the data. So, any handlers that are in subPhase will
0617: // not get called, and thus will not set or restore any data.
0618: // Notice we do currently have a subhandler in the subPhase object.
0619: // When axis2 decides to support nested handlers, the
0620: // first three assertEquals below will fail.
0621: log
0622: .debug("MessageContextSelfManagedDataTest::testPause06_saveRestorePhases():: invokecallcount ["
0623: + invokecallcount + "]");
0624: assertEquals(11, invokecallcount);
0625:
0626: // even though there are two occurrances of the same instance of
0627: // handlerA in the executionChain, its serialize and deserialize
0628: // should only be called once per unique instance in the list
0629: int count_s = handlerA.getSerializecallcount();
0630: log
0631: .debug("MessageContextSelfManagedDataTest::testPause06_saveRestorePhases():: handlerA serialize call count ["
0632: + count_s + "]");
0633: assertEquals(11, count_s);
0634:
0635: // here comes some fun math...
0636: // Since a handler (TempHandler02) in this case
0637: // doesn't add any data until its invoke method gets called,
0638: // and the invoke is what causes the save/restore (and thus
0639: // the serialize/deserialize) there is no data for handlerA
0640: // to deserialize until the first occurance of handlerA
0641: // invoke is called in the executionChain. Observing our phases,
0642: // we see it is in slot #3. 11 - 3 = 8
0643: int count_d = handlerA.getDeserializecallcount();
0644: log
0645: .debug("MessageContextSelfManagedDataTest::testPause06_saveRestorePhases():: handlerA deserialize call count ["
0646: + count_d + "]");
0647: assertEquals(8, count_d);
0648: assertEquals(subhandler.testData, subhandler
0649: .getTestDataFromMessageContext(mc));
0650:
0651: }
0652:
0653: /**
0654: * Test for save and restore of binary self managed data
0655: */
0656: public void testSelfManagedData07() {
0657: log
0658: .debug("MessageContextSelfManagedDataTest::testSelfManagedData07()=======================================");
0659: try {
0660: ArrayList handlers = new ArrayList();
0661: handlers.add(handler03);
0662: cfgContext.getAxisConfiguration()
0663: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0664:
0665: mc
0666: .setTo(new EndpointReference(
0667: "axis2/services/NullService"));
0668: mc.setWSAAction("DummyOp");
0669:
0670: AxisEngine engine = new AxisEngine(cfgContext);
0671: engine.receive(mc);
0672: } catch (Exception e) {
0673: e.printStackTrace();
0674: }
0675:
0676: boolean isOk3 = isEquals(testData03, (byte[]) handler03
0677: .getTestDataFromMessageContext(mc));
0678: assertTrue(isOk3);
0679:
0680: assertEquals(1, invokecallcount);
0681: }
0682:
0683: /**
0684: * Test for handler04
0685: */
0686: public void testSelfManagedData08() {
0687: log
0688: .debug("MessageContextSelfManagedDataTest::testSelfManagedData08()=======================================");
0689: try {
0690:
0691: ArrayList handlers = new ArrayList();
0692: handlers.add(handler04);
0693: cfgContext.getAxisConfiguration()
0694: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0695:
0696: mc
0697: .setTo(new EndpointReference(
0698: "axis2/services/NullService"));
0699: mc.setWSAAction("DummyOp");
0700:
0701: AxisEngine engine = new AxisEngine(cfgContext);
0702: engine.receive(mc);
0703:
0704: } catch (Exception e) {
0705: e.printStackTrace();
0706: }
0707:
0708: boolean isOk4 = isEquals(testData04, (long[]) handler04
0709: .getTestDataFromMessageContext(mc));
0710: assertTrue(isOk4);
0711:
0712: assertEquals(1, invokecallcount);
0713: }
0714:
0715: /**
0716: * Test for handler03 and handler04
0717: */
0718: public void testSelfManagedData09() {
0719: log
0720: .debug("MessageContextSelfManagedDataTest::testSelfManagedData09()=======================================");
0721: try {
0722:
0723: ArrayList handlers = new ArrayList();
0724: handlers.add(handler03);
0725: handlers.add(handler04);
0726: cfgContext.getAxisConfiguration()
0727: .setInPhasesUptoAndIncludingPostDispatch(handlers);
0728:
0729: mc
0730: .setTo(new EndpointReference(
0731: "axis2/services/NullService"));
0732: mc.setWSAAction("DummyOp");
0733:
0734: AxisEngine engine = new AxisEngine(cfgContext);
0735: engine.receive(mc);
0736:
0737: } catch (Exception e) {
0738: e.printStackTrace();
0739: }
0740:
0741: boolean isOk3 = isEquals(testData03, (byte[]) handler03
0742: .getTestDataFromMessageContext(mc));
0743: assertTrue(isOk3);
0744:
0745: boolean isOk4 = isEquals(testData04, (long[]) handler04
0746: .getTestDataFromMessageContext(mc));
0747: assertTrue(isOk4);
0748:
0749: assertEquals(2, invokecallcount);
0750: }
0751:
0752: //-------------------------------------------------------------------------
0753: // internal helper methods
0754: //-------------------------------------------------------------------------
0755:
0756: /**
0757: * Saves the specified message context to a temporary
0758: * file, then restores it.
0759: *
0760: * @param mc1 The message context object to save
0761: * @param fnprefix A prefix for the filename of the temporary file
0762: * @param desc Text that describes the caller's situation
0763: * @return The restored message context object or NULL
0764: */
0765: private MessageContext saveAndRestore(MessageContext mc1,
0766: String fnprefix, String desc) {
0767: MessageContext msgContext2 = null;
0768: String title = "MessageContextSelfManagedDataTest::saveAndRestore::["
0769: + desc + "] ";
0770: log.debug(title);
0771:
0772: try {
0773: theFile = File.createTempFile(fnprefix, null);
0774: log
0775: .debug(title + "temp file = [" + theFile.getName()
0776: + "]");
0777: } catch (Exception ex) {
0778: log.debug(title + "error creating temp file = ["
0779: + ex.getMessage() + "]");
0780: theFile = null;
0781: }
0782:
0783: if (theFile != null) {
0784: try {
0785: // setup an output stream to a physical file
0786: FileOutputStream outStream = new FileOutputStream(
0787: theFile);
0788:
0789: // attach a stream capable of writing objects to the
0790: // stream connected to the file
0791: ObjectOutputStream outObjStream = new ObjectOutputStream(
0792: outStream);
0793:
0794: // try to save the message context
0795: log.debug(title + "saving message context ....");
0796: savedMessageContext = false;
0797:
0798: outObjStream.writeObject(mc1);
0799: outObjStream.close();
0800: outStream.close();
0801:
0802: // no exceptions, set savedMessageContext to true
0803: savedMessageContext = true;
0804:
0805: log.debug(title + "....saved message context .....");
0806:
0807: // setup an input stream to the file
0808: FileInputStream inStream = new FileInputStream(theFile);
0809:
0810: // attach a stream capable of reading objects from the
0811: // stream connected to the file
0812: ObjectInputStream inObjStream = new ObjectInputStream(
0813: inStream);
0814:
0815: // try to restore the message context
0816: log.debug(title + "restoring message context .....");
0817: restoredMessageContext = false;
0818: msgContext2 = (MessageContext) inObjStream.readObject();
0819: inObjStream.close();
0820: inStream.close();
0821: msgContext2.activate(mc1.getConfigurationContext());
0822:
0823: // no exceptions, set restoredMessageContext to true
0824: restoredMessageContext = true;
0825:
0826: // VERY IMPORTANT: replace testcase's messagecontext object with the new restored one
0827: mc = msgContext2;
0828:
0829: log.debug(title + "....restored message context .....");
0830:
0831: } catch (Exception ex2) {
0832: if (savedMessageContext != true) {
0833: log.debug(title
0834: + "Error with saving message context = ["
0835: + ex2.getClass().getName() + " : "
0836: + ex2.getMessage() + "]");
0837: ex2.printStackTrace();
0838: } else {
0839: log
0840: .debug(title
0841: + "Error with restoring message context = ["
0842: + ex2.getClass().getName() + " : "
0843: + ex2.getMessage() + "]");
0844: ex2.printStackTrace();
0845: }
0846: }
0847:
0848: // if the save/restore of the message context succeeded,
0849: // then don't keep the temporary file around
0850: boolean removeTmpFile = savedMessageContext
0851: && restoredMessageContext;
0852: if (removeTmpFile) {
0853: try {
0854: theFile.delete();
0855: } catch (Exception e) {
0856: // just absorb it
0857: }
0858: }
0859: }
0860:
0861: return msgContext2;
0862: }
0863:
0864: /**
0865: * Gets the ID associated with the handler object.
0866: *
0867: * @param o The handler object
0868: * @return The ID associated with the handler,
0869: * -1 otherwise
0870: */
0871: private int getHandlerID(Object o) {
0872: int id = -1;
0873:
0874: if (o instanceof TempHandler01) {
0875: id = ((TempHandler01) o).getID();
0876: } else if (o instanceof TempHandler02) {
0877: id = ((TempHandler02) o).getID();
0878: } else if (o instanceof TempHandler03) {
0879: id = ((TempHandler03) o).getID();
0880: } else if (o instanceof TempHandler04) {
0881: id = ((TempHandler04) o).getID();
0882: }
0883:
0884: return id;
0885: }
0886:
0887: /**
0888: * Check the handler objects to see if they are equivalent.
0889: *
0890: * @param o1 The first handler
0891: * @param o2 The second handler
0892: * @return TRUE if the handler objects are equivalent,
0893: * FALSE otherwise
0894: */
0895: private boolean compareHandlers(Object o1, Object o2) {
0896: if ((o1 == null) && (o2 == null)) {
0897: return true;
0898: }
0899:
0900: if ((o1 != null) && (o2 != null)) {
0901: String c1 = o1.getClass().getName();
0902: String c2 = o2.getClass().getName();
0903:
0904: if (c1.equals(c2)) {
0905: log
0906: .debug("MessagecontextSelfManagedDataTest::compareHandlers: class ["
0907: + c1 + "] match ");
0908:
0909: int id1 = getHandlerID(o1);
0910: int id2 = getHandlerID(o2);
0911:
0912: if (id1 == id2) {
0913: log
0914: .debug("MessagecontextSelfManagedDataTest::compareHandlers: id ["
0915: + id1 + "] match");
0916: return true;
0917: } else {
0918: log
0919: .debug("MessagecontextSelfManagedDataTest::compareHandlers: id1 ["
0920: + id1 + "] != id2 [" + id2 + "] ");
0921: return false;
0922: }
0923: } else {
0924: log
0925: .debug("MessagecontextSelfManagedDataTest::compareHandlers: class1 ["
0926: + c1 + "] != class2 [" + c2 + "] ");
0927: return false;
0928: }
0929: }
0930:
0931: return false;
0932: }
0933:
0934: /**
0935: * Compare two phases.
0936: *
0937: * @param o1 The first phase object
0938: * @param o2 The second phase object
0939: * @return TRUE if the phases are equivalent,
0940: * FALSE otherwise
0941: */
0942: private boolean comparePhases(Object o1, Object o2) {
0943: if ((o1 == null) && (o2 == null)) {
0944: return true;
0945: }
0946:
0947: try {
0948: if (((o1 != null) && (o2 != null))
0949: && ((o1 instanceof Phase) && (o2 instanceof Phase))) {
0950: Phase p1 = (Phase) o1;
0951: Phase p2 = (Phase) o2;
0952:
0953: ArrayList list1 = p1.getHandlers();
0954: ArrayList list2 = p2.getHandlers();
0955:
0956: if ((list1 == null) && (list2 == null)) {
0957: return true;
0958: }
0959:
0960: if ((list1 != null) && (list2 != null)) {
0961: int size1 = list1.size();
0962: int size2 = list2.size();
0963:
0964: if (size1 != size2) {
0965: return false;
0966: }
0967:
0968: for (int j = 0; j < size1; j++) {
0969: Object obj1 = list1.get(j);
0970: Object obj2 = list2.get(j);
0971:
0972: if ((obj1 == null) && (obj2 == null)) {
0973: // ok
0974: } else if ((obj1 != null) && (obj2 != null)) {
0975: boolean check = false;
0976:
0977: if (obj1 instanceof Phase) {
0978: check = comparePhases(obj1, obj2);
0979: } else {
0980: // must be a handler
0981: check = compareHandlers(obj1, obj2);
0982: }
0983:
0984: if (check == false) {
0985: return false;
0986: }
0987: } else {
0988: // mismatch
0989: return false;
0990: }
0991: }
0992:
0993: // if we got here, the comparison completed ok
0994: // with a match
0995:
0996: return true;
0997: }
0998:
0999: }
1000: } catch (Exception e) {
1001: // some error
1002: e.printStackTrace();
1003: }
1004:
1005: return false;
1006: }
1007:
1008: /*
1009: private void comparePhases(Iterator it, ArrayList al)
1010: {
1011: int it_count = -1;
1012:
1013: while (it.hasNext())
1014: {
1015: it_count++;
1016: Handler handler = (Handler)it.next();
1017: if (handler instanceof Phase)
1018: {
1019: comparePhases(((Phase)handler).getHandlers().iterator(), ((Phase)al.get(it_count)).getHandlers());
1020: }
1021: else
1022: {
1023: assertEquals(handler, (TempHandler02)al.get(it_count));
1024: }
1025: }
1026: }
1027:
1028: */
1029:
1030: //-------------------------------------------------------------------------
1031: // internal handlers
1032: //-------------------------------------------------------------------------
1033:
1034: /*
1035: * TempHandler01 simply sets and/or removes data from the MessageContext
1036: * object.
1037: */
1038: public class TempHandler01 extends AbstractHandler {
1039: private int id = -2;
1040:
1041: public TempHandler01() {
1042: id = -1;
1043: }
1044:
1045: public TempHandler01(int id) {
1046: this .id = id;
1047: }
1048:
1049: public int getID() {
1050: return this .id;
1051: }
1052:
1053: public String getName() {
1054: return new String(Integer.toString(id));
1055: }
1056:
1057: public String getTestData01FromMessageContext(MessageContext _mc) {
1058: return (String) _mc.getSelfManagedData(this .getClass(),
1059: key01);
1060: }
1061:
1062: public String getTestData02FromMessageContext(MessageContext _mc) {
1063: return (String) _mc.getSelfManagedData(this .getClass(),
1064: key02);
1065: }
1066:
1067: public byte[] getTestData03FromMessageContext(MessageContext _mc) {
1068: return (byte[]) _mc.getSelfManagedData(this .getClass(),
1069: key03);
1070: }
1071:
1072: public long[] getTestData04FromMessageContext(MessageContext _mc) {
1073: return (long[]) _mc.getSelfManagedData(this .getClass(),
1074: key04);
1075: }
1076:
1077: /* we're just using the invoke to set/change/remove data
1078: * Tests:
1079: * for key01: set, remove, set, get
1080: * for key02: set, get, set
1081: * for key03: set, remove, set, get
1082: * for key04: set, get, set, get
1083: */
1084: public InvocationResponse invoke(MessageContext _mc)
1085: throws AxisFault {
1086:
1087: String desc = "TempHandler01[id=" + id + "].invoke()";
1088: String title = "MessageContextSelfManagedDataTest::" + desc;
1089: log.debug(title);
1090:
1091: String tmp = "whaaa?";
1092: _mc.setSelfManagedData(this .getClass(), key02, tmp);
1093: String data2 = (String) _mc.getSelfManagedData(this
1094: .getClass(), key02);
1095: assertEquals(tmp, data2);
1096:
1097: _mc.setSelfManagedData(this .getClass(), key01, testData01);
1098: _mc.removeSelfManagedData(this .getClass(), key01);
1099: _mc.setSelfManagedData(this .getClass(), key01, testData01);
1100: String data1 = (String) _mc.getSelfManagedData(this
1101: .getClass(), key01);
1102: assertEquals(data1, testData01);
1103:
1104: _mc.setSelfManagedData(this .getClass(), key02, testData02);
1105:
1106: _mc.setSelfManagedData(this .getClass(), key03, testData03);
1107: byte[] data3 = (byte[]) _mc.getSelfManagedData(this
1108: .getClass(), key03);
1109: boolean isOk3 = isEquals(data3, testData03);
1110: assertTrue(isOk3);
1111:
1112: _mc.removeSelfManagedData(this .getClass(), key03);
1113: _mc.setSelfManagedData(this .getClass(), key03, testData03);
1114:
1115: _mc.setSelfManagedData(this .getClass(), key04, testData04);
1116: long[] data4 = (long[]) _mc.getSelfManagedData(this
1117: .getClass(), key04);
1118: boolean isOk4 = isEquals(data4, testData04);
1119: assertTrue(isOk4);
1120:
1121: invokecallcount++;
1122:
1123: log.debug(title + ": Completed");
1124: return InvocationResponse.CONTINUE;
1125: }
1126:
1127: }
1128:
1129: /*
1130: * TempHandler02 sets data, saves it (via the serializeSelfManagedData method
1131: * called by the MessageContext object when saved in this handler's invoke()),
1132: *
1133: */
1134: public class TempHandler02 extends AbstractHandler implements
1135: SelfManagedDataManager {
1136: private int id = -2;
1137: private int serializecallcount = 0;
1138: private int deserializecallcount = 0;
1139: public String testData = new String("this is some test data");
1140:
1141: /*
1142: * constructor
1143: */
1144: public TempHandler02() {
1145: this .id = -1;
1146: this .serializecallcount = 0;
1147: this .deserializecallcount = 0;
1148: }
1149:
1150: /*
1151: * constructor
1152: */
1153: public TempHandler02(int id) {
1154: this .id = id;
1155: this .serializecallcount = 0;
1156: this .deserializecallcount = 0;
1157: }
1158:
1159: public int getID() {
1160: return this .id;
1161: }
1162:
1163: public String getName() {
1164: return new String(Integer.toString(id));
1165: }
1166:
1167: public String getTestDataFromMessageContext(MessageContext _mc) {
1168: return (String) _mc.getSelfManagedData(this .getClass(),
1169: Integer.toString(id));
1170: }
1171:
1172: /*
1173: * (non-Javadoc)
1174: * @see org.apache.axis2.engine.Handler#invoke(org.apache.axis2.context.MessageContext)
1175: *
1176: * This invoke() method will test several things:
1177: * 1. verify that serializeSelfManagedData is actually called when saving the MessageContext object
1178: * 2. verify that deserializeSelfManagedData is actually called when restoring the MessageContext object
1179: * 3. verify that any SelfManagedData stored in the MessageContext is properly saved/restored (via serializeSelfManagedData/deserializeSelfManagedData)
1180: */
1181: public InvocationResponse invoke(MessageContext _mc)
1182: throws AxisFault {
1183: String desc = "TempHandler02[id=" + id + "].invoke()";
1184: String title = "MessageContextSelfManagedDataTest::" + desc;
1185: log.debug(title);
1186:
1187: MessageContext msgContext2 = null; // this will be the restored one
1188:
1189: _mc.setSelfManagedData(this .getClass(), Integer
1190: .toString(id), testData + Integer.toString(id));
1191:
1192: log.debug(title
1193: + ": Setting self managed data using key ["
1194: + Integer.toString(id) + "]");
1195:
1196: msgContext2 = saveAndRestore(_mc, "TempHandler02_", desc);
1197:
1198: boolean result = (msgContext2 != null);
1199: assertTrue(result);
1200:
1201: invokecallcount++;
1202:
1203: log.debug(title + ": Completed");
1204: return InvocationResponse.CONTINUE;
1205: }
1206:
1207: public void deserializeSelfManagedData(
1208: ByteArrayInputStream data, MessageContext _mc)
1209: throws IOException {
1210: deserializecallcount++;
1211:
1212: String desc = "TempHandler02[id=" + id
1213: + "].deserializeSelfManagedData() count ["
1214: + deserializecallcount + "]";
1215: String title = "MessageContextSelfManagedDataTest::" + desc;
1216: log.debug(title);
1217:
1218: ObjectInputStream ois = new ObjectInputStream(data);
1219: testData = ois.readUTF().concat(
1220: " with extra text " + Integer.toString(id));
1221: _mc.setSelfManagedData(this .getClass(), Integer
1222: .toString(id), testData);
1223: }
1224:
1225: public void restoreTransientData(MessageContext _mc) {
1226: // not necessary to test
1227: }
1228:
1229: public ByteArrayOutputStream serializeSelfManagedData(
1230: MessageContext _mc) throws IOException {
1231: serializecallcount++;
1232:
1233: String desc = "TempHandler02[id=" + id
1234: + "].serializeSelfManagedData() count ["
1235: + serializecallcount + "]";
1236: String title = "MessageContextSelfManagedDataTest::" + desc;
1237: log.debug(title);
1238:
1239: String storedTestData = (String) _mc.getSelfManagedData(
1240: this .getClass(), Integer.toString(id));
1241: if (storedTestData == null) {
1242: log.debug(title
1243: + ": No self managed data to serialize");
1244: return null;
1245: }
1246:
1247: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1248: ObjectOutputStream oos = new ObjectOutputStream(baos);
1249: oos.writeUTF(storedTestData);
1250: oos.close();
1251: int baos_size = baos.size();
1252: baos.close();
1253:
1254: log.debug(title
1255: + ": saved self managed data string length ["
1256: + storedTestData.length() + "]");
1257: log
1258: .debug(title
1259: + ": saved self managed data byte array output stream (UTF) length ["
1260: + baos_size + "]");
1261: return baos;
1262: }
1263:
1264: public int getDeserializecallcount() {
1265: return deserializecallcount;
1266: }
1267:
1268: public int getSerializecallcount() {
1269: return serializecallcount;
1270: }
1271: }
1272:
1273: /*
1274: * TempHandler03 sets binary data, saves it (via the serializeSelfManagedData method
1275: * called by the MessageContext object when saved in this handler's invoke()),
1276: *
1277: */
1278: public class TempHandler03 extends AbstractHandler implements
1279: SelfManagedDataManager {
1280: private int id = -2;
1281: private int serializecallcount = 0;
1282: private int deserializecallcount = 0;
1283:
1284: public TempHandler03() {
1285: id = -1;
1286: }
1287:
1288: public TempHandler03(int id) {
1289: this .id = id;
1290: }
1291:
1292: public int getID() {
1293: return this .id;
1294: }
1295:
1296: public String getName() {
1297: return new String(Integer.toString(id));
1298: }
1299:
1300: public Object getTestDataFromMessageContext(MessageContext _mc) {
1301: return _mc.getSelfManagedData(this .getClass(), Integer
1302: .toString(id));
1303: }
1304:
1305: /*
1306: * (non-Javadoc)
1307: * @see org.apache.axis2.engine.Handler#invoke(org.apache.axis2.context.MessageContext)
1308: *
1309: * This invoke() method will test several things:
1310: * 1. verify that serializeSelfManagedData is actually called when saving the MessageContext object
1311: * 2. verify that deserializeSelfManagedData is actually called when restoring the MessageContext object
1312: * 3. verify that any SelfManagedData stored in the MessageContext is properly saved/restored (via serializeSelfManagedData/deserializeSelfManagedData)
1313: */
1314: public InvocationResponse invoke(MessageContext _mc)
1315: throws AxisFault {
1316:
1317: String desc = "TempHandler03[id=" + id + "].invoke()";
1318: String title = "MessageContextSelfManagedDataTest::" + desc;
1319: log.debug(title);
1320:
1321: MessageContext msgContext2 = null; // this will be the restored one
1322:
1323: // add the byte [] of data
1324: _mc.setSelfManagedData(this .getClass(), Integer
1325: .toString(id), testData03);
1326:
1327: log.debug(title
1328: + ": Setting self managed data using key ["
1329: + Integer.toString(id) + "]");
1330:
1331: msgContext2 = saveAndRestore(_mc, "TempHandler03_", desc);
1332:
1333: boolean result = (msgContext2 != null);
1334: assertTrue(result);
1335:
1336: invokecallcount++;
1337:
1338: log.debug(title + ": Completed");
1339: return InvocationResponse.CONTINUE;
1340: }
1341:
1342: public void deserializeSelfManagedData(
1343: ByteArrayInputStream data, MessageContext _mc)
1344: throws IOException {
1345: boolean isOK = true;
1346:
1347: deserializecallcount++;
1348:
1349: String desc = "TempHandler03[id=" + id
1350: + "].deserializeSelfManagedData() count ["
1351: + deserializecallcount + "]";
1352: String title = "MessageContextSelfManagedDataTest::" + desc;
1353: log.debug(title);
1354:
1355: int expected = testData03.length;
1356:
1357: byte[] tmp = new byte[expected];
1358:
1359: boolean keepGoing = true;
1360: int index = 0;
1361:
1362: while (keepGoing) {
1363: int tmpdata = data.read();
1364:
1365: if (tmpdata != -1) {
1366: Integer value = new Integer(tmpdata);
1367: tmp[index] = value.byteValue();
1368: index++;
1369:
1370: if (index > expected) {
1371: isOK = false;
1372: assertTrue(isOK);
1373: }
1374: } else {
1375: keepGoing = false;
1376: }
1377: }
1378:
1379: isOK = isEquals(tmp, testData03);
1380: assertTrue(isOK);
1381:
1382: _mc.setSelfManagedData(this .getClass(), Integer
1383: .toString(id), tmp);
1384: }
1385:
1386: public void restoreTransientData(MessageContext _mc) {
1387: // not necessary to test
1388: }
1389:
1390: public ByteArrayOutputStream serializeSelfManagedData(
1391: MessageContext _mc) throws IOException {
1392:
1393: serializecallcount++;
1394:
1395: String desc = "TempHandler03[id=" + id
1396: + "].serializeSelfManagedData() count ["
1397: + serializecallcount + "]";
1398: String title = "MessageContextSelfManagedDataTest::" + desc;
1399: log.debug(title);
1400:
1401: byte[] tmp = (byte[]) _mc.getSelfManagedData(this
1402: .getClass(), Integer.toString(id));
1403:
1404: if (tmp == null) {
1405: log.debug(title
1406: + ": No self managed data to serialize");
1407: return null;
1408: }
1409:
1410: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1411: baos.write(tmp, 0, tmp.length);
1412: int baos_size = baos.size();
1413: baos.close();
1414:
1415: log.debug(title
1416: + ": saved self managed data byte [] length ["
1417: + tmp.length + "]");
1418: log
1419: .debug(title
1420: + ": saved self managed data byte array output stream length ["
1421: + baos_size + "]");
1422: return baos;
1423: }
1424:
1425: public int getDeserializecallcout() {
1426: return deserializecallcount;
1427: }
1428:
1429: public int getSerializecallcount() {
1430: return serializecallcount;
1431: }
1432: }
1433:
1434: /*
1435: * TempHandler04 sets binary data, saves it (via the serializeSelfManagedData method
1436: * called by the MessageContext object when saved in this handler's invoke()),
1437: *
1438: */
1439: public class TempHandler04 extends AbstractHandler implements
1440: SelfManagedDataManager {
1441: private int id = -2;
1442: private int serializecallcount = 0;
1443: private int deserializecallcount = 0;
1444:
1445: public TempHandler04() {
1446: id = -1;
1447: }
1448:
1449: public TempHandler04(int id) {
1450: this .id = id;
1451: }
1452:
1453: public int getID() {
1454: return this .id;
1455: }
1456:
1457: public String getName() {
1458: return new String(Integer.toString(id));
1459: }
1460:
1461: public Object getTestDataFromMessageContext(MessageContext _mc) {
1462: return _mc.getSelfManagedData(this .getClass(), Integer
1463: .toString(id));
1464: }
1465:
1466: /*
1467: * (non-Javadoc)
1468: * @see org.apache.axis2.engine.Handler#invoke(org.apache.axis2.context.MessageContext)
1469: *
1470: * This invoke() method will test several things:
1471: * 1. verify that serializeSelfManagedData is actually called when saving the MessageContext object
1472: * 2. verify that deserializeSelfManagedData is actually called when restoring the MessageContext object
1473: * 3. verify that any SelfManagedData stored in the MessageContext is properly saved/restored (via serializeSelfManagedData/deserializeSelfManagedData)
1474: */
1475: public InvocationResponse invoke(MessageContext _mc)
1476: throws AxisFault {
1477:
1478: String desc = "TempHandler04[id=" + id + "].invoke()";
1479: String title = "MessageContextSelfManagedDataTest::" + desc;
1480: log.debug(title);
1481:
1482: MessageContext msgContext2 = null; // this will be the restored one
1483:
1484: // add the byte [] of data
1485: _mc.setSelfManagedData(this .getClass(), Integer
1486: .toString(id), testData04);
1487:
1488: log.debug(title
1489: + ": Setting self managed data using key ["
1490: + Integer.toString(id) + "]");
1491:
1492: msgContext2 = saveAndRestore(_mc, "TempHandler04_", desc);
1493:
1494: boolean result = (msgContext2 != null);
1495: assertTrue(result);
1496:
1497: invokecallcount++;
1498:
1499: log.debug(title + ": Completed");
1500: return InvocationResponse.CONTINUE;
1501: }
1502:
1503: public void deserializeSelfManagedData(
1504: ByteArrayInputStream data, MessageContext _mc)
1505: throws IOException {
1506: boolean isOK = true;
1507:
1508: deserializecallcount++;
1509:
1510: String desc = "TempHandler04[id=" + id
1511: + "].deserializeSelfManagedData() count ["
1512: + deserializecallcount + "]";
1513: String title = "MessageContextSelfManagedDataTest::" + desc;
1514: log.debug(title);
1515:
1516: int expected = testData04.length;
1517:
1518: long[] tmp = new long[expected];
1519:
1520: ObjectInputStream ois = new ObjectInputStream(data);
1521:
1522: boolean keepGoing = true;
1523: int index = 0;
1524:
1525: int sizeWritten = ois.readInt();
1526:
1527: if (sizeWritten > 0) {
1528: while (keepGoing) {
1529: try {
1530: long tmpdata = ois.readLong();
1531:
1532: tmp[index] = tmpdata;
1533: index++;
1534:
1535: if (index > expected) {
1536: isOK = false;
1537: assertTrue(isOK);
1538: }
1539: } catch (Exception exc) {
1540: keepGoing = false;
1541: }
1542: }
1543:
1544: isOK = isEquals(tmp, testData04);
1545: assertTrue(isOK);
1546:
1547: _mc.setSelfManagedData(this .getClass(), Integer
1548: .toString(id), tmp);
1549: }
1550:
1551: }
1552:
1553: public void restoreTransientData(MessageContext _mc) {
1554: // not necessary to test
1555: }
1556:
1557: public ByteArrayOutputStream serializeSelfManagedData(
1558: MessageContext _mc) throws IOException {
1559:
1560: serializecallcount++;
1561:
1562: String desc = "TempHandler04[id=" + id
1563: + "].serializeSelfManagedData() count ["
1564: + serializecallcount + "]";
1565: String title = "MessageContextSelfManagedDataTest::" + desc;
1566: log.debug(title);
1567:
1568: long[] tmp = (long[]) _mc.getSelfManagedData(this
1569: .getClass(), Integer.toString(id));
1570:
1571: if (tmp == null) {
1572: log.debug(title
1573: + ": No self managed data to serialize");
1574: return null;
1575: }
1576:
1577: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1578: ObjectOutputStream oos = new ObjectOutputStream(baos);
1579:
1580: int size = tmp.length;
1581:
1582: oos.writeInt(size);
1583:
1584: for (int i = 0; i < size; i++) {
1585: oos.writeLong(tmp[i]);
1586: }
1587:
1588: oos.close();
1589: int baos_size = baos.size();
1590: baos.close();
1591:
1592: log.debug(title
1593: + ": saved self managed data byte [] length ["
1594: + tmp.length + "]");
1595: log
1596: .debug(title
1597: + ": saved self managed data byte array output stream length ["
1598: + baos_size + "]");
1599: return baos;
1600: }
1601:
1602: public int getDeserializecallcount() {
1603: return deserializecallcount;
1604: }
1605:
1606: public int getSerializecallcount() {
1607: return serializecallcount;
1608: }
1609: }
1610:
1611: /*
1612: * compares the two byte arrays to see if they are equal
1613: */
1614: private boolean isEquals(byte[] b1, byte[] b2) {
1615: int size1 = b1.length;
1616: int size2 = b2.length;
1617:
1618: if (size1 != size2) {
1619: return false;
1620: }
1621:
1622: for (int i = 0; i < size1; i++) {
1623: if (b1[i] != b2[i]) {
1624: return false;
1625: }
1626: }
1627:
1628: return true;
1629: }
1630:
1631: /*
1632: * compares the two long arrays to see if they are equal
1633: */
1634: private boolean isEquals(long[] L1, long[] L2) {
1635: int size1 = L1.length;
1636: int size2 = L2.length;
1637:
1638: if (size1 != size2) {
1639: return false;
1640: }
1641:
1642: for (int i = 0; i < size1; i++) {
1643: if (L1[i] != L2[i]) {
1644: return false;
1645: }
1646: }
1647:
1648: return true;
1649: }
1650:
1651: }
|