001: package org.objectweb.celtix.bus.configuration.spring;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import junit.framework.TestCase;
007:
008: import org.easymock.EasyMock;
009: import org.objectweb.celtix.configuration.Configuration;
010:
011: public class BeanNameTest extends TestCase {
012:
013: public void testNormalisation() {
014:
015: String n = "*.*abc*efg...hij.klm***nop.*.qrs.?.xyz";
016: String nn = "*abc*efg.hij.klm*nop*qrs.?.xyz";
017:
018: String nbn;
019: nbn = new BeanName((String) null, true).getNormalisedName();
020: assertEquals(nbn, null, nbn);
021: nbn = new BeanName("", true).getNormalisedName();
022: assertEquals(nbn, "", nbn);
023: nbn = new BeanName("?", true).getNormalisedName();
024: assertEquals(nbn, "?", nbn);
025: nbn = new BeanName(".", true).getNormalisedName();
026: assertEquals(nbn, ".", nbn);
027: nbn = new BeanName("*", true).getNormalisedName();
028: assertEquals(nbn, "*", nbn);
029: nbn = new BeanName("a", true).getNormalisedName();
030: assertEquals(nbn, "a", nbn);
031: nbn = new BeanName("a.", true).getNormalisedName();
032: assertEquals(nbn, "a.", nbn);
033: nbn = new BeanName("a.b**", true).getNormalisedName();
034: assertEquals(nbn, "a.b*", nbn);
035: nbn = new BeanName("{a.b**}", true).getNormalisedName();
036: assertEquals(nbn, "{a.b**}", nbn);
037: nbn = new BeanName("x.{a.b**}", true).getNormalisedName();
038: assertEquals(nbn, "x.{a.b**}", nbn);
039: nbn = new BeanName("*{a.b**}", true).getNormalisedName();
040: assertEquals(nbn, "*{a.b**}", nbn);
041: nbn = new BeanName("*{a.b**}x.y", true).getNormalisedName();
042: assertEquals(nbn, "*{a.b**}x.y", nbn);
043:
044: nbn = new BeanName(n, true).getNormalisedName();
045: assertEquals(nbn, nn, nbn);
046:
047: BeanName bn = new BeanName(n, false);
048: assertNull(bn.getNormalisedName());
049: assertEquals(n, bn.getName());
050: bn.normalise();
051: assertEquals(nn, bn.getNormalisedName());
052: assertEquals(n, bn.getName());
053:
054: bn = new BeanName(n, true);
055: assertEquals(n, bn.getName());
056: assertEquals(nn, bn.getNormalisedName());
057: bn.normalise();
058: assertEquals(n, bn.getName());
059: assertEquals(nn, bn.getNormalisedName());
060: }
061:
062: public void testConstruction() {
063: Configuration bottom = EasyMock.createMock(Configuration.class);
064: Configuration middle = EasyMock.createMock(Configuration.class);
065: Configuration top = EasyMock.createMock(Configuration.class);
066:
067: bottom.getId();
068: EasyMock.expectLastCall().andReturn("a");
069: bottom.getParent();
070: EasyMock.expectLastCall().andReturn(middle);
071: middle.getId();
072: EasyMock.expectLastCall().andReturn("b");
073: middle.getParent();
074: EasyMock.expectLastCall().andReturn(top);
075: top.getId();
076: EasyMock.expectLastCall().andReturn("c");
077: top.getParent();
078: EasyMock.expectLastCall().andReturn(null);
079:
080: EasyMock.replay(top);
081: EasyMock.replay(middle);
082: EasyMock.replay(bottom);
083:
084: BeanName bn = new BeanName(bottom);
085: assertEquals(bn.getName(), "c.b.a", bn.getName());
086:
087: EasyMock.verify(bottom);
088: EasyMock.verify(middle);
089: EasyMock.verify(top);
090: }
091:
092: public void testIterator() {
093: BeanName bn = new BeanName("", true);
094: BeanName.ComponentIterator it = bn.getIterator();
095: assertTrue(!it.hasNext());
096:
097: String n = "simple";
098: bn = new BeanName(n, true);
099: 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: BeanName[] nonMatchingBeanNames = { new BeanName("simple", true), };
157:
158: BeanName[] emptyBeanNames = {
159: new BeanName((String) null, true),
160: new BeanName("", true), };
161:
162: BeanName ref = new BeanName("one.two.three", true);
163:
164: List<BeanName> candidates = new ArrayList<BeanName>();
165:
166: assertNull(ref.findBestMatch(candidates));
167:
168: for (BeanName bn : emptyBeanNames) {
169: candidates.add(bn);
170: assertNull(ref.findBestMatch(candidates));
171: }
172: candidates.clear();
173:
174: for (BeanName bn : matchingBeanNames) {
175: candidates.add(bn);
176: assertTrue("no match with " + bn.getNormalisedName(),
177: bn == ref.findBestMatch(candidates));
178: candidates.clear();
179: }
180:
181: for (BeanName bn : nonMatchingBeanNames) {
182: candidates.add(bn);
183: assertNull("match with " + bn.getNormalisedName(), ref
184: .findBestMatch(candidates));
185: candidates.clear();
186: }
187:
188: for (BeanName bn : matchingBeanNames) {
189: candidates.add(bn);
190: }
191: assertTrue(matchingBeanNames[0] == ref
192: .findBestMatch(candidates));
193:
194: for (BeanName bn : nonMatchingBeanNames) {
195: candidates.add(0, bn);
196: }
197: assertTrue(matchingBeanNames[0] == ref
198: .findBestMatch(candidates));
199: candidates.clear();
200:
201: }
202:
203: public void testBestMatch() {
204:
205: BeanName ref = new BeanName("a.b");
206:
207: BeanName[] beanNames = { new BeanName("?.b"),
208: new BeanName("*b"), };
209:
210: BeanName bestMatch;
211: List<BeanName> candidates = new ArrayList<BeanName>();
212:
213: candidates.add(beanNames[0]);
214: candidates.add(beanNames[1]);
215: bestMatch = ref.findBestMatch(candidates);
216: assertTrue(bestMatch.getName(), beanNames[0] == bestMatch);
217: candidates.clear();
218:
219: candidates.add(beanNames[1]);
220: candidates.add(beanNames[0]);
221: bestMatch = ref.findBestMatch(candidates);
222: assertTrue(bestMatch.getName(), beanNames[0] == bestMatch);
223: candidates.clear();
224:
225: ref = new BeanName("a.b.c");
226:
227: beanNames = new BeanName[] { new BeanName("*c"),
228: new BeanName("?.?.c"), new BeanName("a*c"),
229: new BeanName("a.?.c") };
230:
231: for (int i = 0; i < beanNames.length; i++) {
232: for (int j = i + 1; j < beanNames.length; j++) {
233: candidates.add(beanNames[i]);
234: candidates.add(beanNames[j]);
235: bestMatch = ref.findBestMatch(candidates);
236: assertTrue("i = " + i + ", j = " + j,
237: beanNames[j] == bestMatch);
238: candidates.clear();
239: candidates.add(beanNames[j]);
240: candidates.add(beanNames[i]);
241: bestMatch = ref.findBestMatch(candidates);
242: assertTrue("i = " + i + ", j = " + j,
243: beanNames[j] == bestMatch);
244: candidates.clear();
245: }
246: }
247:
248: for (int i = 0; i < beanNames.length; i++) {
249: candidates.add(beanNames[i]);
250: }
251: bestMatch = ref.findBestMatch(candidates);
252: assertTrue(bestMatch.getName(),
253: beanNames[beanNames.length - 1] == bestMatch);
254: candidates.clear();
255:
256: for (int i = beanNames.length - 1; i >= 0; i--) {
257: candidates.add(beanNames[i]);
258: }
259: bestMatch = ref.findBestMatch(candidates);
260: assertTrue(bestMatch.getName(),
261: beanNames[beanNames.length - 1] == bestMatch);
262: candidates.clear();
263: }
264:
265: public void testBestMatchWithEscapedBindingChars() {
266: String busId = "celtix";
267: String serviceName = "{http://www.objectweb.org/handlers}AddNumbersService";
268: String portName = "addNumbersPort";
269:
270: BeanName ref = new BeanName(busId + "." + serviceName + "."
271: + portName);
272:
273: BeanName[] beanNames = { new BeanName("*" + portName),
274: new BeanName("?.?." + portName),
275: new BeanName("?." + serviceName + "." + portName),
276: new BeanName(busId + "*" + portName),
277: new BeanName(busId + ".?." + portName), };
278:
279: List<BeanName> candidates = new ArrayList<BeanName>();
280: for (BeanName bn : beanNames) {
281: candidates.add(bn);
282: }
283: BeanName bestMatch = ref.findBestMatch(candidates);
284: assertTrue(bestMatch.getName(),
285: beanNames[beanNames.length - 1] == bestMatch);
286: }
287:
288: public void testBug304382() {
289:
290: BeanName ref = new BeanName("a.b.c");
291: BeanName[] beanNames = { new BeanName("a.b"), };
292:
293: List<BeanName> candidates = new ArrayList<BeanName>();
294: for (BeanName bn : beanNames) {
295: candidates.add(bn);
296: }
297: BeanName bestMatch = ref.findBestMatch(candidates);
298: assertNull(bestMatch);
299: }
300:
301: public void testLastComponentAny() {
302: BeanName ref = new BeanName("a.b.c");
303: BeanName[] beanNames = { new BeanName("a.b.?"),
304: new BeanName("a.b.?"), new BeanName("a.?.?"),
305: new BeanName("?.?.?")
306:
307: };
308:
309: List<BeanName> candidates = new ArrayList<BeanName>();
310:
311: for (BeanName bn : beanNames) {
312: candidates.add(bn);
313: BeanName match = ref.findBestMatch(candidates);
314: assertTrue(match == bn);
315: candidates.clear();
316: }
317:
318: for (BeanName bn : beanNames) {
319: candidates.add(bn);
320: }
321: BeanName match = ref.findBestMatch(candidates);
322: assertTrue(beanNames[0] == match);
323: }
324: }
|