001: package fit.decorator.util;
002:
003: import fit.Parse;
004: import fit.decorator.exceptions.InvalidInputException;
005: import junit.framework.TestCase;
006:
007: public class TableTest extends TestCase {
008: private String fitPage = "<table><tr><td>eg.Division</td></tr>"
009: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
010: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
011: private Table table;
012:
013: protected void setUp() throws Exception {
014: super .setUp();
015: table = new Table(fitPage);
016: }
017:
018: public void testToStringShouldParseTableWithMultipleRowsAndColumns()
019: throws Exception {
020: assertTable(fitPage);
021: }
022:
023: public void testConstructorShouldBuildTableFromParseObject()
024: throws Exception {
025: String expectedTableContents = "<tr><td>fit.decorator.MaxTime</td><td>10</td></tr>";
026: Parse parse = new Parse(expectedTableContents, new String[] {
027: "tr", "td" });
028: table = new Table(parse);
029: assertTable(expectedTableContents);
030: }
031:
032: public void testCopyAndAppendLastRow() throws Exception {
033: table.copyAndAppendLastRow(1);
034: String expectedTableContents = "<table><tr><td>eg.Division</td></tr>"
035: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
036: + "<tr><td>10</td><td>2</td><td>5</td></tr>"
037: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
038: assertTable(expectedTableContents);
039: }
040:
041: public void testCopyAndAppendLastRowMultipleTimes()
042: throws Exception {
043: table.copyAndAppendLastRow(3);
044: String expectedTableContents = "<table><tr><td>eg.Division</td></tr>"
045: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
046: + "<tr><td>10</td><td>2</td><td>5</td></tr><tr><td>10</td><td>2</td><td>5</td></tr>"
047: + "<tr><td>10</td><td>2</td><td>5</td></tr>"
048: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
049: assertTable(expectedTableContents);
050: }
051:
052: public void testCopyAndAppendLastRowZeroTime() throws Exception {
053: table.copyAndAppendLastRow(0);
054: assertTable(fitPage);
055: }
056:
057: public void testIncrementColumnValueThrowsInvalidInputExceptionIfColumnNameIsNotFound()
058: throws Exception {
059: try {
060: table.incrementColumnValues(1, "invalidColumnName", null);
061: } catch (InvalidInputException e) {
062: assertEquals(
063: "'invalidColumnName' was not found in the table "
064: + fitPage, e.getMessage());
065: }
066: }
067:
068: public void testIncrementColumnValueShouldAddTheGivenDeltaToAllRowsOfTheGivenColumn()
069: throws Exception {
070: table.incrementColumnValues("denominator",
071: new Delta("int", "1"), 1);
072: assertTable(fitPage);
073: }
074:
075: public void testIncrementColumnValueShouldIncrementallyAddTheGivenDeltaMultipleTimes()
076: throws Exception {
077: table.incrementColumnValues(3, "denominator", new Delta("int",
078: "1"));
079: String expectedFitPage = "<table><tr><td>eg.Division</td></tr>"
080: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
081: + "<tr><td>10</td><td>2</td><td>5</td></tr><tr><td>10</td><td>3</td><td>5</td></tr>"
082: + "<tr><td>10</td><td>4</td><td>5</td></tr></table>";
083: assertTable(expectedFitPage);
084: }
085:
086: public void testInsertAsFirstRow() throws Exception {
087: Parse firstRow = new Parse("<tr><td>first row</td></tr>",
088: new String[] { "tr", "td" });
089: table.insertAsFirstRow(firstRow);
090: String expectedTableContents = "<table><tr><td>first row</td></tr><tr><td>eg.Division</td></tr>"
091: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
092: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
093: assertTable(expectedTableContents);
094: }
095:
096: public void testStripFirstRow() throws Exception {
097: table.stripFirstRow();
098: String expectedTableContents = "<table>"
099: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
100: + "<tr><td>10</td><td>2</td><td>5</td></tr></table>";
101: assertTable(expectedTableContents);
102: }
103:
104: public void testColumnNumberShouldReturnColumnNumberForTheGivenColumnName()
105: throws Exception {
106: assertEquals(0, table
107: .columnNumberContainingText("numerator", 1));
108: assertEquals(1, table.columnNumberContainingText("denominator",
109: 1));
110: assertEquals(2, table.columnNumberContainingText("quotient()",
111: 1));
112: }
113:
114: public void testColumnNumberShouldThrowsInvalidInputExceptionIfColumnNameIsNotFound()
115: throws Exception {
116: try {
117: table.columnNumberContainingText("invalidColumnName", 1);
118: } catch (InvalidInputException e) {
119: assertEquals(
120: "'invalidColumnName' was not found in the table "
121: + fitPage, e.getMessage());
122: }
123: }
124:
125: public void testColumnValueShouldReturnColumnValueForTheGivenColumnNumber()
126: throws Exception {
127: assertEquals("10", table.columnValue(2, 0));
128: assertEquals("2", table.columnValue(2, 1));
129: assertEquals("5", table.columnValue(2, 2));
130: }
131:
132: public void testGetLastRow() throws Exception {
133: Parse lastRow = table.lastRow();
134: String expectedLastRow = "<tr><td>10</td><td>2</td><td>5</td></tr>";
135: assertParseObject(expectedLastRow, lastRow);
136: }
137:
138: public void testGetSecondLastRow() throws Exception {
139: String fitPage = "<table><tr><td>eg.Division</td></tr>"
140: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
141: + "<tr><td>10</td><td>2</td><td>5</td></tr>"
142: + "<tr><td>5</td><td>1</td><td>5</td></tr></table>";
143: table = new Table(fitPage);
144: Parse secondLastRow = table.secondLastRow(table.lastRow());
145: String expectedSecondLastRow = "<tr><td>10</td><td>2</td><td>5</td></tr>";
146: assertParseObject(expectedSecondLastRow, secondLastRow);
147: }
148:
149: public void testRowNumberContainingTextShouldReturnIndexOfTheRowContainingTheText()
150: throws Exception {
151: assertEquals(0, table.rowNumberContainingText("eg.Division"));
152: assertEquals(1, table.rowNumberContainingText("numerator"));
153: assertEquals(1, table.rowNumberContainingText("denominator"));
154: }
155:
156: public void testRowNumberContainingTextShouldThrowInvalidInputExceptionIfSearchTextIsNotFound()
157: throws Exception {
158: try {
159: table.rowNumberContainingText("invalidColumnName");
160: } catch (InvalidInputException e) {
161: assertEquals(
162: "'invalidColumnName' was not found in the table "
163: + fitPage, e.getMessage());
164: }
165: }
166:
167: public void testCopyAndAppendLastRowShouldLeaveTheTableAsItIsIfTotalRowsAreLessThanThree()
168: throws Exception {
169: String fitPage = "<table><tr><td>eg.Division</td></tr>"
170: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr></table>";
171: table = new Table(fitPage);
172: table.copyAndAppendLastRow(4);
173: assertTable(fitPage);
174: }
175:
176: private void assertTable(String expectedTableContents) {
177: assertEquals(expectedTableContents, table.toString());
178: }
179:
180: private void assertParseObject(String expectedTableContents,
181: Parse parse) {
182: assertEquals(expectedTableContents, new Table(parse).toString());
183: }
184: }
|