01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.core.tests.internal.databinding.validation;
11:
12: import junit.framework.TestCase;
13:
14: import org.eclipse.core.databinding.validation.IValidator;
15: import org.eclipse.core.runtime.IStatus;
16:
17: import com.ibm.icu.text.NumberFormat;
18:
19: /**
20: * A test harness for testing string to number validators.
21: *
22: * @since 1.1
23: */
24: public abstract class StringToNumberValidatorTestHarness extends
25: TestCase {
26: private NumberFormat numberFormat;
27: private IValidator validator;
28:
29: /*
30: * (non-Javadoc)
31: *
32: * @see junit.framework.TestCase#setUp()
33: */
34: protected void setUp() throws Exception {
35: super .setUp();
36:
37: numberFormat = setupNumberFormat();
38: validator = setupValidator(numberFormat);
39: }
40:
41: /**
42: * Invoked during setup to instantiate the number format.
43: *
44: * @return number format
45: */
46: protected abstract NumberFormat setupNumberFormat();
47:
48: /**
49: * Invoked during setup to instantiate the validator.
50: *
51: * @param numberFormat
52: * @return validator
53: */
54: protected abstract IValidator setupValidator(
55: NumberFormat numberFormat);
56:
57: /**
58: * Returns a string value that will not parse.
59: *
60: * @return string
61: */
62: protected abstract String getInvalidString();
63:
64: /**
65: * Returns a number value that is out of range for the validator.
66: *
67: * @return number
68: */
69: protected abstract Number getOutOfRangeNumber();
70:
71: /**
72: * Returns a number that is in range for the validator.
73: *
74: * @return number
75: */
76: protected abstract Number getInRangeNumber();
77:
78: public void testInvalidValueReturnsError() throws Exception {
79: IStatus status = validator.validate(getInvalidString());
80: assertEquals("error severify", IStatus.ERROR, status
81: .getSeverity());
82: assertNotNull("message not null", status.getMessage());
83: }
84:
85: public void testOutOfRangeValueReturnsError() throws Exception {
86: String string = numberFormat.format(getOutOfRangeNumber());
87: IStatus status = validator.validate(string);
88: assertEquals(IStatus.ERROR, status.getSeverity());
89: assertNotNull(status.getMessage());
90: }
91:
92: public void testValidateValidValue() throws Exception {
93: String string = numberFormat.format(getInRangeNumber());
94: assertTrue(validator.validate(string).isOK());
95: }
96: }
|