Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6eea845cb8f0d604af63c61f16c2c238ef878f6 (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
/*******************************************************************************
 * Copyright (c) 2007, 2009 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *     Matthew Hall - bug 270461
 ******************************************************************************/

package org.eclipse.core.tests.databinding;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

import org.eclipse.core.databinding.BindingException;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.internal.databinding.conversion.IdentityConverter;
import org.eclipse.core.internal.databinding.validation.NumberToByteValidator;
import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator;
import org.eclipse.core.internal.databinding.validation.NumberToFloatValidator;
import org.eclipse.core.internal.databinding.validation.NumberToIntegerValidator;
import org.eclipse.core.internal.databinding.validation.NumberToLongValidator;
import org.eclipse.core.internal.databinding.validation.NumberToShortValidator;
import org.eclipse.core.internal.databinding.validation.NumberToUnboundedNumberValidator;
import org.eclipse.core.internal.databinding.validation.StringToByteValidator;
import org.eclipse.core.internal.databinding.validation.StringToDateValidator;
import org.eclipse.core.internal.databinding.validation.StringToDoubleValidator;
import org.eclipse.core.internal.databinding.validation.StringToFloatValidator;
import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator;
import org.eclipse.core.internal.databinding.validation.StringToLongValidator;
import org.eclipse.core.internal.databinding.validation.StringToShortValidator;
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;

/**
 * @since 1.1
 */
public class UpdateValueStrategyTest extends AbstractDefaultRealmTestCase {
	public void testDefaultValidatorForStringToInteger() throws Exception {
		assertDefaultValidator(String.class, Integer.class,
				StringToIntegerValidator.class);
	}

	public void testDefaultValidatorForStringToIntegerPrimitive()
			throws Exception {
		assertDefaultValidator(String.class, Integer.TYPE,
				StringToIntegerValidator.class);
	}

	public void testDefaultValidatorForStringToLong() throws Exception {
		assertDefaultValidator(String.class, Long.class,
				StringToLongValidator.class);
	}

	public void testDefaultValidatorForStringToLongPrimitive() throws Exception {
		assertDefaultValidator(String.class, Long.TYPE,
				StringToLongValidator.class);
	}

	public void testDefaultValidatorForStringToFloat() throws Exception {
		assertDefaultValidator(String.class, Float.class,
				StringToFloatValidator.class);
	}

	public void testDefaultValidatorForStringToFloatPrimitive()
			throws Exception {
		assertDefaultValidator(String.class, Float.TYPE,
				StringToFloatValidator.class);
	}

	public void testDefaultValidatorForStringToDouble() throws Exception {
		assertDefaultValidator(String.class, Double.class,
				StringToDoubleValidator.class);
	}

	public void testDefaultValidatorForStringToDoublePrimitive()
			throws Exception {
		assertDefaultValidator(String.class, Double.TYPE,
				StringToDoubleValidator.class);
	}

	public void testDefaultValidatorForStringToByte() throws Exception {
		assertDefaultValidator(String.class, Byte.class,
				StringToByteValidator.class);
	}

	public void testDefaultValidatorForStringToBytePrimitive() throws Exception {
		assertDefaultValidator(String.class, Byte.TYPE,
				StringToByteValidator.class);
	}

	public void testDefaultValidatorForStringToShort() throws Exception {
		assertDefaultValidator(String.class, Short.class,
				StringToShortValidator.class);
	}

	public void testDefaultValidatorForStringToShortPrimitive()
			throws Exception {
		assertDefaultValidator(String.class, Short.TYPE,
				StringToShortValidator.class);
	}

	public void testDefaultValidatorForStringToDate() throws Exception {
		assertDefaultValidator(String.class, Date.class,
				StringToDateValidator.class);
	}

	public void testDefaultValidatorForNumberToByte() throws Exception {
		assertDefaultValidator(Integer.class, Byte.class,
				NumberToByteValidator.class);
	}

	public void testDefaultValidatorForNumberToShort() throws Exception {
		assertDefaultValidator(Integer.class, Short.class,
				NumberToShortValidator.class);
	}

	public void testDefaultValidatorForNumberToInteger() throws Exception {
		assertDefaultValidator(Short.class, Integer.class,
				NumberToIntegerValidator.class);
	}

	public void testDefaultValidatorForNumberToLong() throws Exception {
		assertDefaultValidator(Short.class, Long.class,
				NumberToLongValidator.class);
	}

	public void testDefaultValidatorForNumberToFloat() throws Exception {
		assertDefaultValidator(Short.class, Float.class,
				NumberToFloatValidator.class);
	}

	public void testDefaultValidatorForNumberToDouble() throws Exception {
		assertDefaultValidator(Short.class, Double.class,
				NumberToDoubleValidator.class);
	}

	public void testDefaultValidatorForNumberToBigInteger() throws Exception {
		assertDefaultValidator(Short.class, BigInteger.class,
				NumberToUnboundedNumberValidator.class);
	}

	public void testDefaultValidatorForNumberToBigDecimal() throws Exception {
		assertDefaultValidator(Short.class, BigDecimal.class,
				NumberToUnboundedNumberValidator.class);
	}

	public void testCachesDefaultedValidators() throws Exception {
		WritableValue source = WritableValue.withValueType(String.class);
		WritableValue destination = WritableValue.withValueType(Integer.class);

		UpdateValueStrategyStub strategy = new UpdateValueStrategyStub();
		strategy.fillDefaults(source, destination);

		IValidator validator = strategy.validator;
		assertNotNull(validator);

		strategy = new UpdateValueStrategyStub();
		strategy.fillDefaults(source, destination);

		assertSame(validator, strategy.validator);
	}

	public void testFillDefaults_AssertSourceTypeExtendsConverterFromType() {
		// Valid use: source type String extends converter from-type Object
		UpdateValueStrategyStub strategy = new UpdateValueStrategyStub();
		strategy
				.setConverter(new IdentityConverter(Object.class, Object.class));
		strategy.fillDefaults(WritableValue.withValueType(String.class),
				WritableValue.withValueType(Object.class));

		// Invalid use: source type Object does not extend converter from-type
		// String
		strategy = new UpdateValueStrategyStub();
		strategy
				.setConverter(new IdentityConverter(String.class, Object.class));
		try {
			strategy.fillDefaults(WritableValue.withValueType(Object.class),
					WritableValue.withValueType(Object.class));
			fail("Expected BindingException since Object does not extend String");
		} catch (BindingException expected) {
		}
	}

	public void testFillDefaults_AssertConverterToTypeExtendsDestinationType() {
		// Valid use: converter to-type String extends destination type Object
		UpdateValueStrategyStub strategy = new UpdateValueStrategyStub();
		strategy
				.setConverter(new IdentityConverter(Object.class, String.class));
		strategy.fillDefaults(WritableValue.withValueType(Object.class),
				WritableValue.withValueType(Object.class));

		// Invalid use: converter to-type Object does not extend destination
		// type String
		strategy = new UpdateValueStrategyStub();
		strategy
				.setConverter(new IdentityConverter(Object.class, Object.class));
		try {
			strategy.fillDefaults(WritableValue.withValueType(Object.class),
					WritableValue.withValueType(String.class));
			fail("Expected BindingException since Object does not extend String");
		} catch (BindingException expected) {
		}
	}

	private void assertDefaultValidator(Class fromType, Class toType,
			Class validatorType) {
		WritableValue source = WritableValue.withValueType(fromType);
		WritableValue destination = WritableValue.withValueType(toType);

		UpdateValueStrategyStub strategy = new UpdateValueStrategyStub();
		strategy.fillDefaults(source, destination);

		IValidator validator = strategy.validator;
		assertNotNull("validator not null", validator);
		assertTrue("converter instanceof " + validatorType, validatorType
				.isInstance(validator));
	}

	class UpdateValueStrategyStub extends UpdateValueStrategy {
		IValidator validator;

		protected void fillDefaults(IObservableValue source,
				IObservableValue destination) {
			super.fillDefaults(source, destination);
		}

		protected IValidator createValidator(Object fromType, Object toType) {
			validator = super.createValidator(fromType, toType);
			return validator;
		}
	}
}

Back to the top