1 19 package org.netbeans.tax; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Iterator ; 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 63 64 public class EncodingTest extends XTest { 65 66 public EncodingTest(String testName) { 67 super(testName); 68 } 69 70 public void testEncoding() throws Exception { 71 final String DATA_OBJECT = "encoding.xml"; 72 final String NDATA_OBJECT = "newEncoding.xml"; 73 TreeEditorCookie cake; 74 75 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 defEncoding = cake.getDocumentRoot().getEncoding(); 83 String gString = TestUtil.THIS.nodeToString(docRoot); 84 Iterator encodings = TreeUtilities.getSupportedEncodings().iterator(); 85 86 File workDir = getWorkDir(); 88 try { 89 clearWorkDir(); 90 } catch (IOException 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 encoding = (String ) encodings.next(); 99 String fileName = encoding + ".xml"; 100 try { 101 dbg.println("Testing encoding: " + encoding + " ... "); 102 if (encoding.equals(defEncoding)) break; 104 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 cake = (TreeEditorCookie) xdao.getCookie(TreeEditorCookie.class); 115 TreeElement newRoot = ((TreeDocument) cake.getDocumentRoot()).getDocumentElement(); 116 String nString = TestUtil.THIS.nodeToString(newRoot); 117 assertEquals("Encoding: " + encoding + ", documents are differ", gString, nString); 118 } catch (Exception ex) { 119 ex.printStackTrace(dbg); 120 fail("Encoding: " + encoding + ", test faill due:\n" + ex); 121 } 122 } 123 } 124 125 129 public static void main(String args[]) throws Exception { 130 DEBUG = true; 131 TestRunner.run(EncodingTest.class); 132 } 133 } 134 | Popular Tags |