KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
22 import javax.swing.JEditorPane JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import junit.framework.*;
25
26 import org.netbeans.junit.*;
27 import org.openide.DialogDescriptor;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.LocalFileSystem;
32 import org.openide.loaders.DataObject;
33 import org.openide.util.lookup.*;
34 import org.openide.util.Mutex;
35
36 /** Modified editor shall not be closed when its file is externally removed.
37  *
38  * @author Jaroslav Tulach
39  */

40 public class ExternalDeleteOfModifiedFileTest extends NbTestCase {
41     private DataObject obj;
42     private EditorCookie edit;
43     
44     
45     public ExternalDeleteOfModifiedFileTest (java.lang.String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public static Test suite() {
50         TestSuite suite = new NbTestSuite(ExternalDeleteOfModifiedFileTest.class);
51         return suite;
52     }
53     
54
55     protected void setUp () throws Exception JavaDoc {
56         System.setProperty ("org.openide.util.Lookup", "org.openide.text.ExternalDeleteOfModifiedFileTest$Lkp");
57         
58         
59         clearWorkDir();
60         LocalFileSystem fs = new LocalFileSystem();
61         fs.setRootDirectory(getWorkDir());
62         
63         FileObject fo = fs.getRoot().createData("Ahoj", "txt");
64         
65         obj = DataObject.find(fo);
66         edit = (EditorCookie)obj.getCookie(EditorCookie.class);
67         assertNotNull("we have editor", edit);
68     }
69
70     public void testModifyTheFileAndThenPreventItToBeSavedOnFileDisappear() throws Exception JavaDoc {
71         Document JavaDoc doc = edit.openDocument();
72         
73         doc.insertString(0, "Ahoj", null);
74         assertTrue("Modified", edit.isModified());
75         
76         edit.open();
77         waitEQ();
78
79         JEditorPane JavaDoc[] arr = getPanes();
80         assertNotNull("There is one opened pane", arr);
81         
82         java.awt.Component JavaDoc c = arr[0];
83         while (!(c instanceof CloneableEditor)) {
84             c = c.getParent();
85         }
86         CloneableEditor ce = (CloneableEditor)c;
87
88         // select close
89
DD.toReturn = DialogDescriptor.CANCEL_OPTION;
90         
91         java.io.File JavaDoc f = FileUtil.toFile(obj.getPrimaryFile());
92         assertNotNull("There is file behind the fo", f);
93         f.delete();
94         obj.getPrimaryFile().getParent().refresh();
95
96         waitEQ();
97         
98         assertNotNull ("Text message was there", DD.message);
99         assertEquals("Ok/cancel type", DialogDescriptor.OK_CANCEL_OPTION, DD.type);
100         
101         String JavaDoc txt = doc.getText(0, doc.getLength());
102         assertEquals("The right text is there", txt, "Ahoj");
103         
104         arr = getPanes();
105         assertNotNull("Panes are still open", arr);
106         assertTrue("Document is remains modified", edit.isModified());
107         
108         // explicit close request, shall show the get another dialog
109
// and now say yes to close
110
DD.clear(DialogDescriptor.OK_OPTION);
111         
112         ce.close();
113         waitEQ();
114         
115         arr = getPanes();
116         assertNull("Now everything is closed", arr);
117     }
118
119     private JEditorPane JavaDoc[] getPanes() {
120         return Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane JavaDoc[]>() {
121             public JEditorPane JavaDoc[] run() {
122                 return edit.getOpenedPanes();
123             }
124         });
125     }
126     
127     private void waitEQ() throws InterruptedException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
128         javax.swing.SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
129             public void run () {
130             }
131         });
132     }
133
134     //
135
// Our fake lookup
136
//
137
public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
138         public Lkp () {
139             this (new org.openide.util.lookup.InstanceContent ());
140         }
141         
142         private Lkp (org.openide.util.lookup.InstanceContent ic) {
143             super (ic);
144             ic.add (new DD ());
145         }
146     }
147
148     /** Our own dialog displayer.
149      */

150     private static final class DD extends org.openide.DialogDisplayer {
151         public static Object JavaDoc[] options;
152         public static Object JavaDoc toReturn;
153         public static Object JavaDoc message;
154         public static int type = -1;
155         
156         public static void clear(Object JavaDoc t) {
157             type = -1;
158             message = null;
159             options = null;
160             toReturn = t;
161         }
162         
163         public java.awt.Dialog JavaDoc createDialog(org.openide.DialogDescriptor descriptor) {
164             throw new IllegalStateException JavaDoc ("Not implemented");
165         }
166         
167         public Object JavaDoc notify(org.openide.NotifyDescriptor descriptor) {
168             assertNull (options);
169             if (type != -1) {
170                 fail("Second question: " + type);
171             }
172             if (toReturn == null) {
173                 fail("Not specified what we shall return: " + toReturn);
174             }
175             options = descriptor.getOptions();
176             message = descriptor.getMessage();
177             type = descriptor.getOptionType();
178             Object JavaDoc r = toReturn;
179             toReturn = null;
180             return r;
181         }
182         
183     } // end of DD
184

185 }
186
Popular Tags