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-2005 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.modules.masterfs.providers; |
15 |
|
16 |
import junit.framework.*; |
17 |
import java.io.IOException; |
18 |
|
19 |
/** Check the behaviour of masterfs's annation provider. |
20 |
* |
21 |
* @author Jaroslav Tulach |
22 |
*/ |
23 |
public class AnnotationProviderTest extends org.netbeans.junit.NbTestCase { |
24 |
private AnnoProv a1, a2, a3, a4; |
25 |
|
26 |
private org.openide.filesystems.FileSystem fs; |
27 |
private org.openide.filesystems.FileObject fo; |
28 |
|
29 |
public AnnotationProviderTest (String testName) { |
30 |
super (testName); |
31 |
} |
32 |
|
33 |
static { |
34 |
System.setProperty ("org.openide.util.Lookup", "org.netbeans.modules.masterfs.providers.AnnotationProviderTest$Lkp"); // NOI18N |
35 |
} |
36 |
|
37 |
// TODO add test methods here. The name must begin with 'test'. For example: |
38 |
// public void testHello() {} |
39 |
|
40 |
protected void setUp () throws java.lang.Exception { |
41 |
assertEquals (Lkp.class, org.openide.util.Lookup.getDefault ().getClass ()); |
42 |
|
43 |
// we need some masterfs fileobject |
44 |
fs = org.netbeans.modules.masterfs.MasterFileSystem.settingsFactory (null); |
45 |
fo = fs.getRoot (); |
46 |
assertEquals (org.netbeans.modules.masterfs.MasterFileSystem.class, fo.getFileSystem ().getClass ()); |
47 |
|
48 |
|
49 |
a1 = new AnnoProv (); |
50 |
a2 = new AnnoProv (); |
51 |
a3 = new AnnoProv (); |
52 |
a4 = new AnnoProv (); |
53 |
|
54 |
Lkp.ic.add (a1); |
55 |
Lkp.ic.add (a2); |
56 |
Lkp.ic.add (a3); |
57 |
Lkp.ic.add (a4); |
58 |
} |
59 |
|
60 |
protected void tearDown () throws java.lang.Exception { |
61 |
Lkp.ic.remove (a1); |
62 |
Lkp.ic.remove (a2); |
63 |
Lkp.ic.remove (a3); |
64 |
Lkp.ic.remove (a4); |
65 |
} |
66 |
|
67 |
public static junit.framework.Test suite () { |
68 |
junit.framework.TestSuite suite = new junit.framework.TestSuite(AnnotationProviderTest.class); |
69 |
|
70 |
return suite; |
71 |
} |
72 |
|
73 |
public void testAnnotateName () { |
74 |
|
75 |
a2.returnName = "MyName"; |
76 |
|
77 |
String r = fs.getStatus ().annotateName ("Kuk", java.util.Collections.singleton (fo)); |
78 |
|
79 |
assertEquals ("My name returned", a2.returnName, r); |
80 |
assertEquals ("Kuk", a1.queriedName); |
81 |
assertEquals ("Kuk", a2.queriedName); |
82 |
assertNull ("Not queried at all", a3.queriedName); |
83 |
assertNull ("Not queried at all", a4.queriedName); |
84 |
|
85 |
assertTrue ("fo is the file", a1.filesName.contains (fo)); |
86 |
assertTrue ("fo is the file", a2.filesName.contains (fo)); |
87 |
|
88 |
assertNull (a1.filesActions); |
89 |
assertNull (a1.filesHtml); |
90 |
assertNull (a1.filesIcon); |
91 |
|
92 |
assertNull (a2.filesActions); |
93 |
assertNull (a2.filesHtml); |
94 |
assertNull (a2.filesIcon); |
95 |
} |
96 |
|
97 |
public void testAnnotateIcon () { |
98 |
|
99 |
int t = java.awt.image.BufferedImage.TYPE_BYTE_GRAY; |
100 |
java.awt.image.BufferedImage my = new java.awt.image.BufferedImage (10, 10, t); |
101 |
java.awt.image.BufferedImage his = new java.awt.image.BufferedImage (20, 20, t); |
102 |
a2.returnIcon = his; |
103 |
|
104 |
java.awt.Image r = fs.getStatus ().annotateIcon (my, 0, java.util.Collections.singleton (fo)); |
105 |
|
106 |
assertEquals ("My name returned", his, r); |
107 |
assertSame (my, a1.queriedIcon); |
108 |
assertSame (my, a2.queriedIcon); |
109 |
assertNull ("Not queried at all", a3.queriedIcon); |
110 |
assertNull ("Not queried at all", a4.queriedIcon); |
111 |
|
112 |
|
113 |
assertNull (a1.filesActions); |
114 |
assertNull (a1.filesHtml); |
115 |
assertNull (a1.filesName); |
116 |
|
117 |
assertNull (a2.filesActions); |
118 |
assertNull (a2.filesHtml); |
119 |
assertNull (a2.filesName); |
120 |
} |
121 |
|
122 |
public void testAnnotateNameHtml () { |
123 |
|
124 |
a2.returnHtml = "MyName"; |
125 |
|
126 |
Object o = fs.getStatus (); |
127 |
assertTrue ("Must be HtmlStatus", o instanceof org.openide.filesystems.FileSystem.HtmlStatus); |
128 |
org.openide.filesystems.FileSystem.HtmlStatus status = (org.openide.filesystems.FileSystem.HtmlStatus)o; |
129 |
|
130 |
|
131 |
|
132 |
String r = status.annotateNameHtml ("Kuk", java.util.Collections.singleton (fo)); |
133 |
|
134 |
assertEquals ("My name returned", a2.returnHtml, r); |
135 |
assertEquals ("Kuk", a1.queriedHtml); |
136 |
assertEquals ("Kuk", a2.queriedHtml); |
137 |
assertNull ("Not queried at all", a3.queriedHtml); |
138 |
assertNull ("Not queried at all", a4.queriedHtml); |
139 |
|
140 |
assertTrue ("fo is the file", a1.filesHtml.contains (fo)); |
141 |
assertTrue ("fo is the file", a2.filesHtml.contains (fo)); |
142 |
|
143 |
assertNull (a1.filesActions); |
144 |
assertNull (a1.filesName); |
145 |
assertNull (a1.filesIcon); |
146 |
|
147 |
assertNull (a2.filesActions); |
148 |
assertNull (a2.filesName); |
149 |
assertNull (a2.filesIcon); |
150 |
} |
151 |
|
152 |
public void testActionsWorksOnSystemActionsCorrectly () { |
153 |
|
154 |
a2.returnActions = new javax.swing.Action[] { |
155 |
org.openide.actions.OpenAction.get (org.openide.actions.OpenAction.class) |
156 |
}; |
157 |
|
158 |
java.util.Set s = java.util.Collections.singleton (fo); |
159 |
javax.swing.Action[] actions = fs.getActions (s); |
160 |
|
161 |
assertEquals ("Actions has the same size", a2.returnActions.length, actions.length); |
162 |
assertEquals ("And that is one", 1, actions.length); |
163 |
assertEquals ("First elem is same", a2.returnActions[0], actions[0]); |
164 |
assertEquals ("a1 queried", s, a1.filesActions); |
165 |
assertEquals ("a2 queried", s, a2.filesActions); |
166 |
assertNull ("Not queried at all", a3.filesActions); |
167 |
assertNull ("Not queried at all", a4.filesActions); |
168 |
|
169 |
assertNull (a1.filesHtml); |
170 |
assertNull (a1.filesName); |
171 |
assertNull (a1.filesIcon); |
172 |
|
173 |
assertNull (a2.filesHtml); |
174 |
assertNull (a2.filesName); |
175 |
assertNull (a2.filesIcon); |
176 |
} |
177 |
|
178 |
public void testListeningCapabilities () { |
179 |
class L implements org.openide.filesystems.FileStatusListener { |
180 |
public org.openide.filesystems.FileStatusEvent ev; |
181 |
|
182 |
public void annotationChanged (org.openide.filesystems.FileStatusEvent ev) { |
183 |
assertNull (this.ev); |
184 |
this.ev = ev; |
185 |
} |
186 |
} |
187 |
L l = new L (); |
188 |
|
189 |
try { |
190 |
fs.addFileStatusListener (l); |
191 |
|
192 |
a2.returnName = "xyz"; |
193 |
String r = fs.getStatus().annotateName ("my name", java.util.Collections.singleton (fo)); |
194 |
assertEquals (r, a2.returnName); |
195 |
|
196 |
org.openide.filesystems.FileStatusEvent ev; |
197 |
ev = new org.openide.filesystems.FileStatusEvent (fs, false, true); |
198 |
a3.fireFileStatusChanged (ev); |
199 |
|
200 |
assertEquals ("Our listener was called", ev, l.ev); |
201 |
l.ev = null; |
202 |
Lkp.ic.remove (a3); |
203 |
|
204 |
a3.fireFileStatusChanged (ev); |
205 |
assertNull ("Now our listener was not called", l.ev); |
206 |
|
207 |
} finally { |
208 |
fs.removeFileStatusListener (l); |
209 |
} |
210 |
} |
211 |
|
212 |
private class AnnotationProviderImpl extends AnnotationProvider { |
213 |
|
214 |
public java.lang.String annotateName (java.lang.String name, java.util.Set files) { |
215 |
return null; |
216 |
} |
217 |
|
218 |
public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files) { |
219 |
return null; |
220 |
} |
221 |
|
222 |
public java.lang.String annotateNameHtml (java.lang.String name, java.util.Set files) { |
223 |
return null; |
224 |
} |
225 |
|
226 |
public javax.swing.Action[] actions (java.util.Set files) { |
227 |
return null; |
228 |
} |
229 |
} |
230 |
|
231 |
|
232 |
public static final class Lkp extends org.openide.util.lookup.AbstractLookup { |
233 |
static org.openide.util.lookup.InstanceContent ic; |
234 |
|
235 |
public Lkp () { |
236 |
this (new org.openide.util.lookup.InstanceContent ()); |
237 |
} |
238 |
|
239 |
private Lkp (org.openide.util.lookup.InstanceContent ic) { |
240 |
super (ic); |
241 |
this.ic = ic; |
242 |
} |
243 |
} // end of Lkp |
244 |
|
245 |
static final class AnnoProv extends AnnotationProvider { |
246 |
public String returnHtml; |
247 |
public String returnName; |
248 |
public java.awt.Image returnIcon; |
249 |
public javax.swing.Action[] returnActions; |
250 |
|
251 |
public String queriedHtml; |
252 |
public String queriedName; |
253 |
public java.awt.Image queriedIcon; |
254 |
|
255 |
public java.util.Set filesHtml; |
256 |
public java.util.Set filesName; |
257 |
public java.util.Set filesIcon; |
258 |
public java.util.Set filesActions; |
259 |
|
260 |
public String annotateNameHtml (String name, java.util.Set files) { |
261 |
assertNull (queriedHtml); |
262 |
queriedHtml = name; |
263 |
filesHtml = files; |
264 |
return returnHtml; |
265 |
} |
266 |
|
267 |
public String annotateName (String name, java.util.Set files) { |
268 |
assertNull (queriedName); |
269 |
queriedName = name; |
270 |
filesName = files; |
271 |
return returnName; |
272 |
} |
273 |
|
274 |
public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files) { |
275 |
assertNull (queriedIcon); |
276 |
queriedIcon = icon; |
277 |
filesIcon = files; |
278 |
return returnIcon; |
279 |
} |
280 |
|
281 |
public javax.swing.Action[] actions (java.util.Set files) { |
282 |
assertNull (filesActions); |
283 |
filesActions = files; |
284 |
return returnActions; |
285 |
} |
286 |
|
287 |
} |
288 |
} |