KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > engine > EnginePausingTest


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis2.engine;
18
19 import junit.framework.TestCase;
20 import org.apache.axis2.addressing.AddressingConstants;
21 import org.apache.axis2.addressing.EndpointReference;
22 import org.apache.axis2.context.ConfigurationContext;
23 import org.apache.axis2.context.MessageContext;
24 import org.apache.axis2.context.ServiceContext;
25 import org.apache.axis2.description.*;
26 import org.apache.axis2.handlers.AbstractHandler;
27 import org.apache.axis2.om.OMAbstractFactory;
28 import org.apache.axis2.soap.SOAPFactory;
29 import org.apache.axis2.transport.http.HTTPTransportSender;
30 import org.apache.wsdl.WSDLService;
31
32 import javax.xml.namespace.QName JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 public class EnginePausingTest extends TestCase {
36
37     private QName JavaDoc serviceName = new QName JavaDoc("NullService");
38     private QName JavaDoc operationName = new QName JavaDoc("DummyOp");
39     private ConfigurationContext engineContext;
40
41     private TransportOutDescription transportOut;
42     private TransportInDescription transportIn;
43     private MessageContext mc;
44     private ArrayList JavaDoc executedHandlers;
45
46     public EnginePausingTest(String JavaDoc arg0) throws AxisFault {
47         super(arg0);
48         executedHandlers = new ArrayList JavaDoc();
49         AxisConfiguration engineRegistry = new AxisConfigurationImpl();
50         engineContext = new ConfigurationContext(engineRegistry);
51         transportOut = new TransportOutDescription(new QName JavaDoc("null"));
52         transportOut.setSender(new HTTPTransportSender());
53         transportIn = new TransportInDescription(new QName JavaDoc("null"));
54
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58
59         ServiceDescription service = new ServiceDescription(serviceName);
60         service.setStyle(WSDLService.STYLE_DOC);
61         engineContext.getAxisConfiguration().addService(service);
62
63         OperationDescription axisOp = new OperationDescription(operationName);
64         axisOp.setMessageReciever(new MessageReceiver() {
65             public void recieve(MessageContext messgeCtx) throws AxisFault {
66
67             }
68         });
69         service.addOperation(axisOp);
70
71         mc = new MessageContext(engineContext, transportIn, transportOut);
72
73         mc.setTransportOut(transportOut);
74         mc.setServerSide(true);
75         mc.setProperty(MessageContext.TRANSPORT_OUT, System.out);
76         SOAPFactory omFac = OMAbstractFactory.getSOAP11Factory();
77         mc.setEnvelope(omFac.getDefaultEnvelope());
78
79         Phase phase1 = new Phase("1");
80         phase1.addHandler(new TempHandler(1));
81         phase1.addHandler(new TempHandler(2));
82         phase1.addHandler(new TempHandler(3));
83         phase1.addHandler(new TempHandler(4));
84         phase1.addHandler(new TempHandler(5));
85         phase1.addHandler(new TempHandler(6));
86         phase1.addHandler(new TempHandler(7));
87         phase1.addHandler(new TempHandler(8));
88         phase1.addHandler(new TempHandler(9));
89
90         Phase phase2 = new Phase("2");
91         phase2.addHandler(new TempHandler(10));
92         phase2.addHandler(new TempHandler(11));
93         phase2.addHandler(new TempHandler(12));
94         phase2.addHandler(new TempHandler(13));
95         phase2.addHandler(new TempHandler(14));
96         phase2.addHandler(new TempHandler(15, true));
97         phase2.addHandler(new TempHandler(16));
98         phase2.addHandler(new TempHandler(17));
99         phase2.addHandler(new TempHandler(18));
100
101         Phase phase3 = new Phase("3");
102         phase3.addHandler(new TempHandler(19));
103         phase3.addHandler(new TempHandler(20));
104         phase3.addHandler(new TempHandler(21));
105         phase3.addHandler(new TempHandler(22));
106         phase3.addHandler(new TempHandler(23));
107         phase3.addHandler(new TempHandler(24));
108         phase3.addHandler(new TempHandler(25));
109         phase3.addHandler(new TempHandler(26));
110         phase3.addHandler(new TempHandler(27));
111
112         ServiceContext serviceContext = new ServiceContext(service, engineContext);
113         engineContext.registerServiceContext(serviceContext.getServiceInstanceID(), serviceContext);
114
115         //TODO
116
axisOp.getRemainingPhasesInFlow().add(phase1);
117         axisOp.getRemainingPhasesInFlow().add(phase2);
118         axisOp.getRemainingPhasesInFlow().add(phase3);
119
120         mc.setWSAAction(operationName.getLocalPart());
121         mc.setSoapAction(operationName.getLocalPart());
122         System.out.flush();
123
124     }
125
126     public void testReceive() throws Exception JavaDoc {
127         mc.setTo(
128             new EndpointReference(AddressingConstants.WSA_TO, "axis/services/NullService/DummyOp"));
129         AxisEngine engine = new AxisEngine(engineContext);
130         engine.receive(mc);
131         assertEquals(executedHandlers.size(), 14);
132         for (int i = 0; i < 14; i++) {
133             assertEquals(((Integer JavaDoc) executedHandlers.get(i)).intValue(), i + 1);
134         }
135         engine.receive(mc);
136
137         assertEquals(executedHandlers.size(), 27);
138         for (int i = 15; i < 27; i++) {
139             assertEquals(((Integer JavaDoc) executedHandlers.get(i)).intValue(), i + 1);
140         }
141
142     }
143
144     public class TempHandler extends AbstractHandler {
145         private Integer JavaDoc index;
146         private boolean pause = false;
147
148         public TempHandler(int index, boolean pause) {
149             this.index = new Integer JavaDoc(index);
150             this.pause = pause;
151             init(new HandlerDescription(new QName JavaDoc("handler" + index)));
152         }
153
154         public TempHandler(int index) {
155             this.index = new Integer JavaDoc(index);
156             init(new HandlerDescription(new QName JavaDoc("handler" + index)));
157         }
158
159         public void invoke(MessageContext msgContext) throws AxisFault {
160             String JavaDoc paused = "paused";
161             if (pause && msgContext.getProperty(paused) == null) {
162                 msgContext.setProperty(paused, "true");
163                 msgContext.setPausedTrue(getName());
164             }else{
165                 executedHandlers.add(index);
166             }
167         }
168
169     }
170 }
171
Popular Tags