001: package fit.decorator;
002:
003: import java.text.ParseException;
004:
005: import fit.Parse;
006: import fit.decorator.exceptions.InvalidInputException;
007: import fit.decorator.util.Delta;
008: import fit.decorator.util.TestCaseHelper;
009:
010: public class IncrementColumnsValueTest extends FixtureDecoratorTestCase {
011: private static final String FIRST_HTML_ROW = "<tr><td>"
012: + IncrementColumnsValue.class.getName()
013: + "</td><td>ColumnName</td><td>of type</td><td>int</td><td>by</td><td>1</td></tr>";
014: private FixtureDecorator decorator = new IncrementColumnsValue();
015:
016: protected String geDecoratorHTMLRow() {
017: return FIRST_HTML_ROW;
018: }
019:
020: protected int numberOfAssertionsOnDecorator() {
021: return 0;
022: }
023:
024: public void testSetupDecoratorShouldAddColumnNameDataTypeAndDeltaToSummary()
025: throws Exception {
026: decorator
027: .setupDecorator(new String[] { "Column1", "int", "1" });
028: assertEquals("Column1", (String) decorator.summary
029: .get(IncrementColumnsValue.COLUMN_NAME));
030: assertEquals(new Delta("int", "1"), decorator.summary
031: .get(IncrementColumnsValue.DELTA));
032: }
033:
034: public void testSetupDecoratorShouldAddColumnNameDataTypeAndDeltaToSummaryForDates()
035: throws Exception {
036: decorator
037: .setupDecorator(new String[] { "Column1", "date", "5" });
038: assertEquals("Column1", (String) decorator.summary
039: .get(IncrementColumnsValue.COLUMN_NAME));
040: assertEquals(new Delta("date", "5"), decorator.summary
041: .get(IncrementColumnsValue.DELTA));
042: }
043:
044: public void testSetupDecoratorShouldThrowInvalidInputExceptionIfColumnNameIsNotSpecified()
045: throws ParseException {
046: assertInvalidInputException(new String[] {},
047: "Column name, Data type and Delta Value must be specified");
048: }
049:
050: public void testSetupDecoratorShouldThrowInvalidInputExceptionIfDataTypeIsNotSpecified()
051: throws ParseException {
052: assertInvalidInputException(new String[] { "Column1" },
053: "Column name, Data type and Delta Value must be specified");
054: }
055:
056: public void testSetupDecoratorShouldThrowInvalidInputExceptionIfDeltaValueIsNotSpecified()
057: throws ParseException {
058: assertInvalidInputException(
059: new String[] { "Column1", "double" },
060: "Column name, Data type and Delta Value must be specified");
061: }
062:
063: public void testSetupDecoratorShouldThrowInvalidInputExceptionIfDataTypeAndDeltaValueDoNotMatch()
064: throws ParseException {
065: assertInvalidInputException(new String[] { "Column1", "double",
066: "xyz" },
067: "value 'xyz' is not a valid DataType = 'double'");
068: assertInvalidInputException(new String[] { "Column1", "int",
069: "1.2" }, "value '1.2' is not a valid DataType = 'int'");
070: assertInvalidInputException(new String[] { "Column1", "date",
071: "12-5-2006" },
072: "value '12-5-2006' is not a valid DataType = 'date'");
073: }
074:
075: public void testSetupDecoratorShouldDefaultDataTypeToStringIfItDoesNotMatch_int_integer_double_or_date()
076: throws Exception {
077: decorator
078: .setupDecorator(new String[] { "Column1", "long", "1" });
079: assertEquals(new Delta("String", "1"), decorator.summary
080: .get(IncrementColumnsValue.DELTA));
081: }
082:
083: public void testSetupDecoratorShouldDefaultDataTypeTo_int()
084: throws Exception {
085: decorator.setupDecorator(new String[] { "Column1", "integer",
086: "1" });
087: assertEquals(new Delta("INT", "1"), decorator.summary
088: .get(IncrementColumnsValue.DELTA));
089: }
090:
091: public void testShouldIncrementColumnValuesFromSecondRowForTheGivenColumnName()
092: throws Exception {
093: String fitPage = "<table><tr><td>"
094: + IncrementColumnsValue.class.getName()
095: + "</td><td>numerator</td><td>of type</td><td>int</td><td>by</td><td>5</td></tr>"
096: + "<tr><td>eg.Division</td></tr>"
097: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
098: + "<tr><td>10</td><td>2</td><td>5</td></tr><tr><td>10</td><td>3</td><td>5</td></tr>"
099: + "<tr><td>10</td><td>3</td><td>5</td></tr></table>";
100: decorator.doTable(new Parse(fitPage));
101: TestCaseHelper.assertCounts(TestCaseHelper.counts(2, 1, 0, 0),
102: decorator.counts);
103: }
104:
105: public void testShouldIncrementDateColumnValuesFromSecondRowForTheGivenColumnName()
106: throws Exception {
107: String fitPage = "<table><tr><td>"
108: + IncrementColumnsValue.class.getName()
109: + "</td><td>inDate</td><td>of type</td><td>date</td><td>by</td><td>5</td></tr>"
110: + "<tr><td>eg.GetDates</td></tr><tr><td>inDate</td><td>updatedDate()</td></tr>"
111: + "<tr><td>12/02/2006</td><td>12/02/2006</td></tr>"
112: + "<tr><td>12/02/2006</td><td>12/07/2006</td></tr>"
113: + "<tr><td>12/22/2006</td><td>01/01/2007</td></tr></table>";
114: decorator.doTable(new Parse(fitPage));
115: TestCaseHelper.assertCounts(TestCaseHelper.counts(3, 0, 0, 0),
116: decorator.counts);
117: }
118:
119: public void testShouldLeaveTheTableAsItIsIfTablesHasLessThanFourRows()
120: throws Exception {
121: String fitPage = "<table><tr><td>"
122: + IncrementColumnsValue.class.getName()
123: + "</td><td>numerator</td><td>of type</td><td>int</td><td>by</td><td>5</td></tr>"
124: + "<tr><td>eg.Division</td></tr>"
125: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
126: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
127: decorator.doTable(new Parse(fitPage));
128: TestCaseHelper.assertCounts(TestCaseHelper.counts(1, 0, 0, 0),
129: decorator.counts);
130: }
131:
132: public void testShouldHandleExceptionsByIgnoringDecorator()
133: throws Exception {
134: String fitPage = "<table><tr><td>"
135: + IncrementColumnsValue.class.getName()
136: + "</td><td>invalidColumnName</td><td>of type</td><td>int</td><td>by</td><td>5</td></tr>"
137: + "<tr><td>eg.Division</td></tr>"
138: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
139: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
140: decorator.doTable(new Parse(fitPage));
141: TestCaseHelper.assertCounts(TestCaseHelper.counts(1, 0, 0, 0),
142: decorator.counts);
143: }
144:
145: public void testShouldIncrementMultipleColumnsValueIfDecoratorIsPiped()
146: throws Exception {
147: String fitPage = "<table><tr><td>"
148: + IncrementColumnsValue.class.getName()
149: + "</td><td>numerator</td><td>of type</td><td>int</td><td>by</td><td>5</td></tr>"
150: + "<tr><td>"
151: + IncrementColumnsValue.class.getName()
152: + "</td><td>denominator</td><td>of type</td><td>int</td><td>by</td><td>1</td></tr>"
153: + "<tr><td>eg.Division</td></tr>"
154: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
155: + "<tr><td>10</td><td>2</td><td>5</td></tr>"
156: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
157: decorator.doTable(new Parse(fitPage));
158: TestCaseHelper.assertCounts(TestCaseHelper.counts(2, 0, 0, 0),
159: decorator.counts);
160: }
161:
162: private void assertInvalidInputException(String[] args,
163: String errorMsg) throws ParseException {
164: try {
165: decorator.setupDecorator(args);
166: fail("Should blow up");
167: } catch (InvalidInputException e) {
168: assertEquals(errorMsg, e.getMessage());
169: }
170: }
171: }
|