KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import junit.framework.*;
26 import org.netbeans.junit.*;
27 import org.openide.NotifyDescriptor;
28 import org.openide.util.*;
29 import org.openide.util.lookup.*;
30
31
32 /** Testing usage of UserQuestionException in CES.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class CloneableEditorUserQuestionTest extends NbTestCase
37 implements CloneableEditorSupport.Env {
38     /** the support to work with */
39     private CloneableEditorSupport support;
40     /** the content of lookup of support */
41     private InstanceContent ic;
42
43     
44     // Env variables
45
private String JavaDoc content = "";
46     private boolean valid = true;
47     private boolean modified = false;
48     /** if not null contains message why this document cannot be modified */
49     private String JavaDoc cannotBeModified;
50     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
51     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
52     private java.beans.VetoableChangeListener JavaDoc vetoL;
53     private IOException JavaDoc toThrow;
54
55     
56     public CloneableEditorUserQuestionTest (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(CloneableEditorUserQuestionTest.class);
66         
67         return suite;
68     }
69     
70
71     protected void setUp () {
72         System.setProperty ("org.openide.util.Lookup", "org.openide.text.CloneableEditorUserQuestionTest$Lkp");
73         
74         ic = new InstanceContent ();
75         support = new CES (this, new AbstractLookup (ic));
76     }
77
78     
79     public void testExceptionThrownWhenDocumentIsBeingReadInAWT () throws Exception JavaDoc {
80         class Run implements Runnable JavaDoc {
81             public Exception JavaDoc ex;
82             public Error JavaDoc err;
83             public void run () {
84                 
85                 try {
86                     doExceptionThrownWhenDocumentIsBeingRead ();
87                 } catch (Exception JavaDoc ex) {
88                     this.ex = ex;
89                 } catch (Error JavaDoc err) {
90                     this.err = err;
91                 }
92             }
93         }
94         Run r = new Run ();
95         SwingUtilities.invokeAndWait (r);
96         if (r.ex != null) throw r.ex;
97         if (r.err != null) throw r.err;
98     }
99
100     public void testExceptionThrownWhenDocumentIsBeingRead () throws Exception JavaDoc {
101         assertFalse (SwingUtilities.isEventDispatchThread ());
102         doExceptionThrownWhenDocumentIsBeingRead ();
103     }
104     
105     
106     public void testOpenDocumentIsLoadedUsingIOException() throws Exception JavaDoc{
107         doOpenDocumentIsLoaded (new IOException JavaDoc ("Plain I/O exc"));
108     }
109     
110     public void testOpenDocumentIsLoadedUsingUserQuestionException() throws Exception JavaDoc{
111         class MyEx extends UserQuestionException {
112             private int confirmed;
113             
114             public String JavaDoc getLocalizedMessage () {
115                 return "locmsg";
116             }
117             
118             public String JavaDoc getMessage () {
119                 return "msg";
120             }
121             
122             public void confirmed () {
123                 confirmed++;
124                 toThrow = null;
125             }
126         }
127         doOpenDocumentIsLoaded (new MyEx ());
128     }
129     
130     private void doOpenDocumentIsLoaded (IOException JavaDoc my) throws Exception JavaDoc {
131         toThrow = my;
132         try{
133             support.openDocument();
134             fail ("Document should not be loaded, we throw an exception");
135         }
136         catch (IOException JavaDoc e){
137             assertSame ("The expected exception", my, e);
138         }
139         
140         assertNull ("No document", support.getDocument());
141         assertFalse ("Not loaded", support.isDocumentLoaded());
142
143         toThrow = null;
144         support.openDocument ();
145         
146         assertNotNull ("We can later open the document", support.getDocument ());
147         assertTrue ("And it is correctly marked as loaded", support.isDocumentLoaded ());
148     }
149     
150     private void doExceptionThrownWhenDocumentIsBeingRead () throws Exception JavaDoc {
151         class MyEx extends UserQuestionException {
152             private int confirmed;
153             
154             public String JavaDoc getLocalizedMessage () {
155                 return "locmsg";
156             }
157             
158             public String JavaDoc getMessage () {
159                 return "msg";
160             }
161             
162             public void confirmed () {
163                 confirmed++;
164                 toThrow = null;
165             }
166         }
167         
168         MyEx my = new MyEx ();
169         toThrow = my;
170
171         DD.toReturn = org.openide.NotifyDescriptor.NO_OPTION;
172         support.open ();
173         
174         if (!SwingUtilities.isEventDispatchThread ()) {
175             javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () {} });
176         }
177         
178         assertNotNull ("Some otions", DD.options);
179         assertEquals ("Two options", 2, DD.options.length);
180         assertEquals ("Yes", NotifyDescriptor.YES_OPTION, DD.options[0]);
181         assertEquals ("No", NotifyDescriptor.NO_OPTION, DD.options[1]);
182         assertEquals ("confirmed not called", 0, my.confirmed);
183         
184         assertNull ("Still no document", support.getDocument ());
185         
186         DD.options = null;
187         DD.toReturn = NotifyDescriptor.YES_OPTION;
188         support.open ();
189
190         if (!SwingUtilities.isEventDispatchThread ()) {
191             javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () {} });
192         }
193         
194         assertEquals ("confirmed called", 1, my.confirmed);
195         assertNotNull ("Some otions", DD.options);
196         assertEquals ("Two options", 2, DD.options.length);
197         assertEquals ("Yes", NotifyDescriptor.YES_OPTION, DD.options[0]);
198         assertEquals ("No", NotifyDescriptor.NO_OPTION, DD.options[1]);
199         DD.options = null;
200         
201         assertNotNull ("Document opened", support.getDocument ());
202         
203     }
204         
205     //
206
// Implementation of the CloneableEditorSupport.Env
207
//
208

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

309     //
310
// Our fake lookup
311
//
312
public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
313         public Lkp () {
314             this (new org.openide.util.lookup.InstanceContent ());
315         }
316         
317         private Lkp (org.openide.util.lookup.InstanceContent ic) {
318             super (ic);
319             ic.add (new DD ());
320         }
321     }
322
323     /** Our own dialog displayer.
324      */

325     private static final class DD extends org.openide.DialogDisplayer {
326         public static Object JavaDoc[] options;
327         public static Object JavaDoc toReturn;
328         
329         public java.awt.Dialog JavaDoc createDialog(org.openide.DialogDescriptor descriptor) {
330             throw new IllegalStateException JavaDoc ("Not implemented");
331         }
332         
333         public Object JavaDoc notify(org.openide.NotifyDescriptor descriptor) {
334             assertNull (options);
335             assertNotNull (toReturn);
336             options = descriptor.getOptions();
337             Object JavaDoc r = toReturn;
338             toReturn = null;
339             return r;
340         }
341         
342     } // end of DD
343

344 }
345
Popular Tags