1 19 20 package org.netbeans.modules.html; 21 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStream ; 26 import java.io.Reader ; 27 import javax.swing.text.Document ; 28 import junit.textui.TestRunner; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.cookies.EditorCookie; 31 import org.openide.cookies.OpenCookie; 32 import org.openide.cookies.SaveCookie; 33 import org.openide.filesystems.FileLock; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.filesystems.LocalFileSystem; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataObject; 39 40 public class EncodingTest extends NbTestCase { 41 42 private LocalFileSystem fs; 43 44 public EncodingTest(String name) { 45 super(name); 46 } 47 48 49 protected void setUp() throws Exception { 50 Utils.setUp(); 51 52 File f = File.createTempFile (this.getName (), ""); 53 f.delete (); 54 f.mkdirs (); 55 56 fs = new LocalFileSystem (); 57 fs.setRootDirectory(f); 58 59 FileUtil.setMIMEType("html", "text/html"); 61 62 Repository.getDefault ().addFileSystem(fs); 63 } 64 65 protected void tearDown() throws Exception { 66 Repository.getDefault ().removeFileSystem(fs); 67 68 fs.getRootDirectory().deleteOnExit (); 69 } 70 71 73 public void testLoadEmptyFile () throws Exception { 74 checkEncoding (null, "empty.html", true); 75 } 76 77 79 public void testLoadOfNoEncoding () throws Exception { 80 checkEncoding (null, "sample.html", true); 81 } 82 83 85 public void testLoadOfWrongEncoding () throws Exception { 86 checkEncoding (null, "wrongencoding.html", false); 87 } 88 89 91 public void testEncodingUTF8 () throws Exception { 92 checkEncoding ("UTF-8", "UTF8.html", true); 93 } 94 96 public void testEncodingApostrof () throws Exception { 97 checkEncoding ("UTF-8", "apostrof.html", true); 98 } 99 100 103 public void testEncodingApostrofWithQuote () throws Exception { 104 checkEncoding ("UTF-8", "apostrofwithoutquote.html", true); 105 } 106 107 111 private void checkEncoding (String enc, String res, boolean withCmp) throws Exception { 112 InputStream is = getClass ().getResourceAsStream ("data/"+res); 113 assertNotNull (res+" should exist", is); 114 115 FileObject data = FileUtil.createData (fs.getRoot (), res); 116 FileLock lock = data.lock(); 117 OutputStream os = data.getOutputStream (lock); 118 FileUtil.copy (is, os); 119 is.close (); 120 os.close (); 121 lock.releaseLock (); 122 123 DataObject obj = DataObject.find (data); 124 125 assertEquals ("Must be HtmlDataObject", HtmlDataObject.class, obj.getClass ()); 126 127 OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class); 128 assertNotNull("There is an open cookie", open); 129 130 open.open (); 131 132 EditorCookie ec = (EditorCookie)obj.getCookie (EditorCookie.class); 133 assertNotNull ("There is an editor cookie", ec); 134 135 Document doc = ec.openDocument(); 136 assertNotNull ("Need a document", doc); 137 138 139 Reader r; 140 if (enc == null) { 141 r = new InputStreamReader (getClass ().getResourceAsStream ("data/"+res)); 142 } else { 143 r = new InputStreamReader (getClass ().getResourceAsStream ("data/"+res), enc); 144 } 145 146 if (!withCmp) 147 return; 148 149 compareDoc (r, doc); 150 r.close (); 151 152 doc.insertString (0, "X", null); 153 doc.remove (0, 1); 154 155 SaveCookie sc = (SaveCookie)obj.getCookie(SaveCookie.class); 156 assertNotNull ("Document is modified", sc); 157 sc.save (); 158 159 InputStream i1 = getClass ().getResourceAsStream ("data/"+res); 160 InputStream i2 = obj.getPrimaryFile().getInputStream(); 161 compareStream (i1, i2); 162 i2.close (); 163 i1.close (); 164 165 } 166 167 169 private static void compareDoc (Reader r, Document doc) throws Exception { 170 for (int i = 0; i < doc.getLength(); i++) { 171 String ch = doc.getText (i, 1); 172 assertEquals ("Really one char", 1, ch.length()); 173 174 char fromStream = (char)r.read (); 175 if (fromStream != ch.charAt (0) && fromStream == (char)13 && ch.charAt (0) == (char)10) { 176 fromStream = (char)r.read (); 178 } 179 180 181 assertEquals ("Stream and doc should be the same on index " + i, (int)fromStream, (int)ch.charAt (0)); 182 } 183 } 184 185 187 static void compareStream (InputStream i1, InputStream i2) throws Exception { 188 for (int i = 0; true; i++) { 189 int c1 = i1.read (); 190 int c2 = i2.read (); 191 192 assertEquals (i + "th bytes are different", c1, c2); 193 194 if (c1 == -1) return; 195 } 196 } 197 198 } 199 | Popular Tags |