KickJava   Java API By Example, From Geeks To Geeks.

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


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.
16  * Portions Copyrighted 2007 Sun Microsystems, Inc.
17  */

18 package org.openide.text;
19
20 import java.beans.PropertyChangeListener JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.EditorKit JavaDoc;
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31 import org.netbeans.junit.MockServices;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.junit.NbTestSuite;
34 import org.openide.util.Exceptions;
35 import org.openide.util.Lookup;
36 import org.openide.util.lookup.AbstractLookup;
37 import org.openide.util.lookup.InstanceContent;
38
39 /**
40  *
41  * @author Jaroslav Tulach
42  */

43 public class CloneableEditorSupportRedirectorTest extends NbTestCase
44 implements CloneableEditorSupport.Env {
45     /** the content of lookup of support */
46     private InstanceContent ic;
47     Redirector red;
48
49     
50     // Env variables
51
private String JavaDoc content = "";
52     private boolean valid = true;
53     private boolean modified = false;
54     /** if not null contains message why this document cannot be modified */
55     private String JavaDoc cannotBeModified;
56     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
57     private java.util.List JavaDoc<PropertyChangeListener JavaDoc> propL = new java.util.ArrayList JavaDoc<PropertyChangeListener JavaDoc>();
58     private java.beans.VetoableChangeListener JavaDoc vetoL;
59
60     
61     
62     public CloneableEditorSupportRedirectorTest(String JavaDoc testName) {
63         super(testName);
64     }
65     
66     public static Test suite() {
67         TestSuite suite = new NbTestSuite(CloneableEditorSupportRedirectorTest.class);
68         
69         return suite;
70     }
71     
72
73     protected void setUp () {
74         ic = new InstanceContent ();
75         CES support = new CES (this, new AbstractLookup(ic));
76         
77         MockServices.setServices(Redirector.class);
78         red = Lookup.getDefault().lookup(Redirector.class);
79         assertNotNull(red);
80
81         CloneableEditorSupportRedirectorTest t = new CloneableEditorSupportRedirectorTest("");
82         red.master = support;
83         InstanceContent slave = new InstanceContent();
84         red.slave = new CES(t, new AbstractLookup (slave));
85         slave.add(red.master);
86     }
87     
88     public void testSameDocument() throws Exception JavaDoc {
89         javax.swing.text.Document JavaDoc doc = red.slave.openDocument ();
90         assertNotNull (doc);
91         
92         assertSame(doc, red.master.getDocument());
93         
94         String JavaDoc s = doc.getText (0, doc.getLength ());
95         assertEquals ("Same text as in the stream", content, s);
96         
97         assertFalse ("No redo", red.slave.getUndoRedo ().canRedo ());
98         assertFalse ("No undo", red.slave.getUndoRedo ().canUndo ());
99     }
100     
101     public void testLineLookupIsPropagated () throws Exception JavaDoc {
102         content = "Line1\nLine2\n";
103         Integer JavaDoc template = new Integer JavaDoc (1);
104         ic.add (template); // put anything into the lookup
105

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

158         assertFalse ("The document cannot be modified", red.master.getUndoRedo ().canUndo ());
159         
160         String JavaDoc s = doc.getText (0, doc.getLength ());
161         assertEquals ("The document is now the same as at the begining", content, s);
162         
163         assertEquals ("Message has been shown to user in status bar", cannotBeModified, org.openide.awt.StatusDisplayer.getDefault ().getStatusText ());
164     }
165     
166     public void testDocumentCanBeGarbageCollectedWhenClosed () throws Exception JavaDoc {
167         content = "Ahoj\nMyDoc";
168         javax.swing.text.Document JavaDoc doc = red.master.openDocument ();
169         assertNotNull (doc);
170         
171         assertTrue ("Document is loaded", red.master.isDocumentLoaded ());
172         assertTrue ("Document is loaded", red.slave.isDocumentLoaded ());
173         assertTrue ("Can be closed without problems", red.slave.close ());
174         assertFalse ("Document is not loaded", red.master.isDocumentLoaded ());
175         assertFalse ("Document is not loaded", red.slave.isDocumentLoaded ());
176         
177         WeakReference JavaDoc<?> ref = new WeakReference JavaDoc<Document JavaDoc>(doc);
178         doc = null;
179         
180         assertGC ("Document can dissapear", ref);
181     }
182
183     /**
184      * Tests that the wrapEditorComponent() method returns the passed
185      * parameter (doesn't wrap the passed component in some additional UI).
186      */

187     public void testWrapEditorComponent() {
188         javax.swing.JPanel JavaDoc panel = new javax.swing.JPanel JavaDoc();
189         assertSame(red.master.wrapEditorComponent(panel), panel);
190         assertSame(red.slave.wrapEditorComponent(panel), panel);
191     }
192
193     public void testAfterOpenOfSlaveThereAreMasterPanes() throws Exception JavaDoc {
194         red.slave.open();
195         
196         class Check implements Runnable JavaDoc {
197             public void run() {
198                 assertTrue("Some panes are now open", red.master.getOpenedPanes() != null);
199             }
200         }
201         Check check = new Check();
202         
203         SwingUtilities.invokeAndWait(check);
204     }
205
206     public void testGetEditorKit() {
207         EditorKit JavaDoc kit = CloneableEditorSupport.getEditorKit("text/plain");
208         assertNotNull("EditorKit should never be null", kit);
209         // There shouldn't be any EK registered and we should get the default one
210
assertEquals("Wrong default EditorKit", "org.openide.text.CloneableEditorSupport$PlainEditorKit", kit.getClass().getName());
211     }
212     
213     private void compareStreamWithString(InputStream JavaDoc is, String JavaDoc s) throws Exception JavaDoc{
214         int i;
215         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
216         while ((i = is.read()) != -1) {
217             baos.write(i);
218         }
219         byte b1[] = baos.toByteArray();
220         byte b2[] = s.getBytes();
221         assertTrue("Same bytes as would result from the string: " + s, Arrays.equals(b1, b2));
222     }
223     
224     //
225
// Implementation of the CloneableEditorred.master.Env
226
//
227

228     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
229         propL.add (l);
230     }
231     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
232         propL.remove (l);
233     }
234     
235     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
236         assertNull ("This is the first veto listener", vetoL);
237         vetoL = l;
238     }
239     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
240         assertEquals ("Removing the right veto one", vetoL, l);
241         vetoL = null;
242     }
243     
244     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
245         return red.master;
246     }
247     
248     public String JavaDoc getMimeType() {
249         return "text/plain";
250     }
251     
252     public java.util.Date JavaDoc getTime() {
253         return date;
254     }
255     
256     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
257         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
258     }
259     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
260         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
261             public void close () throws java.io.IOException JavaDoc {
262                 super.close ();
263                 content = new String JavaDoc (toByteArray ());
264             }
265         }
266         
267         return new ContentStream ();
268     }
269     
270     public boolean isValid() {
271         return valid;
272     }
273     
274     public boolean isModified() {
275         return modified;
276     }
277
278     public void markModified() throws java.io.IOException JavaDoc {
279         if (cannotBeModified != null) {
280             final String JavaDoc notify = cannotBeModified;
281             IOException JavaDoc e = new IOException JavaDoc () {
282                 public String JavaDoc getLocalizedMessage () {
283                     return notify;
284                 }
285             };
286             Exceptions.attachLocalizedMessage(e, cannotBeModified);
287             throw e;
288         }
289         
290         modified = true;
291     }
292     
293     public void unmarkModified() {
294         modified = false;
295     }
296
297     /** Implementation of the CES */
298     private static final class CES extends CloneableEditorSupport {
299         public CES (Env env, Lookup l) {
300             super (env, l);
301         }
302         
303         protected String JavaDoc messageName() {
304             return "Name";
305         }
306         
307         protected String JavaDoc messageOpened() {
308             return "Opened";
309         }
310         
311         protected String JavaDoc messageOpening() {
312             return "Opening";
313         }
314         
315         protected String JavaDoc messageSave() {
316             return "Save";
317         }
318         
319         protected String JavaDoc messageToolTip() {
320             return "ToolTip";
321         }
322         
323     }
324
325     
326     public static final class Redirector extends CloneableEditorSupportRedirector {
327         CES master;
328         CES slave;
329     
330         protected CloneableEditorSupport redirect(Lookup ces) {
331             return ces.lookup(CloneableEditorSupport.class);
332         }
333 }
334 }
335
Popular Tags