jdk/test/javax/xml/soap/XmlTest.java
author mchung
Wed, 10 Aug 2016 15:51:25 -0700
changeset 40261 86a49ba76f52
parent 37779 7c84df693837
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers Reviewed-by: alanb, ksrini, henryjen, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import javax.xml.soap.AttachmentPart;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import javax.xml.soap.MessageFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import javax.xml.soap.Name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import javax.xml.soap.SOAPEnvelope;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import javax.xml.soap.SOAPException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import javax.xml.soap.SOAPMessage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import javax.xml.transform.stream.StreamSource;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.FileInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.io.FileOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Iterator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * @test
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
    41
 * @modules java.xml.ws
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
    42
 * @run main XmlTest
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * @summary tests JAF DataHandler can be instantiated; test serialize and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 *   deserialize SOAP message containing xml attachment
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
public class XmlTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        File file = new File("message.xml");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        file.deleteOnExit();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        MessageFactory mf = MessageFactory.newInstance();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        SOAPMessage msg = createMessage(mf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        // Save the soap message to file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        try (FileOutputStream sentFile = new FileOutputStream(file)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            msg.writeTo(sentFile);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        // See if we get the image object back
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        try (FileInputStream fin = new FileInputStream(file)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            SOAPMessage newMsg = mf.createMessage(msg.getMimeHeaders(), fin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            newMsg.writeTo(new ByteArrayOutputStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            Iterator<?> i = newMsg.getAttachments();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            while (i.hasNext()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                AttachmentPart att = (AttachmentPart) i.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                Object obj = att.getContent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                if (!(obj instanceof StreamSource)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                    fail("Got incorrect attachment type [" + obj.getClass() + "], " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
                         "expected [javax.xml.transform.stream.StreamSource]");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    private SOAPMessage createMessage(MessageFactory mf) throws SOAPException, IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        SOAPMessage msg = mf.createMessage();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        Name name = envelope.createName("hello", "ex", "http://example.com");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        envelope.getBody().addChildElement(name).addTextNode("THERE!");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        String s = "<root><hello>THERE!</hello></root>";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        AttachmentPart ap = msg.createAttachmentPart(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                new StreamSource(new ByteArrayInputStream(s.getBytes())),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                "text/xml"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        msg.addAttachmentPart(ap);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        msg.saveChanges();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        return msg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    private void fail(String s) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        throw new RuntimeException(s);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        new XmlTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
}