1 19 20 package org.netbeans.api.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.junit.NbTestCase; 24 import org.netbeans.junit.NbTestSuite; 25 import org.openide.modules.ModuleInfo; 26 import org.openide.util.Lookup; 27 28 import javax.swing.*; 29 30 35 public class AttributeTest extends NbTestCase { 36 private static final String MY_NULL = new String ("MY_NULL"); 37 38 public AttributeTest (String name) { 39 super (name); 40 } 41 42 public static void main(String [] args) { 43 TestRunner.run(new NbTestSuite(AttributeTest.class)); 44 } 45 46 protected void setUp () throws Exception { 47 Lookup.getDefault().lookup(ModuleInfo.class); 48 } 49 50 51 public void testAttrsOnObjectBinding() throws Exception { 52 getRootContext().putObject("XXXaa11", new Integer (123)); 53 implTestAttrs(getRootContext(), "XXXaa11"); 54 getRootContext().putObject("XXXaa11", null); 55 } 56 57 public void testAttrsOnObjectBinding2() throws Exception { 58 Context ctx = getRootContext().createSubcontext("abcd"); 59 ctx.putObject("XXXaa11", new Integer (123)); 60 implTestAttrs(ctx, "XXXaa11"); 61 getRootContext().destroySubcontext("abcd"); 62 } 63 64 public void testAttrsOnPrimitiveBinding() throws Exception { 65 getRootContext().putInt("aa11", 123); 66 implTestAttrs(getRootContext(), "aa11"); 67 getRootContext().putObject("aa11", null); 68 } 69 70 public void testAttrsOnPrimitiveBinding2() throws Exception { 71 Context ctx = getRootContext().createSubcontext("abcd"); 72 ctx.putInt("aa11", 123); 73 implTestAttrs(ctx, "aa11"); 74 getRootContext().destroySubcontext("abcd"); 75 } 76 77 public void testAttrsOnContext() throws Exception { 78 Context ctx = getRootContext().createSubcontext("abcd"); 79 implTestAttrs(ctx, null); 80 getRootContext().destroySubcontext("abcd"); 81 } 82 83 84 public void implTestAttrs(Context context, String bindingName) throws Exception { 85 String attrName1 = "attr1"; 86 String attrName2 = "attr2"; 87 assertTrue("Attribute should not exist", context.getAttribute(bindingName, attrName1, MY_NULL) == MY_NULL); 88 assertTrue("Attribute should not exist", context.getAttribute(bindingName, attrName2, MY_NULL) == MY_NULL); 89 90 context.setAttribute(bindingName, attrName1, "someValue1111"); 91 assertTrue("Attribute value does not match - " + context.getAttribute(bindingName, attrName1, MY_NULL), "someValue1111".equals(context.getAttribute(bindingName, attrName1, MY_NULL))); 92 93 context.setAttribute(bindingName, attrName2, "someValue"); 94 assertTrue("Attribute value does not match - " + context.getAttribute(bindingName, attrName2, MY_NULL), "someValue".equals(context.getAttribute(bindingName, attrName2, MY_NULL))); 95 96 context.setAttribute(bindingName, attrName1, null); 97 assertTrue("Attribute should not exist", context.getAttribute(bindingName, attrName1, MY_NULL) == MY_NULL); 98 99 context.setAttribute(bindingName, attrName2, null); 100 assertTrue("Attribute should not exist", context.getAttribute(bindingName, attrName2, MY_NULL) == MY_NULL); 101 } 102 103 public void testRemovedBindingAttrs() throws Exception { 104 Context ctx = getRootContext().createSubcontext("spam"); 105 106 ctx.setAttribute("XXXaa11", "A1", "valueOfA1"); 107 assertEquals("Attribute should not exist", "nononono", ctx.getAttribute("XXXaa11", "A1", "nononono")); 108 109 ctx.putObject("XXXaa11", new JLabel("123")); 110 ctx.setAttribute("XXXaa11", "A1", "valueOfA1"); 111 assertEquals("Attribute does not match", "valueOfA1", ctx.getAttribute("XXXaa11", "A1", "nononono")); 112 113 ctx.putObject("XXXaa11", null); 114 assertEquals("Attribute should not exist", "nononono", ctx.getAttribute("XXXaa11", "A1", "nononono")); 115 116 ctx.setAttribute("prim", "at", "va"); 117 assertEquals("Attribute should not exist", "nova", ctx.getAttribute("prim", "at", "nova")); 118 119 ctx.putInt("prim", 1989); 120 ctx.setAttribute("prim", "at", "vava"); 121 assertEquals("Attribute does not match", "vava", ctx.getAttribute("prim", "at", "nova")); 122 123 ctx.putObject("prim", null); 124 assertEquals("Attribute should not exist", "nova", ctx.getAttribute("prim", "at", "nova")); 125 126 getRootContext().destroySubcontext("spam"); 127 } 128 129 protected Context getRootContext () { 130 return Context.getDefault(); 131 } 132 133 } 134 | Popular Tags |