KickJava   Java API By Example, From Geeks To Geeks.

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


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.MessageSender;
26 import org.apache.axis2.context.ConfigurationContext;
27 import org.apache.axis2.context.ConfigurationContextFactory;
28 import org.apache.axis2.context.MessageContext;
29 import org.apache.axis2.context.ServiceContext;
30 import org.apache.axis2.description.OperationDescription;
31 import org.apache.axis2.description.ServiceDescription;
32 import org.apache.axis2.engine.AxisFault;
33 import org.apache.axis2.engine.MessageReceiver;
34 import org.apache.axis2.om.OMAbstractFactory;
35 import org.apache.axis2.om.OMElement;
36 import org.apache.axis2.om.OMFactory;
37 import org.apache.axis2.om.OMNamespace;
38 import org.apache.axis2.soap.SOAPEnvelope;
39 import org.apache.axis2.soap.SOAPFactory;
40 import org.apache.axis2.transport.mail.SimpleMailListener;
41 import org.apache.axis2.transport.mail.server.MailConstants;
42 import org.apache.axis2.transport.mail.server.MailServer;
43 import org.apache.axis2.util.Utils;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47 import javax.xml.namespace.QName JavaDoc;
48 import java.io.File JavaDoc;
49
50 public class MailOneWayRawXMLTest extends TestCase {
51     private static final String JavaDoc MAIL_TRANSPORT_ENABLED_REPO_PATH = Constants.TESTING_PATH+ "mail-transport-enabledRepository";
52     
53     
54     private EndpointReference targetEPR =
55         new EndpointReference(
56             AddressingConstants.WSA_TO,
57             "axis2@127.0.0.1" + "/axis/services/EchoXMLService/echoOMElement");
58     private Log log = LogFactory.getLog(getClass());
59     private QName JavaDoc serviceName = new QName JavaDoc("EchoXMLService");
60     private QName JavaDoc operationName = new QName JavaDoc("echoOMElement");
61
62
63
64     private ConfigurationContext configContext;
65
66     private SOAPEnvelope envelope;
67
68     private boolean finish = false;
69
70     public MailOneWayRawXMLTest() {
71         super(MailOneWayRawXMLTest.class.getName());
72     }
73
74     public MailOneWayRawXMLTest(String JavaDoc testName) {
75         super(testName);
76     }
77
78     protected void setUp() throws Exception JavaDoc {
79         configContext = createNewConfigurationContext();
80         //start the mail server
81
MailServer server = new MailServer(configContext,MailConstants.POP_SERVER_PORT,MailConstants.SMTP_SERVER_PORT);
82         
83         SimpleMailListener ml = new SimpleMailListener();
84         ml.init(
85             configContext,
86             configContext.getAxisConfiguration().getTransportIn(
87                 new QName JavaDoc(Constants.TRANSPORT_MAIL)));
88         ml.start();
89
90         ServiceDescription service = new ServiceDescription(serviceName);
91         OperationDescription operation = new OperationDescription(operationName);
92         operation.setMessageReciever(new MessageReceiver() {
93             public void recieve(MessageContext messgeCtx) throws AxisFault {
94                 envelope = messgeCtx.getEnvelope();
95             }
96         });
97         service.addOperation(operation);
98         configContext.getAxisConfiguration().addService(service);
99         Utils.resolvePhases(configContext.getAxisConfiguration(), service);
100         ServiceContext serviceContext = configContext.createServiceContext(serviceName);
101     }
102
103     protected void tearDown() throws Exception JavaDoc {
104     }
105
106     private OMElement createEnvelope() {
107         OMFactory fac = OMAbstractFactory.getOMFactory();
108         OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
109         OMElement method = fac.createOMElement("echoOMElement", omNs);
110         OMElement value = fac.createOMElement("myValue", omNs);
111         value.addChild(fac.createText(value, "Isaac Assimov, the foundation Sega"));
112         method.addChild(value);
113
114         return method;
115     }
116
117     public void testOneWay() throws Exception JavaDoc {
118         ServiceDescription service = new ServiceDescription(serviceName);
119         OperationDescription operation = new OperationDescription(operationName);
120         operation.setMessageReciever(new MessageReceiver() {
121             public void recieve(MessageContext messgeCtx) throws AxisFault {
122                 envelope = messgeCtx.getEnvelope();
123             }
124         });
125         service.addOperation(operation);
126         configContext.getAxisConfiguration().addService(service);
127         Utils.resolvePhases(configContext.getAxisConfiguration(), service);
128         ServiceContext serviceContext = configContext.createServiceContext(serviceName);
129
130         SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
131
132         OMElement payload = createEnvelope();
133
134         MessageSender sender = new MessageSender(serviceContext);
135
136         sender.setTo(targetEPR);
137         sender.setSenderTransport(Constants.TRANSPORT_MAIL);
138
139         sender.send(operationName.getLocalPart(), payload);
140         int index = 0;
141         while (envelope == null) {
142 // if(index < 10){
143
Thread.sleep(4000);
144 // index++;
145
// }else{
146
// fail("The messsge was not delivered even after 40 seconds");
147
// }
148
}
149     }
150
151     public ConfigurationContext createNewConfigurationContext() throws Exception JavaDoc {
152         File JavaDoc file = new File JavaDoc(MAIL_TRANSPORT_ENABLED_REPO_PATH);
153         assertTrue("Mail repository directory "+ file.getAbsolutePath() + " does not exsist",file.exists());
154         ConfigurationContextFactory builder = new ConfigurationContextFactory();
155         ConfigurationContext configContext =
156             builder.buildConfigurationContext(file.getAbsolutePath());
157         return configContext;
158     }
159     
160 }
161
Popular Tags