Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.text; |
15 |
|
16 |
import java.io.PrintStream; |
17 |
import javax.swing.Action; |
18 |
import junit.textui.TestRunner; |
19 |
import org.netbeans.junit.*; |
20 |
import org.openide.DialogDescriptor; |
21 |
import org.openide.cookies.EditorCookie; |
22 |
import org.openide.cookies.OpenCookie; |
23 |
import org.openide.filesystems.*; |
24 |
import org.openide.loaders.DataObject; |
25 |
import org.openide.util.actions.SystemAction; |
26 |
|
27 |
/** DefaultDataObject is supposed to have open operation that shows the text |
28 |
* editor or invokes a dialog with questions. |
29 |
* |
30 |
* @author Jaroslav Tulach |
31 |
*/ |
32 |
public final class SimpleDESTest extends NbTestCase { |
33 |
|
34 |
private FileSystem lfs; |
35 |
private DataObject obj; |
36 |
|
37 |
/** Creates a new instance of DefaultSettingsContextTest */ |
38 |
public SimpleDESTest(String name) { |
39 |
super(name); |
40 |
} |
41 |
|
42 |
public static void main(String[] args) { |
43 |
TestRunner.run(new NbTestSuite(SimpleDESTest.class)); |
44 |
System.exit (0); |
45 |
} |
46 |
|
47 |
protected void setUp() throws java.lang.Exception { |
48 |
clearWorkDir (); |
49 |
|
50 |
System.setProperty("org.openide.util.Lookup", "org.openide.text.SimpleDESTest$Lkp"); |
51 |
super.setUp(); |
52 |
|
53 |
LocalFileSystem l = new LocalFileSystem (); |
54 |
l.setRootDirectory (getWorkDir ()); |
55 |
lfs = l; |
56 |
|
57 |
FileObject fo = FileUtil.createData (lfs.getRoot (), "AA/" + getName () + ".test"); |
58 |
assertNotNull("file not found", fo); |
59 |
obj = DataObject.find(fo); |
60 |
|
61 |
assertEquals ("The right class", obj.getClass (), SO.class); |
62 |
} |
63 |
|
64 |
protected void tearDown() throws java.lang.Exception { |
65 |
super.tearDown(); |
66 |
} |
67 |
|
68 |
public void testHasEditorCookieForResonableContentOfFiles () throws Exception { |
69 |
doCookieCheck (true); |
70 |
} |
71 |
|
72 |
private void doCookieCheck (boolean hasEditCookie) throws Exception { |
73 |
EditorCookie c = tryToOpen ( |
74 |
"Ahoj Jardo," + |
75 |
"how are you" + |
76 |
"\t\n\rBye" |
77 |
); |
78 |
assertNotNull (c); |
79 |
|
80 |
assertEquals ( |
81 |
"Next questions results in the same cookie", |
82 |
c, |
83 |
obj.getCookie(EditorCookie.class) |
84 |
); |
85 |
assertEquals ( |
86 |
"Print cookie is provided", |
87 |
c, |
88 |
obj.getCookie(org.openide.cookies.PrintCookie.class) |
89 |
); |
90 |
assertEquals ( |
91 |
"CloseCookie as well", |
92 |
c, |
93 |
obj.getCookie(org.openide.cookies.CloseCookie.class) |
94 |
); |
95 |
|
96 |
if (hasEditCookie) { |
97 |
assertEquals ( |
98 |
"EditCookie as well", |
99 |
c, |
100 |
obj.getCookie(org.openide.cookies.EditCookie.class) |
101 |
); |
102 |
} else { |
103 |
assertNull ( |
104 |
"No EditCookie", |
105 |
obj.getCookie(org.openide.cookies.EditCookie.class) |
106 |
); |
107 |
|
108 |
} |
109 |
|
110 |
OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class); |
111 |
open.open (); |
112 |
|
113 |
javax.swing.text.Document d = c.getDocument(); |
114 |
assertNotNull (d); |
115 |
|
116 |
d.insertString(0, "Kuk", null); |
117 |
|
118 |
assertNotNull ( |
119 |
"Now there is a save cookie", |
120 |
obj.getCookie (org.openide.cookies.SaveCookie.class) |
121 |
); |
122 |
} |
123 |
|
124 |
public void testItIsPossibleToMaskEditCookie () throws Exception { |
125 |
doCookieCheck (false); |
126 |
} |
127 |
|
128 |
private EditorCookie tryToOpen (String content) throws Exception { |
129 |
FileObject fo = obj.getPrimaryFile(); |
130 |
FileLock lock = fo.lock(); |
131 |
PrintStream os = new PrintStream (fo.getOutputStream(lock)); |
132 |
os.print (content); |
133 |
os.close (); |
134 |
lock.releaseLock(); |
135 |
|
136 |
return (EditorCookie)obj.getCookie (EditorCookie.class); |
137 |
} |
138 |
|
139 |
// |
140 |
// Our fake lookup |
141 |
// |
142 |
public static final class Lkp extends org.openide.util.lookup.AbstractLookup { |
143 |
public Lkp () { |
144 |
this (new org.openide.util.lookup.InstanceContent ()); |
145 |
} |
146 |
|
147 |
private Lkp (org.openide.util.lookup.InstanceContent ic) { |
148 |
super (ic); |
149 |
ic.add (new DLP ()); |
150 |
} |
151 |
} |
152 |
|
153 |
private static final class SL extends org.openide.loaders.UniFileLoader { |
154 |
public SL () { |
155 |
super (SO.class.getName ()); |
156 |
getExtensions().addExtension("test"); |
157 |
} |
158 |
protected org.openide.loaders.MultiDataObject createMultiObject(FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, java.io.IOException { |
159 |
return new SO (primaryFile); |
160 |
} |
161 |
} // end of SL |
162 |
|
163 |
private static final class SO extends org.openide.loaders.MultiDataObject implements org.openide.nodes.CookieSet.Factory { |
164 |
private org.openide.nodes.Node.Cookie cookie = (org.openide.nodes.Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ()); |
165 |
|
166 |
|
167 |
public SO (FileObject fo) throws org.openide.loaders.DataObjectExistsException { |
168 |
super (fo, (SL)SL.getLoader(SL.class)); |
169 |
|
170 |
if (fo.getNameExt().indexOf ("MaskEdit") == -1) { |
171 |
getCookieSet ().add (cookie); |
172 |
} else { |
173 |
getCookieSet ().add (new Class[] { |
174 |
OpenCookie.class, |
175 |
org.openide.cookies.CloseCookie.class, EditorCookie.class, |
176 |
org.openide.cookies.PrintCookie.class |
177 |
}, this); |
178 |
} |
179 |
} |
180 |
|
181 |
|
182 |
public org.openide.nodes.Node.Cookie createCookie (Class c) { |
183 |
return cookie; |
184 |
} |
185 |
} // end of SO |
186 |
|
187 |
private static final class DLP extends org.openide.loaders.DataLoaderPool { |
188 |
protected java.util.Enumeration loaders() { |
189 |
return java.util.Collections.enumeration( |
190 |
java.util.Collections.singleton( |
191 |
SL.getLoader (SL.class) |
192 |
) |
193 |
); |
194 |
} |
195 |
} // end of DataLoaderPool |
196 |
} |