Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1f126b396e6b5ffb364e71db3da0224a1f9324ee (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * Copyright (c) 2012, 2016 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 java.util.HashMap;
import org.eclipse.jpt.common.utility.internal.model.AbstractModel;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
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.tests.internal.TestTools;
import junit.framework.TestCase;

@SuppressWarnings("nls")
public class CompoundPropertyValueModelTests
	extends TestCase
{
	protected SimplePropertyValueModel<String> keyModel;
	protected HashMap<String, SimplePropertyValueModel<String>> valueModels;
	protected PropertyValueModel<SimplePropertyValueModel<String>> valueModelModel;

	protected PropertyValueModel<String> testModel;
	protected ChangeListener testModelListener;
	protected PropertyChangeEvent testModelEvent;

	public CompoundPropertyValueModelTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.keyModel = new SimplePropertyValueModel<>(null);
		this.valueModels = new HashMap<>();
		this.valueModelModel = this.buildValueModelModel();

		this.testModel = this.buildTestModel(this.valueModelModel);
		this.testModelListener = new TestModelListener();
	}

	protected PropertyValueModel<SimplePropertyValueModel<String>> buildValueModelModel() {
		return PropertyValueModelTools.transform(this.keyModel, new KeyTransformer());
	}

	public class KeyTransformer
		extends TransformerAdapter<String, SimplePropertyValueModel<String>>
	{
		@Override
		public SimplePropertyValueModel<String> transform(String key) {
			return CompoundPropertyValueModelTests.this.getValueModel(key);
		}
	}

	protected SimplePropertyValueModel<String> getValueModel(String key) {
		SimplePropertyValueModel<String> valueModel = this.valueModels.get(key);
		if (valueModel == null) {
			valueModel = new SimplePropertyValueModel<>(key + key);
			this.valueModels.put(key, valueModel);
		}
		return valueModel;
	}

	protected PropertyValueModel<String> buildTestModel(PropertyValueModel<SimplePropertyValueModel<String>> modelModel) {
		return PropertyValueModelTools.compound(modelModel);
	}

	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}

	public void testGetValue() {
		assertNull(this.keyModel.getValue());
		assertNull(this.valueModelModel.getValue());
		assertNull(this.testModel.getValue());

		this.testModel.addPropertyChangeListener(PropertyValueModel.VALUE, this.testModelListener);

		assertNull(this.keyModel.getValue());
		assertNull(this.valueModelModel.getValue());
		assertNull(this.testModel.getValue());

		this.keyModel.setValue("foo");
		assertEquals("foo", this.keyModel.getValue());
		assertEquals(this.valueModels.get("foo"), this.valueModelModel.getValue());
		assertEquals("foofoo", this.testModel.getValue());

		this.keyModel.setValue("bar");
		assertEquals("bar", this.keyModel.getValue());
		assertEquals(this.valueModels.get("bar"), this.valueModelModel.getValue());
		assertEquals("barbar", this.testModel.getValue());

		this.keyModel.setValue("baz");
		assertEquals("baz", this.keyModel.getValue());
		assertEquals(this.valueModels.get("baz"), this.valueModelModel.getValue());
		assertEquals("bazbaz", this.testModel.getValue());

		this.keyModel.setValue(null);
		assertNull(this.keyModel.getValue());
		assertNull(this.valueModelModel.getValue());
		assertNull(this.testModel.getValue());

		this.keyModel.setValue("foo");
		assertEquals("foo", this.keyModel.getValue());
		assertEquals(this.valueModels.get("foo"), this.valueModelModel.getValue());
		assertEquals("foofoo", this.testModel.getValue());

		this.valueModels.get("foo").setValue("XXX");
		assertEquals("foo", this.keyModel.getValue());
		assertEquals(this.valueModels.get("foo"), this.valueModelModel.getValue());
		assertEquals("XXX", this.testModel.getValue());

		this.testModel.removePropertyChangeListener(PropertyValueModel.VALUE, this.testModelListener);

		assertEquals("foo", this.keyModel.getValue()); // simple PVM
		assertNull(this.valueModelModel.getValue());
		assertNull(this.testModel.getValue());
	}

	public void testLazyListening() {
		assertTrue(((AbstractModel) this.keyModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.valueModelModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.testModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));

		this.testModel.addChangeListener(this.testModelListener);

		assertTrue(((AbstractModel) this.keyModel).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.valueModelModel).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.testModel).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.testModel.removeChangeListener(this.testModelListener);
		assertTrue(((AbstractModel) this.keyModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.valueModelModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.testModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));

		this.testModel.addPropertyChangeListener(PropertyValueModel.VALUE, this.testModelListener);
		assertTrue(((AbstractModel) this.keyModel).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.testModel.removePropertyChangeListener(PropertyValueModel.VALUE, this.testModelListener);
		assertTrue(((AbstractModel) this.keyModel).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
	}

	public void testPropertyChange() {
		this.testModel.addPropertyChangeListener(PropertyValueModel.VALUE, this.testModelListener);

		this.testModelEvent = null;
		this.keyModel.setValue("foo");
		this.verifyEvent(this.testModelEvent, this.testModel, null, "foofoo");

		this.testModelEvent = null;
		this.keyModel.setValue("bar");
		this.verifyEvent(this.testModelEvent, this.testModel, "foofoo", "barbar");

		this.testModelEvent = null;
		this.keyModel.setValue(null);
		this.verifyEvent(this.testModelEvent, this.testModel, "barbar", null);

		this.testModelEvent = null;
		this.keyModel.setValue("foo");
		this.verifyEvent(this.testModelEvent, this.testModel, null, "foofoo");

		this.testModelEvent = null;
		this.valueModels.get("foo").setValue("XXX");
		this.verifyEvent(this.testModelEvent, this.testModel, "foofoo", "XXX");
	}

	public void testConstructor_NPE() {
		boolean exCaught = false;
		try {
			this.testModel = this.buildTestModel(null);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

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

	protected class TestModelListener
		extends ChangeAdapter
	{
		@Override
		public void propertyChanged(PropertyChangeEvent event) {
			CompoundPropertyValueModelTests.this.testModelEvent = event;
		}
	}
}

Back to the top