KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > mergedctx > ReusedAttributeTest


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 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 JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashSet JavaDoc;
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 JavaDoc name) {
40         super(name);
41     }
42
43     protected void setUp() throws Exception JavaDoc {
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 JavaDoc bindingName = "myBindingName";
52         final String JavaDoc bindingValue = "myValue";
53
54         final String JavaDoc attrName = "myAttrName";
55         final String JavaDoc attrValue = "myValue";
56         final String JavaDoc defAttrValue = "defBindingValue";
57
58         Object JavaDoc 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 JavaDoc attribs = "cattr1";
84         String JavaDoc 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         /*important tests*/
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 JavaDoc[] attribs = new String JavaDoc[] {"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 JavaDoc original = new HashSet JavaDoc (Arrays.asList(attribs));
111         Collection JavaDoc 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 JavaDoc[] attribs = new String JavaDoc[] {"battr1","battr2","battr3"};
124         String JavaDoc 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 JavaDoc original = new ArrayList JavaDoc();
136         original.add(attribs[0]);
137         Collection JavaDoc 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