KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > 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.core.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.api.registry.Context;
24 import org.netbeans.api.registry.ObjectRef;
25 import org.netbeans.api.registry.fs.FileSystemContextFactory;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.junit.NbTestSuite;
28 import org.netbeans.spi.registry.BasicContext;
29 import org.netbeans.spi.registry.SpiUtils;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.LocalFileSystem;
33 import org.openide.filesystems.XMLFileSystem;
34
35 import javax.swing.*;
36 import java.awt.*;
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.lang.ref.Reference JavaDoc;
40 import java.lang.ref.ReferenceQueue JavaDoc;
41 import java.lang.ref.WeakReference JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.Collection JavaDoc;
44
45 /**
46  *
47  * @author David Konecny
48  */

49 public class BindingTest extends NbTestCase {
50     
51     private Context rootContext = null;
52     private FileObject root = null;
53     
54     private static final String JavaDoc MY_NULL = new String JavaDoc("MY_NULL");
55
56     public BindingTest(String JavaDoc name) {
57         super (name);
58     }
59     
60     public static void main(String JavaDoc[] args) {
61         TestRunner.run(new NbTestSuite(BindingTest.class));
62     }
63     
64     protected void setUp () throws Exception JavaDoc {
65     }
66     
67     public void testBasicBindingOperations() throws Exception JavaDoc {
68         URL JavaDoc u1 = getClass().getResource("data/layer_defaults.xml");
69                
70         FileSystem xfs1 = new XMLFileSystem( u1 );
71         LocalFileSystem lfs = new LocalFileSystem();
72         lfs.setRootDirectory(getWorkDir());
73         FileSystem mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } );
74     
75         BasicContext rootCtx = FileSystemContextFactory.createContext(mfs.getRoot());
76         Context root = SpiUtils.createContext(rootCtx);
77         Context ctx = root.getSubcontext("contextX");
78
79         Collection JavaDoc coll = ctx.getBindingNames();
80         assertEquals("Number of bindings is not equal", 4, coll.size());
81         assertTrue("Binding 'objectBindingOne' is missing", coll.contains("objectBindingOne"));
82         assertTrue("Binding 'objectBindingTwo' is missing", coll.contains("objectBindingTwo"));
83         assertTrue("Binding 'primitiveBindingOne' is missing", coll.contains("primitiveBindingOne"));
84         assertTrue("Binding 'primitiveBindingTwo' is missing", coll.contains("primitiveBindingTwo"));
85         
86         coll = ctx.getAttributeNames(null);
87         assertEquals("Number of context attributes is not equal", 2, coll.size());
88         assertTrue("Attribute 'contextXAttribute' is missing", coll.contains("contextXAttribute"));
89         assertTrue("Attribute 'default.context.sorting' is missing", coll.contains("default.context.sorting"));
90         
91         coll = ctx.getAttributeNames("objectBindingOne");
92         assertEquals("Number of binding 'objectBindingOne' attributes is not equal", 1, coll.size());
93         assertTrue("Attribute 'objectBindingOneAttribute' is missing", coll.contains("objectBindingOneAttribute"));
94         
95         coll = ctx.getAttributeNames("objectBindingTwo");
96         assertEquals("Number of binding 'objectBindingTwo' attributes is not equal", 1, coll.size());
97         assertTrue("Attribute 'objectBindingTwoAttribute' is missing", coll.contains("objectBindingTwoAttribute"));
98         
99         coll = ctx.getAttributeNames("primitiveBindingOne");
100         assertEquals("Number of binding 'primitiveBindingOne' attributes is not equal", 1, coll.size());
101         assertTrue("Attribute 'primitiveBindingOneAttribute' is missing", coll.contains("primitiveBindingOneAttribute"));
102         
103         coll = ctx.getAttributeNames("primitiveBindingTwo");
104         assertEquals("Number of binding 'primitiveBindingTwo' attributes is not equal", 1, coll.size());
105         assertTrue("Attribute 'primitiveBindingTwoAttribute' is missing", coll.contains("primitiveBindingTwoAttribute"));
106                 
107         
108         ctx = root.getSubcontext("contextY");
109         String JavaDoc ss = ctx.getString("bndString", "nono");
110         assertEquals("string val", ss);
111         
112         int ii = ctx.getInt("bndInteger", 25);
113         assertEquals(1984, ii);
114         
115         long ll = ctx.getLong("bndLong", 25L);
116         assertTrue("Value does not match: "+ll, 19841984L == ll);
117         
118         float ff = ctx.getFloat("bndFloat", 25F);
119         assertTrue("Value does not match: "+ff, 1112.1112F == ff);
120         
121         boolean bb = ctx.getBoolean("bndBoolean", false);
122         assertEquals(true, bb);
123         
124         URL JavaDoc uu = ctx.getURL("bndURL", null);
125         assertEquals(new URL JavaDoc("http://www.netbeans.org/download/"), uu);
126         
127         Color cc = ctx.getColor("bndColor", null);
128         assertEquals(new Color(25000), cc);
129         
130         Font ft = ctx.getFont("bndFont", null);
131         assertEquals(new Font("Arial", Font.BOLD | Font.ITALIC, 15), ft);
132
133         ObjectRef or = SpiUtils.createObjectRef(rootCtx, "/contextX", "primitiveBindingOne");
134         ctx.putObject("ref", or);
135         or = null;
136         System.gc(); System.gc(); System.gc(); System.gc(); System.gc();
137         or = ctx.getRef("ref");
138         assertEquals("Objects must be equals", SpiUtils.createObjectRef(rootCtx, "/contextX", "primitiveBindingOne"), or);
139         
140         ctx = root.getSubcontext("contextREF");
141         or = ctx.getRef("ref");
142         assertEquals("Objects must be equals", SpiUtils.createObjectRef(rootCtx, "/contextZ", "objectBindingOne"), or);
143         Object JavaDoc o = ctx.getObject("ref", null);
144         o = ctx.getObject("ref", null);
145         assertNotNull(o);
146         assertEquals("Objects must be equals", new String JavaDoc(), o);
147
148         or = ctx.getRef("ref2");
149         assertEquals("Objects must be equals", SpiUtils.createObjectRef(rootCtx, "/contextZ", "primitiveBindingOne"), or);
150         o = ctx.getObject("ref2", null);
151         assertNotNull(o);
152         assertEquals("Objects must be equals", "primitiveBindingOneValue", o);
153     }
154     
155     public void XXX_failing_testBindingAttrs() throws Exception JavaDoc {
156         LocalFileSystem lfs = new LocalFileSystem();
157         lfs.setRootDirectory(getWorkDir());
158         FileSystem mfs = new TestMFS( new FileSystem[] { lfs } );
159     
160         BasicContext rootCtx = FileSystemContextFactory.createContext(mfs.getRoot());
161         Context root = SpiUtils.createContext(rootCtx);
162         Context ctx = root.createSubcontext("cotoxo");
163         
164         ctx.putObject("test", new JLabel("labelo"));
165         ctx.setAttribute("test", "attr1", "some value");
166         ctx.setAttribute("test", "attr2", "some value");
167         assertEquals("There must be two attributes", 2, ctx.getAttributeNames("test").size());
168         ctx.putObject("test", "labelo");
169         assertEquals("There must be two attributes", 2, ctx.getAttributeNames("test").size());
170         ctx.setAttribute("test", "attr3", "some value");
171         ctx.setAttribute("test", "attr4", "some value");
172         assertEquals("There must be two attributes", 4, ctx.getAttributeNames("test").size());
173         ctx.putObject("test", new JLabel("labelo"));
174         assertEquals("There must be two attributes", 4, ctx.getAttributeNames("test").size());
175     }
176     
177     public void testBindingGCed() throws Exception JavaDoc {
178         URL JavaDoc u1 = getClass().getResource("data/layer_defaults.xml");
179         File JavaDoc tmpDir = getWorkDir();
180         
181         FileSystem xfs1 = new XMLFileSystem( u1 );
182         LocalFileSystem lfs1 = new LocalFileSystem();
183         lfs1.setRootDirectory(tmpDir);
184         
185         FileSystem mfs = new TestMFS( new FileSystem[] { lfs1, xfs1 } );
186     
187         BasicContext rootCtx = FileSystemContextFactory.createContext(mfs.getRoot());
188         Context root = SpiUtils.createContext(rootCtx);
189         Context ctx = root.getSubcontext("contextX");
190         
191         Integer JavaDoc objInt = new Integer JavaDoc(123);
192         ReferenceQueue JavaDoc rqPobj = new ReferenceQueue JavaDoc ();
193         WeakReference JavaDoc wrPobj = new WeakReference JavaDoc(objInt, rqPobj);
194
195         ctx.putObject("myBinding", objInt);
196         
197         // let PM be GCed
198
objInt = null;
199         for (int i = 0; i < 10; i++) {
200             if (i == 4) {
201                 Thread.sleep (1000);
202             }
203             System.gc ();
204         }
205         
206         Reference JavaDoc rPobj = rqPobj.poll ();
207         assertNotNull("Bound object has not been GCed", rPobj);
208     }
209
210 }
211
Popular Tags