001    /*
002     * Copyright 2002-2008 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.selection.binding.support;
017    
018    import java.util.Arrays;
019    import java.util.List;
020    
021    import junit.framework.TestCase;
022    
023    import org.springframework.binding.value.ValueChangeDetector;
024    import org.springframework.binding.value.ValueModel;
025    import org.springframework.binding.value.support.DefaultValueChangeDetector;
026    import org.springframework.binding.value.support.ValueHolder;
027    import org.springframework.richclient.application.ApplicationServices;
028    import org.springframework.richclient.application.ApplicationServicesLocator;
029    
030    import ca.odell.glazedlists.BasicEventList;
031    import ca.odell.glazedlists.EventList;
032    
033    /**
034     * Testcase for ValueModel2EventListBridge
035     * 
036     * @author Peter De Bruycker
037     */
038    public class ValueModel2EventListBridgeTests extends TestCase {
039        public void testValueHolderMustContainCollection() {
040            EventList eventList = new BasicEventList();
041    
042            ValueModel valueModel = new ValueHolder("test");
043    
044            try {
045                new ValueModel2EventListBridge(valueModel, eventList);
046                fail("Must throw exception");
047            }
048            catch (IllegalArgumentException e) {
049                // test passes
050            }
051        }
052    
053        public void testAutomaticSynchronization() {
054            List list1 = Arrays.asList(new String[] { "item 1", "item2", "item3" });
055            List list2 = Arrays.asList(new String[] { "item 4", "item5", "item6" });
056    
057            EventList eventList = new BasicEventList();
058            ValueModel valueModel = new ValueHolder(list1);
059    
060            ValueModel2EventListBridge bridge = new ValueModel2EventListBridge(valueModel, eventList);
061            assertEquals("auto sync: data copied in constructor", list1, eventList);
062    
063            valueModel.setValue(list2);
064            assertEquals("when value in ValueModel changes, it's copied to the EventList", list2, eventList);
065        }
066    
067        public void testManualSynchronization() {
068            List list1 = Arrays.asList(new String[] { "item 1", "item2", "item3" });
069            List list2 = Arrays.asList(new String[] { "item 4", "item5", "item6" });
070    
071            EventList eventList = new BasicEventList();
072            ValueModel valueModel = new ValueHolder(list1);
073    
074            ValueModel2EventListBridge bridge = new ValueModel2EventListBridge(valueModel, eventList, true);
075            assertTrue("manual sync: data not copied in constructor", eventList.isEmpty());
076    
077            bridge.synchronize();
078            assertEquals("sync copies data", list1, eventList);
079    
080            valueModel.setValue(list2);
081            assertEquals("when value in ValueModel changes, it's NOT copied to the EventList", list1, eventList);
082    
083            bridge.synchronize();
084            assertEquals(list2, eventList);
085        }
086    
087        protected void setUp() throws Exception {
088            ApplicationServices services = new ApplicationServices() {
089    
090                public Object getService(Class serviceType) {
091                    return new DefaultValueChangeDetector();
092                }
093    
094                public boolean containsService(Class serviceType) {
095                    return ValueChangeDetector.class.equals(serviceType);
096                }
097    
098            };
099            ApplicationServicesLocator.load(new ApplicationServicesLocator(services));
100        }
101    }