1   /*
2    * Copyright 2002-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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   * Testcase for ValueModel2EventListBridge
35   * 
36   * @author Peter De Bruycker
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              // test passes
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 }