Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b25ede951c5bbb0a683a1e984aa8b9a073236197 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************************
 * Copyright (c) 2009, 2012 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal.model.value;

import junit.framework.TestCase;

import org.eclipse.jpt.common.utility.internal.model.AbstractModel;
import org.eclipse.jpt.common.utility.internal.model.value.ReadOnlyWritablePropertyValueModelWrapper;
import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.ChangeAdapter;
import org.eclipse.jpt.common.utility.model.listener.ChangeListener;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;

@SuppressWarnings("nls")
public class ReadOnlyWritablePropertyValueModelWrapperTests 
	extends TestCase
{
	private ModifiablePropertyValueModel<String> objectHolder;
	
	PropertyChangeEvent event;
	
	private ModifiablePropertyValueModel<String> wrapperObjectHolder;
	
	PropertyChangeEvent wrapperEvent;
	
	
	public ReadOnlyWritablePropertyValueModelWrapperTests(String name) {
		super(name);
	}
	
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.objectHolder = new SimplePropertyValueModel<String>("foo");
		this.wrapperObjectHolder = new ReadOnlyWritablePropertyValueModelWrapper<String>(this.objectHolder);
	}
	
	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}
	
	
	public void testValue() {
		assertEquals("foo", this.objectHolder.getValue());
		assertEquals("foo", this.wrapperObjectHolder.getValue());
		
		this.objectHolder.setValue("bar");
		assertEquals("bar", this.objectHolder.getValue());
		assertEquals("bar", this.wrapperObjectHolder.getValue());
		
		this.objectHolder.setValue(null);
		assertNull(this.objectHolder.getValue());
		assertNull(this.wrapperObjectHolder.getValue());
		
		this.objectHolder.setValue("foo");
		assertEquals("foo", this.objectHolder.getValue());
		assertEquals("foo", this.wrapperObjectHolder.getValue());
	}
	
	public void testSetValue() {
		boolean exceptionOccurred = false;
		try {
			this.wrapperObjectHolder.setValue("bar");
		}
		catch (UnsupportedOperationException uoe) {
			exceptionOccurred = true;
		}
		
		assertTrue(exceptionOccurred);
		assertEquals("foo", this.objectHolder.getValue());
		assertEquals("foo", this.wrapperObjectHolder.getValue());
	}
	
	public void testLazyListening() {
		assertTrue(((AbstractModel) this.objectHolder).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
		ChangeListener listener = buildWrapperListener();
		this.wrapperObjectHolder.addChangeListener(listener);
		assertTrue(((AbstractModel) this.objectHolder).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.wrapperObjectHolder.removeChangeListener(listener);
		assertTrue(((AbstractModel) this.objectHolder).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
		
		this.wrapperObjectHolder.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertTrue(((AbstractModel) this.objectHolder).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.wrapperObjectHolder.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertTrue(((AbstractModel) this.objectHolder).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
	}
	
	public void testPropertyChange1() {
		this.objectHolder.addChangeListener(this.buildListener());
		this.wrapperObjectHolder.addChangeListener(this.buildWrapperListener());
		this.verifyPropertyChanges();
	}
	
	public void testPropertyChange2() {
		this.objectHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener());
		this.wrapperObjectHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildWrapperListener());
		this.verifyPropertyChanges();
	}
	
	private void verifyPropertyChanges() {
		this.event = null;
		this.wrapperEvent = null;
		this.objectHolder.setValue("bar");
		verifyEvent(this.event, this.objectHolder, "foo", "bar");
		verifyEvent(this.wrapperEvent, this.wrapperObjectHolder, "foo", "bar");
		
		this.event = null;
		this.wrapperEvent = null;
		this.objectHolder.setValue(null);
		verifyEvent(this.event, this.objectHolder, "bar", null);
		verifyEvent(this.wrapperEvent, this.wrapperObjectHolder, "bar", null);
		
		this.event = null;
		this.wrapperEvent = null;
		this.objectHolder.setValue("foo");
		verifyEvent(this.event, this.objectHolder, null, "foo");
		verifyEvent(this.wrapperEvent, this.wrapperObjectHolder, null, "foo");
	}

	private ChangeListener buildListener() {
		return new ChangeAdapter() {
			@Override
			public void propertyChanged(PropertyChangeEvent e) {
				ReadOnlyWritablePropertyValueModelWrapperTests.this.event = e;
			}
		};
	}

	private ChangeListener buildWrapperListener() {
		return new ChangeAdapter() {
			@Override
			public void propertyChanged(PropertyChangeEvent e) {
				ReadOnlyWritablePropertyValueModelWrapperTests.this.wrapperEvent = e;
			}
		};
	}

	private void verifyEvent(PropertyChangeEvent e, Object source, Object oldValue, Object newValue) {
		assertEquals(source, e.getSource());
		assertEquals(PropertyValueModel.VALUE, e.getPropertyName());
		assertEquals(oldValue, e.getOldValue());
		assertEquals(newValue, e.getNewValue());
	}
}

Back to the top