1 19 package org.netbeans.modules.websvc.wsitconf.util; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.OutputStream ; 30 import java.io.PrintWriter ; 31 import java.net.URI ; 32 import java.util.Collection ; 33 import javax.swing.text.Document ; 34 import org.netbeans.modules.websvc.wsitconf.util.TestCatalogModel; 35 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 36 import org.netbeans.modules.xml.schema.model.SchemaModel; 37 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 38 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 39 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.filesystems.FileLock; 43 44 48 public class TestUtil { 49 public static final String EMPTY_XSD = "resources/Empty.wsdl"; 50 51 public static Document getResourceAsDocument(String path) throws Exception { 52 InputStream in = TestUtil.class.getResourceAsStream(path); 53 return loadDocument(in); 54 } 55 56 public static Document loadDocument(InputStream in) throws Exception { 57 Document sd = new org.netbeans.editor.BaseDocument( 58 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 59 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 60 StringBuffer sbuf = new StringBuffer (); 61 try { 62 String line = null; 63 while ((line = br.readLine()) != null) { 64 sbuf.append(line); 65 sbuf.append(System.getProperty("line.separator")); 66 } 67 } finally { 68 br.close(); 69 } 70 sd.insertString(0,sbuf.toString(),null); 71 return sd; 72 } 73 74 public static int count = 0; 75 public static WSDLModel loadWSDLModel(String resourcePath) throws Exception { 76 NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath); 77 if (nl != null) { 78 return TestCatalogModel.getDefault().getWSDLModel(nl); 79 } 80 String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1); 81 URI locationURI = new URI (location); 82 TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath)); 83 return TestCatalogModel.getDefault().getWSDLModel(locationURI); 84 } 85 86 public static WSDLModel createEmptyWSDLModel() throws Exception { 87 return loadWSDLModel(EMPTY_XSD); 88 } 89 90 93 94 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 95 PrintWriter w = new PrintWriter (out); 96 w.print(doc.getText(0, doc.getLength())); 97 w.close(); 98 out.close(); 99 } 100 101 public static void dumpToFile(Document doc, File f) throws Exception { 102 if (! f.exists()) { 103 f.createNewFile(); 104 } 105 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 106 PrintWriter w = new PrintWriter (out); 107 w.print(doc.getText(0, doc.getLength())); 108 w.close(); 109 out.close(); 110 } 111 112 public static File dumpToTempFile(Document doc) throws Exception { 113 File f = File.createTempFile("xsm", "xsd"); 114 dumpToFile(doc, f); 115 return f; 116 } 117 118 public static WSDLModel dumpAndReloadModel(Document doc) throws Exception { 119 File f = dumpToTempFile(doc); 120 URI dumpURI = new URI ("dummyDump" + count++); 121 TestCatalogModel.getDefault().addURI(dumpURI, f.toURI()); 122 return TestCatalogModel.getDefault().getWSDLModel(dumpURI); 123 } 124 125 public static Document loadDocument(File f) throws Exception { 126 InputStream in = new BufferedInputStream (new FileInputStream (f)); 127 return loadDocument(in); 128 } 129 130 public static URI getResourceURI(String path) throws RuntimeException { 131 try { 132 return TestUtil.class.getResource(path).toURI(); 133 } catch (Exception ex) { 134 throw new RuntimeException (ex); 135 } 136 } 137 138 public static File getTempDir(String path) throws Exception { 139 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 140 tempdir.mkdirs(); 141 return tempdir; 142 } 143 144 public static FileObject copyResource(String path, FileObject destFolder) throws Exception { 145 String filename = getFileName(path); 146 147 FileObject dest = destFolder.getFileObject(filename); 148 if (dest == null) { 149 dest = destFolder.createData(filename); 150 } 151 FileLock lock = dest.lock(); 152 OutputStream out = dest.getOutputStream(lock); 153 InputStream in = TestUtil.class.getResourceAsStream(path); 154 try { 155 FileUtil.copy(in, out); 156 } finally { 157 out.close(); 158 in.close(); 159 if (lock != null) lock.releaseLock(); 160 } 161 return dest; 162 } 163 164 public static String getFileName(String path) { 165 int i = path.lastIndexOf('/'); 166 if (i > -1) { 167 return path.substring(i+1); 168 } 169 return path; 170 } 171 172 public static File getGoldenFile(File dir, String testName, String methodName) { 173 String s = dir.getPath() + File.separator + "goldenfiles" + File.separator + 174 testName + File.separator + methodName + ".pass"; 175 return new File (s); 176 } 177 } 178 | Popular Tags |