KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > mail > MailetRequestResponceRawXMLTest


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 package org.apache.axis2.mail;
17
18
19 import javax.xml.namespace.QName JavaDoc;
20 import javax.xml.stream.XMLOutputFactory;
21 import javax.xml.stream.XMLStreamException;
22
23 import junit.framework.TestCase;
24
25 import org.apache.axis2.Constants;
26 import org.apache.axis2.addressing.AddressingConstants;
27 import org.apache.axis2.addressing.EndpointReference;
28 import org.apache.axis2.clientapi.AsyncResult;
29 import org.apache.axis2.clientapi.Callback;
30 import org.apache.axis2.context.ConfigurationContext;
31 import org.apache.axis2.context.ConfigurationContextFactory;
32 import org.apache.axis2.context.MessageContext;
33 import org.apache.axis2.context.ServiceContext;
34 import org.apache.axis2.description.OperationDescription;
35 import org.apache.axis2.description.ParameterImpl;
36 import org.apache.axis2.description.ServiceDescription;
37 import org.apache.axis2.description.TransportInDescription;
38 import org.apache.axis2.description.TransportOutDescription;
39 import org.apache.axis2.engine.AxisConfiguration;
40 import org.apache.axis2.engine.AxisFault;
41 import org.apache.axis2.engine.Echo;
42 import org.apache.axis2.engine.MessageReceiver;
43 import org.apache.axis2.om.OMAbstractFactory;
44 import org.apache.axis2.om.OMElement;
45 import org.apache.axis2.om.OMFactory;
46 import org.apache.axis2.om.OMNamespace;
47 import org.apache.axis2.om.OMOutput;
48 import org.apache.axis2.soap.SOAPEnvelope;
49 import org.apache.axis2.transport.mail.server.MailConstants;
50 import org.apache.axis2.transport.mail.MailTransportSender;
51 import org.apache.axis2.transport.mail.SimpleMailListener;
52 import org.apache.axis2.transport.mail.server.MailServer;
53 import org.apache.axis2.util.Utils;
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57 /**
58  * This will use the In out functionality to test the mailet functionality. This code was
59  * based on the code on the MailRequestResponceRawXMLTest.java.
60  *
61  * @author Chamil Thanthrimudalige <chamilt@gmail.com>
62  *
63   */

64 public class MailetRequestResponceRawXMLTest extends TestCase {
65         private EndpointReference targetEPR =
66             new EndpointReference(
67                 AddressingConstants.WSA_TO,
68                 "axis2-server@127.0.0.1" + "/axis/services/EchoXMLService/echoOMElement");
69         private Log log = LogFactory.getLog(getClass());
70         private QName JavaDoc serviceName = new QName JavaDoc("EchoXMLService");
71         private QName JavaDoc operationName = new QName JavaDoc("echoOMElement");
72         private QName JavaDoc transportName = new QName JavaDoc("http://localhost/my", "NullTransport");
73
74         private AxisConfiguration engineRegistry;
75         private MessageContext mc;
76
77         private SOAPEnvelope envelope;
78
79         private boolean finish = false;
80
81         public MailetRequestResponceRawXMLTest() {
82             super(MailetRequestResponceRawXMLTest.class.getName());
83         }
84
85         public MailetRequestResponceRawXMLTest(String JavaDoc testName) {
86             super(testName);
87         }
88
89         protected void setUp() throws Exception JavaDoc {
90             ConfigurationContext configContext = createServerConfigurationContext();
91             MailServer server = new MailServer(configContext, MailConstants.POP_SERVER_PORT, MailConstants.SMTP_SERVER_PORT);
92             configContext.getAxisConfiguration().engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
93             ServiceDescription service =
94                 Utils.createSimpleService(serviceName, Echo.class.getName(), operationName);
95             configContext.getAxisConfiguration().addService(service);
96             Utils.resolvePhases(configContext.getAxisConfiguration(), service);
97             ServiceContext serviceContext = configContext.createServiceContext(serviceName);
98         }
99
100         protected void tearDown() throws Exception JavaDoc {
101         }
102
103         private OMElement createEnvelope() {
104             OMFactory fac = OMAbstractFactory.getOMFactory();
105             OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
106             OMElement method = fac.createOMElement("echoOMElement", omNs);
107             OMElement value = fac.createOMElement("myValue", omNs);
108             value.addChild(fac.createText(value, "Isaac Assimov, the foundation Sega"));
109             method.addChild(value);
110
111             return method;
112         }
113
114         public void testEchoXMLCompleteASync() throws Exception JavaDoc {
115
116             ConfigurationContext configContext = createClientConfigurationContext();
117             ServiceDescription service = new ServiceDescription(serviceName);
118             OperationDescription operation = new OperationDescription(operationName);
119             operation.setMessageReciever(new MessageReceiver() {
120                 public void recieve(MessageContext messgeCtx) throws AxisFault {
121                     envelope = messgeCtx.getEnvelope();
122                 }
123             });
124             service.addOperation(operation);
125             configContext.getAxisConfiguration().addService(service);
126             Utils.resolvePhases(configContext.getAxisConfiguration(), service);
127             ServiceContext serviceContext = configContext.createServiceContext(serviceName);
128
129             org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call(serviceContext);
130             call.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
131
132                 call.setTo(targetEPR);
133                 call.setTransportInfo(Constants.TRANSPORT_MAIL, Constants.TRANSPORT_MAIL, true);
134                 Callback callback = new Callback() {
135                     public void onComplete(AsyncResult result) {
136                         try {
137                             result.getResponseEnvelope().serialize(
138                                 new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
139                         } catch (XMLStreamException e) {
140                             reportError(e);
141                         } finally {
142                             finish = true;
143                         }
144                     }
145
146                     public void reportError(Exception JavaDoc e) {
147                         e.printStackTrace();
148                         finish = true;
149                     }
150                 };
151
152                 call.invokeNonBlocking(operationName.getLocalPart(), createEnvelope(), callback);
153                 int index = 0;
154                 while (!finish) {
155                     Thread.sleep(1000);
156                     index++;
157                     if (index > 10) {
158                         throw new AxisFault("Async response is taking too long[10s+]. Server is being shut down.");
159                     }
160                 }
161                 call.close();
162
163         }
164         public ConfigurationContext createServerConfigurationContext() throws Exception JavaDoc {
165             ConfigurationContextFactory builder = new ConfigurationContextFactory();
166             ConfigurationContext configContext =
167                 builder.buildConfigurationContext(org.apache.axis2.Constants.TESTING_REPOSITORY);
168
169             TransportInDescription transportIn =
170                 new TransportInDescription(new QName JavaDoc(Constants.TRANSPORT_MAIL));
171             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.host", "127.0.0.1"));
172             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.user", "server@127.0.0.1"));
173             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.password", "axis2"));
174             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.port", "1134"));
175             transportIn.addParameter(
176                 new ParameterImpl("transport.mail.replyToAddress", "foo@127.0.0.1"));
177             transportIn.setReciver(new SimpleMailListener());
178             transportIn.getReciever().init(configContext,transportIn);
179
180             TransportOutDescription transportOut =
181                 new TransportOutDescription(new QName JavaDoc(Constants.TRANSPORT_MAIL));
182
183             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.host", "127.0.0.1"));
184             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.user", "server"));
185             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.password", "axis2"));
186             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.port", "1049"));
187             transportOut.setSender(new MailTransportSender());
188             transportOut.getSender().init(configContext,transportOut);
189
190             configContext.getAxisConfiguration().addTransportIn(transportIn);
191             configContext.getAxisConfiguration().addTransportOut(transportOut);
192             return configContext;
193         }
194
195         public ConfigurationContext createClientConfigurationContext() throws Exception JavaDoc {
196             ConfigurationContextFactory builder = new ConfigurationContextFactory();
197             ConfigurationContext configContext =
198                 builder.buildConfigurationContext(org.apache.axis2.Constants.TESTING_REPOSITORY);
199
200             TransportInDescription transportIn =
201                 new TransportInDescription(new QName JavaDoc(Constants.TRANSPORT_MAIL));
202             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.host", "127.0.0.1"));
203             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.user", "client@127.0.0.1"));
204             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.password", "axis2"));
205             transportIn.addParameter(new ParameterImpl("transport.mail.pop3.port", "1134"));
206             transportIn.addParameter(
207                 new ParameterImpl("transport.mail.replyToAddress", "client@127.0.0.1"));
208             transportIn.setReciver(new SimpleMailListener());
209             transportIn.getReciever().init(configContext,transportIn);
210
211             TransportOutDescription transportOut =
212                 new TransportOutDescription(new QName JavaDoc(Constants.TRANSPORT_MAIL));
213
214             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.host", "127.0.0.1"));
215             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.user", "client"));
216             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.password", "axis2"));
217             transportOut.addParameter(new ParameterImpl("transport.mail.smtp.port", "1049"));
218             transportOut.setSender(new MailTransportSender());
219             transportOut.getSender().init(configContext,transportOut);
220
221             configContext.getAxisConfiguration().addTransportIn(transportIn);
222             configContext.getAxisConfiguration().addTransportOut(transportOut);
223             return configContext;
224         }
225
226
227 }
228
Popular Tags