Source Code Cross Referenced for XPathBasedConfigItemTest.java in  » Net » Terracotta » com » tc » config » schema » dynamic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.config.schema.dynamic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice.  All rights reserved.
003:         */
004:        package com.tc.config.schema.dynamic;
005:
006:        import org.apache.xmlbeans.XmlObject;
007:
008:        import com.tc.config.schema.MockXmlObject;
009:        import com.tc.config.schema.context.ConfigContext;
010:        import com.tc.config.schema.context.MockConfigContext;
011:        import com.tc.test.TCTestCase;
012:        import com.tc.util.TCAssertionError;
013:
014:        import java.util.ArrayList;
015:        import java.util.List;
016:
017:        /**
018:         * Unit test for {@link XPathBasedConfigItem}.
019:         */
020:        public class XPathBasedConfigItemTest extends TCTestCase {
021:
022:            private static class TestXPathBasedConfigItem extends
023:                    XPathBasedConfigItem {
024:                private int numFetchDataFromXmlObjects;
025:                private List lastXmlObjects;
026:                private Object[] returnedFetchDataFromXmlObject;
027:                private int returnedFetchDataFromXmlObjectPos;
028:
029:                public TestXPathBasedConfigItem(ConfigContext context,
030:                        String xpath, Object defaultValue) {
031:                    super (context, xpath, defaultValue);
032:                    this .lastXmlObjects = new ArrayList();
033:                    this .returnedFetchDataFromXmlObject = null;
034:                    reset();
035:                }
036:
037:                public TestXPathBasedConfigItem(ConfigContext context,
038:                        String xpath) {
039:                    super (context, xpath);
040:                    this .lastXmlObjects = new ArrayList();
041:                    this .returnedFetchDataFromXmlObject = null;
042:                    reset();
043:                }
044:
045:                public void reset() {
046:                    this .numFetchDataFromXmlObjects = 0;
047:                    this .lastXmlObjects.clear();
048:                    this .returnedFetchDataFromXmlObjectPos = 0;
049:                }
050:
051:                protected Object fetchDataFromXmlObject(XmlObject xmlObject) {
052:                    ++this .numFetchDataFromXmlObjects;
053:                    this .lastXmlObjects.add(xmlObject);
054:                    int numReturned = this .returnedFetchDataFromXmlObject == null ? 0
055:                            : this .returnedFetchDataFromXmlObject.length;
056:                    if (this .returnedFetchDataFromXmlObjectPos >= numReturned)
057:                        this .returnedFetchDataFromXmlObjectPos = 0;
058:                    if (this .returnedFetchDataFromXmlObject == null)
059:                        return null;
060:                    else
061:                        return this .returnedFetchDataFromXmlObject[this .returnedFetchDataFromXmlObjectPos++];
062:                }
063:
064:                public XmlObject getLastXmlObject() {
065:                    return (XmlObject) this .lastXmlObjects
066:                            .get(this .lastXmlObjects.size() - 1);
067:                }
068:
069:                public int getNumFetchDataFromXmlObjects() {
070:                    return numFetchDataFromXmlObjects;
071:                }
072:
073:                public XmlObject[] getLastXmlObjects() {
074:                    return (XmlObject[]) this .lastXmlObjects
075:                            .toArray(new XmlObject[this .lastXmlObjects.size()]);
076:                }
077:
078:                public void setReturnedFetchDataFromXmlObject(
079:                        Object[] returnedFetchDataFromXmlObject) {
080:                    this .returnedFetchDataFromXmlObject = returnedFetchDataFromXmlObject;
081:                }
082:            }
083:
084:            private MockConfigContext context;
085:            private String xpath;
086:
087:            private TestXPathBasedConfigItem item;
088:
089:            private MockXmlObject bean;
090:            private MockXmlObject subBean;
091:            private Object convertedValue;
092:
093:            private MockConfigItemListener listener1;
094:            private MockConfigItemListener listener2;
095:
096:            public void setUp() throws Exception {
097:                this .context = new MockConfigContext();
098:                this .xpath = "foobar/baz";
099:
100:                this .item = new TestXPathBasedConfigItem(this .context,
101:                        this .xpath);
102:
103:                this .bean = new MockXmlObject();
104:                this .context.setReturnedBean(this .bean);
105:
106:                this .subBean = new MockXmlObject();
107:                this .bean
108:                        .setReturnedSelectPath(new XmlObject[] { this .subBean });
109:
110:                this .convertedValue = new Object();
111:                this .item
112:                        .setReturnedFetchDataFromXmlObject(new Object[] { this .convertedValue });
113:
114:                this .listener1 = new MockConfigItemListener();
115:                this .listener2 = new MockConfigItemListener();
116:
117:                this .item.defaultValue(); // to trigger default mechanism
118:            }
119:
120:            public void testConstruction() throws Exception {
121:                try {
122:                    new TestXPathBasedConfigItem(null, this .xpath);
123:                    fail("Didn't get NPE on no context");
124:                } catch (NullPointerException npe) {
125:                    // ok
126:                }
127:
128:                try {
129:                    new TestXPathBasedConfigItem(this .context, null);
130:                    fail("Didn't get NPE on no XPath");
131:                } catch (NullPointerException npe) {
132:                    // ok
133:                }
134:
135:                try {
136:                    new TestXPathBasedConfigItem(this .context, "");
137:                    fail("Didn't get IAE on empty context");
138:                } catch (IllegalArgumentException iae) {
139:                    // ok
140:                }
141:
142:                try {
143:                    new TestXPathBasedConfigItem(this .context, "   ");
144:                    fail("Didn't get TCAE on blank context");
145:                } catch (IllegalArgumentException iae) {
146:                    // ok
147:                }
148:            }
149:
150:            public void testInitialSetup() throws Exception {
151:                assertEquals(1, this .context.getNumItemCreateds());
152:                assertSame(this .item, this .context.getLastItemCreated());
153:
154:                assertEquals(1, this .context.getNumHasDefaultFors());
155:                assertEquals(this .xpath, this .context
156:                        .getLastHasDefaultForXPath());
157:
158:                assertEquals(0, this .context.getNumBeans());
159:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
160:            }
161:
162:            public void testComponents() throws Exception {
163:                assertSame(this .context, this .item.context());
164:                assertSame(this .xpath, this .item.xpath());
165:                assertNull(this .item.defaultValue());
166:            }
167:
168:            public void testFetchObject() throws Exception {
169:                assertEquals(0, this .context.getNumBeans());
170:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
171:                assertEquals(0, this .bean.getNumSelectPaths());
172:
173:                this .context.reset();
174:
175:                assertEquals(0, this .context.getNumItemCreateds());
176:                assertEquals(0, this .context.getNumHasDefaultFors());
177:
178:                assertSame(this .convertedValue, this .item.getObject());
179:
180:                assertEquals(0, this .context.getNumItemCreateds());
181:                assertEquals(0, this .context.getNumHasDefaultFors());
182:                assertEquals(1, this .context.getNumBeans());
183:                assertEquals(1, this .bean.getNumSelectPaths());
184:                assertEquals(this .xpath, this .bean.getLastSelectPath());
185:                assertEquals(1, this .item.getNumFetchDataFromXmlObjects());
186:                assertSame(this .subBean, this .item.getLastXmlObject());
187:
188:                // Fetch again -- make sure it caches
189:                this .context.reset();
190:                this .bean.reset();
191:                this .item.reset();
192:
193:                assertSame(this .convertedValue, this .item.getObject());
194:                assertEquals(0, this .context.getNumItemCreateds());
195:                assertEquals(0, this .context.getNumHasDefaultFors());
196:                assertEquals(0, this .context.getNumBeans());
197:                assertEquals(0, this .bean.getNumSelectPaths());
198:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
199:            }
200:
201:            public void testFetchObjectWithDefaultsWithValue() throws Exception {
202:                Object defaultValue = new Object();
203:
204:                this .item = new TestXPathBasedConfigItem(this .context,
205:                        this .xpath, defaultValue);
206:                this .item
207:                        .setReturnedFetchDataFromXmlObject(new Object[] { this .convertedValue });
208:
209:                assertEquals(0, this .context.getNumBeans());
210:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
211:                assertEquals(0, this .bean.getNumSelectPaths());
212:
213:                this .context.reset();
214:
215:                assertEquals(0, this .context.getNumItemCreateds());
216:                assertEquals(0, this .context.getNumHasDefaultFors());
217:
218:                assertSame(this .convertedValue, this .item.getObject());
219:
220:                assertEquals(0, this .context.getNumItemCreateds());
221:                assertEquals(0, this .context.getNumHasDefaultFors());
222:                assertEquals(1, this .context.getNumBeans());
223:                assertEquals(1, this .bean.getNumSelectPaths());
224:                assertEquals(this .xpath, this .bean.getLastSelectPath());
225:                assertEquals(1, this .item.getNumFetchDataFromXmlObjects());
226:                assertSame(this .subBean, this .item.getLastXmlObject());
227:
228:                // Fetch again -- make sure it caches
229:                this .context.reset();
230:                this .bean.reset();
231:                this .item.reset();
232:
233:                assertSame(this .convertedValue, this .item.getObject());
234:                assertEquals(0, this .context.getNumItemCreateds());
235:                assertEquals(0, this .context.getNumHasDefaultFors());
236:                assertEquals(0, this .context.getNumBeans());
237:                assertEquals(0, this .bean.getNumSelectPaths());
238:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
239:            }
240:
241:            public void testFetchObjectWithDefaultsWithNoValueInXPath()
242:                    throws Exception {
243:                Object defaultValue = new Object();
244:
245:                this .item = new TestXPathBasedConfigItem(this .context,
246:                        this .xpath, defaultValue);
247:                this .item
248:                        .setReturnedFetchDataFromXmlObject(new Object[] { this .convertedValue });
249:
250:                assertEquals(0, this .context.getNumBeans());
251:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
252:                assertEquals(0, this .bean.getNumSelectPaths());
253:
254:                this .context.reset();
255:
256:                assertEquals(0, this .context.getNumItemCreateds());
257:                assertEquals(0, this .context.getNumHasDefaultFors());
258:
259:                this .bean.setReturnedSelectPath(null);
260:                this .item = new TestXPathBasedConfigItem(this .context,
261:                        this .xpath, defaultValue);
262:                assertSame(defaultValue, this .item.getObject());
263:
264:                assertEquals(1, this .context.getNumItemCreateds());
265:                assertEquals(0, this .context.getNumHasDefaultFors());
266:                assertEquals(1, this .context.getNumBeans());
267:                assertEquals(1, this .bean.getNumSelectPaths());
268:                assertEquals(this .xpath, this .bean.getLastSelectPath());
269:
270:                // Fetch again -- make sure it caches
271:                this .context.reset();
272:                this .bean.reset();
273:                this .item.reset();
274:
275:                assertSame(defaultValue, this .item.getObject());
276:                assertEquals(0, this .context.getNumItemCreateds());
277:                assertEquals(0, this .context.getNumHasDefaultFors());
278:                assertEquals(0, this .context.getNumBeans());
279:                assertEquals(0, this .bean.getNumSelectPaths());
280:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
281:            }
282:
283:            public void testFetchObjectWithDefaultsWithNoConvertedValue()
284:                    throws Exception {
285:                Object defaultValue = new Object();
286:
287:                this .item = new TestXPathBasedConfigItem(this .context,
288:                        this .xpath, defaultValue);
289:                this .item
290:                        .setReturnedFetchDataFromXmlObject(new Object[] { null });
291:
292:                assertEquals(0, this .context.getNumBeans());
293:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
294:                assertEquals(0, this .bean.getNumSelectPaths());
295:
296:                this .context.reset();
297:
298:                assertEquals(0, this .context.getNumItemCreateds());
299:                assertEquals(0, this .context.getNumHasDefaultFors());
300:
301:                assertSame(defaultValue, this .item.getObject());
302:
303:                assertEquals(0, this .context.getNumItemCreateds());
304:                assertEquals(0, this .context.getNumHasDefaultFors());
305:                assertEquals(1, this .context.getNumBeans());
306:                assertEquals(1, this .bean.getNumSelectPaths());
307:                assertEquals(this .xpath, this .bean.getLastSelectPath());
308:                assertEquals(1, this .item.getNumFetchDataFromXmlObjects());
309:                assertSame(this .subBean, this .item.getLastXmlObject());
310:
311:                // Fetch again -- make sure it caches
312:                this .context.reset();
313:                this .bean.reset();
314:                this .item.reset();
315:
316:                assertSame(defaultValue, this .item.getObject());
317:                assertEquals(0, this .context.getNumItemCreateds());
318:                assertEquals(0, this .context.getNumHasDefaultFors());
319:                assertEquals(0, this .context.getNumBeans());
320:                assertEquals(0, this .bean.getNumSelectPaths());
321:                assertEquals(0, this .item.getNumFetchDataFromXmlObjects());
322:            }
323:
324:            public void testListenerDefaultBeforehandNoConvertedValue()
325:                    throws Exception {
326:                Object defaultValue = new Object();
327:
328:                this .item = new TestXPathBasedConfigItem(this .context,
329:                        this .xpath, defaultValue);
330:                Object newValue = new Object();
331:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
332:                        null, newValue });
333:
334:                this .context.reset();
335:
336:                MockXmlObject newBean = new MockXmlObject();
337:                MockXmlObject newSubBean = new MockXmlObject();
338:                newBean.setReturnedSelectPath(new XmlObject[] { newSubBean });
339:                this .item.addListener(this .listener1);
340:
341:                this .item.configurationChanged(this .bean, newBean);
342:
343:                assertEquals(1, this .listener1.getNumValueChangeds());
344:                assertSame(defaultValue, this .listener1.getLastOldValue());
345:                assertSame(newValue, this .listener1.getLastNewValue());
346:
347:                assertSame(newValue, this .item.getObject());
348:            }
349:
350:            public void testListenerDefaultBeforehandNoXPath() throws Exception {
351:                Object defaultValue = new Object();
352:
353:                this .item = new TestXPathBasedConfigItem(this .context,
354:                        this .xpath, defaultValue);
355:                Object newValue = new Object();
356:                this .bean.setReturnedSelectPath(null);
357:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
358:                        null, newValue });
359:
360:                this .context.reset();
361:
362:                MockXmlObject newBean = new MockXmlObject();
363:                MockXmlObject newSubBean = new MockXmlObject();
364:                newBean.setReturnedSelectPath(new XmlObject[] { newSubBean });
365:                this .item.addListener(this .listener1);
366:
367:                this .item.configurationChanged(this .bean, newBean);
368:
369:                assertEquals(1, this .listener1.getNumValueChangeds());
370:                assertSame(defaultValue, this .listener1.getLastOldValue());
371:                assertSame(newValue, this .listener1.getLastNewValue());
372:
373:                assertSame(newValue, this .item.getObject());
374:            }
375:
376:            public void testListenerDefaultAfterwardsNoConvertedValue()
377:                    throws Exception {
378:                Object defaultValue = new Object();
379:
380:                this .item = new TestXPathBasedConfigItem(this .context,
381:                        this .xpath, defaultValue);
382:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
383:                        this .convertedValue, null });
384:
385:                this .context.reset();
386:
387:                MockXmlObject newBean = new MockXmlObject();
388:                MockXmlObject newSubBean = new MockXmlObject();
389:                newBean.setReturnedSelectPath(new XmlObject[] { newSubBean });
390:                this .item.addListener(this .listener1);
391:
392:                this .item.configurationChanged(this .bean, newBean);
393:
394:                assertEquals(1, this .listener1.getNumValueChangeds());
395:                assertSame(this .convertedValue, this .listener1
396:                        .getLastOldValue());
397:                assertSame(defaultValue, this .listener1.getLastNewValue());
398:
399:                assertSame(defaultValue, this .item.getObject());
400:            }
401:
402:            public void testListenerDefaultAfterwardsNoXPath() throws Exception {
403:                Object defaultValue = new Object();
404:
405:                this .item = new TestXPathBasedConfigItem(this .context,
406:                        this .xpath, defaultValue);
407:                Object newValue = new Object();
408:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
409:                        this .convertedValue, null, newValue });
410:
411:                this .context.reset();
412:
413:                MockXmlObject newBean = new MockXmlObject();
414:                newBean.setReturnedSelectPath(null);
415:                this .item.addListener(this .listener1);
416:
417:                this .item.configurationChanged(this .bean, newBean);
418:
419:                assertEquals(1, this .listener1.getNumValueChangeds());
420:                assertSame(this .convertedValue, this .listener1
421:                        .getLastOldValue());
422:                assertSame(defaultValue, this .listener1.getLastNewValue());
423:
424:                assertSame(defaultValue, this .item.getObject());
425:            }
426:
427:            public void testListenerDefaultBeforeAndAfterNoConvertedValue()
428:                    throws Exception {
429:                Object defaultValue = new Object();
430:
431:                this .item = new TestXPathBasedConfigItem(this .context,
432:                        this .xpath, defaultValue);
433:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
434:                        null, null });
435:
436:                this .context.reset();
437:
438:                MockXmlObject newBean = new MockXmlObject();
439:                MockXmlObject newSubBean = new MockXmlObject();
440:                newBean.setReturnedSelectPath(new XmlObject[] { newSubBean });
441:                this .item.addListener(this .listener1);
442:
443:                this .item.configurationChanged(this .bean, newBean);
444:
445:                assertEquals(0, this .listener1.getNumValueChangeds());
446:
447:                assertSame(defaultValue, this .item.getObject());
448:            }
449:
450:            public void testPathReturnsRObjects() throws Exception {
451:                this .context.reset();
452:
453:                assertEquals(0, this .context.getNumItemCreateds());
454:                assertEquals(0, this .context.getNumHasDefaultFors());
455:
456:                this .bean.setReturnedSelectPath(null);
457:
458:                assertSame(this .convertedValue, this .item.getObject());
459:
460:                assertEquals(0, this .context.getNumItemCreateds());
461:                assertEquals(0, this .context.getNumHasDefaultFors());
462:                assertEquals(1, this .context.getNumBeans());
463:                assertEquals(1, this .bean.getNumSelectPaths());
464:                assertEquals(this .xpath, this .bean.getLastSelectPath());
465:                assertEquals(1, this .item.getNumFetchDataFromXmlObjects());
466:            }
467:
468:            public void testPathReturnsZeroLengthArray() throws Exception {
469:                this .context.reset();
470:
471:                assertEquals(0, this .context.getNumItemCreateds());
472:                assertEquals(0, this .context.getNumHasDefaultFors());
473:
474:                this .bean.setReturnedSelectPath(new XmlObject[0]);
475:
476:                assertSame(this .convertedValue, this .item.getObject());
477:
478:                assertEquals(0, this .context.getNumItemCreateds());
479:                assertEquals(0, this .context.getNumHasDefaultFors());
480:                assertEquals(1, this .context.getNumBeans());
481:                assertEquals(1, this .bean.getNumSelectPaths());
482:                assertEquals(this .xpath, this .bean.getLastSelectPath());
483:                assertEquals(1, this .item.getNumFetchDataFromXmlObjects());
484:            }
485:
486:            public void testPathReturnsMultipleObjects() throws Exception {
487:                this .context.reset();
488:
489:                assertEquals(0, this .context.getNumItemCreateds());
490:                assertEquals(0, this .context.getNumHasDefaultFors());
491:
492:                this .bean.setReturnedSelectPath(new XmlObject[] {
493:                        new MockXmlObject(), new MockXmlObject() });
494:
495:                try {
496:                    this .item.getObject();
497:                    fail("Didn't get TCAE on multiple return values");
498:                } catch (TCAssertionError tcae) {
499:                    // ok
500:                }
501:            }
502:
503:            public void testListeners() throws Exception {
504:                checkListeners(false, false, false, null);
505:
506:                resetItem();
507:                this .item.addListener(this .listener1);
508:                checkListeners(true, false, false, null);
509:
510:                resetItem();
511:                this .item.addListener(this .listener1);
512:                this .item.addListener(this .listener2);
513:                checkListeners(true, true, false, null);
514:
515:                resetItem();
516:                this .item.addListener(this .listener1);
517:                this .item.addListener(this .listener2);
518:                this .item.removeListener(this .listener1);
519:                checkListeners(false, true, false, null);
520:
521:                resetItem();
522:                this .item.addListener(this .listener1);
523:                this .item.addListener(this .listener2);
524:                this .item.removeListener(this .listener1);
525:                this .item.removeListener(this .listener2);
526:                this .item.removeListener(this .listener2);
527:                this .item.removeListener(this .listener1);
528:                checkListeners(false, false, false, null);
529:            }
530:
531:            public void testListenersWithDataAlready() throws Exception {
532:                Object curVal = this .convertedValue;
533:
534:                assertSame(this .convertedValue, this .item.getObject());
535:                curVal = checkListeners(false, false, true, curVal);
536:
537:                this .item.addListener(this .listener1);
538:                curVal = checkListeners(true, false, true, curVal);
539:
540:                this .item.addListener(this .listener2);
541:                curVal = checkListeners(true, true, true, curVal);
542:
543:                this .item.removeListener(this .listener2);
544:                curVal = checkListeners(true, false, true, curVal);
545:
546:                this .item.removeListener(this .listener1);
547:                curVal = checkListeners(false, false, true, curVal);
548:
549:                this .item.removeListener(this .listener1);
550:                curVal = checkListeners(false, false, true, curVal);
551:            }
552:
553:            public void testNoListenerTriggerOnEqualObjects() throws Exception {
554:                Integer one = new Integer(24);
555:                Integer two = new Integer(24);
556:
557:                assertNotSame(one, two);
558:
559:                this .item.addListener(this .listener1);
560:                this .item.setReturnedFetchDataFromXmlObject(new Object[] { one,
561:                        two });
562:
563:                MockXmlObject newBean = new MockXmlObject();
564:                newBean
565:                        .setReturnedSelectPath(new XmlObject[] { new MockXmlObject() });
566:
567:                this .item.configurationChanged(this .bean, newBean);
568:
569:                assertEquals(0, this .listener1.getNumValueChangeds());
570:            }
571:
572:            public void testNoListenerTriggerOnBothNull() throws Exception {
573:                this .item.addListener(this .listener1);
574:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
575:                        null, null });
576:
577:                MockXmlObject newBean = new MockXmlObject();
578:                newBean
579:                        .setReturnedSelectPath(new XmlObject[] { new MockXmlObject() });
580:
581:                this .item.configurationChanged(this .bean, newBean);
582:
583:                assertEquals(0, this .listener1.getNumValueChangeds());
584:            }
585:
586:            public void testListenerTriggerOnNullToSomething() throws Exception {
587:                Integer two = new Integer(24);
588:
589:                this .item.addListener(this .listener1);
590:                this .item.setReturnedFetchDataFromXmlObject(new Object[] {
591:                        null, two });
592:
593:                MockXmlObject newBean = new MockXmlObject();
594:                newBean
595:                        .setReturnedSelectPath(new XmlObject[] { new MockXmlObject() });
596:
597:                this .item.configurationChanged(this .bean, newBean);
598:
599:                assertEquals(1, this .listener1.getNumValueChangeds());
600:                assertNull(this .listener1.getLastOldValue());
601:                assertSame(two, this .listener1.getLastNewValue());
602:            }
603:
604:            public void testListenerTriggerOnSomethingToNull() throws Exception {
605:                Integer one = new Integer(24);
606:
607:                this .item.addListener(this .listener1);
608:                this .item.setReturnedFetchDataFromXmlObject(new Object[] { one,
609:                        null });
610:
611:                MockXmlObject newBean = new MockXmlObject();
612:                newBean
613:                        .setReturnedSelectPath(new XmlObject[] { new MockXmlObject() });
614:
615:                this .item.configurationChanged(this .bean, newBean);
616:
617:                assertEquals(1, this .listener1.getNumValueChangeds());
618:                assertSame(one, this .listener1.getLastOldValue());
619:                assertNull(this .listener1.getLastNewValue());
620:            }
621:
622:            private void resetItem() throws Exception {
623:                this .item = new TestXPathBasedConfigItem(this .context,
624:                        this .xpath);
625:            }
626:
627:            private Object checkListeners(boolean expectedOne,
628:                    boolean expectedTwo, boolean hasDataAlready, Object oldData) {
629:                this .listener1.reset();
630:                this .listener2.reset();
631:                this .item.reset();
632:
633:                MockXmlObject oldBean = new MockXmlObject();
634:                MockXmlObject newBean = new MockXmlObject();
635:
636:                MockXmlObject oldSubBean = new MockXmlObject();
637:                MockXmlObject newSubBean = new MockXmlObject();
638:
639:                oldBean.setReturnedSelectPath(new XmlObject[] { oldSubBean });
640:                newBean.setReturnedSelectPath(new XmlObject[] { newSubBean });
641:
642:                Object oldObject = new Object();
643:                Object newObject = new Object();
644:
645:                if (!hasDataAlready)
646:                    this .item.setReturnedFetchDataFromXmlObject(new Object[] {
647:                            oldObject, newObject });
648:                else
649:                    this .item
650:                            .setReturnedFetchDataFromXmlObject(new Object[] { newObject });
651:
652:                this .item.configurationChanged(oldBean, newBean);
653:
654:                if (!hasDataAlready) {
655:                    assertEquals(1, oldBean.getNumSelectPaths());
656:                    assertEquals(this .xpath, oldBean.getLastSelectPath());
657:
658:                    assertEquals(1, newBean.getNumSelectPaths());
659:                    assertEquals(this .xpath, newBean.getLastSelectPath());
660:
661:                    assertEquals(2, this .item.getNumFetchDataFromXmlObjects());
662:                    assertSame(oldSubBean, this .item.getLastXmlObjects()[0]);
663:                    assertSame(newSubBean, this .item.getLastXmlObjects()[1]);
664:                } else {
665:                    assertEquals(1, newBean.getNumSelectPaths());
666:                    assertEquals(this .xpath, newBean.getLastSelectPath());
667:
668:                    assertEquals(1, this .item.getNumFetchDataFromXmlObjects());
669:                    assertSame(newSubBean, this .item.getLastXmlObjects()[0]);
670:                }
671:
672:                if (expectedOne) {
673:                    assertEquals(1, this .listener1.getNumValueChangeds());
674:                    if (hasDataAlready)
675:                        assertSame(oldData, this .listener1.getLastOldValue());
676:                    else
677:                        assertSame(oldObject, this .listener1.getLastOldValue());
678:                    assertSame(newObject, this .listener1.getLastNewValue());
679:                } else {
680:                    assertEquals(0, this .listener1.getNumValueChangeds());
681:                }
682:
683:                if (expectedTwo) {
684:                    assertEquals(1, this .listener2.getNumValueChangeds());
685:                    if (hasDataAlready)
686:                        assertSame(oldData, this .listener2.getLastOldValue());
687:                    else
688:                        assertSame(oldObject, this .listener2.getLastOldValue());
689:                    assertSame(newObject, this .listener2.getLastNewValue());
690:                } else {
691:                    assertEquals(0, this .listener2.getNumValueChangeds());
692:                }
693:
694:                return newObject;
695:            }
696:
697:            public void testToString() throws Exception {
698:                this.item.toString();
699:            }
700:
701:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.