KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > BindingTest


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.netbeans.api.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.api.registry.fs.FileSystemContextFactory;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26 import org.netbeans.spi.registry.BasicContext;
27 import org.netbeans.spi.registry.SpiUtils;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.Repository;
30 import org.openide.loaders.DataObject;
31 import org.openide.modules.ModuleInfo;
32 import org.openide.util.Lookup;
33
34 import javax.swing.*;
35 import java.awt.*;
36 import java.net.URL JavaDoc;
37
38 /**
39  *
40  * @author Vitezslav Stejskal
41  * @author David Konecny
42  */

43 public class BindingTest extends NbTestCase {
44     private static final String JavaDoc MY_NULL = new String JavaDoc("MY_NULL");
45
46     public BindingTest (String JavaDoc name) {
47         super (name);
48     }
49     
50     public static void main(String JavaDoc[] args) {
51         TestRunner.run(new NbTestSuite(BindingTest.class));
52     }
53     
54     protected void setUp () throws Exception JavaDoc {
55         Lookup.getDefault().lookup(ModuleInfo.class);
56     }
57     
58     public void testBindObject() throws Exception JavaDoc {
59         implBindObject(getRootContext(), "foo", getRoot(), new JLabel("obj 11"), new JLabel("second OBJ"));
60     }
61
62     public void testBindObject2() throws Exception JavaDoc {
63         Context ctx = getRootContext().createSubcontext("a/b/c");
64         FileObject fo = findResource ("a/b/c");
65         implBindObject(ctx, "foo", fo, new JLabel("objQ"), new JLabel("secondW")) ;
66         getRootContext().destroySubcontext("a");
67     }
68
69     // XXX #27494 Names over 50 chars.
70
public void _EXCLUDE_issue36334_testLongNameObject() throws Exception JavaDoc {
71         implBindObject(getRootContext(), "ThisIsVeryLongNameOfInstanceFileToTestIssueDealingWithInstanceNamesWhichLenghtIsOver50Characters", getRoot(), new JLabel("objQ1"), new JLabel("secondW1"));
72     }
73     
74     // XXX #27494 Names containing some special chars
75
public void _EXCLUDE_testStrangeNameObject() throws Exception JavaDoc {
76         implBindObject(getRootContext(), ":[]<>?*|.\"\u0020\u007E#", getRoot(), new JLabel("objQ111"), new JLabel("secondW111"));
77     }
78     
79     public void implBindObject(Context context, String JavaDoc bindingName, FileObject rootFO, Object JavaDoc objA, Object JavaDoc objA2) throws Exception JavaDoc {
80         FileObject f;
81         
82         context.putObject(bindingName, objA);
83         String JavaDoc fileName = escapeAndCut(bindingName);
84         f = rootFO.getFileObject (fileName, "settings");
85         assertTrue ("Instance file wasn't created for bound object", f != null);
86         assertTrue (f.isData ());
87
88         // XXX: hold IDO instance otherwise, it can be GCed and another instance of
89
// objA will be looked up from the context, this is bug of IDO
90
DataObject ido = DataObject.find (f);
91
92         Object JavaDoc obj2 = context.getObject(bindingName, MY_NULL);
93         assertTrue ("Bound object wasn't looked up", obj2 != null);
94         assertTrue ("Bind obj and lookup result are different", objA == obj2);
95         
96         // object and subcontext must coexist
97
try {
98             context.createSubcontext (bindingName);
99         } catch (Exception JavaDoc e) {
100             fail ("Context with same name as object binding must coexist");
101         }
102
103         context.destroySubcontext (bindingName);
104         obj2 = context.getObject (bindingName, MY_NULL);
105         assertTrue ("Subcontext destroyed, but object can't be found, obj2=" + obj2 + " objA=" + objA, obj2 == objA);
106
107         // incorrect binding name
108
{
109             // this binding will not be stored
110
context.putObject("ctx1/"+bindingName, objA);
111             Object JavaDoc o = context.getObject ("ctx1/"+bindingName, MY_NULL);
112             assertTrue ("ContextException expected when slash character is used in name",
113                 o == MY_NULL);
114         }
115
116         // rebind
117
Object JavaDoc objB = objA2;
118         context.putObject (bindingName, objB);
119         Object JavaDoc obj = context.getObject (bindingName, MY_NULL);
120         assertEquals ("New object not found", obj, objB);
121         
122         // unbind
123
context.putObject (bindingName, null);
124         f = rootFO.getFileObject (bindingName, "settings");
125         assertTrue ("Instance file wasn't destroyed for unbound object", f == null);
126         
127         // binding doesn't exist anymore
128
obj2 = context.getObject(bindingName, MY_NULL);
129         assertTrue("Object is still reachable even if unbound", obj2 == MY_NULL);
130         
131         // unbind for non-existing binding must be OK
132
context.putObject (bindingName, null);
133     }
134
135     public void testBindPrimitive() throws Exception JavaDoc {
136         implBindPrimitive(getRootContext(), "foo", getRoot(), new String JavaDoc("11"), new String JavaDoc("789"));
137     }
138
139     public void testBindPrimitive2() throws Exception JavaDoc {
140         Context ctx = getRootContext().createSubcontext("a/b/c");
141         FileObject fo = findResource ("a/b/c");
142         implBindPrimitive(ctx, "foo", fo, new String JavaDoc("171"), new String JavaDoc("7879")) ;
143         getRootContext().destroySubcontext("a");
144     }
145
146     // XXX #27494 Names over 50 chars.
147
public void testLongNamePrimitive() throws Exception JavaDoc {
148         implBindPrimitive(getRootContext(), "ThisIsVeryLongNameOfInstanceFileToTestIssueDealingWithInstanceNamesWhichLenghtIsOver50Characters", getRoot(), new String JavaDoc("119"), new String JavaDoc("9789"));
149     }
150     
151     // XXX #27494 Names containing some special chars
152
public void _EXCLUDE_testStrangeNamePrimitive() throws Exception JavaDoc {
153         implBindPrimitive(getRootContext(), ":[]<>?*|.\"\u0020\u007E#", getRoot(), new String JavaDoc("1175"), new String JavaDoc("7895"));
154     }
155     
156     public void implBindPrimitive(Context context, String JavaDoc bindingName, FileObject rootFO, String JavaDoc objA, String JavaDoc objA2) throws Exception JavaDoc {
157         FileObject f;
158         
159         context.putObject(bindingName, objA);
160
161         String JavaDoc obj2 = (String JavaDoc)context.getObject(bindingName, MY_NULL);
162         assertTrue ("Bound object wasn't looked up", obj2 != null);
163         assertTrue ("Bind obj and lookup result are different: A="+objA+" B="+obj2, objA.equals(obj2));
164         
165         // object and subcontext must coexist
166
try {
167             context.createSubcontext (bindingName);
168         } catch (Exception JavaDoc e) {
169             fail ("Context with same name as object binding must coexist");
170         }
171
172         context.destroySubcontext (bindingName);
173         obj2 = (String JavaDoc)context.getObject (bindingName, MY_NULL);
174         assertTrue ("Subcontext destroyed, but object can't be found, obj2=" + obj2 + " objA=" + objA, obj2.equals(objA));
175
176         // incorrect binding name
177
{
178             // this binding will not be stored
179
context.putObject("ctx1/"+bindingName, objA);
180             String JavaDoc o = (String JavaDoc)context.getObject ("ctx1/"+bindingName, MY_NULL);
181             assertTrue ("ContextException expected when slash character is used in name",
182                 o == MY_NULL);
183         }
184
185         // rebind
186
String JavaDoc objB = (String JavaDoc)objA2;
187         context.putObject (bindingName, objB);
188         String JavaDoc obj = (String JavaDoc)context.getObject (bindingName, MY_NULL);
189         assertEquals ("New object not found", obj, objB);
190         
191         // unbind
192
context.putObject (bindingName, null);
193         f = rootFO.getFileObject (bindingName, "settings");
194         assertTrue ("Instance file wasn't destroyed for unbound object", f == null);
195         
196         // binding doesn't exist anymore
197
obj2 = (String JavaDoc)context.getObject(bindingName, MY_NULL);
198         assertTrue("Object is still reachable even if unbound", obj2 == MY_NULL);
199         
200         // unbind for non-existing binding must be OK
201
context.putObject (bindingName, null);
202     }
203     
204     public void testPrimitive() throws Exception JavaDoc {
205         implTestPrimitive(getRootContext());
206     }
207     
208     public void testPrimitive2() throws Exception JavaDoc {
209         Context ctx = getRootContext().createSubcontext("a/b/c");
210         FileObject fo = findResource ("a/b/c");
211         implTestPrimitive(ctx);
212         getRootContext().destroySubcontext("a");
213     }
214
215     public void implTestPrimitive(Context ctx) throws Exception JavaDoc {
216         String JavaDoc s = "bsdfmdbmfd";
217         ctx.putString("b1", s);
218         String JavaDoc s_ = ctx.getString("b1", null);
219         assertTrue("Values do not match", s_.equals(s));
220         assertTrue("Object type does not match", ctx.getObject("b1", null) instanceof String JavaDoc);
221         
222         int i = 1889;
223         ctx.putInt("b2", i);
224         int i_ = ctx.getInt("b2", 1112);
225         assertTrue("Values do not match", i_ == i);
226         assertTrue("Object type does not match", ctx.getObject("b2", null) instanceof Integer JavaDoc);
227         
228         long l = 188918891L;
229         ctx.putLong("b3", l);
230         long l_ = ctx.getLong("b3", 11121112L);
231         assertTrue("Values do not match", l_ == l);
232         assertTrue("Object type does not match", ctx.getObject("b3", null) instanceof Long JavaDoc);
233         
234         float f = 18891111.1889F;
235         ctx.putFloat("b4", f);
236         float f_ = ctx.getFloat("b4", 1112.1112F);
237         assertTrue("Values do not match: "+f_+" "+f, f_ == f);
238         assertTrue("Object type does not match", ctx.getObject("b4", null) instanceof Float JavaDoc);
239         
240         double d = 181889333333.18989989891889D;
241         ctx.putDouble("b5", d);
242         double d_ = ctx.getDouble("b5", 11121112.11121112D);
243         assertTrue("Values do not match", d_ == d);
244         assertTrue("Object type does not match", ctx.getObject("b5", null) instanceof Double JavaDoc);
245         
246         boolean b = true;
247         ctx.putBoolean("b6", b);
248         boolean b_ = ctx.getBoolean("b6", false);
249         assertTrue("Values do not match", b_ == b);
250         assertTrue("Object type does not match", ctx.getObject("b6", null) instanceof Boolean JavaDoc);
251         
252         Font ff = new Font("Dialog", Font.ITALIC, 31);
253         ctx.putFont("b7", ff);
254         Font ff_ = ctx.getFont("b7", null);
255         assertEquals("Values do not match", ff, ff_ );
256         assertTrue("Object type does not match", ctx.getObject("b7", null) instanceof Font);
257         
258         Color c = new Color(10, 20, 30);
259         ctx.putColor("b8", c);
260         Color c_ = ctx.getColor("b8", new Color(1, 2, 3));
261         assertTrue("Values do not match", c_.getRGB() == c.getRGB());
262         assertTrue("Object type does not match", ctx.getObject("b8", null) instanceof Color);
263         
264         URL JavaDoc u = new URL JavaDoc("http://www.netbeans.org/download/");
265         ctx.putURL("b9", u);
266         URL JavaDoc u_ = ctx.getURL("b9", null);
267         assertEquals("Values do not match", u_ , u);
268         assertTrue("Object type does not match", ctx.getObject("b9", null) instanceof URL JavaDoc);
269         
270         String JavaDoc sa[] = new String JavaDoc[]{"aaa", "bbb", "ccc"};
271         ctx.putStringArray("b0", '#', sa);
272         String JavaDoc sa_[] = ctx.getStringArray("b0", '#', new String JavaDoc[]{"1", "2", "3"});
273         assertEquals("Values do not match", sa_[0] , "aaa");
274         assertEquals("Values do not match", sa_[1] , "bbb");
275         assertEquals("Values do not match", sa_[2] , "ccc");
276         
277     }
278
279
280     public void testObjectRef() throws Exception JavaDoc {
281         BasicContext rootCtx = FileSystemContextFactory.createContext(getRoot());
282         BasicContext basicCtx = rootCtx.createSubcontext("someCOTOXO");
283         BasicContext basicCtx2 = basicCtx.createSubcontext("subocontexto");
284         Context ctx = SpiUtils.createContext(basicCtx);
285         Context ctx2 = SpiUtils.createContext(basicCtx2);
286         
287         Object JavaDoc o = new JLabel("my label Y");
288         ctx.putObject("obj25", o);
289         ObjectRef or = SpiUtils.createObjectRef(basicCtx, "obj25");
290
291         ctx.putObject("ref", or);;
292         Object JavaDoc val = ctx.getObject("ref", MY_NULL);
293         assertTrue("Retrieved object cannot be null", val != MY_NULL);
294         assertTrue("Retrived object is not the same", val instanceof JLabel);
295         assertTrue("Retrived object is not the same", ((JLabel)val).getText().equals("my label Y"));
296
297         ObjectRef or2 = ctx.getRef("ref");
298         assertTrue("Retrieved object cannot be null", or2 != null);
299         assertTrue("Retrived object is not the same", or2.getBindingName().equals(or.getBindingName()));
300         assertTrue("Retrived object is not the same", or2.getContextAbsoluteName().equals(or.getContextAbsoluteName()));
301         
302         ObjectRef or3 = ctx.getRef("obj25");
303         assertTrue("Retrieved object must be null", or3 == null);
304         
305         ObjectRef or4 = SpiUtils.createObjectRef(basicCtx, "ref");
306         ctx2.putObject("refref", or4);
307         ObjectRef or5 = SpiUtils.createObjectRef(basicCtx2, "refref");
308         ctx2.putObject("refrefref", or5);
309         val = ctx2.getObject("refrefref", MY_NULL);
310         assertTrue("Retrieved object cannot be null", val != MY_NULL);
311         assertTrue("Retrived object is not the same", val instanceof JLabel);
312         assertTrue("Retrived object is not the same", ((JLabel)val).getText().equals("my label Y"));
313
314         ObjectRef or6 = ctx2.getRef("refrefref");
315         assertTrue("Retrieved object cannot be null", or6 != null);
316         assertTrue("Retrived object is not the same", or6.equals(or5));
317         
318         getRootContext().destroySubcontext("someCOTOXO");
319     }
320     
321
322 // copied from org.netbeans.modules.registry.olddssimpl.Utils.
323
// this can be easily broken if not properly synchronized with changes
324
// in InstanceDataObject, org.netbeans.modules.registry.olddssimpl.Utils and
325
// here.
326
private static final char OPEN = '[';
327     private static final char CLOSE = ']';
328     private static final int MAX_FILENAME_LENGTH = 50;
329     private static String JavaDoc escapeAndCut (String JavaDoc name) {
330         int maxLen = MAX_FILENAME_LENGTH;
331         
332         String JavaDoc ename = escape(name);
333         if (ename.length() <= maxLen) return ename;
334         String JavaDoc hash = Integer.toHexString(ename.hashCode());
335         maxLen = (maxLen > hash.length()) ? (maxLen-hash.length()) / 2 :1;
336         String JavaDoc start = ename.substring(0, maxLen);
337         String JavaDoc end = ename.substring(ename.length() - maxLen);
338
339         return start + hash + end;
340     }
341     
342     private static String JavaDoc escape (String JavaDoc text) {
343         boolean spacenasty = text.startsWith(" ") || text.endsWith(" ") || text.indexOf(" ") != -1; // NOI18N
344
int len = text.length ();
345         StringBuffer JavaDoc escaped = new StringBuffer JavaDoc (len);
346         for (int i = 0; i < len; i++) {
347             char c = text.charAt (i);
348             // For some reason Windoze throws IOException if angle brackets in filename...
349
if (c == '/' || c == ':' || c == '\\' || c == OPEN || c == CLOSE || c == '<' || c == '>' ||
350                     // ...and also for some other chars (#16479):
351
c == '?' || c == '*' || c == '|' ||
352                     (c == ' ' && spacenasty) ||
353                     c == '.' || c == '"' || c < '\u0020' || c > '\u007E' || c == '#') {
354                 // Hex escape.
355
escaped.append ('#');
356                 String JavaDoc hex = Integer.toString (c, 16).toUpperCase ();
357                 if (hex.length () < 4) escaped.append ('0');
358                 if (hex.length () < 3) escaped.append ('0');
359                 if (hex.length () < 2) escaped.append ('0');
360                 escaped.append (hex);
361             } else {
362                 escaped.append (c);
363             }
364         }
365         return escaped.toString ();
366     }
367
368     protected Context getRootContext() {
369         return Context.getDefault();
370     }
371
372     protected FileObject getRoot() {
373         return Repository.getDefault ().getDefaultFileSystem ().getRoot ();
374     }
375     
376     protected FileObject findResource(String JavaDoc resource) {
377         return Repository.getDefault ().getDefaultFileSystem().findResource (resource);
378     }
379 }
380
Popular Tags