001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.editors.tests;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import junit.framework.Test;
016: import junit.framework.TestCase;
017: import junit.framework.TestSuite;
018:
019: import org.eclipse.jface.preference.IPreferenceStore;
020: import org.eclipse.jface.preference.PreferenceStore;
021: import org.eclipse.jface.util.IPropertyChangeListener;
022: import org.eclipse.jface.util.PropertyChangeEvent;
023:
024: import org.eclipse.ui.texteditor.ChainedPreferenceStore;
025:
026: public class ChainedPreferenceStoreTest extends TestCase {
027:
028: private class PropertyChangeListener implements
029: IPropertyChangeListener {
030:
031: public void propertyChange(PropertyChangeEvent event) {
032: fEvents.add(event);
033: }
034: }
035:
036: private List fEvents = new ArrayList();
037: private PropertyChangeListener fPropertyChangeListener = new PropertyChangeListener();
038:
039: private static final String PROPERTY = "some.property";
040: private static final String VALUE = "8";
041: private static final String DEFAULT_VALUE = "4";
042: private static final String DEFAULT_DEFAULT_VALUE = "";
043:
044: public static Test suite() {
045: return new TestSuite(ChainedPreferenceStoreTest.class);
046: }
047:
048: /**
049: * [implementation] ChainedPreferenceStore
050: * https://bugs.eclipse.org/bugs/show_bug.cgi?id=69419
051: */
052: public void testChainedStore0() {
053: IPreferenceStore store1 = new PreferenceStore();
054: IPreferenceStore store2 = new PreferenceStore();
055: IPreferenceStore chainedStore = new ChainedPreferenceStore(
056: new IPreferenceStore[] { store1, store2 });
057: store2.setDefault(PROPERTY, DEFAULT_VALUE);
058:
059: chainedStore.addPropertyChangeListener(fPropertyChangeListener);
060: store1.firePropertyChangeEvent(PROPERTY, VALUE,
061: DEFAULT_DEFAULT_VALUE); // simulated removal with newValue != null
062: chainedStore
063: .removePropertyChangeListener(fPropertyChangeListener);
064:
065: assertEquals(1, fEvents.size());
066: PropertyChangeEvent event = (PropertyChangeEvent) fEvents
067: .get(0);
068: assertEquals(chainedStore, event.getSource());
069: assertEquals(PROPERTY, event.getProperty());
070: assertEquals(VALUE, event.getOldValue());
071: assertEquals(DEFAULT_VALUE, event.getNewValue());
072: }
073:
074: /**
075: * Assertion failed in ChainedPreferenceStore.handlePropertyChangeEvent(..)
076: * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52827
077: */
078: public void testChainedStore1() {
079: IPreferenceStore store1 = new PreferenceStore();
080: IPreferenceStore store2 = new PreferenceStore();
081: IPreferenceStore chainedStore = new ChainedPreferenceStore(
082: new IPreferenceStore[] { store1, store2 });
083:
084: chainedStore.addPropertyChangeListener(fPropertyChangeListener);
085: store1.firePropertyChangeEvent(PROPERTY, VALUE,
086: DEFAULT_DEFAULT_VALUE); // simulated removal with newValue != null
087: chainedStore
088: .removePropertyChangeListener(fPropertyChangeListener);
089:
090: assertEquals(1, fEvents.size());
091: PropertyChangeEvent event = (PropertyChangeEvent) fEvents
092: .get(0);
093: assertEquals(store1, event.getSource());
094: assertEquals(PROPERTY, event.getProperty());
095: assertEquals(VALUE, event.getOldValue());
096: assertEquals(DEFAULT_DEFAULT_VALUE, event.getNewValue());
097: }
098:
099: /**
100: * Third case where the initial implementation used to have an assertion which would fail in this case
101: */
102: public void testChainedStore2() {
103: IPreferenceStore store1 = new PreferenceStore();
104: IPreferenceStore store2 = new PreferenceStore();
105: IPreferenceStore chainedStore = new ChainedPreferenceStore(
106: new IPreferenceStore[] { store1, store2 });
107: store1.setValue(PROPERTY, VALUE);
108:
109: chainedStore.addPropertyChangeListener(fPropertyChangeListener);
110: store1.firePropertyChangeEvent(PROPERTY, DEFAULT_VALUE, null); // simulated change with newValue == null
111: chainedStore
112: .removePropertyChangeListener(fPropertyChangeListener);
113:
114: assertEquals(1, fEvents.size());
115: PropertyChangeEvent event = (PropertyChangeEvent) fEvents
116: .get(0);
117: assertEquals(store1, event.getSource());
118: assertEquals(PROPERTY, event.getProperty());
119: assertEquals(DEFAULT_VALUE, event.getOldValue());
120: assertEquals(null, event.getNewValue());
121: }
122:
123: /**
124: * Case where the initial implementation used to throw an IAE
125: */
126: public void testChainedStore3() {
127: IPreferenceStore store1 = new PreferenceStore();
128: IPreferenceStore store2 = new PreferenceStore();
129: IPreferenceStore chainedStore = new ChainedPreferenceStore(
130: new IPreferenceStore[] { store1, store2 });
131: store2.setDefault(PROPERTY, DEFAULT_VALUE);
132:
133: chainedStore.addPropertyChangeListener(fPropertyChangeListener);
134: store1.firePropertyChangeEvent(PROPERTY, null, null); // simulated removal with oldValue == null
135: chainedStore
136: .removePropertyChangeListener(fPropertyChangeListener);
137:
138: assertEquals(1, fEvents.size());
139: PropertyChangeEvent event = (PropertyChangeEvent) fEvents
140: .get(0);
141: assertEquals(chainedStore, event.getSource());
142: assertEquals(PROPERTY, event.getProperty());
143: assertEquals(null, event.getOldValue());
144: assertEquals(DEFAULT_VALUE, event.getNewValue());
145: }
146:
147: // /**
148: // * Case where the old value cannot be determined. (Not handled by the current implementation.)
149: // */
150: // public void testChainedStore4() {
151: // IPreferenceStore store1= EditorsUI.getPreferenceStore();
152: // IPreferenceStore store2= new PreferenceStore();
153: // IPreferenceStore chainedStore= new ChainedPreferenceStore(new IPreferenceStore[] { store1, store2 });
154: // store2.setDefault(PROPERTY, DEFAULT_VALUE);
155: //
156: // chainedStore.addPropertyChangeListener(fPropertyChangeListener);
157: // store1.setValue(PROPERTY, VALUE);
158: // chainedStore.removePropertyChangeListener(fPropertyChangeListener);
159: //
160: // assertEquals(1, fEvents.size());
161: // PropertyChangeEvent event= (PropertyChangeEvent) fEvents.get(0);
162: // assertEquals(PROPERTY, event.getProperty());
163: // assertNull(event.getOldValue());
164: // assertEquals(VALUE, event.getNewValue());
165: // }
166: }
|