1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.selection.binding.support;
17
18 import java.util.Arrays;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.binding.value.ValueChangeDetector;
24 import org.springframework.binding.value.ValueModel;
25 import org.springframework.binding.value.support.DefaultValueChangeDetector;
26 import org.springframework.binding.value.support.ValueHolder;
27 import org.springframework.richclient.application.ApplicationServices;
28 import org.springframework.richclient.application.ApplicationServicesLocator;
29
30 import ca.odell.glazedlists.BasicEventList;
31 import ca.odell.glazedlists.EventList;
32
33
34
35
36
37
38 public class ValueModel2EventListBridgeTests extends TestCase {
39 public void testValueHolderMustContainCollection() {
40 EventList eventList = new BasicEventList();
41
42 ValueModel valueModel = new ValueHolder("test");
43
44 try {
45 new ValueModel2EventListBridge(valueModel, eventList);
46 fail("Must throw exception");
47 }
48 catch (IllegalArgumentException e) {
49
50 }
51 }
52
53 public void testAutomaticSynchronization() {
54 List list1 = Arrays.asList(new String[] { "item 1", "item2", "item3" });
55 List list2 = Arrays.asList(new String[] { "item 4", "item5", "item6" });
56
57 EventList eventList = new BasicEventList();
58 ValueModel valueModel = new ValueHolder(list1);
59
60 ValueModel2EventListBridge bridge = new ValueModel2EventListBridge(valueModel, eventList);
61 assertEquals("auto sync: data copied in constructor", list1, eventList);
62
63 valueModel.setValue(list2);
64 assertEquals("when value in ValueModel changes, it's copied to the EventList", list2, eventList);
65 }
66
67 public void testManualSynchronization() {
68 List list1 = Arrays.asList(new String[] { "item 1", "item2", "item3" });
69 List list2 = Arrays.asList(new String[] { "item 4", "item5", "item6" });
70
71 EventList eventList = new BasicEventList();
72 ValueModel valueModel = new ValueHolder(list1);
73
74 ValueModel2EventListBridge bridge = new ValueModel2EventListBridge(valueModel, eventList, true);
75 assertTrue("manual sync: data not copied in constructor", eventList.isEmpty());
76
77 bridge.synchronize();
78 assertEquals("sync copies data", list1, eventList);
79
80 valueModel.setValue(list2);
81 assertEquals("when value in ValueModel changes, it's NOT copied to the EventList", list1, eventList);
82
83 bridge.synchronize();
84 assertEquals(list2, eventList);
85 }
86
87 protected void setUp() throws Exception {
88 ApplicationServices services = new ApplicationServices() {
89
90 public Object getService(Class serviceType) {
91 return new DefaultValueChangeDetector();
92 }
93
94 public boolean containsService(Class serviceType) {
95 return ValueChangeDetector.class.equals(serviceType);
96 }
97
98 };
99 ApplicationServicesLocator.load(new ApplicationServicesLocator(services));
100 }
101 }