1 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 ; 38 import java.io.IOException ; 39 import java.lang.ref.Reference ; 40 import java.lang.ref.ReferenceQueue ; 41 import java.lang.ref.WeakReference ; 42 import java.net.URL ; 43 import java.util.Collection ; 44 45 49 public class BindingTest extends NbTestCase { 50 51 private Context rootContext = null; 52 private FileObject root = null; 53 54 private static final String MY_NULL = new String ("MY_NULL"); 55 56 public BindingTest(String name) { 57 super (name); 58 } 59 60 public static void main(String [] args) { 61 TestRunner.run(new NbTestSuite(BindingTest.class)); 62 } 63 64 protected void setUp () throws Exception { 65 } 66 67 public void testBasicBindingOperations() throws Exception { 68 URL 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 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 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 uu = ctx.getURL("bndURL", null); 125 assertEquals(new URL ("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 o = ctx.getObject("ref", null); 144 o = ctx.getObject("ref", null); 145 assertNotNull(o); 146 assertEquals("Objects must be equals", new String (), 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 { 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 { 178 URL u1 = getClass().getResource("data/layer_defaults.xml"); 179 File 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 objInt = new Integer (123); 192 ReferenceQueue rqPobj = new ReferenceQueue (); 193 WeakReference wrPobj = new WeakReference (objInt, rqPobj); 194 195 ctx.putObject("myBinding", objInt); 196 197 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 rPobj = rqPobj.poll (); 207 assertNotNull("Bound object has not been GCed", rPobj); 208 } 209 210 } 211 | Popular Tags |