KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > html > 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
20 package org.netbeans.modules.html;
21
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.Reader JavaDoc;
27 import javax.swing.text.Document JavaDoc;
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     /** the fs to work on */
42     private LocalFileSystem fs;
43     
44     public EncodingTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     /**/
49     protected void setUp() throws Exception JavaDoc {
50         Utils.setUp();
51
52         File JavaDoc f = File.createTempFile (this.getName (), "");
53         f.delete ();
54         f.mkdirs ();
55         
56         fs = new LocalFileSystem ();
57         fs.setRootDirectory(f);
58         
59         // to help the loader to recognize our files
60
FileUtil.setMIMEType("html", "text/html");
61         
62         Repository.getDefault ().addFileSystem(fs);
63     }
64     
65     protected void tearDown() throws Exception JavaDoc {
66         Repository.getDefault ().removeFileSystem(fs);
67         
68         fs.getRootDirectory().deleteOnExit ();
69     }
70     
71     /** Loads an empty file.
72      */

73     public void testLoadEmptyFile () throws Exception JavaDoc {
74         checkEncoding (null, "empty.html", true);
75     }
76     
77     /** Loades a file that does not specify an encoding.
78      */

79     public void testLoadOfNoEncoding () throws Exception JavaDoc {
80         checkEncoding (null, "sample.html", true);
81     }
82     
83     /** Loades a file that does not specify an encoding.
84      */

85     public void testLoadOfWrongEncoding () throws Exception JavaDoc {
86         checkEncoding (null, "wrongencoding.html", false);
87     }
88     
89     /** Test load of UTF-8 encoding.
90      */

91     public void testEncodingUTF8 () throws Exception JavaDoc {
92         checkEncoding ("UTF-8", "UTF8.html", true);
93     }
94     /** Test load of UTF-8 encoding specified in ' ' instead of " "
95      */

96     public void testEncodingApostrof () throws Exception JavaDoc {
97         checkEncoding ("UTF-8", "apostrof.html", true);
98     }
99     
100     /** Test load of UTF-8 encoding specified in ' ' instead of " "
101      * with a text that is followed with "
102      */

103     public void testEncodingApostrofWithQuote () throws Exception JavaDoc {
104         checkEncoding ("UTF-8", "apostrofwithoutquote.html", true);
105     }
106     
107     /** @param enc expected encoding
108      * @param res resource path
109      * @param withCmp should also document content be compared?
110      */

111     private void checkEncoding (String JavaDoc enc, String JavaDoc res, boolean withCmp) throws Exception JavaDoc {
112         InputStream JavaDoc 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 JavaDoc 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 JavaDoc doc = ec.openDocument();
136         assertNotNull ("Need a document", doc);
137         
138         
139         Reader JavaDoc r;
140         if (enc == null) {
141             r = new InputStreamReader JavaDoc (getClass ().getResourceAsStream ("data/"+res));
142         } else {
143             r = new InputStreamReader JavaDoc (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 JavaDoc i1 = getClass ().getResourceAsStream ("data/"+res);
160         InputStream JavaDoc i2 = obj.getPrimaryFile().getInputStream();
161         compareStream (i1, i2);
162         i2.close ();
163         i1.close ();
164         
165     }
166     
167     /** Compares content of document and reader
168      */

169     private static void compareDoc (Reader JavaDoc r, Document JavaDoc doc) throws Exception JavaDoc {
170         for (int i = 0; i < doc.getLength(); i++) {
171             String JavaDoc 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                 // new line in document is always represented by 13, read next character
177
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     /** Compares content of two streams.
186      */

187     /*package*/ static void compareStream (InputStream JavaDoc i1, InputStream JavaDoc i2) throws Exception JavaDoc {
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