001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package wsdl;
059:
060: import java.io.File;
061: import java.io.FileWriter;
062: import java.net.URL;
063: import java.util.Properties;
064:
065: import javax.wsdl.Definition;
066: import javax.wsdl.WSDLException;
067: import javax.wsdl.factory.WSDLFactory;
068: import javax.wsdl.xml.WSDLReader;
069: import javax.wsdl.xml.WSDLWriter;
070: import junit.framework.Test;
071: import junit.framework.TestCase;
072: import junit.framework.TestSuite;
073: import junit.textui.TestRunner;
074: import util.TestUtilities;
075:
076: import com.ibm.wsdl.Constants;
077:
078: /**
079: * Junit test to test the deserializing and serializing
080: * of a wsdl document including testing reading extension
081: * attributes on Parts.
082: * @author Owen Burroughs
083: * @author Jeremy Hughes <hughesj@apache.org>
084: */
085: public class WSDLTest extends TestCase {
086: private final static String DEF_FACTORY_PROPERTY_NAME = "javax.wsdl.factory.DefinitionFactory";
087: private final static String PRIVATE_DEF_FACTORY_CLASS = "org.apache.wsif.wsdl.WSIFWSDLFactoryImpl";
088:
089: private static String WSDL_GOOD = "";
090: private static String WSDL_BAD = "";
091: private static String WSDL_OUT = "";
092: private static boolean debugging = true;
093:
094: public static void main(String[] args) {
095: junit.textui.TestRunner.run(suite());
096: }
097:
098: public static Test suite() {
099: return new TestSuite(WSDLTest.class);
100: }
101:
102: public WSDLTest(String arg0) {
103: super (arg0);
104: }
105:
106: protected void setUp() {
107: WSDL_GOOD = TestUtilities.getWsdlPath("java\\test\\wsdl")
108: + "WSDLTestGood.wsdl";
109: WSDL_BAD = TestUtilities.getWsdlPath("java\\test\\wsdl")
110: + "WSDLTestBad.wsdl";
111: WSDL_OUT = TestUtilities.getWsdlPath("java\\test\\wsdl")
112: + "WSDLTestOut.wsdl";
113: }
114:
115: public void testReadWriteGoodWSDL() {
116: debug("--- Try good WSDL ---");
117: Definition d = readWSDL(null, WSDL_GOOD);
118: assertNotNull("Definition is null after reading wsdl", d);
119: if (d == null) {
120: fail("--- Cannot write wsdl since definition is null ---");
121: } else {
122: debug("--- Definition read OK ---");
123: boolean ok = writeWSDL(d);
124: assertTrue("Writing WSDL failed", ok);
125: }
126: }
127:
128: public void testReadBadWSDL() {
129: debug("--- Try bad WSDL, expect exception ---");
130: Definition d2 = readWSDL(null, WSDL_BAD);
131: assertNull("Bad wsdl was read without error!!", d2);
132: }
133:
134: public void testReadMissingDefaultXMLNamepsaceWSDL() {
135: debug("--- Test WSDL with missing default XML namespace is good");
136: Definition d = readWSDL(null, TestUtilities
137: .getWsdlPath("java\\test\\wsdl")
138: + "WSDLTestNested2.wsdl");
139: assertNotNull(
140: "Definition is null after reading WSDLTestNested2.wsdl",
141: d);
142: }
143:
144: private static Definition readWSDL(URL contextURL, String wsdlLoc) {
145: try {
146: Properties props = System.getProperties();
147: String oldPropValue = props
148: .getProperty(DEF_FACTORY_PROPERTY_NAME);
149:
150: props.setProperty(DEF_FACTORY_PROPERTY_NAME,
151: PRIVATE_DEF_FACTORY_CLASS);
152:
153: WSDLFactory factory = WSDLFactory.newInstance();
154: WSDLReader wsdlReader = factory.newWSDLReader();
155: wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false);
156: String context = null;
157: if (contextURL != null)
158: context = contextURL.toString();
159: Definition def = wsdlReader.readWSDL(context, wsdlLoc);
160:
161: if (oldPropValue != null) {
162: props.setProperty(DEF_FACTORY_PROPERTY_NAME,
163: oldPropValue);
164: } else {
165: props.remove(DEF_FACTORY_PROPERTY_NAME);
166: }
167: return def;
168: } catch (WSDLException e) {
169: debug("EXCEPTION: " + e.getMessage());
170: return null;
171: }
172: }
173:
174: private static boolean writeWSDL(Definition def) {
175: try {
176: WSDLFactory factory = WSDLFactory.newInstance();
177: WSDLWriter wsdlWriter = factory.newWSDLWriter();
178: File f = new File(WSDL_OUT);
179: FileWriter out = new FileWriter(f);
180: wsdlWriter.writeWSDL(def, out);
181: out.close();
182: debug("--- Definition written OK ---");
183: return true;
184: } catch (Exception e) {
185: debug("EXCEPTION: " + e);
186: return false;
187: }
188: }
189:
190: private static void debug(String s) {
191: if (debugging)
192: System.out.println(s);
193: }
194: }
|