Lines 18-23
Link Here
|
18 |
import java.io.File; |
18 |
import java.io.File; |
19 |
import java.io.IOException; |
19 |
import java.io.IOException; |
20 |
import java.lang.ref.WeakReference; |
20 |
import java.lang.ref.WeakReference; |
|
|
21 |
import java.lang.reflect.Field; |
22 |
import java.lang.reflect.Method; |
21 |
import java.util.Arrays; |
23 |
import java.util.Arrays; |
22 |
import java.util.Collection; |
24 |
import java.util.Collection; |
23 |
import java.util.Collections; |
25 |
import java.util.Collections; |
Lines 30-39
Link Here
|
30 |
import javax.swing.AbstractAction; |
32 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.Action; |
33 |
import javax.swing.Action; |
32 |
import javax.swing.KeyStroke; |
34 |
import javax.swing.KeyStroke; |
|
|
35 |
import javax.swing.text.Keymap; |
33 |
import junit.framework.*; |
36 |
import junit.framework.*; |
34 |
import org.netbeans.junit.*; |
37 |
import org.netbeans.junit.*; |
35 |
import org.openide.ErrorManager; |
38 |
import org.openide.ErrorManager; |
36 |
import org.openide.cookies.InstanceCookie; |
39 |
import org.openide.cookies.InstanceCookie; |
|
|
40 |
import org.openide.filesystems.FileLock; |
37 |
import org.openide.filesystems.FileObject; |
41 |
import org.openide.filesystems.FileObject; |
38 |
import org.openide.filesystems.FileSystem; |
42 |
import org.openide.filesystems.FileSystem; |
39 |
import org.openide.filesystems.LocalFileSystem; |
43 |
import org.openide.filesystems.LocalFileSystem; |
Lines 63-77
Link Here
|
63 |
super(s); |
67 |
super(s); |
64 |
} |
68 |
} |
65 |
|
69 |
|
|
|
70 |
ShortcutsFolder sf = null; |
71 |
FileSystem fs = null; |
72 |
FileObject fld = null; |
73 |
FileObject fo = null; |
74 |
|
75 |
static Repository repository = null; |
76 |
protected void setUp () { |
77 |
fs = createTestingFilesystem(); |
78 |
fo = getFolderForShortcuts(fs); |
79 |
sf = createShortcutsFolder(fs); |
80 |
sf.waitShortcutsFinished(); |
81 |
} |
82 |
|
83 |
protected void tearDown() { |
84 |
try { |
85 |
FileLock lock = fo.lock(); |
86 |
fo.delete(lock); |
87 |
lock.releaseLock(); |
88 |
} catch (Exception ioe) { |
89 |
} |
90 |
|
91 |
ShortcutsFolder.shortcutsFolder = null; |
92 |
sf = null; |
93 |
fs = null; |
94 |
fld = null; |
95 |
fo = null; |
96 |
try { |
97 |
if (dir.exists()) { |
98 |
File[] f = dir.listFiles(); |
99 |
for (int i=0; i < f.length; i++) { |
100 |
f[i].delete(); |
101 |
} |
102 |
dir.delete(); |
103 |
} |
104 |
dir = null; |
105 |
folderName = null; |
106 |
repository = null; |
107 |
|
108 |
} catch (Exception e) { |
109 |
throw new RuntimeException(e); |
110 |
} |
111 |
} |
112 |
|
66 |
public void testHyphenation (String s) { |
113 |
public void testHyphenation (String s) { |
|
|
114 |
System.out.println("testHyphenation"); |
67 |
HashSet set = new HashSet(); |
115 |
HashSet set = new HashSet(); |
68 |
char[] c = new String("ABCD").toCharArray(); |
116 |
char[] c = new String("ABCD").toCharArray(); |
69 |
ShortcutsFolder.createHyphenatedPermutation (c, set, "-F5"); |
117 |
ShortcutsFolder.createHyphenatedPermutation (c, set, "-F5"); |
70 |
|
118 |
|
71 |
assertTrue (set.contains("A-B-C-D-F5")); |
119 |
assertTrue (set.contains("A-B-C-D-F5")); |
72 |
} |
120 |
} |
73 |
|
121 |
|
74 |
public void testPermutations () { |
122 |
public void testPermutations () { |
|
|
123 |
System.out.println("testPermutations"); |
75 |
HashSet set = new HashSet(); |
124 |
HashSet set = new HashSet(); |
76 |
|
125 |
|
77 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
126 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
Lines 89-94
Link Here
|
89 |
} |
138 |
} |
90 |
|
139 |
|
91 |
public void testPermutationsIncludeHyphenatedVariants() { |
140 |
public void testPermutationsIncludeHyphenatedVariants() { |
|
|
141 |
System.out.println("testPermutationsIncludeHyphenatedVariants"); |
92 |
HashSet set = new HashSet(); |
142 |
HashSet set = new HashSet(); |
93 |
|
143 |
|
94 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
144 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
Lines 106-115
Link Here
|
106 |
} |
156 |
} |
107 |
|
157 |
|
108 |
public void testPermutationsContainConvertedWildcard () { |
158 |
public void testPermutationsContainConvertedWildcard () { |
109 |
String targetChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
159 |
doPermutationsContainConvertedWildcard (true); |
|
|
160 |
doPermutationsContainConvertedWildcard (false); |
161 |
} |
162 |
|
163 |
public void doPermutationsContainConvertedWildcard (boolean mac) { |
164 |
System.out.println("testPermutationsContainConvertedWildcard mac=" + mac); |
165 |
String targetChar = mac |
110 |
? "M" : "C"; |
166 |
? "M" : "C"; |
111 |
|
167 |
|
112 |
String[] s = ShortcutsFolder.getPermutations("DA-F5"); |
168 |
String[] s = ShortcutsFolder.getPermutations("DA-F5", mac); |
113 |
HashSet set = new HashSet (Arrays.asList(s)); |
169 |
HashSet set = new HashSet (Arrays.asList(s)); |
114 |
set.add ("DA-F5"); //Permutations will not contain the passed value |
170 |
set.add ("DA-F5"); //Permutations will not contain the passed value |
115 |
|
171 |
|
Lines 125-136
Link Here
|
125 |
set.contains(permutations[i])); |
181 |
set.contains(permutations[i])); |
126 |
} |
182 |
} |
127 |
} |
183 |
} |
128 |
|
184 |
|
129 |
public void testPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator () { |
185 |
public void testPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator () { |
130 |
String targetChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
186 |
doTestPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator(true); |
|
|
187 |
doTestPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator(false); |
188 |
} |
189 |
|
190 |
public void doTestPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator (boolean macintosh) { |
191 |
System.out.println("testPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator - mac=" + macintosh); |
192 |
String targetChar = macintosh |
131 |
? "M" : "C"; |
193 |
? "M" : "C"; |
132 |
|
194 |
|
133 |
String[] s = ShortcutsFolder.getPermutations(targetChar + "A-F5"); |
195 |
String[] s = ShortcutsFolder.getPermutations(targetChar + "A-F5", macintosh); |
134 |
|
196 |
|
135 |
String[] permutations = new String[] { |
197 |
String[] permutations = new String[] { |
136 |
"A" + targetChar + "-F5", "A-" + targetChar + "-F5", |
198 |
"A" + targetChar + "-F5", "A-" + targetChar + "-F5", |
Lines 147-160
Link Here
|
147 |
} |
209 |
} |
148 |
} |
210 |
} |
149 |
|
211 |
|
|
|
212 |
public void testPermutationsContainConvertedAltWildcard () { |
213 |
doTestPermutationsContainConvertedAltWildcard (true); |
214 |
doTestPermutationsContainConvertedAltWildcard (false); |
215 |
} |
216 |
|
217 |
public void doTestPermutationsContainConvertedAltWildcard (boolean macintosh) { |
218 |
System.out.println("testPermutationsContainConvertedAltWildcard mac = " + macintosh); |
219 |
String cmdChar = macintosh |
220 |
? "M" : "C"; |
221 |
|
222 |
String altKey = macintosh ? |
223 |
"C" : "A"; //NOI18N |
224 |
|
225 |
String[] s = ShortcutsFolder.getPermutations("DO-F5", macintosh); |
226 |
HashSet set = new HashSet (Arrays.asList(s)); |
227 |
set.add ("DO-F5"); //Permutations will not contain the passed value |
228 |
|
229 |
String[] permutations = new String[] { |
230 |
"OD-F5", |
231 |
"O" + cmdChar + "-F5", |
232 |
"DO-F5", |
233 |
"D-O-F5", |
234 |
"O-D-F5", |
235 |
altKey + "-D-F5", |
236 |
altKey + "-" + cmdChar + "-F5", |
237 |
altKey + "D-F5", |
238 |
}; |
239 |
|
240 |
for (int i=0; i < permutations.length; i++) { |
241 |
assertTrue ("Permutation of D"+ altKey + "-F5 not generated:" |
242 |
+ permutations[i] + "; (generated:" + set + ")", |
243 |
set.contains(permutations[i])); |
244 |
} |
245 |
} |
246 |
|
247 |
public void testDualWildcardPermutations() { |
248 |
doTestDualWildcardPermutations(true); |
249 |
doTestDualWildcardPermutations(false); |
250 |
} |
251 |
|
252 |
public void doTestDualWildcardPermutations(boolean macintosh) { |
253 |
System.out.println("testDualWildcardPermutations mac=" + macintosh); |
254 |
String cmdChar = macintosh |
255 |
? "M" : "C"; |
256 |
|
257 |
String altKey = macintosh ? |
258 |
"C" : "A"; //NOI18N |
259 |
|
260 |
String[] s = ShortcutsFolder.getPermutations("OD-F5", macintosh); |
261 |
HashSet set = new HashSet (Arrays.asList(s)); |
262 |
set.add ("OD-F5"); //Permutations will not contain the passed value |
263 |
|
264 |
String[] permutations = new String[] { |
265 |
"OD-F5", "O" + cmdChar + "-F5", "DO-F5", "D-O-F5", |
266 |
"O-D-F5", altKey + "-D-F5", altKey + "-" + cmdChar + "-F5", altKey + "D-F5", |
267 |
altKey + cmdChar + "-F5" |
268 |
}; |
269 |
|
270 |
for (int i=0; i < permutations.length; i++) { |
271 |
assertTrue ("Permutation of OD-F5 not generated:" |
272 |
+ permutations[i] + "; (generated:" + set + ")", |
273 |
set.contains(permutations[i])); |
274 |
} |
275 |
} |
276 |
|
277 |
public void testOPermutationOfAlt () throws Exception { |
278 |
System.out.println("testOPermutationOfAlt"); |
279 |
// FileSystem fs = createTestingFilesystem(); |
280 |
// FileObject fo = getFolderForShortcuts(fs); |
281 |
|
282 |
// assertEquals (lastDir, repository.getDefaultFileSystem().getRoot().getPath()); |
283 |
|
284 |
FileObject data1 = fo.createData("OD-F6.instance"); |
285 |
assertNotNull(data1); |
286 |
data1.setAttribute("instanceClass", "org.netbeans.core.ShortcutsFolderTest$TestAction"); |
287 |
|
288 |
FileObject data2 = fo.createData("OS-F6.instance"); |
289 |
assertNotNull(data2); |
290 |
data2.setAttribute("instanceClass", "org.netbeans.core.ShortcutsFolderTest$TestAction"); |
291 |
|
292 |
File file = new File (lastDir + folderName + File.separator + "OD-F6.instance"); |
293 |
assertTrue ("Actual file not created: " + file.getPath(), file.exists()); |
294 |
|
295 |
sf.refreshGlobalMap(); |
296 |
|
297 |
DataObject ob = DataObject.find (data1); |
298 |
assertNotNull("Data object not found: " + data1.getPath(), ob); |
299 |
|
300 |
InstanceCookie ck = (InstanceCookie) ob.getCookie(InstanceCookie.class); |
301 |
Object obj = ck.instanceCreate(); |
302 |
|
303 |
assertTrue ("InstanceCookie was not an instanceof TestAction - " + obj, obj instanceof TestAction); |
304 |
|
305 |
int mask = System.getProperty("mrj.version") == null ? KeyEvent.ALT_MASK : |
306 |
KeyEvent.CTRL_MASK; |
307 |
|
308 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F6, mask | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); |
309 |
Action action = (Action) obj; |
310 |
|
311 |
ShortcutsFolder.applyChanges(Arrays.asList(new Object[] {new ShortcutsFolder.ChangeRequest (stroke, action, false)})); |
312 |
|
313 |
ShortcutsFolder.refreshGlobalMap(); |
314 |
|
315 |
FileObject now = fo.getFileObject ("OD-F6.instance"); |
316 |
//XXX WTF?? |
317 |
assertNull ("File object should be deleted - but is " + (now == null ? " null " : now.getPath()), now); |
318 |
|
319 |
assertFalse ("File still exists: " + lastDir + "OD-F6.instance", file.exists()); |
320 |
|
321 |
file = new File (lastDir + "Shortcuts" + File.separator + "OS-F6.instance"); |
322 |
assertTrue ("File should not have been deleted: " + file.getPath(), file.exists()); |
323 |
|
324 |
} |
325 |
|
326 |
|
327 |
|
328 |
private static String lastDir = null; |
329 |
private static File dir = null; |
330 |
private File getTempDir() { |
331 |
String outdir = System.getProperty("java.io.tmpdir"); //NOI18N |
332 |
if (!outdir.endsWith(File.separator)) { |
333 |
outdir += File.separator; |
334 |
} |
335 |
String dirname = Long.toHexString(System.currentTimeMillis()); |
336 |
lastDir = outdir + dirname + File.separator; |
337 |
dir = new File (outdir + dirname); |
338 |
try { |
339 |
dir.mkdir(); |
340 |
} catch (Exception ioe) { |
341 |
ioe.printStackTrace(); |
342 |
fail ("Exception creating temporary dir for tests " + dirname + " - " + ioe.getMessage()); |
343 |
} |
344 |
dir.deleteOnExit(); |
345 |
|
346 |
return dir; |
347 |
} |
348 |
|
150 |
private FileSystem createTestingFilesystem () { |
349 |
private FileSystem createTestingFilesystem () { |
151 |
FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); |
350 |
FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); |
152 |
try { |
351 |
try { |
|
|
352 |
LocalFileSystem result = new LocalFileSystem(); |
353 |
result.setRootDirectory(getTempDir()); |
354 |
repository = new Repository (result); |
355 |
fixDefaultRepository(); |
356 |
System.setProperty ("org.openide.util.Lookup", "org.netbeans.core.ShortcutsFolderTest$LKP"); |
153 |
FileObject[] arr = root.getChildren (); |
357 |
FileObject[] arr = root.getChildren (); |
154 |
for (int i = 0; i < arr.length; i++) { |
358 |
for (int i = 0; i < arr.length; i++) { |
155 |
arr[i].delete (); |
359 |
arr[i].delete (); |
156 |
} |
360 |
} |
157 |
return root.getFileSystem (); |
361 |
return result; |
158 |
} catch (Exception e) { |
362 |
} catch (Exception e) { |
159 |
e.printStackTrace(); |
363 |
e.printStackTrace(); |
160 |
fail (e.getMessage()); |
364 |
fail (e.getMessage()); |
Lines 162-173
Link Here
|
162 |
return null; |
366 |
return null; |
163 |
} |
367 |
} |
164 |
|
368 |
|
|
|
369 |
private void fixDefaultRepository () { |
370 |
try { |
371 |
Class c = ClassLoader.getSystemClassLoader().loadClass("org.openide.filesystems.ExternalUtil"); |
372 |
Method m = c.getDeclaredMethod ("setRepository", new Class[] {Repository.class}); |
373 |
m.setAccessible(true); |
374 |
m.invoke (null, new Object[] { repository }); |
375 |
} catch (Exception e) { |
376 |
throw new RuntimeException (e); |
377 |
} |
378 |
} |
379 |
|
165 |
private FileObject getFolderForShortcuts(FileSystem fs) { |
380 |
private FileObject getFolderForShortcuts(FileSystem fs) { |
166 |
FileObject result = null; |
381 |
FileObject result = null; |
167 |
try { |
382 |
try { |
168 |
result = fs.getRoot().getFileObject("Shortcuts"); |
383 |
folderName = "Shortcuts"; |
|
|
384 |
result = fs.getRoot().getFileObject(folderName); |
169 |
if (result == null) { |
385 |
if (result == null) { |
170 |
result = fs.getRoot().createFolder("Shortcuts"); |
386 |
result = fs.getRoot().createFolder(folderName); |
171 |
} |
387 |
} |
172 |
} catch (Exception e) { |
388 |
} catch (Exception e) { |
173 |
e.printStackTrace(); |
389 |
e.printStackTrace(); |
Lines 176-181
Link Here
|
176 |
return result; |
392 |
return result; |
177 |
} |
393 |
} |
178 |
|
394 |
|
|
|
395 |
private String folderName = null; |
179 |
private ShortcutsFolder createShortcutsFolder(FileSystem fs) { |
396 |
private ShortcutsFolder createShortcutsFolder(FileSystem fs) { |
180 |
try { |
397 |
try { |
181 |
DataObject dob = DataObject.find(getFolderForShortcuts(fs)); |
398 |
DataObject dob = DataObject.find(getFolderForShortcuts(fs)); |
Lines 219-225
Link Here
|
219 |
ShortcutsFolder.applyChanges(Arrays.asList(new Object[] {new ShortcutsFolder.ChangeRequest (stroke, action, false)})); |
436 |
ShortcutsFolder.applyChanges(Arrays.asList(new Object[] {new ShortcutsFolder.ChangeRequest (stroke, action, false)})); |
220 |
|
437 |
|
221 |
ShortcutsFolder.refreshGlobalMap(); |
438 |
ShortcutsFolder.refreshGlobalMap(); |
222 |
|
|
|
223 |
FileObject now = fo.getFileObject ("AD-F5.instance"); |
439 |
FileObject now = fo.getFileObject ("AD-F5.instance"); |
224 |
assertNull ("File object should be deleted - ", now); |
440 |
assertNull ("File object should be deleted - ", now); |
225 |
|
441 |
|