Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1cd4bb25cbe5ac98740e142b61ef47155d9b1aa9 (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
/*******************************************************************************
 * Copyright (c) 2010 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.ArrayList;
import java.util.Collection;

import junit.framework.TestCase;

import org.eclipse.jpt.common.utility.internal.model.value.CompositeBooleanPropertyValueModel;
import org.eclipse.jpt.common.utility.internal.model.value.SimpleCollectionValueModel;
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.CollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.WritablePropertyValueModel;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;

public class CompositeBooleanPropertyValueModelTests extends TestCase {
	private SimplePropertyValueModel<Boolean> pvm1;
	private WritablePropertyValueModel<Boolean> pvm2;
	private WritablePropertyValueModel<Boolean> pvm3;
	private WritablePropertyValueModel<Boolean> pvm4;
	private Collection<WritablePropertyValueModel<Boolean>> collection;
	private SimpleCollectionValueModel<WritablePropertyValueModel<Boolean>> cvm;
	private PropertyValueModel<Boolean> compositePVM;
	PropertyChangeEvent event;


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

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.pvm1 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE);
		this.pvm2 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE);
		this.pvm3 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE);
		this.pvm4 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE);
		this.collection = new ArrayList<WritablePropertyValueModel<Boolean>>();
		this.collection.add(this.pvm1);
		this.collection.add(this.pvm2);
		this.collection.add(this.pvm3);
		this.collection.add(this.pvm4);
		this.cvm = new SimpleCollectionValueModel<WritablePropertyValueModel<Boolean>>(this.collection);
		
		this.compositePVM = this.buildCompositePVM(cvm);
	}

	private PropertyValueModel<Boolean> buildCompositePVM(CollectionValueModel<WritablePropertyValueModel<Boolean>> pvms) {
		return CompositeBooleanPropertyValueModel.and(pvms);
	}

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

	public void testGetValue() {
		assertNull(this.compositePVM.getValue());
		ChangeListener listener = this.buildListener();
		this.compositePVM.addChangeListener(listener);
		assertTrue(this.compositePVM.getValue().booleanValue());
	}

	public void testValueAndListeners1() {
		assertNull(this.compositePVM.getValue());
		ChangeListener listener = this.buildListener();
		this.compositePVM.addChangeListener(listener);
		assertTrue(this.compositePVM.getValue().booleanValue());
		this.compositePVM.removeChangeListener(listener);
		assertNull(this.compositePVM.getValue());
	}	

	public void testValueAndListeners2() {
		assertNull(this.compositePVM.getValue());
		ChangeListener listener = this.buildListener();
		this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertTrue(this.compositePVM.getValue().booleanValue());
		this.compositePVM.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertNull(this.compositePVM.getValue());
	}

	public void testPropertyChange1() {
		this.compositePVM.addChangeListener(this.buildListener());
		this.verifyPropertyChange();
	}

	public void testPropertyChange2() {
		this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener());
		this.verifyPropertyChange();
	}

	private void verifyPropertyChange() {
		this.event = null;
		this.pvm1.setValue(Boolean.FALSE);
		this.verifyEvent(true, false);

		this.event = null;
		this.pvm2.setValue(Boolean.FALSE);
		assertNull(this.event);  // no change

		this.event = null;
		this.pvm2.setValue(Boolean.TRUE);
		assertNull(this.event);  // no change

		this.event = null;
		this.pvm1.setValue(Boolean.TRUE);
		this.verifyEvent(false, true);

		this.event = null;
		this.pvm4.setValue(Boolean.FALSE);
		this.verifyEvent(true, false);
	}

	public void testCollectionChange1() {
		this.compositePVM.addChangeListener(this.buildListener());
		this.verifyCollectionChange();
	}

	public void testCollectionChange2() {
		this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener());
		this.verifyCollectionChange();
	}

	private void verifyCollectionChange() {
		this.event = null;
		WritablePropertyValueModel<Boolean> pvm = new SimplePropertyValueModel<Boolean>(Boolean.FALSE);
		this.cvm.add(pvm);
		this.verifyEvent(true, false);

		this.event = null;
		this.cvm.remove(pvm);
		this.verifyEvent(false, true);

		this.event = null;
		this.cvm.clear();
		this.verifyEvent(Boolean.TRUE, null);

		Collection<WritablePropertyValueModel<Boolean>> c2 = new ArrayList<WritablePropertyValueModel<Boolean>>();
		c2.add(this.pvm1);
		c2.add(this.pvm2);
		this.event = null;
		this.cvm.setValues(c2);
		this.verifyEvent(null, Boolean.TRUE);
	}

	public void testLazyListening1() {
		assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		ChangeListener listener = this.buildListener();

		this.compositePVM.addChangeListener(listener);
		assertTrue(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));

		this.compositePVM.removeChangeListener(listener);
		assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
	}	

	public void testLazyListening2() {
		assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		ChangeListener listener = this.buildListener();

		this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertTrue(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));

		this.compositePVM.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
		assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
	}	

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

	private void verifyEvent(boolean oldValue, boolean newValue) {
		this.verifyEvent(Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
	}

	private void verifyEvent(Boolean oldValue, Boolean newValue) {
		assertEquals(this.compositePVM, this.event.getSource());
		assertEquals(PropertyValueModel.VALUE, this.event.getPropertyName());
		assertEquals(oldValue, this.event.getOldValue());
		assertEquals(newValue, this.event.getNewValue());
	}

}

Back to the top