KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > CreateXMLTest


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax;
20
21 import junit.textui.TestRunner;
22 import org.netbeans.modules.xml.core.DTDDataObject;
23 import org.netbeans.modules.xml.core.XMLDataObject;
24 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie;
25 import org.netbeans.tests.xml.XTest;
26 import org.openide.loaders.DataFolder;
27 import org.openide.loaders.DataObject;
28
29 /**
30  * <P>
31  * <P>
32  * <FONT COLOR="#CC3333" FACE="Courier New, Monospaced" SIZE="+1">
33  * <B>
34  * <BR> XML Module API Test: CreateSimpleXML
35  * </B>
36  * </FONT>
37  * <BR><BR><B>What it tests:</B><BR>
38  *
39  * This test creates simple XML document with DTD and writes it into output.
40  *
41  * <BR><BR><B>How it works:</B><BR>
42  *
43  * 1) create empty XML document from template<BR>
44  * 2) create new Document Type and add it into document<BR>
45  * 3) append XML elements<BR>
46  * 4) write the document into output<BR>
47  *
48  * <BR><BR><B>Settings:</B><BR>
49  * none<BR>
50  *
51  * <BR><BR><B>Output (Golden file):</B><BR>
52  * XML document with DTD.<BR>
53  *
54  * <BR><B>To Do:</B><BR>
55  * none<BR>
56  *
57  * <P>Created on December 20, 2000, 12:33 PM
58  * <P>
59  */

60 public class CreateXMLTest extends XTest {
61     private static String JavaDoc XML_EXT = "xml";
62     private static String JavaDoc DOCUMENT_NAME = "Delme";
63     private static String JavaDoc DTD_SYS_ID = "simpleXXL.dtd";
64     private static String JavaDoc INTERNAL_DTD = "internalDTD.dtd";
65     
66     /** Creates new CoreSettingsTest */
67     public CreateXMLTest(String JavaDoc testName) {
68         super(testName);
69     }
70     
71     public void testCreateXML() throws Exception JavaDoc {
72         String JavaDoc content
73         = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"
74         + "<root/>\n";
75         
76         try {
77             // delete document if exists
78
DataObject dao = TestUtil.THIS.findData(DOCUMENT_NAME + '.' + XML_EXT);
79             if (dao != null) dao.delete();
80             // create new Data Object
81
DataFolder dataFolder = DataFolder.findFolder(TestUtil.THIS.findData("").getPrimaryFile());
82             XMLDataObject xmlDataObject = (XMLDataObject) TestUtil.createDataObject(dataFolder, DOCUMENT_NAME, XML_EXT, content);
83             TreeEditorCookie cake = (TreeEditorCookie) xmlDataObject.getCookie(TreeEditorCookie.class);
84             TreeDocument document = (TreeDocument) cake.openDocumentRoot();
85             
86             // Create Document Type
87
DTDDataObject dtdDataObject = (DTDDataObject) TestUtil.THIS.findData(INTERNAL_DTD);
88             cake = (TreeEditorCookie) dtdDataObject.getCookie(TreeEditorCookie.class);
89             TreeDTD treeDTD = (TreeDTD) cake.openDocumentRoot();
90             TreeDocumentType docType = new TreeDocumentType(DOCUMENT_NAME);
91             docType.setSystemId(DTD_SYS_ID);
92             TreeChild child = treeDTD.getFirstChild();
93             while (child != null) {
94                 docType.appendChild((TreeChild) child.clone());
95                 child = child.getNextSibling();
96             }
97             document.setDocumentType(docType);
98             
99             // Create document
100
TreeElement root = document.getDocumentElement();
101             // Create root node
102
root.addAttribute(new TreeAttribute("manager", "Tom Jerry"));
103             root.addAttribute("id", "a");
104             // Create node Product
105
TreeElement product = new TreeElement("Product");
106             root.appendChild(product);
107             root.appendChild(new TreeText("\n"));
108             product.addAttribute("isbn", "123456");
109             product.addAttribute(new TreeAttribute("id", "b"));
110             product.appendChild(new TreeText("\nXML Book\n"));
111             // Create node Descript
112
TreeElement descript = new TreeElement("Descript");
113             product.appendChild(descript);
114             product.appendChild(new TreeText("\n"));
115             descript.addAttribute("lang", "Eng");
116             descript.appendChild(new TreeText("\n"));
117             descript.appendChild(new TreeText("The book describe how is using XML in"));
118             descript.appendChild(new TreeText("\n"));
119             descript.appendChild(new TreeGeneralEntityReference("company"));
120             descript.appendChild(new TreeText("from "));
121             descript.appendChild(new TreeGeneralEntityReference("cz"));
122             descript.appendChild(new TreeText("\n"));
123             descript.appendChild(new TreeText("Very important is author\n"));
124             descript.appendChild(new TreeGeneralEntityReference("notice"));
125             descript.appendChild(new TreeText("\n"));
126             
127             TestUtil.saveDataObject(xmlDataObject);
128             ref(TestUtil.nodeToString(document));
129             compareReferenceFiles();
130         } catch (Exception JavaDoc ex) {
131             log("\nCreating XML fails due:\n", ex);
132             ex.printStackTrace();
133             fail("\nCreating XML fails due:\n" + ex);
134         }
135     }
136     
137     /**
138      * Performs this testsuite.
139      * @param args the command line arguments
140      */

141     public static void main(String JavaDoc args[]) {
142         TestRunner.run(CreateXMLTest.class);
143     }
144 }
145
Popular Tags