1 19 package org.netbeans.api.registry.mergedctx; 20 21 import org.netbeans.api.registry.AttributeTest; 22 import org.netbeans.api.registry.Context; 23 import org.netbeans.api.registry.ContextException; 24 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.HashSet ; 29 30 31 public class ReusedAttributeTest extends AttributeTest { 32 private Context rootCtx; 33 protected Context getRootContext() { 34 if (rootCtx == null) 35 rootCtx = SetUpUtils.getSimpleContext(super.getRootContext()); 36 return rootCtx; 37 } 38 39 public ReusedAttributeTest(String name) { 40 super(name); 41 } 42 43 protected void setUp() throws Exception { 44 rootCtx = null; 45 super.setUp(); 46 getRootContext(); 47 } 48 49 public void testFlipFlap () throws ContextException { 50 Context ctx = getRootContext().createSubcontext("test");; 51 final String bindingName = "myBindingName"; 52 final String bindingValue = "myValue"; 53 54 final String attrName = "myAttrName"; 55 final String attrValue = "myValue"; 56 final String defAttrValue = "defBindingValue"; 57 58 Object tmpObj; 59 60 ctx.putString(bindingName, bindingValue); 61 assertEquals(bindingValue, ctx.getString(bindingName,"")); 62 63 ctx.setAttribute(bindingName, attrName, attrValue); 64 tmpObj = ctx.getAttribute(bindingName, attrName, defAttrValue); 65 assertEquals(attrValue, tmpObj); 66 67 ctx.setAttribute(bindingName, attrName, null); 68 tmpObj = ctx.getAttribute(bindingName, attrName, defAttrValue); 69 assertEquals(defAttrValue, tmpObj); 70 71 ctx.setAttribute(bindingName, attrName, attrValue); 72 tmpObj = ctx.getAttribute(bindingName, attrName, defAttrValue); 73 assertEquals(attrValue, tmpObj); 74 75 ctx.setAttribute(bindingName, attrName, null); 76 tmpObj = ctx.getAttribute(bindingName, attrName, defAttrValue); 77 assertEquals(defAttrValue, tmpObj); 78 79 } 80 81 public void testCopyAttribs () throws ContextException { 82 Context rCtx = SetUpUtils.getSubctx2(); 83 String attribs = "cattr1"; 84 String binding = "cbinding"; 85 86 rCtx.putString(binding, binding); 87 rCtx.setAttribute(binding, attribs, attribs); 88 assertEquals(attribs, rCtx.getAttribute(binding, attribs, "defValue")); 89 assertEquals(attribs, getRootContext().getAttribute(binding, attribs, "defValue")); 90 91 92 93 getRootContext().putString(binding, binding); 94 assertEquals(attribs, getRootContext().getAttribute(binding, attribs, "defValue")); 95 rCtx.putString(binding, null); 96 assertEquals(attribs, getRootContext().getAttribute(binding, attribs, "defValue")); 97 } 98 99 public void testAttribsForCtx () throws ContextException { 100 Context ctx1 = SetUpUtils.getSubctx1(); 101 Context ctx2 = SetUpUtils.getSubctx2(); 102 Context ctx3 = SetUpUtils.getSubctx3(); 103 String [] attribs = new String [] {"mattr1","mattr2","mattr3"}; 104 105 ctx1.setAttribute(null,attribs[0],attribs[0]); 106 ctx2.setAttribute(null,attribs[1],attribs[1]); 107 ctx3.setAttribute(null,attribs[2],attribs[2]); 108 109 Context ctx = getRootContext(); 110 Collection original = new HashSet (Arrays.asList(attribs)); 111 Collection names = ctx.getAttributeNames(null); 112 original.removeAll(names); 113 assertTrue (original.size() == 0); 114 assertEquals(attribs[0], ctx.getAttribute(null, attribs[0], "defValue")); 115 assertEquals(attribs[1], ctx.getAttribute(null, attribs[1], "defValue")); 116 assertEquals(attribs[2], ctx.getAttribute(null, attribs[2], "defValue")); 117 } 118 119 public void testAttribsForBinding () throws ContextException { 120 Context ctx1 = SetUpUtils.getSubctx1(); 121 Context ctx2 = SetUpUtils.getSubctx2(); 122 Context ctx3 = SetUpUtils.getSubctx3(); 123 String [] attribs = new String [] {"battr1","battr2","battr3"}; 124 String binding = "bbinding"; 125 126 ctx1.putString(binding, binding); 127 ctx2.putString(binding, binding); 128 ctx3.putString(binding, binding); 129 130 ctx1.setAttribute(binding,attribs[0],attribs[0]); 131 ctx2.setAttribute(binding,attribs[1],attribs[1]); 132 ctx3.setAttribute(binding,attribs[2],attribs[2]); 133 134 Context ctx = getRootContext(); 135 Collection original = new ArrayList (); 136 original.add(attribs[0]); 137 Collection names = ctx.getAttributeNames(binding); 138 original.removeAll(names); 139 assertTrue(names.size() == 1); 140 assertTrue (original.size() == 0); 141 assertEquals(attribs[0], ctx.getAttribute(binding, attribs[0], "defValue")); 142 assertEquals("defValue", ctx.getAttribute(binding, attribs[1], "defValue")); 143 assertEquals("defValue", ctx.getAttribute(binding, attribs[2], "defValue")); 144 } 145 146 147 } 148 | Popular Tags |