001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055:
056: /**
057: * $Id: WSISupportTest.java,v 1.2 2007/07/16 16:42:02 ofung Exp $
058: */package wsi;
059:
060: import java.io.ByteArrayOutputStream;
061: import java.util.Locale;
062:
063: import javax.xml.soap.*;
064: import javax.xml.transform.dom.DOMSource;
065:
066: import junit.framework.TestCase;
067:
068: /*
069: * Unit test cases to test out the ws-i support.
070: *
071: * @author Manveen Kaur (manveen.kaur@sun.com)
072: */
073:
074: public class WSISupportTest extends TestCase {
075:
076: public WSISupportTest(String name) {
077: super (name);
078: }
079:
080: private SOAPMessage createMessage() throws SOAPException {
081: MessageFactory msgFactory = MessageFactory.newInstance();
082:
083: SOAPMessage msg = msgFactory.createMessage();
084:
085: return msg;
086: }
087:
088: /*
089: * Test to verify set char encoding.
090: */
091: public void testCharEncoding() {
092:
093: try {
094:
095: SOAPMessage message = createMessage();
096: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
097: SOAPHeader hdr = envelope.getHeader();
098:
099: // create a header element
100: SOAPHeaderElement transaction = hdr
101: .addHeaderElement(envelope.createName(
102: "Transaction", "t", "some-uri"));
103:
104: transaction.addTextNode("5");
105:
106: message.setProperty(SOAPMessage.WRITE_XML_DECLARATION,
107: "true");
108: // set char encoding to utf-16
109: message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING,
110: "utf-16");
111: //message.writeTo(System.out);
112:
113: //System.out.println("\n\nContent type = " + message.getContentType());
114: } catch (Exception e) {
115: // e.printStackTrace();
116: fail("No exception should be thrown: " + e);
117: }
118: }
119:
120: public void xtestContentType() {
121:
122: try {
123: MessageFactory factory = MessageFactory.newInstance();
124: SOAPMessage message = factory.createMessage();
125: SOAPPart part = message.getSOAPPart();
126: SOAPEnvelope envelope = part.getEnvelope();
127: SOAPHeader hdr = envelope.getHeader();
128: SOAPBody body = envelope.getBody();
129: SOAPFault fault = body.addFault();
130:
131: part.setMimeHeader("Content-Type", "text/html");
132: fault.setFaultCode("100");
133: fault.setFaultString("some reason for fault");
134:
135: fault.addChildElement("test");
136: //message.writeTo(System.out);
137:
138: // System.out.println("\n\nContent type = " + message.getContentType());
139:
140: } catch (Exception e) {
141: e.printStackTrace();
142: }
143: }
144:
145: public void testFaultDetail() throws Exception {
146:
147: String RETAIL_ORDER_NAMESPACE = "http://www.Something.com";
148:
149: MessageFactory factory = MessageFactory.newInstance();
150: SOAPMessage message = factory.createMessage();
151: SOAPPart part = message.getSOAPPart();
152: SOAPEnvelope envelope = part.getEnvelope();
153: SOAPHeader hdr = envelope.getHeader();
154: SOAPBody body = envelope.getBody();
155:
156: SOAPFault fault = body.addFault();
157: //Detail detail = fault.addDetail();
158:
159: SOAPFactory soapFactory = SOAPFactory.newInstance();
160: Detail detail = soapFactory.createDetail();
161: Name name = soapFactory.createName("InvalidProductCode", "ns1",
162: RETAIL_ORDER_NAMESPACE);
163: DetailEntry detailEntry = detail.addDetailEntry(name);
164: SOAPElement soapElement = detailEntry.addChildElement("Reason",
165: "ns1", RETAIL_ORDER_NAMESPACE);
166: soapElement.addTextNode("InvalidProductCode");
167: soapElement = detailEntry.addChildElement("ProductNumber",
168: "ns1", RETAIL_ORDER_NAMESPACE);
169: soapElement.addTextNode("600510");
170:
171: DOMSource source = new DOMSource(detail);
172:
173: fault.addChildElement(detail);
174:
175: String expected = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/"
176: + "soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:"
177: + "Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>"
178: + "Fault string, and possibly fault code, not set</faultstring>"
179: + "<detail><ns1:InvalidProductCode xmlns:ns1=\"http://www."
180: + "Something.com\"><ns1:Reason>InvalidProductCode</ns1:Reason><ns1:"
181: + "ProductNumber>600510</ns1:ProductNumber></ns1:InvalidProductCode>"
182: + "</detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>";
183:
184: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
185:
186: message.writeTo(bytesOut);
187: //message.writeTo(System.out);
188:
189: assertEquals(expected, bytesOut.toString());
190:
191: }
192:
193: public void testFaultLocale() throws Exception {
194:
195: MessageFactory factory = MessageFactory.newInstance();
196: SOAPMessage message = factory.createMessage();
197: SOAPPart part = message.getSOAPPart();
198: SOAPEnvelope envelope = part.getEnvelope();
199: SOAPHeader hdr = envelope.getHeader();
200: SOAPBody body = envelope.getBody();
201:
202: // add fault here
203: SOAPFault fault = body.addFault();
204: // Setting fault string to locale en_US
205: fault.setFaultString("something", Locale.US);
206:
207: assertTrue("Locale should be english", Locale.US.equals(fault
208: .getFaultStringLocale()));
209:
210: }
211:
212: public static void main(String argv[]) {
213:
214: junit.textui.TestRunner.run(WSISupportTest.class);
215:
216: }
217:
218: }
|