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.Collection ; 30 import java.util.Iterator ; 31 32 37 public class EnumerationTest extends NbTestCase { 38 public EnumerationTest (String name) { 39 super (name); 40 } 41 42 public static void main(String [] args) { 43 TestRunner.run(new NbTestSuite(EnumerationTest.class)); 44 } 45 46 protected void setUp () throws Exception { 47 Lookup.getDefault().lookup(ModuleInfo.class); 48 } 49 50 public void testSubcontextNames() throws Exception { 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 { 57 context.createSubcontext("aa11"); 58 context.createSubcontext("aa22"); 59 context.createSubcontext("aa33"); 60 context.createSubcontext("aa44"); 61 Collection coll = context.getSubcontextNames(); 62 assertTrue("Collection should not contain any other subcontexts - "+coll.size(), coll.size() == 4); 63 Iterator it = coll.iterator(); 64 while (it.hasNext()) { 65 String s = (String )it.next(); 66 if (s.equals("aa11") || s.equals("aa22") || s.equals("aa33") || s.equals("aa44")) { 67 } 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 { 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 { 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 coll = context.getBindingNames(); 90 assertTrue("Collection should not contain any other bindings - "+coll.size(), coll.size() == 4); 91 Iterator it = coll.iterator(); 92 while (it.hasNext()) { 93 String s = (String )it.next(); 94 if (s.equals("aa11") || s.equals("aa22") || s.equals("aa33") || s.equals("aa44")) { 95 } 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 { 107 Context ctx = getRootContext().createSubcontext("aaa1_/bbb2_/ccc3_"); 108 implAttributeNames(null, ctx); 109 getRootContext().destroySubcontext("aaa1_"); 110 } 111 112 public void testAttributeNames1() throws Exception { 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 { 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 bindingName, Context context) throws Exception { 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 coll = context.getAttributeNames(bindingName); 132 int count = 4; 133 if (bindingName == null) { 134 count++; 136 } 137 assertTrue("Number of elements in collection is different.", coll.size() == count); 138 Iterator it = coll.iterator(); 139 while (it.hasNext()) { 140 String s = (String )it.next(); 141 if (s.equals("aa11") || s.equals("aa12") || s.equals("aa13") || s.equals("aa14") || s.equals("default.context.sorting")) { 142 } 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 { 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 coll = context.getAttributeNames(null); 173 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 |