001: package fit.decorator;
002:
003: import fit.Counts;
004: import fit.Fixture;
005: import fit.Parse;
006: import fit.decorator.FixtureDecorator;
007: import fit.decorator.exceptions.InvalidInputException;
008: import fit.decorator.util.TestCaseHelper;
009: import fit.decorator.util.Timer;
010:
011: import junit.framework.TestCase;
012:
013: import java.text.ParseException;
014:
015: public abstract class FixtureDecoratorTestCase extends TestCase {
016: protected static final long ELAPSED = 20;
017: protected Timer stopWatch = new Timer() {
018: public void start() {
019: }
020:
021: public long elapsed() {
022: return ELAPSED;
023: }
024: };
025:
026: public void testShouldBeAbleToExecuteEncapsulatedFixture()
027: throws ParseException {
028: String fitPage = "<table>"
029: + geDecoratorHTMLRow()
030: + "<tr><td>eg.Division</td></tr>"
031: + "<tr><td>numerator</td><td>denominator</td><td>quotient()</td></tr>"
032: + "<tr><td>100</td><td>4</td><td>25</td></tr></table>";
033: Fixture decorator = new Fixture();
034: decorator.doTables(new Parse(fitPage));
035: int right = 1 + numberOfAssertionsOnDecorator();
036: TestCaseHelper.assertCounts(TestCaseHelper.counts(right, 0, 0,
037: 0), decorator.counts);
038: }
039:
040: public void testShouldBeAbleToFindEncapsulatedFixtureName()
041: throws Exception {
042: String fitPage = "<table>" + geDecoratorHTMLRow()
043: + "<tr><td>eg.Division</td></tr></table>";
044: Fixture decorator = new Fixture();
045: decorator.doTables(new Parse(fitPage));
046: String encapsulatedFixtureName = (String) decorator.summary
047: .get(FixtureDecorator.ENCAPSULATED_FIXTURE_NAME);
048: assertEquals("eg.Division", encapsulatedFixtureName);
049: }
050:
051: public void testShouldBeAbleToInstantiateEncapsulatedFixture()
052: throws Exception {
053: String fitPage = "<table>" + geDecoratorHTMLRow() + "<tr><td>"
054: + TestFixture.class.getName() + "</td></tr></table>";
055: Fixture decorator = new Fixture();
056: decorator.doTables(new Parse(fitPage));
057: String encapsulatedFixtureName = (String) decorator.summary
058: .get(FixtureDecorator.ENCAPSULATED_FIXTURE_NAME);
059: assertEquals("fit.decorator.TestFixture",
060: encapsulatedFixtureName);
061: }
062:
063: public void testShouldDoNothingIfThereIsNoEncapsulatedFixturePresent()
064: throws Exception {
065: String fitPage = "<table>" + geDecoratorHTMLRow() + "</table>";
066: Fixture decorator = new Fixture();
067: decorator.doTables(new Parse(fitPage));
068: assertNull(decorator.summary
069: .get(FixtureDecorator.ENCAPSULATED_FIXTURE_NAME));
070: TestCaseHelper.assertCounts(TestCaseHelper.counts(0, 0, 0, 0),
071: decorator.counts);
072: }
073:
074: public void testShouldMarkExceptionIfEncapsulatingFixtureNameIsInvalid()
075: throws Exception {
076: String fitPage = "<table>" + geDecoratorHTMLRow()
077: + "<tr><td>invalidClass</td></tr></table>";
078: Fixture decorator = new Fixture();
079: decorator.doTables(new Parse(fitPage));
080: assertEquals(1, decorator.counts.exceptions);
081: String encapsulatedFixtureName = (String) decorator.summary
082: .get(FixtureDecorator.ENCAPSULATED_FIXTURE_NAME);
083: assertEquals("invalidClass", encapsulatedFixtureName);
084: }
085:
086: public void testShouldStripFirstRowAndPassRestOfTheTableToEncapsulatedFixture()
087: throws Exception {
088: String fitPage = "<table>" + geDecoratorHTMLRow() + "<tr><td>"
089: + TestFixture.class.getName() + "</td></tr></table>";
090: Fixture decorator = new Fixture();
091: decorator.doTables(new Parse(fitPage));
092: String expectedTableContents = "<table><tr><td>"
093: + TestFixture.class.getName() + "</td></tr></table>";
094: assertEquals(expectedTableContents, decorator.summary
095: .get(TestFixture.TABLE_CONTENTS));
096: }
097:
098: public void testShouldHandleInvalidInputExceptionIfThrownBySetUpMethod()
099: throws Exception {
100: String fitPage = "<table>" + geWrongDecoratorHTMLRow()
101: + "<tr><td>" + TestFixture.class.getName()
102: + "</td></tr></table>";
103: Fixture decorator = new Fixture();
104: decorator.doTables(new Parse(fitPage));
105: TestCaseHelper.assertCounts(TestCaseHelper.counts(0, 0, 0, 1),
106: decorator.counts);
107: }
108:
109: public void testSetAlternativeArgsShouldStoreOddNumberedColumnsToArgsVariable()
110: throws Exception {
111: String fitPage = "<table><tr><td>xyz</td><td>1</td><td>skip1</td><td>2</td><td>skip2</td>"
112: + "<td>3</td><td>skip3</td></tr></table>";
113: FixtureDecorator decorator = dummyFitDecorator();
114: Parse table = new Parse(fitPage);
115: decorator.setAlternativeArgs(table);
116: assertArray(new String[] { "1", "2", "3" }, decorator.getArgs());
117: }
118:
119: public void testSetAlternativeArgsShouldIgnoreExpectedAndActualStrings()
120: throws Exception {
121: String fitPage = "<table><tr><td>xyz</td><td>1</td><td>skip1</td><td>2<hr>actual 4</td><td>skip2</td>"
122: + "<td>3</td><td>skip3</td></tr></table>";
123: FixtureDecorator decorator = dummyFitDecorator();
124: Parse table = new Parse(fitPage);
125: decorator.setAlternativeArgs(table);
126: assertArray(new String[] { "1", "2", "3" }, decorator.getArgs());
127: }
128:
129: private FixtureDecorator dummyFitDecorator() {
130: FixtureDecorator decorator = new FixtureDecorator() {
131: protected void setupDecorator(String[] args)
132: throws InvalidInputException {
133: }
134:
135: protected void updateColumnsBasedOnResults(Parse table) {
136: }
137: };
138: return decorator;
139: }
140:
141: private void assertArray(String[] expected, String[] actual) {
142: assertEquals(expected.length, actual.length);
143: for (int i = 0; i < actual.length; i++) {
144: assertEquals(expected[i], actual[i]);
145: }
146: }
147:
148: private String geWrongDecoratorHTMLRow() {
149: return "<tr><td>" + Loop.class.getName() + "</td></tr>";
150: }
151:
152: protected abstract String geDecoratorHTMLRow();
153:
154: protected abstract int numberOfAssertionsOnDecorator();
155:
156: protected void executeAndAssert(Counts expected, String fitPage,
157: Fixture fixture) throws ParseException {
158: fixture.doTable(new Parse(fitPage));
159: TestCaseHelper.assertCounts(expected, fixture.counts);
160: }
161: }
|