01: package fit.decorator.util;
02:
03: import fit.decorator.exceptions.InvalidInputException;
04: import junit.framework.TestCase;
05:
06: public class DeltaTest extends TestCase {
07:
08: private static final String INT_DATA_TYPE = "int";
09: private static final String DOUBLE_DATA_TYPE = "double";
10: private static final String STRING_DATA_TYPE = "string";
11:
12: public void testConstructorDoesNotLeaveTheObjectInAnUnstableState()
13: throws Exception {
14: assertInvalidInputException(INT_DATA_TYPE, "xyz");
15: assertInvalidInputException(INT_DATA_TYPE, "1.2");
16: assertInvalidInputException(DOUBLE_DATA_TYPE, "1.2E");
17:
18: }
19:
20: private void assertInvalidInputException(String dataType,
21: String value) {
22: try {
23: new Delta(dataType, value);
24: } catch (InvalidInputException e) {
25: assertEquals("value '" + value
26: + "' is not a valid DataType = '" + dataType + "'",
27: e.getMessage());
28: }
29: }
30:
31: public void testDeltaShouldEncapsulateTheValueAndDataType()
32: throws Exception {
33: Delta expectedDelta = new Delta(INT_DATA_TYPE, "5");
34: assertEquals(expectedDelta, expectedDelta);
35: assertFalse(expectedDelta.equals(null));
36: assertEquals(expectedDelta, new Delta("int", "5"));
37: assertEquals(expectedDelta, new Delta("Int", "5"));
38: assertEquals(expectedDelta, new Delta("integer", "5"));
39: assertEquals(expectedDelta, new Delta("IntegeR", "5"));
40: expectedDelta = new Delta(DOUBLE_DATA_TYPE, "1.2");
41: assertEquals(expectedDelta, new Delta("double", "1.20"));
42: assertEquals(expectedDelta, new Delta("Double", "1.200"));
43: expectedDelta = new Delta(STRING_DATA_TYPE, "value");
44: assertEquals(expectedDelta, new Delta("String", "value"));
45: assertEquals(new Delta(STRING_DATA_TYPE, "1.200"), new Delta(
46: "anyValueOtherThanIntAndDouble", "1.200"));
47: Delta delta = new Delta("String", "ABC") {
48: public String addTo(String originalValue, int numberofTime) {
49: return null;
50: }
51: };
52: assertFalse(expectedDelta.equals(delta));
53: }
54:
55: public void testAdd() throws Exception {
56: Delta int5 = new Delta(INT_DATA_TYPE, "5");
57: assertEquals("10", int5.addTo("5", 1));
58: assertEquals("20", int5.addTo("10", 2));
59: assertEquals("0", int5.addTo("-5", 1));
60: Delta double5Point2 = new Delta(DOUBLE_DATA_TYPE, "5.2");
61: assertEquals("10.2", double5Point2.addTo("5", 1));
62: assertEquals("10.402", double5Point2.addTo("0.002", 2));
63: assertEquals("0.2", double5Point2.addTo("-5", 1));
64: Delta stringABC = new Delta(STRING_DATA_TYPE, "ABC");
65: assertEquals("5ABC", stringABC.addTo("5", 1));
66: assertEquals("0.002ABC", stringABC.addTo("0.002", 1));
67: assertEquals("-5ABCABCABC", stringABC.addTo("-5", 3));
68: assertEquals("XYZABC", stringABC.addTo("XYZ", 1));
69: }
70:
71: public void testToString() throws Exception {
72: aasertToString("5.3", DOUBLE_DATA_TYPE);
73: aasertToString("5.3", STRING_DATA_TYPE);
74: aasertToString("5", INT_DATA_TYPE);
75: }
76:
77: private void aasertToString(String value, String dataType)
78: throws InvalidInputException {
79: String expectedToStringValue = "DataType = '" + dataType
80: + "' and value = " + value;
81: assertEquals(expectedToStringValue, new Delta(dataType, value)
82: .toString());
83: }
84: }
|