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: *
058: * @author SAAJ RI Development Team
059: */package soap12;
060:
061: import javax.xml.soap.*;
062: import java.io.*;
063: import javax.xml.namespace.QName;
064:
065: import junit.framework.TestCase;
066:
067: public class MetaFactoryTest extends TestCase {
068:
069: public MetaFactoryTest(String name) {
070: super (name);
071: }
072:
073: public void testSOAPFactory11() throws Exception {
074: SOAPFactory factory = SOAPFactory
075: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
076: assertTrue(factory instanceof com.sun.xml.messaging.saaj.soap.ver1_1.SOAPFactory1_1Impl);
077: }
078:
079: public void testMessageFactory11() throws Exception {
080: MessageFactory factory = MessageFactory
081: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
082: assertTrue(factory instanceof com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl);
083: // SOAP 11 Msg factory need not look for a Content-Type header
084: SOAPMessage msg = factory
085: .createMessage(new MimeHeaders(), new FileInputStream(
086: "src/test/resources/dynamic11.xml"));
087: SOAPBody body = msg.getSOAPBody();
088: assertTrue(body instanceof com.sun.xml.messaging.saaj.soap.ver1_1.Body1_1Impl);
089: }
090:
091: public void testSOAPFactory12() throws Exception {
092: SOAPFactory factory = SOAPFactory
093: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
094: assertTrue(factory instanceof com.sun.xml.messaging.saaj.soap.ver1_2.SOAPFactory1_2Impl);
095:
096: QName name = new QName(
097: "http://www.w3.org/2003/05/soap-envelope", "Body");
098: SOAPElement element = factory.createElement(name);
099: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Body1_2Impl);
100:
101: Detail detail = factory.createDetail();
102: assertTrue(detail instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Detail1_2Impl);
103: }
104:
105: public void testMessageFactory12() throws Exception {
106: MessageFactory factory = MessageFactory
107: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
108: assertTrue(factory instanceof com.sun.xml.messaging.saaj.soap.ver1_2.SOAPMessageFactory1_2Impl);
109: // SOAP 12 Msg factory need not look for a Content-Type header
110: SOAPMessage msg = factory.createMessage(null,
111: new FileInputStream(
112: "./src/test/resources/dynamic12.xml"));
113: SOAPBody body = msg.getSOAPBody();
114: assertTrue(body instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Body1_2Impl);
115:
116: SOAPHeader sh = msg.getSOAPHeader();
117: SOAPHeaderElement she = sh.addHeaderElement(msg.getSOAPPart()
118: .getEnvelope().createName("HeaderElement1", "he1",
119: "http://foo.xyz.com"));
120: }
121:
122: public void testSOAPFactoryDynamic() throws Exception {
123: // try to create elements with standard names and ensure the right SOAPDocument is getting
124: // created
125: SOAPFactory factory = SOAPFactory
126: .newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
127: assertTrue(factory instanceof com.sun.xml.messaging.saaj.soap.dynamic.SOAPFactoryDynamicImpl);
128:
129: // test createName methods
130: Name name1 = factory.createName("Envelope", "SOAP-ENV",
131: "http://schemas.xmlsoap.org/soap/envelope/");
132: Name name2 = factory.createName("Envelope", "SOAP-ENV",
133: "http://www.w3.org/2003/05/soap-envelope");
134:
135: Name name3 = factory.createName("test-name");
136:
137: QName name4 = new QName(
138: "http://www.w3.org/2003/05/soap-envelope", "Body");
139:
140: SOAPElement element = factory.createElement(name1);
141: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_1.Envelope1_1Impl);
142:
143: element = factory.createElement(name2);
144: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Envelope1_2Impl);
145:
146: element = factory.createElement(name3);
147: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.impl.ElementImpl);
148:
149: element = factory.createElement(name4);
150: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Body1_2Impl);
151:
152: element = factory.createElement("Header", "SOAP-ENV",
153: "http://www.w3.org/2003/05/soap-envelope");
154: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Header1_2Impl);
155: element = element.addChildElement(factory.createElement(
156: "HeaderChild", null,
157: "http://chemas.xmlsoap.org/soap/envelope/"));
158: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_2.HeaderElement1_2Impl);
159:
160: element = factory.createElement("Header");
161: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.impl.ElementImpl);
162:
163: element = factory.createElement("Header", null,
164: "http://www.w3.org/2003/05/soap-envelope");
165: assertTrue(element instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Header1_2Impl);
166:
167: try {
168: Detail detail = factory.createDetail();
169: fail("Dynamic Protocol does not support createDetail");
170: } catch (UnsupportedOperationException ex) {
171: }
172:
173: }
174:
175: public void testMessageFactoryDynamic() throws Exception {
176: MessageFactory factory = MessageFactory
177: .newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
178: assertTrue(factory instanceof com.sun.xml.messaging.saaj.soap.dynamic.SOAPMessageFactoryDynamicImpl);
179:
180: try {
181: factory.createMessage();
182: assertTrue(false);
183: } catch (UnsupportedOperationException ex) {
184: }
185:
186: // create message with Content-Type header SOAP 1.1
187: MimeHeaders headers = new MimeHeaders();
188: headers.setHeader("Content-Type", "text/xml");
189: SOAPMessage msg = factory
190: .createMessage(headers, new FileInputStream(
191: "src/test/resources/dynamic11.xml"));
192: SOAPBody body = msg.getSOAPBody();
193: assertTrue(body instanceof com.sun.xml.messaging.saaj.soap.ver1_1.Body1_1Impl);
194:
195: // create message with Content-Type header SOAP 1.2
196: headers = new MimeHeaders();
197: headers.setHeader("Content-Type", "application/soap+xml");
198: msg = factory.createMessage(headers, new FileInputStream(
199: "src/test/resources/dynamic12.xml"));
200: body = msg.getSOAPBody();
201: assertTrue(body instanceof com.sun.xml.messaging.saaj.soap.ver1_2.Body1_2Impl);
202:
203: // create message with no Content-Type header
204: try {
205: headers = new MimeHeaders();
206: msg = factory.createMessage(headers, new FileInputStream(
207: "src/test/resources/dynamic12.xml"));
208: assertTrue(false);
209: } catch (SOAPException e) {
210: }
211:
212: // create message with Content-Type header SOAP 1.1, but message type SOAP 1.2
213: try {
214: headers = new MimeHeaders();
215: headers.setHeader("Content-Type", "text/xml");
216: msg = factory.createMessage(headers, new FileInputStream(
217: "src/test/resources/dynamic12.xml"));
218: body = msg.getSOAPBody();
219: fail("expected SOAPException here");
220: } catch (Exception e) {
221: }
222:
223: // create message with Content-Type header SOAP 1.2, but message type SOAP 1.1
224: try {
225: headers = new MimeHeaders();
226: headers.setHeader("Content-Type", "application/soap+xml");
227: msg = factory.createMessage(headers, new FileInputStream(
228: "src/test/resources/dynamic11.xml"));
229: body = msg.getSOAPBody();
230: fail("expected SOAPException here");
231: } catch (Exception e) {
232: }
233: }
234:
235: public void testSOAPFactoryJunk() throws Exception {
236: try {
237: SOAPFactory factory = SOAPFactory.newInstance("junk");
238: assertTrue(false);
239: } catch (SOAPException ex) {
240: }
241: }
242:
243: public void testMessageFactoryJunk() throws Exception {
244: try {
245: MessageFactory factory = MessageFactory.newInstance("junk");
246: assertTrue(false);
247: } catch (SOAPException ex) {
248: }
249: }
250: }
|