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.provider.datasource;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.net.HttpURLConnection;
025: import java.net.URL;
026: import java.util.Properties;
027: import java.util.logging.Logger;
028:
029: import javax.mail.MessagingException;
030: import javax.mail.Session;
031: import javax.mail.internet.MimeMessage;
032: import javax.mail.internet.MimeMultipart;
033: import javax.xml.transform.OutputKeys;
034: import javax.xml.transform.Source;
035: import javax.xml.transform.Transformer;
036: import javax.xml.transform.TransformerFactory;
037: import javax.xml.transform.stream.StreamResult;
038: import javax.xml.transform.stream.StreamSource;
039:
040: import org.apache.cxf.helpers.IOUtils;
041: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
042: import org.junit.Before;
043: import org.junit.BeforeClass;
044: import org.junit.Test;
045:
046: public class DataSourceProviderTest extends
047: AbstractBusClientServerTestBase {
048:
049: static final Logger LOG = Logger
050: .getLogger(DataSourceProviderTest.class.getName());
051: private static final String BOUNDARY = "----=_Part_4_701508.1145579811786";
052: private HttpURLConnection conn;
053: private URL url;
054:
055: @BeforeClass
056: public static void startServers() throws Exception {
057: assertTrue("server did not launch correctly", launchServer(
058: Server.class, true));
059: }
060:
061: @Before
062: public void createConnection() throws Exception {
063: url = new URL("http://localhost:9000/test/foo");
064: conn = (HttpURLConnection) url.openConnection();
065: conn.setDoOutput(true);
066: }
067:
068: @Test
069: public void invokeOnServer() throws Exception {
070: url = new URL("http://localhost:9000/test/foo");
071: conn = (HttpURLConnection) url.openConnection();
072: printSource(new StreamSource(conn.getInputStream()));
073: }
074:
075: @Test
076: public void postAttachmentToServer() throws Exception {
077: String contentType = "multipart/related; type=\"text/xml\"; "
078: + "start=\"attachmentData\"; " + "boundary=\""
079: + BOUNDARY + "\"";
080:
081: InputStream in = getClass().getResourceAsStream(
082: "/attachmentBinaryData");
083: assertNotNull("could not load test data", in);
084:
085: conn.setRequestMethod("POST");
086: conn.addRequestProperty("Content-Type", contentType);
087: OutputStream out = conn.getOutputStream();
088: IOUtils.copy(in, out);
089: out.close();
090:
091: MimeMultipart mm = readAttachmentParts(conn
092: .getRequestProperty("Content-Type"), conn
093: .getInputStream());
094:
095: assertEquals("incorrect number of parts received by server", 3,
096: mm.getCount());
097:
098: }
099:
100: private void printSource(Source source) {
101: try {
102: ByteArrayOutputStream bos = new ByteArrayOutputStream();
103: StreamResult sr = new StreamResult(bos);
104: Transformer trans = TransformerFactory.newInstance()
105: .newTransformer();
106: Properties oprops = new Properties();
107: oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
108: trans.setOutputProperties(oprops);
109: trans.transform(source, sr);
110: assertEquals(bos.toString(),
111: "<doc><response>Hello</response></doc>");
112: bos.close();
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117:
118: public static MimeMultipart readAttachmentParts(String contentType,
119: InputStream bais) throws MessagingException, IOException {
120: Session session = Session.getDefaultInstance(new Properties());
121: MimeMessage mm = new MimeMessage(session, bais);
122: mm.addHeaderLine("Content-Type:" + contentType);
123: return (MimeMultipart) mm.getContent();
124: }
125:
126: }
|