1 package org.objectweb.celtix.bus.configuration.spring; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import junit.framework.TestCase; 7 8 import org.easymock.EasyMock; 9 import org.objectweb.celtix.configuration.Configuration; 10 11 public class BeanNameTest extends TestCase { 12 13 public void testNormalisation() { 14 15 String n = "*.*abc*efg...hij.klm***nop.*.qrs.?.xyz"; 16 String nn = "*abc*efg.hij.klm*nop*qrs.?.xyz"; 17 18 String nbn; 19 nbn = new BeanName((String )null, true).getNormalisedName(); 20 assertEquals(nbn, null, nbn); 21 nbn = new BeanName("", true).getNormalisedName(); 22 assertEquals(nbn, "", nbn); 23 nbn = new BeanName("?", true).getNormalisedName(); 24 assertEquals(nbn, "?", nbn); 25 nbn = new BeanName(".", true).getNormalisedName(); 26 assertEquals(nbn, ".", nbn); 27 nbn = new BeanName("*", true).getNormalisedName(); 28 assertEquals(nbn, "*", nbn); 29 nbn = new BeanName("a", true).getNormalisedName(); 30 assertEquals(nbn, "a", nbn); 31 nbn = new BeanName("a.", true).getNormalisedName(); 32 assertEquals(nbn, "a.", nbn); 33 nbn = new BeanName("a.b**", true).getNormalisedName(); 34 assertEquals(nbn, "a.b*", nbn); 35 nbn = new BeanName("{a.b**}", true).getNormalisedName(); 36 assertEquals(nbn, "{a.b**}", nbn); 37 nbn = new BeanName("x.{a.b**}", true).getNormalisedName(); 38 assertEquals(nbn, "x.{a.b**}", nbn); 39 nbn = new BeanName("*{a.b**}", true).getNormalisedName(); 40 assertEquals(nbn, "*{a.b**}", nbn); 41 nbn = new BeanName("*{a.b**}x.y", true).getNormalisedName(); 42 assertEquals(nbn, "*{a.b**}x.y", nbn); 43 44 nbn = new BeanName(n, true).getNormalisedName(); 45 assertEquals(nbn, nn, nbn); 46 47 BeanName bn = new BeanName(n, false); 48 assertNull(bn.getNormalisedName()); 49 assertEquals(n, bn.getName()); 50 bn.normalise(); 51 assertEquals(nn, bn.getNormalisedName()); 52 assertEquals(n, bn.getName()); 53 54 bn = new BeanName(n, true); 55 assertEquals(n, bn.getName()); 56 assertEquals(nn, bn.getNormalisedName()); 57 bn.normalise(); 58 assertEquals(n, bn.getName()); 59 assertEquals(nn, bn.getNormalisedName()); 60 } 61 62 public void testConstruction() { 63 Configuration bottom = EasyMock.createMock(Configuration.class); 64 Configuration middle = EasyMock.createMock(Configuration.class); 65 Configuration top = EasyMock.createMock(Configuration.class); 66 67 bottom.getId(); 68 EasyMock.expectLastCall().andReturn("a"); 69 bottom.getParent(); 70 EasyMock.expectLastCall().andReturn(middle); 71 middle.getId(); 72 EasyMock.expectLastCall().andReturn("b"); 73 middle.getParent(); 74 EasyMock.expectLastCall().andReturn(top); 75 top.getId(); 76 EasyMock.expectLastCall().andReturn("c"); 77 top.getParent(); 78 EasyMock.expectLastCall().andReturn(null); 79 80 EasyMock.replay(top); 81 EasyMock.replay(middle); 82 EasyMock.replay(bottom); 83 84 BeanName bn = new BeanName(bottom); 85 assertEquals(bn.getName(), "c.b.a", bn.getName()); 86 87 EasyMock.verify(bottom); 88 EasyMock.verify(middle); 89 EasyMock.verify(top); 90 } 91 92 public void testIterator() { 93 BeanName bn = new BeanName("", true); 94 BeanName.ComponentIterator it = bn.getIterator(); 95 assertTrue(!it.hasNext()); 96 97 String n = "simple"; 98 bn = new BeanName(n, true); 99 it = bn.getIterator(); 100 assertEquals(0, it.lastBinding()); 101 assertTrue(it.hasNext()); 102 assertEquals(n, it.next()); 103 assertEquals(0, it.lastBinding()); 104 assertTrue(!it.hasNext()); 105 106 n = "{http://www.objectweb.org/handlers}AddNumbersService"; 107 bn = new BeanName(n, true); 108 it = bn.getIterator(); 109 assertEquals(0, it.lastBinding()); 110 assertTrue(it.hasNext()); 111 assertEquals(n, it.next()); 112 assertEquals(0, it.lastBinding()); 113 assertTrue(!it.hasNext()); 114 115 bn = new BeanName("one.two.three", true); 116 it = bn.getIterator(); 117 assertEquals(0, it.lastBinding()); 118 assertTrue(it.hasNext()); 119 it.next(); 120 assertEquals(0, it.lastBinding()); 121 assertTrue(it.hasNext()); 122 it.next(); 123 assertEquals(BeanName.TIGHT_BINDING, it.lastBinding()); 124 assertTrue(it.hasNext()); 125 it.next(); 126 assertEquals(BeanName.TIGHT_BINDING, it.lastBinding()); 127 assertTrue(!it.hasNext()); 128 129 bn = new BeanName("one.{a.b.c}two.three", true); 130 it = bn.getIterator(); 131 assertEquals(0, it.lastBinding()); 132 assertTrue(it.hasNext()); 133 it.next(); 134 assertEquals(0, it.lastBinding()); 135 assertTrue(it.hasNext()); 136 assertEquals("{a.b.c}two", it.next()); 137 assertEquals(BeanName.TIGHT_BINDING, it.lastBinding()); 138 assertTrue(it.hasNext()); 139 it.next(); 140 assertEquals(BeanName.TIGHT_BINDING, it.lastBinding()); 141 assertTrue(!it.hasNext()); 142 } 143 144 public void testMatch() { 145 146 BeanName[] matchingBeanNames = { 147 new BeanName("one.two.three", true), 148 new BeanName(".one.two.three", true), 149 new BeanName("one.two.three.", true), 150 new BeanName("one.?.three", true), 151 new BeanName("?.?.three", true), 152 new BeanName("*two.three", true), 153 new BeanName("*three", true), 154 new BeanName("one*three", true), 155 }; 156 157 BeanName[] nonMatchingBeanNames = { 158 new BeanName("simple", true), 159 }; 160 161 BeanName[] emptyBeanNames = { 162 new BeanName((String )null, true), 163 new BeanName("", true), 164 }; 165 166 BeanName ref = new BeanName("one.two.three", true); 167 168 List <BeanName> candidates = new ArrayList <BeanName>(); 169 170 assertNull(ref.findBestMatch(candidates)); 171 172 for (BeanName bn : emptyBeanNames) { 173 candidates.add(bn); 174 assertNull(ref.findBestMatch(candidates)); 175 } 176 candidates.clear(); 177 178 for (BeanName bn : matchingBeanNames) { 179 candidates.add(bn); 180 assertTrue("no match with " + bn.getNormalisedName(), 181 bn == ref.findBestMatch(candidates)); 182 candidates.clear(); 183 } 184 185 for (BeanName bn : nonMatchingBeanNames) { 186 candidates.add(bn); 187 assertNull("match with " + bn.getNormalisedName(), 188 ref.findBestMatch(candidates)); 189 candidates.clear(); 190 } 191 192 for (BeanName bn : matchingBeanNames) { 193 candidates.add(bn); 194 } 195 assertTrue(matchingBeanNames[0] == ref.findBestMatch(candidates)); 196 197 for (BeanName bn : nonMatchingBeanNames) { 198 candidates.add(0, bn); 199 } 200 assertTrue(matchingBeanNames[0] == ref.findBestMatch(candidates)); 201 candidates.clear(); 202 203 } 204 205 public void testBestMatch() { 206 207 BeanName ref = new BeanName("a.b"); 208 209 BeanName[] beanNames = { 210 new BeanName("?.b"), 211 new BeanName("*b"), 212 }; 213 214 215 BeanName bestMatch; 216 List <BeanName> candidates = new ArrayList <BeanName>(); 217 218 candidates.add(beanNames[0]); 219 candidates.add(beanNames[1]); 220 bestMatch = ref.findBestMatch(candidates); 221 assertTrue(bestMatch.getName(), beanNames[0] == bestMatch); 222 candidates.clear(); 223 224 candidates.add(beanNames[1]); 225 candidates.add(beanNames[0]); 226 bestMatch = ref.findBestMatch(candidates); 227 assertTrue(bestMatch.getName(), beanNames[0] == bestMatch); 228 candidates.clear(); 229 230 ref = new BeanName("a.b.c"); 231 232 beanNames = new BeanName[] { 233 new BeanName("*c"), 234 new BeanName("?.?.c"), 235 new BeanName("a*c"), 236 new BeanName("a.?.c") 237 }; 238 239 for (int i = 0; i < beanNames.length; i++) { 240 for (int j = i + 1; j < beanNames.length; j++) { 241 candidates.add(beanNames[i]); 242 candidates.add(beanNames[j]); 243 bestMatch = ref.findBestMatch(candidates); 244 assertTrue("i = " + i + ", j = " + j, beanNames[j] == bestMatch); 245 candidates.clear(); 246 candidates.add(beanNames[j]); 247 candidates.add(beanNames[i]); 248 bestMatch = ref.findBestMatch(candidates); 249 assertTrue("i = " + i + ", j = " + j, beanNames[j] == bestMatch); 250 candidates.clear(); 251 } 252 } 253 254 for (int i = 0; i < beanNames.length; i++) { 255 candidates.add(beanNames[i]); 256 } 257 bestMatch = ref.findBestMatch(candidates); 258 assertTrue(bestMatch.getName(), beanNames[beanNames.length - 1] == bestMatch); 259 candidates.clear(); 260 261 for (int i = beanNames.length - 1; i >= 0; i--) { 262 candidates.add(beanNames[i]); 263 } 264 bestMatch = ref.findBestMatch(candidates); 265 assertTrue(bestMatch.getName(), beanNames[beanNames.length - 1] == bestMatch); 266 candidates.clear(); 267 } 268 269 public void testBestMatchWithEscapedBindingChars() { 270 String busId = "celtix"; 271 String serviceName = "{http://www.objectweb.org/handlers}AddNumbersService"; 272 String portName = "addNumbersPort"; 273 274 BeanName ref = new BeanName(busId + "." + serviceName + "." + portName); 275 276 BeanName[] beanNames = { 277 new BeanName("*" + portName), 278 new BeanName("?.?." + portName), 279 new BeanName("?." + serviceName + "." + portName), 280 new BeanName(busId + "*" + portName), 281 new BeanName(busId + ".?." + portName), 282 }; 283 284 List <BeanName> candidates = new ArrayList <BeanName>(); 285 for (BeanName bn : beanNames) { 286 candidates.add(bn); 287 } 288 BeanName bestMatch = ref.findBestMatch(candidates); 289 assertTrue(bestMatch.getName(), beanNames[beanNames.length - 1] == bestMatch); 290 } 291 292 public void testBug304382() { 293 294 BeanName ref = new BeanName("a.b.c"); 295 BeanName[] beanNames = { 296 new BeanName("a.b"), 297 }; 298 299 List <BeanName> candidates = new ArrayList <BeanName>(); 300 for (BeanName bn : beanNames) { 301 candidates.add(bn); 302 } 303 BeanName bestMatch = ref.findBestMatch(candidates); 304 assertNull(bestMatch); 305 } 306 307 public void testLastComponentAny() { 308 BeanName ref = new BeanName("a.b.c"); 309 BeanName[] beanNames = { 310 new BeanName("a.b.?"), 311 new BeanName("a.b.?"), 312 new BeanName("a.?.?"), 313 new BeanName("?.?.?") 314 315 }; 316 317 List <BeanName> candidates = new ArrayList <BeanName>(); 318 319 for (BeanName bn : beanNames) { 320 candidates.add(bn); 321 BeanName match = ref.findBestMatch(candidates); 322 assertTrue(match == bn); 323 candidates.clear(); 324 } 325 326 for (BeanName bn : beanNames) { 327 candidates.add(bn); 328 } 329 BeanName match = ref.findBestMatch(candidates); 330 assertTrue(beanNames[0] == match); 331 } 332 } 333 | Popular Tags |