KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.util.Date JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JEditorPane JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import junit.framework.*;
28
29 import org.netbeans.junit.*;
30
31 import org.openide.util.Mutex;
32 import org.openide.util.Lookup;
33 import org.openide.util.lookup.*;
34
35 /** Testing usage of UserQuestionException in CES.
36  *
37  * @author Jaroslav Tulach
38  */

39 public class NetworkConnectionLostTest extends NbTestCase
40 implements CloneableEditorSupport.Env {
41     /** the support to work with */
42     private CloneableEditorSupport support;
43     /** the content of lookup of support */
44     private InstanceContent ic;
45
46     
47     // Env variables
48
private String JavaDoc content = "";
49     private boolean valid = true;
50     private boolean modified = false;
51     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
52     private java.beans.PropertyChangeSupport JavaDoc propL = new java.beans.PropertyChangeSupport JavaDoc(this);
53     private java.beans.VetoableChangeListener JavaDoc vetoL;
54     private IOException JavaDoc toThrow;
55
56     
57     public NetworkConnectionLostTest (java.lang.String JavaDoc testName) {
58         super(testName);
59     }
60     
61     public static void main(java.lang.String JavaDoc[] args) {
62         junit.textui.TestRunner.run(suite());
63     }
64     
65     public static Test suite() {
66         TestSuite suite = new NbTestSuite(NetworkConnectionLostTest.class);
67         
68         return suite;
69     }
70     
71
72     protected void setUp () {
73         System.setProperty ("org.openide.util.Lookup", "org.openide.text.NetworkConnectionLostTest$Lkp");
74         
75         ic = new InstanceContent ();
76         support = new CES (this, new AbstractLookup (ic));
77     }
78
79     public void testModifyTheFileAndThenPreventItToBeSavedOnClose() throws Exception JavaDoc {
80         Document JavaDoc doc = support.openDocument();
81         
82         doc.insertString(0, "Ahoj", null);
83         assertTrue("Modified", support.isModified());
84         
85         support.open();
86         waitEQ();
87
88         JEditorPane JavaDoc[] arr = getPanes();
89         assertNotNull("There is one opened pane", arr);
90         
91         java.awt.Component JavaDoc c = arr[0];
92         while (!(c instanceof CloneableEditor)) {
93             c = c.getParent();
94         }
95         CloneableEditor ce = (CloneableEditor)c;
96
97         toThrow = new IOException JavaDoc("NetworkConnectionLost");
98
99         // say save at the end
100
DD.toReturn = 0;
101         boolean result = ce.close();
102         assertFalse("Refused to save due to the exception", result);
103         waitEQ();
104         
105         assertNotNull("There was a question", DD.options);
106         
107         String JavaDoc txt = doc.getText(0, doc.getLength());
108         assertEquals("The right text is there", txt, "Ahoj");
109         assertEquals("Nothing has been saved", "", content);
110         
111         arr = getPanes();
112         assertNotNull("Panes are still open", arr);
113     }
114
115     private JEditorPane JavaDoc[] getPanes() {
116         return Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane JavaDoc[]>() {
117             public JEditorPane JavaDoc[] run() {
118                 return support.getOpenedPanes();
119             }
120         });
121     }
122     
123
124     private void waitEQ() throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
125         javax.swing.SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
126             public void run () {
127             }
128         });
129     }
130     //
131
// Implementation of the CloneableEditorSupport.Env
132
//
133

134     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
135         propL.addPropertyChangeListener (l);
136     }
137     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
138         propL.removePropertyChangeListener (l);
139     }
140     
141     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
142         assertNull ("This is the first veto listener", vetoL);
143         vetoL = l;
144     }
145     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
146         assertEquals ("Removing the right veto one", vetoL, l);
147         vetoL = null;
148     }
149     
150     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
151         return support;
152     }
153     
154     public String JavaDoc getMimeType() {
155         return "text/plain";
156     }
157     
158     public java.util.Date JavaDoc getTime() {
159         return date;
160     }
161     
162     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
163         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
164     }
165     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
166         if (toThrow != null) {
167             throw toThrow;
168         }
169         
170         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
171             public void close () throws java.io.IOException JavaDoc {
172                 super.close ();
173                 content = new String JavaDoc (toByteArray ());
174             }
175         }
176         
177         return new ContentStream ();
178     }
179     
180     public boolean isValid() {
181         return valid;
182     }
183     
184     public boolean isModified() {
185         return modified;
186     }
187
188     public void markModified() throws java.io.IOException JavaDoc {
189         modified = true;
190     }
191     
192     public void unmarkModified() {
193         modified = false;
194     }
195
196     /** Implementation of the CES */
197     private static final class CES extends CloneableEditorSupport {
198         public CES (Env env, Lookup l) {
199             super (env, l);
200         }
201         
202         protected String JavaDoc messageName() {
203             return "Name";
204         }
205         
206         protected String JavaDoc messageOpened() {
207             return "Opened";
208         }
209         
210         protected String JavaDoc messageOpening() {
211             return "Opening";
212         }
213         
214         protected String JavaDoc messageSave() {
215             return "Save";
216         }
217         
218         protected String JavaDoc messageToolTip() {
219             return "ToolTip";
220         }
221         
222     } // end of CES
223

224     //
225
// Our fake lookup
226
//
227
public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
228         public Lkp () {
229             this (new org.openide.util.lookup.InstanceContent ());
230         }
231         
232         private Lkp (org.openide.util.lookup.InstanceContent ic) {
233             super (ic);
234             ic.add (new DD ());
235         }
236     }
237
238     /** Our own dialog displayer.
239      */

240     private static final class DD extends org.openide.DialogDisplayer {
241         public static Object JavaDoc[] options;
242         public static int toReturn = -1;
243         
244         public java.awt.Dialog JavaDoc createDialog(org.openide.DialogDescriptor descriptor) {
245             throw new IllegalStateException JavaDoc ("Not implemented");
246         }
247         
248         public Object JavaDoc notify(org.openide.NotifyDescriptor descriptor) {
249             assertNull (options);
250             if (toReturn == -1) {
251                 fail("Not specified what we shall return: " + toReturn);
252             }
253             options = descriptor.getOptions();
254             Object JavaDoc r = options[toReturn];
255             toReturn = -1;
256             return r;
257         }
258         
259     } // end of DD
260

261 }
262
Popular Tags