001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.compapp.projects.common;
042:
043: import java.io.BufferedReader;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047: import java.util.ArrayList;
048: import java.util.List;
049: import org.netbeans.modules.compapp.projects.common.CatalogWSDL.CatalogWSDLSerializer;
050: import org.netbeans.modules.compapp.projects.common.CatalogWSDL.Entry;
051: import org.netbeans.modules.compapp.projects.common.CatalogWSDL.EntryType;
052: import org.openide.filesystems.FileObject;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.junit.NbTestSuite;
055: import org.openide.filesystems.LocalFileSystem;
056: import org.openide.util.Exceptions;
057:
058: /**
059: *
060: * @author chikkala
061: */
062: public class CatalogWSDLTest extends NbTestCase {
063:
064: public static final String TEST_WSDL_NS = "http://test.com/wsdl/test";
065: public static final String TEST_WSDL_LOC = "xyz/wsdl/test";
066: public static final String TEST_XSD_NS = "http://test.com/xsd/test";
067: public static final String TEST_XSD_LOC = "123/xsd/test";
068: FileObject rootDir;
069:
070: public CatalogWSDLTest(String testName) {
071: super (testName);
072: }
073:
074: /** Creates suite from particular test cases. You can define order of testcases here. */
075: public static NbTestSuite suite() {
076: NbTestSuite suite = new NbTestSuite();
077: suite.addTest(new CatalogWSDLTest("testGetEntries"));
078: suite.addTest(new CatalogWSDLTest("testAddAllEntries"));
079: suite.addTest(new CatalogWSDLTest("testClear"));
080: suite.addTest(new CatalogWSDLTest(
081: "testCreateEmptyCatalogWSDLFile"));
082: suite.addTest(new CatalogWSDLTest("testLoadCatalogWSDL"));
083: suite.addTest(new CatalogWSDLTest("testSaveCatalogWSDL"));
084: suite.addTest(new CatalogWSDLTest("testModifyCatalogWSDL"));
085: return suite;
086: }
087:
088: /* Method allowing test execution directly from the IDE. */
089: public static void main(java.lang.String[] args) {
090: // run whole suite
091: junit.textui.TestRunner.run(suite());
092: // run only selected test case
093: //junit.textui.TestRunner.run(new MyTestCaseTest("test1"));
094: }
095:
096: @Override
097: protected void setUp() throws Exception {
098: super .setUp();
099:
100: // CatalogWSDL.setModelSourceProvider(new TestModelSourceProvider());
101:
102: clearWorkDir();
103: LocalFileSystem fs = new LocalFileSystem();
104: fs.setRootDirectory(getWorkDir());
105: rootDir = fs.getRoot();
106: // org.openide.util.test.MockLookup lookup;
107: // FileUtil.setMIMEType("xml", "text/xml");
108: // FileUtil.setMIMEType("wsdl", "text/x-wsdl+xml");
109: // FileUtil.setMIMEType("xml", "text/x-wsdl+xml");
110: // System.out.println("JUNIT Data Dir " + getDataDir());
111: }
112:
113: @Override
114: protected void tearDown() throws Exception {
115: super .tearDown();
116: }
117:
118: public void printFileObject(FileObject fo) {
119: if (true) {
120: return;
121: }
122: InputStream in = null;
123: try {
124: in = fo.getInputStream();
125: InputStreamReader reader = new InputStreamReader(in);
126: BufferedReader buff = new BufferedReader(reader);
127: String line = null;
128: while ((line = buff.readLine()) != null) {
129: System.out.println(line);
130: }
131: } catch (Exception ex) {
132: Exceptions.printStackTrace(ex);
133: } finally {
134: if (in != null) {
135: try {
136: in.close();
137: } catch (IOException ex) {
138: Exceptions.printStackTrace(ex);
139: }
140: }
141: }
142: }
143:
144: /**
145: * Test of getEntries method, of class CatalogWSDL.
146: */
147: public void testGetEntries() {
148: System.out.println("getEntriesEmpty");
149: CatalogWSDL instance = new CatalogWSDL();
150: List<Entry> expResult = new ArrayList<Entry>();
151: List<Entry> result = instance.getEntries();
152: assertEquals(expResult.size(), result.size());
153: instance.addEntry(Entry.createXSDEntry("http://x/y.xsd",
154: "/x/y.xsd"));
155: instance.addEntry(Entry.createWSDLEntry("http://x/y.wsdl",
156: "/x/y.wsdl"));
157: result = instance.getEntries();
158: assertEquals(result.size(), 2);
159: }
160:
161: /**
162: * Test of addAllEntries method, of class CatalogWSDL.
163: */
164: public void testAddAllEntries() {
165: System.out.println("addAllEntries");
166: CatalogWSDL instance = new CatalogWSDL();
167: List<Entry> expResult = new ArrayList<Entry>();
168: List<Entry> result = instance.getEntries();
169: assertEquals(expResult.size(), result.size());
170: instance.addEntry(Entry.createXSDEntry("http://x/y.xsd",
171: "/x/y.xsd"));
172: List<Entry> entries = new ArrayList<Entry>();
173: entries.add(Entry.createWSDLEntry("http://x/y.wsdl",
174: "/x/y.wsdl"));
175: entries.add(Entry.createXSDEntry(TEST_XSD_NS + 1,
176: TEST_XSD_LOC + 1));
177: instance.addAllEntries(entries);
178: result = instance.getEntries();
179: assertEquals(result.size(), 3);
180: }
181:
182: /**
183: * Test of addEntry method, of class CatalogWSDL.
184: */
185: public void testAddEntry() {
186: System.out.println("addEntry");
187: Entry entry = null;
188: CatalogWSDL instance = new CatalogWSDL();
189: boolean expResult = false;
190: boolean result = instance.addEntry(entry);
191: assertEquals(expResult, result);
192: // TODO review the generated test code and remove the default call to fail.
193: fail("The test case is a prototype.");
194: }
195:
196: /**
197: * Test of removeEntry method, of class CatalogWSDL.
198: */
199: public void testRemoveEntry() {
200: System.out.println("removeEntry");
201: Entry entry = null;
202: CatalogWSDL instance = new CatalogWSDL();
203: boolean expResult = false;
204: boolean result = instance.removeEntry(entry);
205: assertEquals(expResult, result);
206: // TODO review the generated test code and remove the default call to fail.
207: fail("The test case is a prototype.");
208: }
209:
210: /**
211: * Test of clear method, of class CatalogWSDL.
212: */
213: public void testClear() {
214: System.out.println("clear");
215: CatalogWSDL instance = new CatalogWSDL();
216: instance.addEntry(Entry.createXSDEntry("http://x/y.xsd",
217: "/x/y.xsd"));
218: instance.addEntry(Entry.createWSDLEntry("http://x/y.wsdl",
219: "/x/y.wsdl"));
220: List<Entry> result = instance.getEntries();
221: assertEquals(result.size(), 2);
222: instance.clear();
223: result = instance.getEntries();
224: assertEquals(result.size(), 0);
225: }
226:
227: /**
228: * Test of testCreateEmptyCatalogWSDLFile method, of class CatalogWSDL.
229: */
230: public void testCreateEmptyCatalogWSDLFile() throws Exception {
231: System.out.println("createCatalogWSDLFile");
232: FileObject catDir = rootDir;
233: FileObject result = CatalogWSDLSerializer
234: .createCatalogWSDLFile(catDir, true);
235: assertNotNull(result);
236: }
237:
238: /**
239: * Test of loadCatalogWSDL method, of class CatalogWSDL.
240: */
241: public void testLoadCatalogWSDL() throws Exception {
242: // *** DataObject org.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLDataObject
243: // *** Document typeorg.netbeans.modules.editor.NbEditorDocument
244: //// FileObject xmlFSRoot = Repository.getDefault().getDefaultFileSystem().getRoot();
245: //// assertNotNull(xmlFSRoot);
246: //// System.out.println("#### Default FileSystem path " + xmlFSRoot.getName());
247: //// Enumeration folders = xmlFSRoot.getFolders(true);
248: //// for (;folders.hasMoreElements();) {
249: //// FileObject fo = (FileObject)folders.nextElement();
250: //// System.out.println(fo.getPath());
251: //// }
252:
253: System.out.println("loadCatalogWSDL");
254: FileObject catDir = rootDir;
255: FileObject catFO = CatalogWSDLSerializer.createCatalogWSDLFile(
256: catDir, true);
257: //// DataObject dObj = DataObject.find(catFO);
258: //// System.out.println("DataObject " + dObj.getClass().getName());
259: //// assertNotNull(dObj);
260: //// EditorCookie ec = (EditorCookie) dObj.getCookie(EditorCookie.class);
261: //// Document doc = ec.openDocument();
262: //// assertNotNull(doc);
263: //// if (!(doc instanceof BaseDocument)) {
264: //// fail("Document return is not a BaseDocument" + doc.getClass().getName());
265: //// }
266: CatalogWSDL result = CatalogWSDL.loadCatalogWSDL(catDir);
267: assertNotNull(result);
268: assertEquals(result.getEntries().size(), 0);
269: }
270:
271: /**
272: * Test of saveCatalogWSDL method, of class CatalogWSDL.
273: */
274: public void testSaveCatalogWSDL() throws Exception {
275: System.out.println("saveCatalogWSDL");
276:
277: FileObject catDir = rootDir;
278:
279: CatalogWSDL catalog = new CatalogWSDL();
280:
281: catalog.addEntry(Entry.createWSDLEntry(TEST_WSDL_NS + 1,
282: TEST_WSDL_LOC + 1));
283: catalog.addEntry(Entry.createXSDEntry(TEST_XSD_NS + 1,
284: TEST_XSD_LOC + 1));
285:
286: CatalogWSDL.saveCatalogWSDL(catalog, catDir);
287:
288: CatalogWSDL saveCat1 = CatalogWSDL.loadCatalogWSDL(rootDir);
289:
290: assertEquals(saveCat1.getEntries().size(), 2);
291:
292: assertEquals(saveCat1.getEntries(EntryType.WSDL).size(), 1);
293:
294: assertEquals(saveCat1.getEntries(EntryType.XSD).size(), 1);
295:
296: catalog.addEntry(Entry.createWSDLEntry(TEST_WSDL_NS + 2,
297: TEST_WSDL_LOC + 2));
298: catalog.addEntry(Entry.createXSDEntry(TEST_XSD_NS + 2,
299: TEST_XSD_LOC + 2));
300:
301: //// FileObject saveDir = FileUtil.createFolder(rootDir, "save");
302: //// CatalogWSDL result = CatalogWSDL.loadCatalogWSDL(catDir);
303: //// assertNotNull(result);
304: ////// for ( Entry entry : result.getEntries() ) {
305: ////// System.out.println(entry);
306: ////// }
307: ////
308: //// CatalogWSDL loadCat1 = CatalogWSDL.loadCatalogWSDL(catDir);
309: //// System.out.println("LoadCat1 Catalog WSDL Entries " + loadCat1.getEntries().size());
310: ////// for ( Entry entry : loadCat1.getEntries() ) {
311: ////// System.out.println(entry);
312: ////// }
313: //// loadCat1.addEntry(Entry.createXSDEntry("http://example/xsd/c", "xyz/123/c.xsd"));
314: //// System.out.println("LoadCat1 Catalog WSDL Entries " + loadCat1.getEntries().size());
315: ////
316: //// CatalogWSDL.saveCatalogWSDL(loadCat1, catDir);
317: //// printFileObject(catDir.getFileObject(CatalogWSDL.CATALOG_WSDL_FILE));
318: ////
319: //// CatalogWSDL loadCat2 = CatalogWSDL.loadCatalogWSDL(catDir);
320: //// System.out.println("loadCat2 Catalog WSDL Entries " + loadCat2.getEntries().size());
321: //// printFileObject(catDir.getFileObject(CatalogWSDL.CATALOG_WSDL_FILE));
322: ////
323: //// assertEquals(result.getEntries().size(), 0);
324: }
325:
326: /**
327: * Test Modify CatalogWSDL .
328: */
329: public void testModifyCatalogWSDL() throws Exception {
330: System.out.println("ModifyCatalogWSDL");
331:
332: FileObject catDir = rootDir;
333:
334: CatalogWSDL catalog = new CatalogWSDL();
335:
336: catalog.addEntry(Entry.createWSDLEntry(TEST_WSDL_NS + 1,
337: TEST_WSDL_LOC + 1));
338: CatalogWSDL.saveCatalogWSDL(catalog, catDir);
339: CatalogWSDL loadCat1 = CatalogWSDL.loadCatalogWSDL(rootDir);
340: assertEquals(loadCat1.getEntries().size(), 1);
341: assertEquals(loadCat1.getEntries(EntryType.WSDL).size(), 1);
342: assertEquals(loadCat1.getEntries(EntryType.XSD).size(), 0);
343:
344: loadCat1.addEntry(Entry.createXSDEntry(TEST_XSD_NS + 1,
345: TEST_XSD_LOC + 1));
346: CatalogWSDL.saveCatalogWSDL(loadCat1, catDir);
347: CatalogWSDL loadCat2 = CatalogWSDL.loadCatalogWSDL(rootDir);
348: assertEquals(loadCat2.getEntries().size(), 2);
349: assertEquals(loadCat2.getEntries(EntryType.WSDL).size(), 1);
350: assertEquals(loadCat2.getEntries(EntryType.XSD).size(), 1);
351:
352: loadCat2.addEntry(Entry.createWSDLEntry(TEST_WSDL_NS + 2,
353: TEST_WSDL_LOC + 2));
354: loadCat2.addEntry(Entry.createXSDEntry(TEST_XSD_NS + 2,
355: TEST_XSD_LOC + 2));
356: CatalogWSDL.saveCatalogWSDL(loadCat2, catDir);
357: CatalogWSDL loadCat3 = CatalogWSDL.loadCatalogWSDL(rootDir);
358: assertEquals(loadCat3.getEntries().size(), 4);
359: assertEquals(loadCat3.getEntries(EntryType.WSDL).size(), 2);
360: assertEquals(loadCat3.getEntries(EntryType.XSD).size(), 2);
361:
362: }
363:
364: //// public static void printDocument(Document doc) {
365: //// try {
366: //// System.out.println(doc.getText(0, doc.getLength()));
367: //// } catch (BadLocationException ex) {
368: //// ex.printStackTrace();
369: //// }
370: //// }
371: ////
372: //// public void printCatalogWSDL(CatalogWSDL cat) {
373: //// System.out.println("BEGIN print catalog entries");
374: //// for (Entry entry : cat.getEntries()) {
375: //// System.out.println(entry.toString());
376: //// }
377: //// System.out.println("END print catalog entries");
378: //// }
379: ////
380: //// public static void printCatalogWSDL(Definitions defs) {
381: ////
382: //// System.out.println("**** BEGIN Printing WSDL Model ****");
383: //// System.out.println("Printing wsdl imports");
384: //// Collection<Import> imports = defs.getImports();
385: //// for (Import imp : imports) {
386: //// System.out.println(imp);
387: //// }
388: //// System.out.println("Printing schema imports");
389: //// Collection<Schema> schemas = defs.getTypes().getSchemas();
390: //// Schema schema = null;
391: //// if (schemas != null && schemas.size() > 0) {
392: //// schema = schemas.iterator().next();
393: //// }
394: ////
395: //// if (schema != null) {
396: //// Collection<org.netbeans.modules.xml.schema.model.Import> schemaImports =
397: //// schema.getImports();
398: //// for (Import imp : imports) {
399: //// System.out.println(imp);
400: //// }
401: //// }
402: //// System.out.println("**** END Printing WSDL Model ****");
403: //// }
404: ////
405: //// public static void printDocumentOfWSDLModel(WSDLModel wsdlModel) {
406: //// printCatalogWSDL(wsdlModel.getDefinitions());
407: //// System.out.println("@@@@@ ModelAccess Class " + wsdlModel.getAccess().getClass().getName());
408: //// XDMAccess access = (XDMAccess) wsdlModel.getAccess();
409: //// XDMModel xdmModel = access.getReferenceModel();
410: //// // Document xdmSwingDoc = xdmModel.get
411: //// FlushVisitor flushvisitor = new FlushVisitor();
412: //// // String newXMLText = flushvisitor.flushModel(xdmModel.getDocument());
413: //// String newXMLText = xdmModel.getCurrentDocumentText();
414: //// System.out.println("####### XMD XML TEXT #### ");
415: //// System.out.println(newXMLText);
416: ////// Document myDoc = sModelSourceProvider.loadSwingDocument(catalogFO);
417: ////// try {
418: ////// Utils.replaceDocument(myDoc, newXMLText);
419: ////// } catch (Exception ex) {
420: ////// ex.printStackTrace();
421: ////// }
422: //// System.out.println("####### SWING DOC TEXT #### ");
423: ////// printDocument(myDoc);
424: ////
425: //// }
426: }
|