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;
019:
020: import java.io.InputStream;
021: import java.net.HttpURLConnection;
022: import java.net.URL;
023:
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NodeList;
030:
031: import org.apache.cxf.common.util.Base64Utility;
032: import org.apache.cxf.helpers.IOUtils;
033: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
034: import org.junit.BeforeClass;
035: import org.junit.Ignore;
036: import org.junit.Test;
037:
038: public class AttachmentProviderXMLClientServerTest extends
039: AbstractBusClientServerTestBase {
040:
041: @BeforeClass
042: public static void startServers() throws Exception {
043: assertTrue("server did not launch correctly", launchServer(
044: AttachementServer.class, true));
045: }
046:
047: @Test
048: @Ignore("REVISIT: I do not think it is valid to use Provider to receive a StreamSource with attachement")
049: public void testRequestWithAttachment() throws Exception {
050:
051: HttpURLConnection connection = (HttpURLConnection) new URL(
052: "http://localhost:9033/XMLServiceAttachment")
053: .openConnection();
054: connection.setRequestMethod("POST");
055:
056: String ct = "multipart/related; type=\"text/xml\"; "
057: + "start=\"rootPart\"; "
058: + "boundary=\"----=_Part_4_701508.1145579811786\"";
059: connection.addRequestProperty("Content-Type", ct);
060:
061: connection.setDoOutput(true);
062:
063: InputStream is = getClass().getResourceAsStream(
064: "attachmentData");
065: IOUtils.copy(is, connection.getOutputStream());
066: connection.getOutputStream().close();
067: is.close();
068:
069: DocumentBuilderFactory factory = DocumentBuilderFactory
070: .newInstance();
071: DocumentBuilder builder = factory.newDocumentBuilder();
072: assertEquals("wrong content type", "application/xml+custom",
073: connection.getContentType());
074: Document result = builder.parse(connection.getInputStream());
075: assertNotNull("result must not be null", result);
076:
077: connection.getInputStream().close();
078:
079: NodeList resList = result.getDocumentElement()
080: .getElementsByTagName("att");
081: assertEquals("Two attachments must've been encoded", 2, resList
082: .getLength());
083:
084: verifyAttachment(resList, "foo", "foobar");
085: verifyAttachment(resList, "bar", "barbaz");
086: }
087:
088: private void verifyAttachment(NodeList atts, String contentId,
089: String value) {
090:
091: for (int i = 0; i < atts.getLength(); i++) {
092: Element expElem = (Element) atts.item(i);
093: String child = expElem.getFirstChild().getNodeValue();
094: String contentIdVal = expElem.getAttribute("contentId");
095: if (contentId.equals(contentIdVal)
096: && (Base64Utility.encode(value.getBytes()).equals(
097: child) || Base64Utility.encode(
098: (value + "\n").getBytes()).equals(child))) {
099: return;
100: }
101: }
102:
103: fail("No encoded attachment with id " + contentId + " found");
104: }
105: }
|