KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > util > TestUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.websvc.wsitconf.util;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.net.URI JavaDoc;
32 import java.util.Collection JavaDoc;
33 import javax.swing.text.Document JavaDoc;
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 /**
45  *
46  * @author nn136682
47  */

48 public class TestUtil {
49     public static final String JavaDoc EMPTY_XSD = "resources/Empty.wsdl";
50     
51     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
52         InputStream JavaDoc in = TestUtil.class.getResourceAsStream(path);
53         return loadDocument(in);
54     }
55     
56     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
57     Document JavaDoc sd = new org.netbeans.editor.BaseDocument(
58             org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
59         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
60         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
61         try {
62             String JavaDoc 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 JavaDoc resourcePath) throws Exception JavaDoc {
76         NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath);
77         if (nl != null) {
78             return TestCatalogModel.getDefault().getWSDLModel(nl);
79         }
80         String JavaDoc location = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
81         URI JavaDoc locationURI = new URI JavaDoc(location);
82         TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
83         return TestCatalogModel.getDefault().getWSDLModel(locationURI);
84     }
85     
86     public static WSDLModel createEmptyWSDLModel() throws Exception JavaDoc {
87         return loadWSDLModel(EMPTY_XSD);
88     }
89     
90     /*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
91         return WSDLModelFactory.getDefault().getModel(doc);
92     }*/

93     
94     public static void dumpToStream(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc{
95         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
96         w.print(doc.getText(0, doc.getLength()));
97         w.close();
98         out.close();
99     }
100     
101     public static void dumpToFile(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
102         if (! f.exists()) {
103             f.createNewFile();
104         }
105         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
106         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
107         w.print(doc.getText(0, doc.getLength()));
108         w.close();
109         out.close();
110     }
111     
112     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
113         File JavaDoc f = File.createTempFile("xsm", "xsd");
114         dumpToFile(doc, f);
115         return f;
116     }
117     
118     public static WSDLModel dumpAndReloadModel(Document JavaDoc doc) throws Exception JavaDoc {
119         File JavaDoc f = dumpToTempFile(doc);
120         URI JavaDoc dumpURI = new URI JavaDoc("dummyDump" + count++);
121         TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
122         return TestCatalogModel.getDefault().getWSDLModel(dumpURI);
123     }
124     
125     public static Document JavaDoc loadDocument(File JavaDoc f) throws Exception JavaDoc {
126         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
127         return loadDocument(in);
128     }
129     
130     public static URI JavaDoc getResourceURI(String JavaDoc path) throws RuntimeException JavaDoc {
131         try {
132             return TestUtil.class.getResource(path).toURI();
133         } catch (Exception JavaDoc ex) {
134             throw new RuntimeException JavaDoc(ex);
135         }
136     }
137     
138     public static File JavaDoc getTempDir(String JavaDoc path) throws Exception JavaDoc {
139         File JavaDoc tempdir = new File JavaDoc(System.getProperty("java.io.tmpdir"), path);
140         tempdir.mkdirs();
141         return tempdir;
142     }
143
144     public static FileObject copyResource(String JavaDoc path, FileObject destFolder) throws Exception JavaDoc {
145         String JavaDoc 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 JavaDoc out = dest.getOutputStream(lock);
153         InputStream JavaDoc 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 JavaDoc getFileName(String JavaDoc 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 JavaDoc getGoldenFile(File JavaDoc dir, String JavaDoc testName, String JavaDoc methodName) {
173         String JavaDoc s = dir.getPath() + File.separator + "goldenfiles" + File.separator +
174                 testName + File.separator + methodName + ".pass";
175         return new File JavaDoc(s);
176     }
177 }
178
Popular Tags