KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.List JavaDoc;
32
33 public class OrderingTest extends NbTestCase {
34     public OrderingTest(String JavaDoc name) {
35         super (name);
36     }
37
38     public static void main(String JavaDoc[] args) {
39         TestRunner.run(new NbTestSuite(OrderingTest.class));
40     }
41     
42     protected void setUp () throws Exception JavaDoc {
43         Lookup.getDefault().lookup(ModuleInfo.class);
44     }
45     
46     public void testOrdering() throws Exception JavaDoc {
47         Context subctx = getContext().createSubcontext ("sorta");
48         
49         Context sub1 = subctx.createSubcontext("subo1");
50         Context sub2 = subctx.createSubcontext("subo2");
51         Context sub3 = subctx.createSubcontext("subo3");
52         Context sub4 = subctx.createSubcontext("subo4");
53         Context sub5 = subctx.createSubcontext("subo5");
54         
55         Object JavaDoc o1 = new JLabel("bino1");
56         subctx.putObject("bin1", o1);
57         Object JavaDoc o2 = new JLabel("bino2");
58         subctx.putObject("bin2", o2);
59         Object JavaDoc o3 = new JLabel("bino3");
60         subctx.putObject("bin3", o3);
61         Object JavaDoc o4 = new JLabel("bino4");
62         subctx.putObject("bin4", o4);
63         Object JavaDoc o5 = new JLabel("bino5");
64         subctx.putObject("bin5", o5);
65         
66         Collection JavaDoc c = subctx.getOrderedObjects();
67         // nothing known about order, but all objects must be there
68
ArrayList JavaDoc ar = new ArrayList JavaDoc(c);
69         assertTrue("Element not found in the ordered list.", ar.remove(sub1));
70         assertTrue("Element not found in the ordered list.", ar.remove(sub2));
71         assertTrue("Element not found in the ordered list.", ar.remove(sub3));
72         assertTrue("Element not found in the ordered list.", ar.remove(sub4));
73         assertTrue("Element not found in the ordered list.", ar.remove(sub5));
74         assertTrue("Element not found in the ordered list.", ar.remove(o1));
75         assertTrue("Element not found in the ordered list.", ar.remove(o2));
76         assertTrue("Element not found in the ordered list.", ar.remove(o3));
77         assertTrue("Element not found in the ordered list.", ar.remove(o4));
78         assertTrue("Element not found in the ordered list.", ar.remove(o5));
79         assertTrue("The collection must be empty now: "+ar, ar.size() == 0);
80         
81         ArrayList JavaDoc newOrder = new ArrayList JavaDoc();
82         ArrayList JavaDoc newOrderValue = new ArrayList JavaDoc();
83         newOrder.add("bin5");
84         newOrderValue.add(o5);
85         newOrder.add("subo5/");
86         newOrderValue.add(sub5);
87         newOrder.add("subo2/");
88         newOrderValue.add(sub2);
89         newOrder.add("bin2");
90         newOrderValue.add(o2);
91         newOrder.add("subo1/");
92         newOrderValue.add(sub1);
93         newOrder.add("subo3/");
94         newOrderValue.add(sub3);
95         newOrder.add("bin3");
96         newOrderValue.add(o3);
97         newOrder.add("bin1");
98         newOrderValue.add(o1);
99         newOrder.add("bin4");
100         newOrderValue.add(o4);
101         newOrder.add("subo4/");
102         newOrderValue.add(sub4);
103         
104         subctx.orderContext(newOrder);
105         List JavaDoc l = subctx.getOrderedObjects();
106         assertTrue("The number of returned items must be the same.", l.size() == newOrderValue.size());
107         for (int i=0; i<l.size(); i++) {
108             assertTrue("Element not found in the ordered list: "+i+". "+
109                 l.get(i)+"] != ["+newOrderValue.get(i)+"]", l.get(i).equals(newOrderValue.get(i)));
110         }
111         
112         newOrder.remove(6);
113         newOrder.remove(6);
114         newOrder.remove(6);
115         newOrder.remove(6);
116         subctx.orderContext(newOrder);
117         l = subctx.getOrderedObjects();
118         assertTrue("The number of returned items must be 10.", l.size() == 10);
119         
120         getContext().destroySubcontext("sorta");
121     }
122     
123     public void testOrderingEmptyContext() throws Exception JavaDoc {
124         Context subctx = getContext().createSubcontext ("sorta_empty");
125         Collection JavaDoc c = subctx.getOrderedObjects();
126         c = subctx.getOrderedNames();
127         getContext().destroySubcontext("sorta_empty");
128     }
129     
130     public void testOrderingNames() throws Exception JavaDoc {
131         Context subctx = getContext().createSubcontext ("sortanama");
132         
133         Context sub1 = subctx.createSubcontext("subo1");
134         Context sub2 = subctx.createSubcontext("subo2");
135         Context sub3 = subctx.createSubcontext("subo3");
136         Context sub4 = subctx.createSubcontext("subo4");
137         Context sub5 = subctx.createSubcontext("subo5");
138         
139         Object JavaDoc o1 = new JLabel("bino1");
140         subctx.putObject("bin1", o1);
141         Object JavaDoc o2 = new JLabel("bino2");
142         subctx.putObject("bin2", o2);
143         Object JavaDoc o3 = new JLabel("bino3");
144         subctx.putObject("bin3", o3);
145         Object JavaDoc o4 = new JLabel("bino4");
146         subctx.putObject("bin4", o4);
147         Object JavaDoc o5 = new JLabel("bino5");
148         subctx.putObject("bin5", o5);
149         
150         Collection JavaDoc c = subctx.getOrderedNames();
151         // nothing known about order, but all objects must be there
152
ArrayList JavaDoc ar = new ArrayList JavaDoc(c);
153         assertTrue("Element not found in the ordered list.", ar.remove("subo1/"));
154         assertTrue("Element not found in the ordered list.", ar.remove("subo2/"));
155         assertTrue("Element not found in the ordered list.", ar.remove("subo3/"));
156         assertTrue("Element not found in the ordered list.", ar.remove("subo4/"));
157         assertTrue("Element not found in the ordered list.", ar.remove("subo5/"));
158         assertTrue("Element not found in the ordered list.", ar.remove("bin1"));
159         assertTrue("Element not found in the ordered list.", ar.remove("bin2"));
160         assertTrue("Element not found in the ordered list.", ar.remove("bin3"));
161         assertTrue("Element not found in the ordered list.", ar.remove("bin4"));
162         assertTrue("Element not found in the ordered list.", ar.remove("bin5"));
163         assertTrue("The collection must be empty now: "+ar, ar.size() == 0);
164         
165         ArrayList JavaDoc newOrder = new ArrayList JavaDoc();
166         newOrder.add("bin5");
167         newOrder.add("subo5/");
168         newOrder.add("subo2/");
169         newOrder.add("bin2");
170         newOrder.add("subo1/");
171         newOrder.add("subo3/");
172         newOrder.add("bin3");
173         newOrder.add("bin1");
174         newOrder.add("bin4");
175         newOrder.add("subo4/");
176         
177         subctx.orderContext(newOrder);
178         List JavaDoc l = subctx.getOrderedNames();
179         assertTrue("The number of returned items must be the same.", l.size() == newOrder.size());
180         for (int i=0; i<l.size(); i++) {
181             assertTrue("Element not found in the ordered list: "+i+". ["+
182                 l.get(i)+"] != ["+newOrder.get(i)+"]", l.get(i).equals(newOrder.get(i)));
183         }
184         
185         newOrder.remove(6);
186         newOrder.remove(6);
187         newOrder.remove(6);
188         newOrder.remove(6);
189         subctx.orderContext(newOrder);
190         l = subctx.getOrderedNames();
191         assertTrue("The number of returned items must be 10.", l.size() == 10);
192         
193         getContext().destroySubcontext("sortanama");
194     }
195
196     public void testPositionalAttrs() throws Exception JavaDoc {
197         Context subctx = getContext().createSubcontext ("sortapos");
198         
199         Context sub1 = subctx.createSubcontext("subo1");
200         sub1.setAttribute(null, "position", "10.5");
201         Context sub2 = subctx.createSubcontext("subo2");
202         sub2.setAttribute(null, "position", "10.49");
203         
204         Object JavaDoc o1 = new JLabel("bino1");
205         subctx.putObject("bin1", o1);
206         subctx.setAttribute("bin1", "position", "10.48");
207         subctx.putObject("bin2", "someval");
208         subctx.setAttribute("bin2", "position", "1");
209         
210         Collection JavaDoc c = subctx.getOrderedNames();
211         ArrayList JavaDoc ar = new ArrayList JavaDoc(c);
212         assertTrue("Element not found in the ordered list.", ar.remove("bin2"));
213         assertTrue("Element not found in the ordered list.", ar.remove("bin1"));
214         assertTrue("Element not found in the ordered list.", ar.remove("subo2/"));
215         assertTrue("Element not found in the ordered list.", ar.remove("subo1/"));
216         assertTrue("The collection must be empty now: "+ar, ar.size() == 0);
217         
218         subctx.setAttribute("bin2", "position", "10.482");
219         c = subctx.getOrderedNames();
220         ar = new ArrayList JavaDoc(c);
221         assertTrue("Element not found in the ordered list.", ar.remove("bin1"));
222         assertTrue("Element not found in the ordered list.", ar.remove("bin2"));
223         assertTrue("Element not found in the ordered list.", ar.remove("subo2/"));
224         assertTrue("Element not found in the ordered list.", ar.remove("subo1/"));
225         assertTrue("The collection must be empty now: "+ar, ar.size() == 0);
226
227         getContext().destroySubcontext("sortapos");
228     }
229     
230     protected Context getContext() {
231         return Context.getDefault();
232     }
233 }
234
Popular Tags