KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import junit.textui.TestRunner;
25 import org.netbeans.modules.xml.core.XMLDataObject;
26 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie;
27 import org.netbeans.tests.xml.XTest;
28 import org.openide.cookies.CloseCookie;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.loaders.DataFolder;
31
32 /**
33  * <P>
34  * <P>
35  * <FONT COLOR="#CC3333" FACE="Courier New, Monospaced" SIZE="+1">
36  * <B>
37  * <BR> XML Module API Test: Encoding Test
38  * </B>
39  * </FONT>
40  * <BR><BR><B>What it tests:</B><BR>
41  * Tests check whether the documents saved with different encodings are identical.<BR>
42  *
43  * <BR><B>How it works:</B><BR>
44  * Test doing for each encoding:<BR>
45  * - save document with selected encoding<BR>
46  * - close the document<BR>
47  * - reload the document from disk<BR>
48  * - check if the reloaded document and the original are identical<BR>
49  *
50  * <BR><BR><B>Settings:</B><BR>
51  * None
52  *
53  * <BR><BR><B>Output (Golden file):</B><BR>
54  * DTD for the XML document.<BR>
55  * <BR><B>Possible reasons of failure:</B>
56  * <UL>
57  * <LI type="circle">
58  * <I>None<BR></I>
59  * </LI>
60  * </UL>
61  * <P>
62  */

63
64 public class EncodingTest extends XTest {
65     /** Creates new CoreSettingsTest */
66     public EncodingTest(String JavaDoc testName) {
67         super(testName);
68     }
69     
70     public void testEncoding() throws Exception JavaDoc {
71         final String JavaDoc DATA_OBJECT = "encoding.xml";
72         final String JavaDoc NDATA_OBJECT = "newEncoding.xml";
73         TreeEditorCookie cake;
74         
75         // prepare data
76
XMLDataObject original = (XMLDataObject) TestUtil.THIS.findData(DATA_OBJECT);
77         if (original == null) {
78             fail("\"" + DATA_OBJECT + "\" data object not found!");
79         }
80         cake = (TreeEditorCookie) original.getCookie(TreeEditorCookie.class);
81         TreeElement docRoot = ((TreeDocument)cake.openDocumentRoot()).getDocumentElement();
82         String JavaDoc defEncoding = cake.getDocumentRoot().getEncoding();
83         String JavaDoc gString = TestUtil.THIS.nodeToString(docRoot);
84         Iterator JavaDoc encodings = TreeUtilities.getSupportedEncodings().iterator();
85         
86         // prepare workdir
87
File JavaDoc workDir = getWorkDir();
88         try {
89             clearWorkDir();
90         } catch (IOException JavaDoc ex) {
91             log("clearWorkDir() throws: " + ex);
92         }
93         
94         FileSystem fs = TestUtil.THIS.mountDirectory(workDir);
95         DataFolder dataFolder = DataFolder.findFolder(fs.getRoot());
96         
97         while (encodings.hasNext()) {
98             String JavaDoc encoding = (String JavaDoc) encodings.next();
99             String JavaDoc fileName = encoding + ".xml";
100             try {
101                 dbg.println("Testing encoding: " + encoding + " ... ");
102                 if (encoding.equals(defEncoding)) break; // Nothing to test.
103

104                 // create new document, set encoding, save and close it
105
XMLDataObject xdao = (XMLDataObject) original.createFromTemplate(dataFolder, encoding);
106                 cake = (TreeEditorCookie) xdao.getCookie(TreeEditorCookie.class);
107                 TreeDocument newDoc = (TreeDocument) cake.openDocumentRoot();
108                 newDoc.setEncoding(encoding);
109                 TestUtil.THIS.saveDataObject(xdao);
110                 CloseCookie cc = (CloseCookie) xdao.getCookie(CloseCookie.class);
111                 cc.close();
112                 
113                 // read the document and check his content
114
cake = (TreeEditorCookie) xdao.getCookie(TreeEditorCookie.class);
115                 TreeElement newRoot = ((TreeDocument) cake.getDocumentRoot()).getDocumentElement();
116                 String JavaDoc nString = TestUtil.THIS.nodeToString(newRoot);
117                 assertEquals("Encoding: " + encoding + ", documents are differ", gString, nString);
118             } catch (Exception JavaDoc ex) {
119                 ex.printStackTrace(dbg);
120                 fail("Encoding: " + encoding + ", test faill due:\n" + ex);
121             }
122         }
123     }
124     
125     /**
126      * Performs this testsuite.
127      * @param args the command line arguments
128      */

129     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
130         DEBUG = true;
131         TestRunner.run(EncodingTest.class);
132     }
133 }
134
Popular Tags