1 16 17 package org.apache.axis2.mail; 18 19 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 ; 48 import java.io.File ; 49 50 public class MailOneWayRawXMLTest extends TestCase { 51 private static final String 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 serviceName = new QName ("EchoXMLService"); 60 private QName operationName = new QName ("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 testName) { 75 super(testName); 76 } 77 78 protected void setUp() throws Exception { 79 configContext = createNewConfigurationContext(); 80 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 (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 { 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 { 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 Thread.sleep(4000); 144 } 149 } 150 151 public ConfigurationContext createNewConfigurationContext() throws Exception { 152 File file = new File (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 |