1 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 ; 30 import java.util.Collection ; 31 import java.util.List ; 32 33 public class OrderingTest extends NbTestCase { 34 public OrderingTest(String name) { 35 super (name); 36 } 37 38 public static void main(String [] args) { 39 TestRunner.run(new NbTestSuite(OrderingTest.class)); 40 } 41 42 protected void setUp () throws Exception { 43 Lookup.getDefault().lookup(ModuleInfo.class); 44 } 45 46 public void testOrdering() throws Exception { 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 o1 = new JLabel("bino1"); 56 subctx.putObject("bin1", o1); 57 Object o2 = new JLabel("bino2"); 58 subctx.putObject("bin2", o2); 59 Object o3 = new JLabel("bino3"); 60 subctx.putObject("bin3", o3); 61 Object o4 = new JLabel("bino4"); 62 subctx.putObject("bin4", o4); 63 Object o5 = new JLabel("bino5"); 64 subctx.putObject("bin5", o5); 65 66 Collection c = subctx.getOrderedObjects(); 67 ArrayList ar = new ArrayList (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 newOrder = new ArrayList (); 82 ArrayList newOrderValue = new ArrayList (); 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 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 { 124 Context subctx = getContext().createSubcontext ("sorta_empty"); 125 Collection c = subctx.getOrderedObjects(); 126 c = subctx.getOrderedNames(); 127 getContext().destroySubcontext("sorta_empty"); 128 } 129 130 public void testOrderingNames() throws Exception { 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 o1 = new JLabel("bino1"); 140 subctx.putObject("bin1", o1); 141 Object o2 = new JLabel("bino2"); 142 subctx.putObject("bin2", o2); 143 Object o3 = new JLabel("bino3"); 144 subctx.putObject("bin3", o3); 145 Object o4 = new JLabel("bino4"); 146 subctx.putObject("bin4", o4); 147 Object o5 = new JLabel("bino5"); 148 subctx.putObject("bin5", o5); 149 150 Collection c = subctx.getOrderedNames(); 151 ArrayList ar = new ArrayList (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 newOrder = new ArrayList (); 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 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 { 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 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 c = subctx.getOrderedNames(); 211 ArrayList ar = new ArrayList (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 (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 |