KickJava   Java API By Example, From Geeks To Geeks.

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


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.mail;
18
19 //todo
20

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