KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > AttributeTest


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.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 /**
31  *
32  * @author Vitezslav Stejskal
33  * @author David Konecny
34  */

35 public class AttributeTest extends NbTestCase {
36     private static final String JavaDoc MY_NULL = new String JavaDoc("MY_NULL");
37
38     public AttributeTest (String JavaDoc name) {
39         super (name);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(new NbTestSuite(AttributeTest.class));
44     }
45     
46     protected void setUp () throws Exception JavaDoc {
47         Lookup.getDefault().lookup(ModuleInfo.class);
48     }
49     
50
51     public void testAttrsOnObjectBinding() throws Exception JavaDoc {
52         getRootContext().putObject("XXXaa11", new Integer JavaDoc(123));
53         implTestAttrs(getRootContext(), "XXXaa11");
54         getRootContext().putObject("XXXaa11", null);
55     }
56     
57     public void testAttrsOnObjectBinding2() throws Exception JavaDoc {
58         Context ctx = getRootContext().createSubcontext("abcd");
59         ctx.putObject("XXXaa11", new Integer JavaDoc(123));
60         implTestAttrs(ctx, "XXXaa11");
61         getRootContext().destroySubcontext("abcd");
62     }
63     
64     public void testAttrsOnPrimitiveBinding() throws Exception JavaDoc {
65         getRootContext().putInt("aa11", 123);
66         implTestAttrs(getRootContext(), "aa11");
67         getRootContext().putObject("aa11", null);
68     }
69     
70     public void testAttrsOnPrimitiveBinding2() throws Exception JavaDoc {
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 JavaDoc {
78         Context ctx = getRootContext().createSubcontext("abcd");
79         implTestAttrs(ctx, null);
80         getRootContext().destroySubcontext("abcd");
81     }
82     
83     
84     public void implTestAttrs(Context context, String JavaDoc bindingName) throws Exception JavaDoc {
85         String JavaDoc attrName1 = "attr1";
86         String JavaDoc 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 JavaDoc {
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