KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  *
34  * @author Vitezslav Stejskal
35  * @author David Konecny
36  */

37 public class EnumerationTest extends NbTestCase {
38     public EnumerationTest (String JavaDoc name) {
39         super (name);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(new NbTestSuite(EnumerationTest.class));
44     }
45     
46     protected void setUp () throws Exception JavaDoc {
47         Lookup.getDefault().lookup(ModuleInfo.class);
48     }
49     
50     public void testSubcontextNames() throws Exception JavaDoc {
51         Context ctx = getRootContext().createSubcontext("aa/bb/cc");
52         implSubcontextNames(ctx);
53         getRootContext().destroySubcontext("aa");
54     }
55     
56     public void implSubcontextNames(Context context) throws Exception JavaDoc {
57         context.createSubcontext("aa11");
58         context.createSubcontext("aa22");
59         context.createSubcontext("aa33");
60         context.createSubcontext("aa44");
61         Collection JavaDoc coll = context.getSubcontextNames();
62         assertTrue("Collection should not contain any other subcontexts - "+coll.size(), coll.size() == 4);
63         Iterator JavaDoc it = coll.iterator();
64         while (it.hasNext()) {
65             String JavaDoc s = (String JavaDoc)it.next();
66             if (s.equals("aa11") || s.equals("aa22") || s.equals("aa33") || s.equals("aa44")) {
67                 // OK
68
} else {
69                 assertTrue("Collection contains unknown subcontext "+s, false);
70             }
71         }
72         context.destroySubcontext("aa11");
73         context.destroySubcontext("aa22");
74         context.destroySubcontext("aa33");
75         context.destroySubcontext("aa44");
76     }
77     
78     public void testBindingNames() throws Exception JavaDoc {
79         Context ctx = getRootContext().createSubcontext("aaa1/bbb2/ccc3");
80         implBindingNames(ctx);
81         getRootContext().destroySubcontext("aaa1");
82     }
83     
84     public void implBindingNames(Context context) throws Exception JavaDoc {
85         context.putObject("aa11", new JLabel("123"));
86         context.putObject("aa22", new JLabel("12"));
87         context.putObject("aa33", new JLabel("1"));
88         context.putObject("aa44", new JLabel("1234"));
89         Collection JavaDoc coll = context.getBindingNames();
90         assertTrue("Collection should not contain any other bindings - "+coll.size(), coll.size() == 4);
91         Iterator JavaDoc it = coll.iterator();
92         while (it.hasNext()) {
93             String JavaDoc s = (String JavaDoc)it.next();
94             if (s.equals("aa11") || s.equals("aa22") || s.equals("aa33") || s.equals("aa44")) {
95                 // OK
96
} else {
97                 assertTrue("Collection contains unknown binding "+s, false);
98             }
99         }
100         context.putObject("aa11", null);
101         context.putObject("aa22", null);
102         context.putObject("aa33", null);
103         context.putObject("aa44", null);
104     }
105     
106     public void testAttributeNames() throws Exception JavaDoc {
107         Context ctx = getRootContext().createSubcontext("aaa1_/bbb2_/ccc3_");
108         implAttributeNames(null, ctx);
109         getRootContext().destroySubcontext("aaa1_");
110     }
111     
112     public void testAttributeNames1() throws Exception JavaDoc {
113         Context ctx = getRootContext().createSubcontext("aaa1__/bbb2_/ccc3_");
114         ctx.putObject("someB", new JLabel("labelo"));
115         implAttributeNames("someB", ctx);
116         getRootContext().destroySubcontext("aaa1__");
117     }
118     
119     public void testAttributeNames2() throws Exception JavaDoc {
120         Context ctx = getRootContext().createSubcontext("aaa1___/bbb2_/ccc3_");
121         ctx.putInt("someI", 654);
122         implAttributeNames("someI", ctx);
123         getRootContext().destroySubcontext("aaa1___");
124     }
125     
126     public void implAttributeNames(String JavaDoc bindingName, Context context) throws Exception JavaDoc {
127         context.setAttribute(bindingName, "aa11", "123");
128         context.setAttribute(bindingName, "aa12", "123");
129         context.setAttribute(bindingName, "aa13", "123");
130         context.setAttribute(bindingName, "aa14", "123");
131         Collection JavaDoc coll = context.getAttributeNames(bindingName);
132         int count = 4;
133         if (bindingName == null) {
134             // each context has in addition artificial Context.DEFAULT_SORTING attribute.
135
count++;
136         }
137         assertTrue("Number of elements in collection is different.", coll.size() == count);
138         Iterator JavaDoc it = coll.iterator();
139         while (it.hasNext()) {
140             String JavaDoc s = (String JavaDoc)it.next();
141             if (s.equals("aa11") || s.equals("aa12") || s.equals("aa13") || s.equals("aa14") || s.equals("default.context.sorting")) {
142                 // OK
143
} else {
144                 assertTrue("Collection contains unknown binding "+s, false);
145             }
146         }
147         context.setAttribute(bindingName, "aa11", null);
148         context.setAttribute(bindingName, "aa12", null);
149         context.setAttribute(bindingName, "aa13", null);
150         context.setAttribute(bindingName, "aa14", null);
151     }
152     
153     public void testMixedAttributeNames() throws Exception JavaDoc {
154         Context context = getRootContext().createSubcontext("soso/bbb2_/ccc3_");
155         context.putInt("someI", 654);
156         context.setAttribute("someI", "aa11", "123");
157         context.setAttribute("someI", "aa12", "123");
158         context.setAttribute("someI", "bb13", "123");
159         context.setAttribute("someI", "bb14", "123");
160         
161         context.putObject("someO", new JLabel("labelo labelo"));
162         context.setAttribute("someO", "aa11", "123");
163         context.setAttribute("someO", "aa12", "123");
164         context.setAttribute("someO", "cc13", "123");
165         context.setAttribute("someO", "cc14", "123");
166         
167         context.setAttribute(null, "aa11", "123");
168         context.setAttribute(null, "aa12", "123");
169         context.setAttribute(null, "dd13", "123");
170         context.setAttribute(null, "dd14", "123");
171         
172         Collection JavaDoc coll = context.getAttributeNames(null);
173         // each context has in addition artificial Context.DEFAULT_SORTING attribute.
174
assertTrue("Collection should contain 5 attrs, but has "+coll.size(), coll.size() == 5);
175         coll = context.getAttributeNames("someO");
176         assertTrue("Collection should contain 4 attrs, but has "+coll.size(), coll.size() == 4);
177         coll = context.getAttributeNames("someI");
178         assertTrue("Collection should contain 4 attrs, but has "+coll, coll.size() == 4);
179         
180         getRootContext().destroySubcontext("soso");
181     }
182
183     protected Context getRootContext () {
184         return Context.getDefault();
185     }
186     
187 }
188
Popular Tags