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: */package org.apache.cxf.systest.swa;
019:
020: import java.awt.Image;
021: import java.io.InputStream;
022: import java.net.URL;
023:
024: import javax.activation.DataHandler;
025: import javax.imageio.ImageIO;
026: import javax.mail.util.ByteArrayDataSource;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.stream.StreamSource;
029: import javax.xml.ws.Holder;
030:
031: import org.apache.cxf.swa.SwAService;
032: import org.apache.cxf.swa.SwAServiceInterface;
033: import org.apache.cxf.swa.types.DataStruct;
034: import org.apache.cxf.swa.types.OutputResponseAll;
035: import org.apache.cxf.swa.types.VoidRequest;
036: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
037: import org.junit.BeforeClass;
038: import org.junit.Test;
039:
040: public class ClientServerSwaTest extends
041: AbstractBusClientServerTestBase {
042:
043: @BeforeClass
044: public static void startServers() throws Exception {
045: assertTrue("server did not launch correctly",
046: launchServer(Server.class));
047: }
048:
049: @Test
050: public void testSwa() throws Exception {
051: SwAService service = new SwAService();
052:
053: SwAServiceInterface port = service.getSwAServiceHttpPort();
054: // ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
055: // "http://localhost:9037/swa");
056:
057: Holder<String> textHolder = new Holder<String>();
058: Holder<DataHandler> data = new Holder<DataHandler>();
059:
060: ByteArrayDataSource source = new ByteArrayDataSource("foobar"
061: .getBytes(), "application/octet-stream");
062: DataHandler handler = new DataHandler(source);
063:
064: data.value = handler;
065:
066: textHolder.value = "Hi";
067:
068: port.echoData(textHolder, data);
069: InputStream bis = null;
070: bis = data.value.getDataSource().getInputStream();
071: byte b[] = new byte[10];
072: bis.read(b, 0, 10);
073: String string = new String(b);
074: assertEquals("testfoobar", string);
075: assertEquals("Hi", textHolder.value);
076: }
077:
078: @Test
079: public void testSwaWithHeaders() throws Exception {
080: SwAService service = new SwAService();
081:
082: SwAServiceInterface port = service.getSwAServiceHttpPort();
083: // ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
084: // "http://localhost:9037/swa");
085:
086: Holder<String> textHolder = new Holder<String>();
087: Holder<String> headerHolder = new Holder<String>();
088: Holder<DataHandler> data = new Holder<DataHandler>();
089:
090: ByteArrayDataSource source = new ByteArrayDataSource("foobar"
091: .getBytes(), "application/octet-stream");
092: DataHandler handler = new DataHandler(source);
093:
094: data.value = handler;
095:
096: textHolder.value = "Hi";
097: headerHolder.value = "Header";
098:
099: port.echoDataWithHeader(textHolder, data, headerHolder);
100: InputStream bis = null;
101: bis = data.value.getDataSource().getInputStream();
102: byte b[] = new byte[10];
103: bis.read(b, 0, 10);
104: String string = new String(b);
105: assertEquals("testfoobar", string);
106: assertEquals("Hi", textHolder.value);
107: assertEquals("Header", headerHolder.value);
108: }
109:
110: @Test
111: public void testSwaDataStruct() throws Exception {
112: SwAService service = new SwAService();
113:
114: SwAServiceInterface port = service.getSwAServiceHttpPort();
115: // ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
116: // "http://localhost:9037/swa");
117:
118: Holder<DataStruct> structHolder = new Holder<DataStruct>();
119:
120: ByteArrayDataSource source = new ByteArrayDataSource("foobar"
121: .getBytes(), "application/octet-stream");
122: DataHandler handler = new DataHandler(source);
123:
124: DataStruct struct = new DataStruct();
125: struct.setDataRef(handler);
126: structHolder.value = struct;
127:
128: port.echoDataRef(structHolder);
129:
130: handler = structHolder.value.getDataRef();
131: InputStream bis = null;
132: bis = handler.getDataSource().getInputStream();
133: byte b[] = new byte[10];
134: bis.read(b, 0, 10);
135: String string = new String(b);
136: assertEquals("testfoobar", string);
137: }
138:
139: @Test
140: public void testSwaTypes() throws Exception {
141: SwAService service = new SwAService();
142:
143: SwAServiceInterface port = service.getSwAServiceHttpPort();
144:
145: URL url1 = this .getClass().getResource("resources/attach.text");
146: URL url2 = this .getClass().getResource("resources/attach.html");
147: URL url3 = this .getClass().getResource("resources/attach.xml");
148: URL url4 = this .getClass()
149: .getResource("resources/attach.jpeg1");
150: URL url5 = this .getClass()
151: .getResource("resources/attach.jpeg2");
152:
153: DataHandler dh1 = new DataHandler(url1);
154: DataHandler dh2 = new DataHandler(url2);
155: DataHandler dh3 = new DataHandler(url3);
156: //DataHandler dh4 = new DataHandler(url4);
157: //DataHandler dh5 = new DataHandler(url5);
158: Holder<DataHandler> attach1 = new Holder<DataHandler>();
159: attach1.value = dh1;
160: Holder<DataHandler> attach2 = new Holder<DataHandler>();
161: attach2.value = dh2;
162: Holder<Source> attach3 = new Holder<Source>();
163: attach3.value = new StreamSource(dh3.getInputStream());
164: Holder<Image> attach4 = new Holder<Image>();
165: Holder<Image> attach5 = new Holder<Image>();
166: attach4.value = ImageIO.read(url4);
167: attach5.value = ImageIO.read(url5);
168: VoidRequest request = new VoidRequest();
169: OutputResponseAll response = port.echoAllAttachmentTypes(
170: request, attach1, attach2, attach3, attach4, attach5);
171: assertNotNull(response);
172: }
173:
174: }
|