1 57 58 package wsdl; 59 60 import java.io.File ; 61 import java.io.FileWriter ; 62 import java.net.URL ; 63 import java.util.Properties ; 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 85 public class WSDLTest extends TestCase { 86 private final static String DEF_FACTORY_PROPERTY_NAME = 87 "javax.wsdl.factory.DefinitionFactory"; 88 private final static String PRIVATE_DEF_FACTORY_CLASS = 89 "org.apache.wsif.wsdl.WSIFWSDLFactoryImpl"; 90 91 private static String WSDL_GOOD = ""; 92 private static String WSDL_BAD = ""; 93 private static String WSDL_OUT = ""; 94 private static boolean debugging = true; 95 96 public static void main(String [] 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 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 contextURL, String wsdlLoc) { 141 try { 142 Properties props = System.getProperties(); 143 String 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 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 f = new File (WSDL_OUT); 172 FileWriter out = new FileWriter (f); 173 wsdlWriter.writeWSDL(def, out); 174 out.close(); 175 debug("--- Definition written OK ---"); 176 return true; 177 } catch (Exception e) { 178 debug("EXCEPTION: " + e); 179 return false; 180 } 181 } 182 183 private static void debug(String s) { 184 if (debugging) 185 System.out.println(s); 186 } 187 } 188 | Popular Tags |