KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > CloneableEditorSupportTest


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.openide.text;
21
22
23 import java.io.*;
24 import java.util.Arrays JavaDoc;
25 import javax.swing.text.EditorKit JavaDoc;
26 import junit.framework.*;
27 import org.netbeans.junit.*;
28 import org.openide.util.Exceptions;
29 import org.openide.util.Lookup;
30 import org.openide.util.lookup.*;
31
32
33 /** Testing different features of CloneableEditorSupport
34  *
35  * @author Jaroslav Tulach
36  */

37 public class CloneableEditorSupportTest extends NbTestCase
38 implements CloneableEditorSupport.Env {
39     /** the support to work with */
40     private CloneableEditorSupport support;
41     /** the content of lookup of support */
42     private InstanceContent ic;
43
44     
45     // Env variables
46
private String JavaDoc content = "";
47     private boolean valid = true;
48     private boolean modified = false;
49     /** if not null contains message why this document cannot be modified */
50     private String JavaDoc cannotBeModified;
51     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
52     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
53     private java.beans.VetoableChangeListener JavaDoc vetoL;
54
55     
56     public CloneableEditorSupportTest(java.lang.String JavaDoc testName) {
57         super(testName);
58     }
59     
60     public static void main(java.lang.String JavaDoc[] args) {
61         junit.textui.TestRunner.run(suite());
62     }
63     
64     public static Test suite() {
65         TestSuite suite = new NbTestSuite(CloneableEditorSupportTest.class);
66         
67         return suite;
68     }
69     
70
71     protected void setUp () {
72         ic = new InstanceContent ();
73         support = new CES (this, new AbstractLookup (ic));
74     }
75     
76     public void testDocumentCanBeRead () throws Exception JavaDoc {
77         content = "Ahoj\nMyDoc";
78         javax.swing.text.Document JavaDoc doc = support.openDocument ();
79         assertNotNull (doc);
80         
81         String JavaDoc s = doc.getText (0, doc.getLength ());
82         assertEquals ("Same text as in the stream", content, s);
83         
84         assertFalse ("No redo", support.getUndoRedo ().canRedo ());
85         assertFalse ("No undo", support.getUndoRedo ().canUndo ());
86     }
87     
88     public void testLineLookupIsPropagated () throws Exception JavaDoc {
89         content = "Line1\nLine2\n";
90         Integer JavaDoc template = new Integer JavaDoc (1);
91         ic.add (template); // put anything into the lookup
92

93         // in order to set.getLines() work correctly, the document has to be loaded
94
support.openDocument();
95         
96         Line.Set set = support.getLineSet();
97         java.util.List JavaDoc list = set.getLines();
98         assertEquals ("Three lines", 3, list.size ());
99         
100         Line l = (Line)list.get (0);
101         Integer JavaDoc i = (Integer JavaDoc)l.getLookup ().lookup (Integer JavaDoc.class);
102         assertEquals ("The original integer", template, i);
103         ic.remove (template);
104         i = (Integer JavaDoc)l.getLookup ().lookup (Integer JavaDoc.class);
105         assertNull ("Lookup is dynamic, so now there is nothing", i);
106     }
107     
108     
109     public void testGetInputStream () throws Exception JavaDoc {
110         content = "goes\nto\nInputStream";
111         String JavaDoc added = "added before\n";
112         javax.swing.text.Document JavaDoc doc = support.openDocument ();
113         assertNotNull (doc);
114         
115         // modify the document
116
doc.insertString(0, added, null);
117         compareStreamWithString(support.getInputStream(), added + content);
118     }
119     
120     public void testGetInputStreamWhenClosed () throws Exception JavaDoc {
121         content = "basic\ncontent";
122         compareStreamWithString(support.getInputStream(), content);
123         // we should be doing this with the document still closed
124
assertNull("The document is supposed to be still closed", support.getDocument ());
125     }
126     
127     public void testDocumentCannotBeModified () throws Exception JavaDoc {
128         content = "Ahoj\nMyDoc";
129         cannotBeModified = "No, you cannot modify this document in this test";
130         
131         javax.swing.text.Document JavaDoc doc = support.openDocument ();
132         assertNotNull (doc);
133         
134         assertFalse ("Nothing to undo", support.getUndoRedo ().canUndo ());
135         
136         // this should not be allowed
137
doc.insertString (0, "Kuk", null);
138         
139         String JavaDoc modifiedForAWhile = doc.getText (0, 3);
140         //assertEquals ("For a while the test really starts with Kuk", "Kuk", doc.getText (0, 3));
141

142         assertFalse ("The document cannot be modified", support.getUndoRedo ().canUndo ());
143         
144         String JavaDoc s = doc.getText (0, doc.getLength ());
145         assertEquals ("The document is now the same as at the begining", content, s);
146         
147         assertEquals ("Message has been shown to user in status bar", cannotBeModified, org.openide.awt.StatusDisplayer.getDefault ().getStatusText ());
148     }
149     
150     public void testDocumentCanBeGarbageCollectedWhenClosed () throws Exception JavaDoc {
151         content = "Ahoj\nMyDoc";
152         javax.swing.text.Document JavaDoc doc = support.openDocument ();
153         assertNotNull (doc);
154         
155         assertTrue ("Document is loaded", support.isDocumentLoaded ());
156         assertTrue ("Can be closed without problems", support.close ());
157         assertFalse ("Document is not loaded", support.isDocumentLoaded ());
158         
159         java.lang.ref.WeakReference JavaDoc ref = new java.lang.ref.WeakReference JavaDoc (doc);
160         doc = null;
161         
162         assertGC ("Document can dissapear", ref);
163     }
164
165     /**
166      * Tests that the wrapEditorComponent() method returns the passed
167      * parameter (doesn't wrap the passed component in some additional UI).
168      */

169     public void testWrapEditorComponent() {
170         javax.swing.JPanel JavaDoc panel = new javax.swing.JPanel JavaDoc();
171         assertSame(support.wrapEditorComponent(panel), panel);
172     }
173
174     public void testSaveWhenNoDocumentOpen() throws IOException {
175         modified = true;
176         support.saveDocument();
177     }
178
179     public void testGetEditorKit() {
180         EditorKit JavaDoc kit = CloneableEditorSupport.getEditorKit("text/plain");
181         assertNotNull("EditorKit should never be null", kit);
182         // There shouldn't be any EK registered and we should get the default one
183
assertEquals("Wrong default EditorKit", "org.openide.text.CloneableEditorSupport$PlainEditorKit", kit.getClass().getName());
184     }
185     
186     private void compareStreamWithString(InputStream is, String JavaDoc s) throws Exception JavaDoc{
187         int i;
188         ByteArrayOutputStream baos = new ByteArrayOutputStream();
189         while ((i = is.read()) != -1) {
190             baos.write(i);
191         }
192         byte b1[] = baos.toByteArray();
193         byte b2[] = s.getBytes();
194         assertTrue("Same bytes as would result from the string: " + s, Arrays.equals(b1, b2));
195     }
196     
197     //
198
// Implementation of the CloneableEditorSupport.Env
199
//
200

201     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
202         propL.add (l);
203     }
204     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
205         propL.remove (l);
206     }
207     
208     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
209         assertNull ("This is the first veto listener", vetoL);
210         vetoL = l;
211     }
212     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
213         assertEquals ("Removing the right veto one", vetoL, l);
214         vetoL = null;
215     }
216     
217     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
218         return support;
219     }
220     
221     public String JavaDoc getMimeType() {
222         return "text/plain";
223     }
224     
225     public java.util.Date JavaDoc getTime() {
226         return date;
227     }
228     
229     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
230         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
231     }
232     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
233         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
234             public void close () throws java.io.IOException JavaDoc {
235                 super.close ();
236                 content = new String JavaDoc (toByteArray ());
237             }
238         }
239         
240         return new ContentStream ();
241     }
242     
243     public boolean isValid() {
244         return valid;
245     }
246     
247     public boolean isModified() {
248         return modified;
249     }
250
251     public void markModified() throws java.io.IOException JavaDoc {
252         if (cannotBeModified != null) {
253             final String JavaDoc notify = cannotBeModified;
254             IOException e = new IOException () {
255                 public String JavaDoc getLocalizedMessage () {
256                     return notify;
257                 }
258             };
259             Exceptions.attachLocalizedMessage(e, cannotBeModified);
260             throw e;
261         }
262         
263         modified = true;
264     }
265     
266     public void unmarkModified() {
267         modified = false;
268     }
269
270     /** Implementation of the CES */
271     private static final class CES extends CloneableEditorSupport {
272         public CES (Env env, Lookup l) {
273             super (env, l);
274         }
275         
276         protected String JavaDoc messageName() {
277             return "Name";
278         }
279         
280         protected String JavaDoc messageOpened() {
281             return "Opened";
282         }
283         
284         protected String JavaDoc messageOpening() {
285             return "Opening";
286         }
287         
288         protected String JavaDoc messageSave() {
289             return "Save";
290         }
291         
292         protected String JavaDoc messageToolTip() {
293             return "ToolTip";
294         }
295         
296     }
297 }
298
Popular Tags