01: package fit.decorator;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import fit.Fixture;
07: import fit.Parse;
08: import fit.decorator.exceptions.InvalidInputException;
09: import fit.decorator.util.Table;
10:
11: public abstract class FixtureDecorator extends Fixture {
12: static final String ENCAPSULATED_FIXTURE_NAME = "EncapsulatedFixtureName";
13:
14: public void doTable(Parse table) {
15: if (table.parts.more == null) {
16: return;
17: }
18: validateDecoratorInput(table);
19: Parse actualHeader = table.parts.more.parts;
20: String encapsulatedFixtureName = actualHeader.text();
21: super .summary.put(ENCAPSULATED_FIXTURE_NAME,
22: encapsulatedFixtureName);
23: Fixture fixture = loadFixture(actualHeader,
24: encapsulatedFixtureName);
25: if (fixture != null) {
26: execute(fixture, table);
27: super .summary.putAll(fixture.summary);
28: counts.tally(fixture.counts);
29: }
30: }
31:
32: protected abstract void setupDecorator(String[] args)
33: throws InvalidInputException;
34:
35: protected abstract void updateColumnsBasedOnResults(Parse table);
36:
37: protected void run(Fixture fixture, Parse table) {
38: fixture.doTable(table);
39: }
40:
41: private void execute(Fixture fixture, Parse table) {
42: Table t = new Table(table);
43: Parse firstRow = t.stripFirstRow();
44: run(fixture, table);
45: t.insertAsFirstRow(firstRow);
46: updateColumnsBasedOnResults(table);
47: }
48:
49: private Fixture loadFixture(Parse actualHeader,
50: String encapsulatedFixtureName) {
51: Fixture fixture = null;
52: try {
53: fixture = loadFixture(encapsulatedFixtureName);
54: } catch (Throwable e) {
55: exception(actualHeader, e);
56: }
57: return fixture;
58: }
59:
60: private void validateDecoratorInput(Parse table) {
61: setAlternativeArgs(table);
62: try {
63: setupDecorator(super .args);
64: } catch (InvalidInputException e) {
65: exception(table.parts, e);
66: }
67: }
68:
69: void setAlternativeArgs(Parse table) {
70: List<String> argumentList = new ArrayList<String>();
71: Parse columns = table.parts.parts;
72: int size = columns.size();
73: for (int i = 0; i < size / 2; ++i) {
74: String columnValue = columns.at(i * 2 + 1).text();
75: columnValue = escapeExpectedAndActualString(columnValue);
76: argumentList.add(columnValue);
77: }
78: args = (String[]) argumentList.toArray(new String[0]);
79: }
80:
81: private String escapeExpectedAndActualString(String columnValue) {
82: int index = columnValue.indexOf("actual");
83: if (index == -1) {
84: index = columnValue.length();
85: }
86: return columnValue.substring(0, index);
87: }
88: }
|