KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DefaultDataObjectHasOpenTest


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.loaders;
21
22 import java.awt.Dialog JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.openide.DialogDescriptor;
28 import org.openide.DialogDisplayer;
29 import org.openide.NotifyDescriptor;
30 import org.openide.cookies.CloseCookie;
31 import org.openide.cookies.EditCookie;
32 import org.openide.cookies.EditorCookie;
33 import org.openide.cookies.OpenCookie;
34 import org.openide.cookies.PrintCookie;
35 import org.openide.cookies.SaveCookie;
36 import org.openide.filesystems.FileLock;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileSystem;
39 import org.openide.filesystems.Repository;
40 import org.openide.util.Lookup;
41 import org.openide.util.Mutex;
42 import org.openide.util.io.NbMarshalledObject;
43 import org.openide.windows.TopComponent;
44
45 /** DefaultDataObject is supposed to have open operation that shows the text
46  * editor or invokes a dialog with questions.
47  *
48  * @author Jaroslav Tulach
49  */

50 public final class DefaultDataObjectHasOpenTest extends LoggingTestCaseHid {
51
52     private FileSystem lfs;
53     private DataObject obj;
54
55     public DefaultDataObjectHasOpenTest(String JavaDoc name) {
56         super(name);
57     }
58
59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61
62         registerIntoLookup(new DD());
63
64         DD x = (DD)Lookup.getDefault().lookup(DD.class);
65         assertNotNull("DD is there", x);
66
67         String JavaDoc fsstruct [] = new String JavaDoc [] {
68             "AA/a.test"
69         };
70
71         TestUtilHid.destroyLocalFileSystem(getName());
72         lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct);
73         Repository.getDefault().addFileSystem(lfs);
74
75         FileObject fo = lfs.findResource("AA/a.test");
76         assertNotNull("file not found", fo);
77         obj = DataObject.find(fo);
78
79         assertEquals("The right class", obj.getClass(), DefaultDataObject.class);
80
81         assertFalse("Designed to run outside of AWT", SwingUtilities.isEventDispatchThread());
82         DD.options = null;
83         DD.disableTest = false;
84     }
85
86     private void waitForAWT() throws Exception JavaDoc {
87         // just wait till all the stuff from AWT is finished
88
Mutex.EVENT.readAccess(new Mutex.Action() {
89             public Object JavaDoc run() {
90                 return null;
91             }
92         });
93     }
94
95     protected void tearDown() throws Exception JavaDoc {
96         waitForAWT();
97         DD.disableTest = true;
98
99         super.tearDown();
100         if (obj != null) {
101             CloseCookie cc;
102             cc = (CloseCookie)obj.getCookie(CloseCookie.class);
103             if (cc != null) {
104                 DD.toReturn = NotifyDescriptor.NO_OPTION;
105                 cc.close();
106             }
107         }
108         Repository.getDefault().removeFileSystem(lfs);
109
110         waitForAWT();
111     }
112
113     public void testHasOpenCookie() {
114         assertNotNull(obj.getCookie(OpenCookie.class));
115     }
116
117     public void testHasEditCookie() {
118         assertNotNull(obj.getCookie(EditCookie.class));
119     }
120
121     public void testHasEditorCookieForResonableContentOfFiles() throws Exception JavaDoc {
122         EditorCookie c = tryToOpen(
123                 "Ahoj Jardo," +
124                 "how are you" +
125                 "\t\n\rBye.\u00a9\u00e1\u00e9\u00ed\u00f3\u00fa"
126                 );
127         assertNotNull(c);
128
129         doRegularCheck(c);
130     }
131
132     private void doRegularCheck(EditorCookie c) throws Exception JavaDoc {
133         assertEquals(
134                 "Next questions results in the same cookie",
135                 c,
136                 obj.getCookie(EditorCookie.class)
137                 );
138         assertEquals(
139                 "Print cookie is provided",
140                 c,
141                 obj.getCookie(PrintCookie.class)
142                 );
143         assertEquals(
144                 "CloseCookie as well",
145                 c,
146                 obj.getCookie(CloseCookie.class)
147                 );
148
149         OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class);
150         open.open();
151
152         Document JavaDoc d = c.getDocument();
153         assertNotNull(d);
154
155         d.insertString(0, "Kuk", null);
156
157         assertNotNull(
158                 "Now there is a save cookie",
159                 obj.getCookie(SaveCookie.class)
160                 );
161     }
162
163     public void testNoEditorCookieForBinaryFiles() throws Exception JavaDoc {
164         EditorCookie c = tryToOpen("\u0003\u0001\u0000");
165         assertNull(c);
166     }
167
168     public void testOpenAsksAQuestionForBinaryFiles() throws Exception JavaDoc {
169         EditorCookie c = tryToOpen("\u0003\u0001\u0000");
170         assertNull(c);
171
172         DD.toReturn = DialogDescriptor.CANCEL_OPTION;
173         OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class);
174         open.open();
175
176         assertNotNull("There was a query", DD.options);
177         assertEquals("Yes no options", 2, DD.options.length);
178         assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]);
179         assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]);
180     }
181
182     public void testBinaryFilesQuestionContainsOpenActions() throws Exception JavaDoc {
183         EditorCookie c = tryToOpen("\u0003\u0001\u0000");
184         assertNull(c);
185
186         DD.toReturn = DialogDescriptor.CLOSED_OPTION;
187         OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class);
188         open.open();
189
190         assertNotNull("There was a query", DD.options);
191         assertEquals("Two options", 2, DD.options.length);
192         assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]);
193         assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]);
194         DD.options = null;
195
196         DD.toReturn = DialogDescriptor.OK_OPTION;
197         open.open();
198
199         assertNotNull("There was a query", DD.options);
200         assertEquals("Still 2 options", 2, DD.options.length);
201         assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]);
202         assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]);
203
204         c = (EditorCookie)obj.getCookie(EditorCookie.class);
205         assertNotNull("Editor cookie created", c);
206
207         doRegularCheck(c);
208     }
209
210     private EditorCookie tryToOpen(String JavaDoc content) throws Exception JavaDoc {
211         FileObject fo = obj.getPrimaryFile();
212         FileLock lock = fo.lock();
213         PrintStream JavaDoc os = new PrintStream JavaDoc(fo.getOutputStream(lock));
214         os.print(content);
215         os.close();
216         lock.releaseLock();
217
218         return (EditorCookie)obj.getCookie(EditorCookie.class);
219     }
220
221     public void testThatTheNameOfDataObjectsNodeContainsTheExtensionIssue18780() throws Exception JavaDoc {
222         String JavaDoc name = obj.getPrimaryFile().getNameExt();
223         assertEquals("Name contains extension", name, obj.getNodeDelegate().getName());
224     }
225
226     public void testComponentCanBeSerialized() throws Exception JavaDoc {
227         doComponentCanBeSerialized("Sample\nTest\nTo\nSerialized");
228     }
229
230     public void testComponentCanBeSerializedEvenItIsBinary() throws Exception JavaDoc {
231         // make sure it has the cookie
232
OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class);
233         DD.toReturn = DialogDescriptor.OK_OPTION;
234         open.open();
235         open = null;
236
237         doComponentCanBeSerialized("\u0003\u0001\u0000");
238     }
239
240     public void testComponentCanBeSerializedEvenItIsBig() throws Exception JavaDoc {
241         // make sure it has the cookie
242
OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class);
243         DD.toReturn = DialogDescriptor.OK_OPTION;
244         open.open();
245         open = null;
246
247         FileObject fo = obj.getPrimaryFile();
248         FileLock lock = fo.lock();
249         PrintStream JavaDoc os = new PrintStream JavaDoc(fo.getOutputStream(lock));
250         for (int i = 0; i < 1024 * 1024 / 10; i++) {
251             os.print("Ahoj " + i + "\n");
252         }
253         os.close();
254         lock.releaseLock();
255         assertTrue("Larger than 1Mb", 1024 * 1024 + 1000 < fo.getSize());
256
257         doComponentCanBeSerialized(null);
258     }
259
260     private void doComponentCanBeSerialized(String JavaDoc txt) throws Exception JavaDoc {
261         final EditorCookie c = (txt != null) ?
262                 tryToOpen(txt) : obj.getCookie(EditorCookie.class);
263         assertNotNull(c);
264         c.open();
265
266         // Get the opened panes inside AWT.
267
JEditorPane JavaDoc[] arr = Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane JavaDoc[]>() {
268             public JEditorPane JavaDoc[] run() {
269                 return c.getOpenedPanes();
270             }
271         });
272         
273         assertNotNull("Something opened", arr);
274         assertEquals("One opened", 1, arr.length);
275
276         Object JavaDoc o = SwingUtilities.getAncestorOfClass(TopComponent.class, arr[0]);
277         assertNotNull("Top component found", o);
278         NbMarshalledObject mar = new NbMarshalledObject(o);
279
280         ((TopComponent)o).close();
281
282         obj.setValid(false);
283
284         TopComponent tc = (TopComponent)mar.get();
285
286         assertNotNull("Successfully deserialized", tc);
287
288         if (obj == DataObject.find(obj.getPrimaryFile())) {
289             fail("Strange, obj should be garbage collected" + obj);
290         }
291     }
292
293     /** Our own dialog displayer.
294      */

295     private static final class DD extends DialogDisplayer {
296         public static Object JavaDoc[] options;
297         public static Object JavaDoc toReturn;
298         public static boolean disableTest;
299
300         public Dialog JavaDoc createDialog(DialogDescriptor descriptor) {
301             throw new IllegalStateException JavaDoc("Not implemented");
302         }
303
304         public Object JavaDoc notify(NotifyDescriptor descriptor) {
305             if (disableTest) {
306                 return toReturn;
307             } else {
308                 assertNull(options);
309                 assertNotNull(toReturn);
310                 options = descriptor.getOptions();
311                 Object JavaDoc r = toReturn;
312                 toReturn = null;
313                 return r;
314             }
315         }
316
317     } // end of DD
318

319 }
320
Popular Tags