KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > mtom > EchoRawMTOMTest


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.mtom;
18
19 /**
20  * @author <a HREF="mailto:thilina@opensource.lk">Thilina Gunarathne </a>
21  */

22 import java.awt.Image JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 import javax.activation.DataHandler JavaDoc;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.axis2.Constants;
32 import org.apache.axis2.addressing.AddressingConstants;
33 import org.apache.axis2.addressing.EndpointReference;
34 import org.apache.axis2.attachments.ImageDataSource;
35 import org.apache.axis2.attachments.JDK13IO;
36 import org.apache.axis2.context.MessageContext;
37 import org.apache.axis2.context.ServiceContext;
38 import org.apache.axis2.description.ServiceDescription;
39 import org.apache.axis2.engine.AxisConfiguration;
40 import org.apache.axis2.engine.Echo;
41 import org.apache.axis2.integration.UtilServer;
42 import org.apache.axis2.om.OMAbstractFactory;
43 import org.apache.axis2.om.OMElement;
44 import org.apache.axis2.om.OMFactory;
45 import org.apache.axis2.om.OMNamespace;
46 import org.apache.axis2.om.OMText;
47 import org.apache.axis2.om.impl.llom.OMTextImpl;
48 import org.apache.axis2.soap.SOAPFactory;
49 import org.apache.axis2.util.Utils;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53 public class EchoRawMTOMTest extends TestCase {
54     private EndpointReference targetEPR = new EndpointReference(
55             AddressingConstants.WSA_TO, "http://127.0.0.1:"
56                     + (UtilServer.TESTING_PORT)
57                     + "/axis/services/EchoXMLService/echoOMElement");
58
59     private Log log = LogFactory.getLog(getClass());
60
61     private QName JavaDoc serviceName = new QName JavaDoc("EchoXMLService");
62
63     private QName JavaDoc operationName = new QName JavaDoc("echoOMElement");
64
65     private QName JavaDoc transportName = new QName JavaDoc("http://localhost/my",
66             "NullTransport");
67
68     private String JavaDoc imageInFileName = "img/test.jpg";
69
70     private String JavaDoc imageOutFileName = "mtom/img/testOut.jpg";
71
72     private AxisConfiguration engineRegistry;
73
74     private MessageContext mc;
75
76     private ServiceContext serviceContext;
77
78     private ServiceDescription service;
79
80     private boolean finish = false;
81
82     public EchoRawMTOMTest() {
83         super(EchoRawMTOMTest.class.getName());
84     }
85
86     public EchoRawMTOMTest(String JavaDoc testName) {
87         super(testName);
88     }
89
90     protected void setUp() throws Exception JavaDoc {
91         UtilServer.start(Constants.TESTING_PATH + "MTOM-enabledRepository");
92         service = Utils.createSimpleService(serviceName, Echo.class.getName(),
93                 operationName);
94         UtilServer.deployService(service);
95         serviceContext = UtilServer.getConfigurationContext()
96                 .createServiceContext(service.getName());
97     }
98
99     protected void tearDown() throws Exception JavaDoc {
100         UtilServer.unDeployService(serviceName);
101         UtilServer.stop();
102     }
103
104     private OMElement createEnvelope() throws Exception JavaDoc {
105
106         DataHandler JavaDoc expectedDH;
107         OMFactory fac = OMAbstractFactory.getOMFactory();
108         OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
109         OMElement rpcWrapEle = fac.createOMElement("echoOMElement", omNs);
110         OMElement data = fac.createOMElement("data", omNs);
111         Image JavaDoc expectedImage;
112         expectedImage = new JDK13IO()
113                 .loadImage(getResourceAsStream("org/apache/axis2/mtom/test.jpg"));
114
115         ImageDataSource dataSource = new ImageDataSource("test.jpg",
116                 expectedImage);
117         expectedDH = new DataHandler JavaDoc(dataSource);
118         OMTextImpl textData = new OMTextImpl(expectedDH, true);
119         data.addChild(textData);
120         //OMTextImpl textData1 = new OMTextImpl(expectedDH, true);
121
//data.addChild(textData1);
122
rpcWrapEle.addChild(data);
123         return rpcWrapEle;
124
125     }
126
127     public void testEchoXMLSync() throws Exception JavaDoc {
128         SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
129
130         OMElement payload = createEnvelope();
131
132         org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call();
133         call.setTo(targetEPR);
134         call.set(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
135         call.setTransportInfo(Constants.TRANSPORT_HTTP,
136                 Constants.TRANSPORT_HTTP, false);
137
138         OMElement result = (OMElement) call.invokeBlocking(operationName
139                 .getLocalPart(), payload);
140         // result.serializeWithCache(new
141
// OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
142
OMElement ele = (OMElement) result.getFirstChild();
143         OMText binaryNode = (OMText) ele.getFirstChild();
144         DataHandler JavaDoc actualDH;
145         actualDH = binaryNode.getDataHandler();
146         Image JavaDoc actualObject = new JDK13IO().loadImage(actualDH.getDataSource()
147                 .getInputStream());
148         FileOutputStream JavaDoc imageOutStream = new FileOutputStream JavaDoc("testout.jpg");
149         new JDK13IO().saveImage("image/jpeg", actualObject, imageOutStream);
150
151     }
152
153     private InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
154         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
155         return cl.getResourceAsStream(path);
156     }
157 }
Popular Tags