KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wsdl > WSDLTest


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package wsdl;
59
60 import java.io.File JavaDoc;
61 import java.io.FileWriter JavaDoc;
62 import java.net.URL JavaDoc;
63 import java.util.Properties JavaDoc;
64
65 import javax.wsdl.Definition;
66 import javax.wsdl.WSDLException;
67 import javax.wsdl.factory.WSDLFactory;
68 import javax.wsdl.xml.WSDLReader;
69 import javax.wsdl.xml.WSDLWriter;
70 import junit.framework.Test;
71 import junit.framework.TestCase;
72 import junit.framework.TestSuite;
73 import junit.textui.TestRunner;
74 import util.TestUtilities;
75
76 import com.ibm.wsdl.Constants;
77
78 /**
79  * Junit test to test the deserializing and serializing
80  * of a wsdl document including testing reading extension
81  * attributes on Parts.
82  * @author Owen Burroughs
83  * @author Jeremy Hughes <hughesj@apache.org>
84  */

85 public class WSDLTest extends TestCase {
86     private final static String JavaDoc DEF_FACTORY_PROPERTY_NAME =
87         "javax.wsdl.factory.DefinitionFactory";
88     private final static String JavaDoc PRIVATE_DEF_FACTORY_CLASS =
89         "org.apache.wsif.wsdl.WSIFWSDLFactoryImpl";
90
91     private static String JavaDoc WSDL_GOOD = "";
92     private static String JavaDoc WSDL_BAD = "";
93     private static String JavaDoc WSDL_OUT = "";
94     private static boolean debugging = true;
95
96     public static void main(String JavaDoc[] args) {
97         junit.textui.TestRunner.run(suite());
98     }
99
100     public static Test suite() {
101         return new TestSuite(WSDLTest.class);
102     }
103
104     public WSDLTest(String JavaDoc arg0) {
105         super(arg0);
106     }
107
108     protected void setUp() {
109         WSDL_GOOD = TestUtilities.getWsdlPath("java\\test\\wsdl") + "WSDLTestGood.wsdl";
110         WSDL_BAD = TestUtilities.getWsdlPath("java\\test\\wsdl") + "WSDLTestBad.wsdl";
111         WSDL_OUT = TestUtilities.getWsdlPath("java\\test\\wsdl") + "WSDLTestOut.wsdl";
112     }
113
114     public void testReadWriteGoodWSDL() {
115         debug("--- Try good WSDL ---");
116         Definition d = readWSDL(null, WSDL_GOOD);
117         assertNotNull("Definition is null after reading wsdl", d);
118         if (d == null) {
119             fail("--- Cannot write wsdl since definition is null ---");
120         } else {
121             debug("--- Definition read OK ---");
122             boolean ok = writeWSDL(d);
123             assertTrue("Writing WSDL failed", ok);
124         }
125     }
126
127     public void testReadBadWSDL() {
128         debug("--- Try bad WSDL, expect exception ---");
129         Definition d2 = readWSDL(null, WSDL_BAD);
130         assertNull("Bad wsdl was read without error!!", d2);
131     }
132
133     public void testReadMissingDefaultXMLNamepsaceWSDL() {
134         debug("--- Test WSDL with missing default XML namespace is good");
135         Definition d =
136             readWSDL(null, TestUtilities.getWsdlPath("java\\test\\wsdl") + "WSDLTestNested2.wsdl");
137         assertNotNull("Definition is null after reading WSDLTestNested2.wsdl", d);
138     }
139     
140     private static Definition readWSDL(URL JavaDoc contextURL, String JavaDoc wsdlLoc) {
141         try {
142             Properties JavaDoc props = System.getProperties();
143             String JavaDoc oldPropValue = props.getProperty(DEF_FACTORY_PROPERTY_NAME);
144
145             props.setProperty(DEF_FACTORY_PROPERTY_NAME, PRIVATE_DEF_FACTORY_CLASS);
146
147             WSDLFactory factory = WSDLFactory.newInstance();
148             WSDLReader wsdlReader = factory.newWSDLReader();
149             wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false);
150             String JavaDoc context = null;
151             if (contextURL != null)
152                 context = contextURL.toString();
153             Definition def = wsdlReader.readWSDL(context, wsdlLoc);
154
155             if (oldPropValue != null) {
156                 props.setProperty(DEF_FACTORY_PROPERTY_NAME, oldPropValue);
157             } else {
158                 props.remove(DEF_FACTORY_PROPERTY_NAME);
159             }
160             return def;
161         } catch (WSDLException e) {
162             debug("EXCEPTION: " + e.getMessage());
163             return null;
164         }
165     }
166
167     private static boolean writeWSDL(Definition def) {
168         try {
169             WSDLFactory factory = WSDLFactory.newInstance();
170             WSDLWriter wsdlWriter = factory.newWSDLWriter();
171             File JavaDoc f = new File JavaDoc(WSDL_OUT);
172             FileWriter JavaDoc out = new FileWriter JavaDoc(f);
173             wsdlWriter.writeWSDL(def, out);
174             out.close();
175             debug("--- Definition written OK ---");
176             return true;
177         } catch (Exception JavaDoc e) {
178             debug("EXCEPTION: " + e);
179             return false;
180         }
181     }
182
183     private static void debug(String JavaDoc s) {
184         if (debugging)
185             System.out.println(s);
186     }
187 }
188
Popular Tags