001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.provider;
020:
021: import java.awt.*;
022: import java.io.File;
023:
024: import javax.activation.DataHandler;
025: import javax.activation.DataSource;
026: import javax.imageio.ImageIO;
027: import javax.imageio.stream.FileImageInputStream;
028: import javax.imageio.stream.ImageInputStream;
029: import javax.xml.bind.JAXBContext;
030: import javax.xml.namespace.QName;
031: import javax.xml.ws.Dispatch;
032: import javax.xml.ws.Service;
033:
034: import org.apache.axiom.attachments.ByteArrayDataSource;
035: import org.apache.axis2.jaxws.TestLogger;
036: import org.test.mtom.ImageDepot;
037: import org.test.mtom.ObjectFactory;
038: import org.test.mtom.SendImage;
039: import org.test.mtom.SendImageResponse;
040:
041: /**
042: * The intended purpose of this testcase is to test the MTOM functions in Axis2.
043: * It demostrate an alternative way of sending an attachment using DataHandler.
044: *
045: * This testcase uses a JAXWS Dispatch invocation with JAXB generated request object
046: * as parameter. The endpoint for these testcase is a JAXWS Source Provider.
047: *
048: * These JAXB generated artifacts is based on jaxws\test-resources\xsd\samplemtom.xsd
049: * schema.
050: *
051: * Available Content types are:
052: * "image/gif"
053: * "image/jpeg"
054: * "text/plain"
055: * "multipart/*"
056: * "text/xml"
057: * "application/xml"
058: * This initial testcase only covers the "multipart/*" and "text/plain" mime types.
059: * The ultimate goal is to provide testcases for the remaining mime types.
060: *
061: */
062: public class JAXBProviderTests extends ProviderTestCase {
063:
064: String endpointUrl = "http://localhost:8080/axis2/services/JAXBProviderService";
065: private QName serviceName = new QName("http://ws.apache.org/axis2",
066: "JAXBProviderService");
067: DataSource stringDS, imageDS;
068:
069: protected void setUp() throws Exception {
070: super .setUp();
071:
072: //Create a DataSource from a String
073: String string = "Sending a JAXB generated string object to Source Provider endpoint";
074: stringDS = new ByteArrayDataSource(string.getBytes(),
075: "text/plain");
076:
077: //Create a DataSource from an image
078: File file = new File(imageResourceDir + File.separator
079: + "test.jpg");
080: ImageInputStream fiis = new FileImageInputStream(file);
081: Image image = ImageIO.read(fiis);
082: imageDS = new DataSourceImpl("image/jpeg", "test.jpg", image);
083:
084: }
085:
086: protected void tearDown() throws Exception {
087: super .tearDown();
088: }
089:
090: public JAXBProviderTests(String name) {
091: super (name);
092: }
093:
094: /**
095: * test String
096: * @throws Exception
097: */
098: public void testMTOMAttachmentString() throws Exception {
099: TestLogger.logger
100: .debug("---------------------------------------");
101: TestLogger.logger.debug("test: " + getName());
102:
103: //Create a DataHandler with the String DataSource object
104: DataHandler dataHandler = new DataHandler(stringDS);
105:
106: //Store the data handler in ImageDepot bean
107: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
108: imageDepot.setImageData(dataHandler);
109:
110: Service svc = Service.create(serviceName);
111: svc.addPort(portName, null, endpointUrl);
112:
113: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
114:
115: Dispatch<Object> dispatch = svc.createDispatch(portName, jbc,
116: Service.Mode.PAYLOAD);
117:
118: //Create a request bean with imagedepot bean as value
119: ObjectFactory factory = new ObjectFactory();
120: SendImage request = factory.createSendImage();
121: request.setInput(imageDepot);
122:
123: TestLogger.logger
124: .debug(">> Invoking Dispatch<Object> JAXBProviderService");
125:
126: SendImageResponse response = (SendImageResponse) dispatch
127: .invoke(request);
128:
129: TestLogger.logger.debug(">> Response [" + response.toString()
130: + "]");
131: }
132:
133: /**
134: * test Image
135: * @throws Exception
136: */
137: public void testMTOMAttachmentImage() throws Exception {
138: TestLogger.logger
139: .debug("---------------------------------------");
140: TestLogger.logger.debug("test: " + getName());
141:
142: //Create a DataHandler with the String DataSource object
143: DataHandler dataHandler = new DataHandler(imageDS);
144:
145: //Store the data handler in ImageDepot bean
146: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
147: imageDepot.setImageData(dataHandler);
148:
149: Service svc = Service.create(serviceName);
150: svc.addPort(portName, null, endpointUrl);
151:
152: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
153:
154: Dispatch<Object> dispatch = svc.createDispatch(portName, jbc,
155: Service.Mode.PAYLOAD);
156:
157: //Create a request bean with imagedepot bean as value
158: ObjectFactory factory = new ObjectFactory();
159: SendImage request = factory.createSendImage();
160: request.setInput(imageDepot);
161:
162: TestLogger.logger
163: .debug(">> Invoking Dispatch<Object> JAXBProviderService");
164:
165: SendImageResponse response = (SendImageResponse) dispatch
166: .invoke(request);
167:
168: TestLogger.logger.debug(">> Response [" + response.toString()
169: + "]");
170: }
171: }
|