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.tax;
042:
043: import junit.textui.TestRunner;
044: import org.netbeans.modules.xml.DTDDataObject;
045: import org.netbeans.modules.xml.XMLDataObject;
046: import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie;
047: import org.netbeans.tests.xml.XTest;
048: import org.openide.loaders.DataFolder;
049: import org.openide.loaders.DataObject;
050:
051: /**
052: * <P>
053: * <P>
054: * <FONT COLOR="#CC3333" FACE="Courier New, Monospaced" SIZE="+1">
055: * <B>
056: * <BR> XML Module API Test: CreateSimpleXML
057: * </B>
058: * </FONT>
059: * <BR><BR><B>What it tests:</B><BR>
060: *
061: * This test creates simple XML document with DTD and writes it into output.
062: *
063: * <BR><BR><B>How it works:</B><BR>
064: *
065: * 1) create empty XML document from template<BR>
066: * 2) create new Document Type and add it into document<BR>
067: * 3) append XML elements<BR>
068: * 4) write the document into output<BR>
069: *
070: * <BR><BR><B>Settings:</B><BR>
071: * none<BR>
072: *
073: * <BR><BR><B>Output (Golden file):</B><BR>
074: * XML document with DTD.<BR>
075: *
076: * <BR><B>To Do:</B><BR>
077: * none<BR>
078: *
079: * <P>Created on December 20, 2000, 12:33 PM
080: * <P>
081: */
082: public class CreateXMLTest extends XTest {
083: private static String XML_EXT = "xml";
084: private static String DOCUMENT_NAME = "Delme";
085: private static String DTD_SYS_ID = "simpleXXL.dtd";
086: private static String INTERNAL_DTD = "internalDTD.dtd";
087:
088: /** Creates new CoreSettingsTest */
089: public CreateXMLTest(String testName) {
090: super (testName);
091: }
092:
093: public void testCreateXML() throws Exception {
094: String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"
095: + "<root/>\n";
096:
097: try {
098: // delete document if exists
099: DataObject dao = TestUtil.THIS.findData(DOCUMENT_NAME + '.'
100: + XML_EXT);
101: if (dao != null)
102: dao.delete();
103: // create new Data Object
104: DataFolder dataFolder = DataFolder.findFolder(TestUtil.THIS
105: .findData("").getPrimaryFile());
106: XMLDataObject xmlDataObject = (XMLDataObject) TestUtil
107: .createDataObject(dataFolder, DOCUMENT_NAME,
108: XML_EXT, content);
109: TreeEditorCookie cake = (TreeEditorCookie) xmlDataObject
110: .getCookie(TreeEditorCookie.class);
111: TreeDocument document = (TreeDocument) cake
112: .openDocumentRoot();
113:
114: // Create Document Type
115: DTDDataObject dtdDataObject = (DTDDataObject) TestUtil.THIS
116: .findData(INTERNAL_DTD);
117: cake = (TreeEditorCookie) dtdDataObject
118: .getCookie(TreeEditorCookie.class);
119: TreeDTD treeDTD = (TreeDTD) cake.openDocumentRoot();
120: TreeDocumentType docType = new TreeDocumentType(
121: DOCUMENT_NAME);
122: docType.setSystemId(DTD_SYS_ID);
123: TreeChild child = treeDTD.getFirstChild();
124: while (child != null) {
125: docType.appendChild((TreeChild) child.clone());
126: child = child.getNextSibling();
127: }
128: document.setDocumentType(docType);
129:
130: // Create document
131: TreeElement root = document.getDocumentElement();
132: // Create root node
133: root
134: .addAttribute(new TreeAttribute("manager",
135: "Tom Jerry"));
136: root.addAttribute("id", "a");
137: // Create node Product
138: TreeElement product = new TreeElement("Product");
139: root.appendChild(product);
140: root.appendChild(new TreeText("\n"));
141: product.addAttribute("isbn", "123456");
142: product.addAttribute(new TreeAttribute("id", "b"));
143: product.appendChild(new TreeText("\nXML Book\n"));
144: // Create node Descript
145: TreeElement descript = new TreeElement("Descript");
146: product.appendChild(descript);
147: product.appendChild(new TreeText("\n"));
148: descript.addAttribute("lang", "Eng");
149: descript.appendChild(new TreeText("\n"));
150: descript.appendChild(new TreeText(
151: "The book describe how is using XML in"));
152: descript.appendChild(new TreeText("\n"));
153: descript.appendChild(new TreeGeneralEntityReference(
154: "company"));
155: descript.appendChild(new TreeText("from "));
156: descript.appendChild(new TreeGeneralEntityReference("cz"));
157: descript.appendChild(new TreeText("\n"));
158: descript.appendChild(new TreeText(
159: "Very important is author\n"));
160: descript.appendChild(new TreeGeneralEntityReference(
161: "notice"));
162: descript.appendChild(new TreeText("\n"));
163:
164: TestUtil.saveDataObject(xmlDataObject);
165: ref(TestUtil.nodeToString(document));
166: compareReferenceFiles();
167: } catch (Exception ex) {
168: log("\nCreating XML fails due:\n", ex);
169: ex.printStackTrace();
170: fail("\nCreating XML fails due:\n" + ex);
171: }
172: }
173:
174: /**
175: * Performs this testsuite.
176: * @param args the command line arguments
177: */
178: public static void main(String args[]) {
179: TestRunner.run(CreateXMLTest.class);
180: }
181: }
|