Skip to main content
summaryrefslogtreecommitdiffstats
blob: c2365c35f475f7b6f0bf14fb75ee5c7c823b43f2 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*******************************************************************************
 * Copyright (c) 2007, 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.Arrays;
import java.util.List;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.model.AbstractModel;
import org.eclipse.jpt.common.utility.internal.model.value.ListTransformationPluggablePropertyValueModelAdapter;
import org.eclipse.jpt.common.utility.internal.model.value.ListValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.SimpleListValueModel;
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.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.ListValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;
import org.eclipse.jpt.common.utility.transformer.Transformer;
import junit.framework.TestCase;

@SuppressWarnings("nls")
public class ListPropertyValueModelAdapterTests
	extends TestCase
{
	private PropertyValueModel<Boolean> adapter;
	private SimpleListValueModel<String> listModel;
	PropertyChangeEvent event;

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

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.listModel = new SimpleListValueModel<>();
		this.adapter = ListValueModelTools.propertyValueModel(this.listModel, new LocalTransformer(2, "666"));
		this.event = null;
	}

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

	private boolean booleanValue() {
		Boolean value = this.adapter.getValue();
		return (value != null) && value.booleanValue();
	}

	private boolean listModelContains(int index, String value) {
		return (this.listModel.size() > index) && ObjectTools.equals(this.listModel.get(index), value);
	}

	public void testValue() {
		assertNull(this.adapter.getValue());
		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {/* OK */}
		});
		assertFalse(this.booleanValue());
		assertFalse(this.listModelContains(2, "666"));

		this.listModel.add("111");
		assertFalse(this.booleanValue());

		this.listModel.add("222");
		assertFalse(this.booleanValue());

		this.listModel.add("666");
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));

		this.listModel.remove("666");
		assertFalse(this.booleanValue());
		assertFalse(this.listModelContains(2, "666"));

		this.listModel.add("666");
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));

		this.listModel.clear();
		assertFalse(this.booleanValue());
		assertFalse(this.listModelContains(2, "666"));

		this.listModel.add("111");
		this.listModel.add("222");
		this.listModel.add("666");
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));

		this.listModel.set(2, "333");
		assertFalse(this.booleanValue());
		assertFalse(this.listModelContains(2, "666"));

		this.listModel.set(2, "666");
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));

		this.listModel.move(0, 2);
		assertFalse(this.booleanValue());
		assertTrue(this.listModelContains(0, "666"));

		this.listModel.setListValues(Arrays.asList("111", "222", "666"));
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));
	}

	public void testEventFiring() {
		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				ListPropertyValueModelAdapterTests.this.event = e;
			}
		});
		assertNull(this.event);

		this.listModel.add("111");
		assertNull(this.event);

		this.listModel.add("222");
		assertNull(this.event);

		this.listModel.add("666");
		this.verifyEvent(false, true);

		this.listModel.remove("666");
		this.verifyEvent(true, false);

		this.listModel.add("666");
		this.verifyEvent(false, true);

		this.listModel.clear();
		this.verifyEvent(true, false);
	}

	private void verifyEvent(boolean oldValue, boolean newValue) {
		assertEquals(this.adapter, this.event.getSource());
		assertEquals(Boolean.valueOf(oldValue), this.event.getOldValue());
		assertEquals(Boolean.valueOf(newValue), this.event.getNewValue());
		this.event = null;
	}

	public void testStaleValue() {
		PropertyChangeListener listener = new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {/* OK */}
		};
		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
		this.listModel.add("111");
		this.listModel.add("222");
		this.listModel.add("666");
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));

		this.adapter.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertFalse(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));

		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertTrue(this.booleanValue());
		assertTrue(this.listModelContains(2, "666"));
	}

	public void testHasListeners() {
		assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertFalse(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES));

		ChangeListener listener = new ChangeAdapter() {
			@Override
			public void propertyChanged(PropertyChangeEvent e) {/* OK */}
		};
		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertTrue(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES));

		this.adapter.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertFalse(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES));

		this.adapter.addChangeListener(listener);
		assertTrue(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES));

		this.adapter.removeChangeListener(listener);
		assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertFalse(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES));
	}

	public void testToString1() {
		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {/* OK */}
		});
		assertTrue(this.adapter.toString().endsWith("(false)"));
		this.listModel.add("111");
		this.listModel.add("222");
		this.listModel.add("666");
		assertTrue(this.adapter.toString().endsWith("(true)"));
	}

	public void testToString3() {
		ListTransformationPluggablePropertyValueModelAdapter.Factory<String, Boolean> f = new ListTransformationPluggablePropertyValueModelAdapter.Factory<>(this.listModel, new LocalTransformer(2, "666"));
		assertTrue(f.toString().indexOf("Factory") != -1);
	}

	public void testToString4() {
		PropertyChangeListener listener = new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {/* OK */}
		};
		this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener);

		Object a = ObjectTools.get(this.adapter, "adapter");
		Object l = ObjectTools.get(a, "listener");
		assertTrue(l.toString().indexOf("AdapterListener") != -1);
	}

	public void testCtor_NPE1A() {
		Object object;
		boolean exCaught = false;
		try {
			object = ListValueModelTools.propertyValueModel(null, new LocalTransformer(2, "666"));
			fail("bogus: " + object);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testCtor_NPE1B() {
		Object object;
		boolean exCaught = false;
		try {
			object = ListValueModelTools.propertyValueModel(null, new LocalTransformer(2, "666"));
			fail("bogus: " + object);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testCtor_NPE2A() {
		Object object;
		boolean exCaught = false;
		try {
			object = ListValueModelTools.propertyValueModel(this.listModel, null);
			fail("bogus: " + object);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testCtor_NPE2B() {
		Object object;
		boolean exCaught = false;
		try {
			object = ListValueModelTools.propertyValueModel(this.listModel, null);
			fail("bogus: " + object);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testCtor_NPE4() {
		Object object;
		boolean exCaught = false;
		try {
			object = PropertyValueModelTools.propertyValueModel(null);
			fail("bogus: " + object);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}


	// ********** member class **********

	/**
	 * Transform the list to <code>true</code> if it contains the specified item
	 * at the specified index, otherwise transform it to <code>false</code>.
	 */
	static class LocalTransformer
		implements Transformer<List<String>, Boolean>
	{
		private final int index;
		private final String item;

		LocalTransformer(int index, String item) {
			super();
			this.index = index;
			this.item = item;
		}

		public Boolean transform(List<String> list) {
			return Boolean.valueOf(this.transform_(list));
		}

		public boolean transform_(List<String> list) {
			return (list.size() > this.index) && ObjectTools.equals(this.item, list.get(this.index));
		}

		@Override
		public String toString() {
			return ObjectTools.toString(this, this.item);
		}
	}
}

Back to the top