Skip to content

Commit

Permalink
Introduced parent class for Fixture named BaseFixture.
Browse files Browse the repository at this point in the history
This seems the best way to get a clean fixture (do dispatching)
while also keeping the Fixture class backwards compatible.
  • Loading branch information
MartinGijsen committed Jul 6, 2013
1 parent 33d5e65 commit ecd0b8d
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/eg/bowling/fixtures/ScoreGameTest.java
Expand Up @@ -83,6 +83,6 @@ public void test() throws Exception {
"</tr>" +
"</table>");
sg.doTable(table);
Assert.assertEquals("10 right, 0 wrong, 0 ignored, 0 exceptions",sg.counts.toString());
Assert.assertEquals("10 right, 0 wrong, 0 ignored, 0 exceptions",sg.counts());
}
}
164 changes: 164 additions & 0 deletions src/fit/BaseFixture.java
@@ -0,0 +1,164 @@
// Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.package fit;

package fit;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import fit.exception.FitFailureException;


public class BaseFixture {
public Map<String, Object> summary = new HashMap<String, Object>();

public Counts counts = new Counts();

protected String[] args;


protected Class<?> getTargetClass() {
return getClass();
}

public static BaseFixture loadFixture(String fixtureName) throws Throwable {
return FixtureLoader.instance().disgraceThenLoad(fixtureName);
}

public void getArgsForTable(Parse table) {
List<String> argumentList = new ArrayList<String>();
Parse parameters = table.parts.parts.more;
for (; parameters != null; parameters = parameters.more)
argumentList.add(parameters.text());

args = argumentList.toArray(new String[argumentList.size()]);
}

public void doTable(Parse table) {
doRows(table.parts.more);
}

public void doRows(Parse rows) {
while (rows != null) {
Parse more = rows.more;
doRow(rows);
rows = more;
}
}

public void doRow(Parse row) {
doCells(row.parts);
}

public void doCells(Parse cells) {
for (int i = 0; cells != null; i++) {
try {
doCell(cells, i);
} catch (Exception e) {
exception(cells, e);
}
cells = cells.more;
}
}

public void doCell(Parse cell, int columnNumber) {
ignore(cell);
}

// Annotation ///////////////////////////////

public void right(Parse cell) {
cell.addToTag(" class=\"pass\"");
counts.right++;
}

public void wrong(Parse cell) {
cell.addToTag(" class=\"fail\"");
counts.wrong++;
}

public void wrong(Parse cell, String actual) {
wrong(cell);
cell.addToBody(label("expected") + "<hr>" + escape(actual) + label("actual"));
}

public void ignore(Parse cell) {
cell.addToTag(" class=\"ignore\"");
counts.ignores++;
}

public void exception(Parse cell, Throwable exception) {
while (exception.getClass().equals(InvocationTargetException.class)) {
exception = ((InvocationTargetException) exception).getTargetException();
}
if (isFriendlyException(exception)) {
cell.addToBody("<hr/>" + label(exception.getMessage()));
} else {
final StringWriter buf = new StringWriter();
exception.printStackTrace(new PrintWriter(buf));
cell.addToBody("<hr><pre><div class=\"fit_stacktrace\">" + (buf.toString()) + "</div></pre>");
}
cell.addToTag(" class=\"error\"");
counts.exceptions++;
}

public boolean isFriendlyException(Throwable exception) {
return exception instanceof FitFailureException;
}

// Utility //////////////////////////////////

public String counts() {
return counts.toString();
}

public static String label(String string) {
return " <span class=\"fit_label\">" + string + "</span>";
}

public static String gray(String string) {
return " <span class=\"fit_grey\">" + string + "</span>";
}

public static String escape(String string) {
return escape(escape(string, '&', "&amp;"), '<', "&lt;");
}

public static String escape(String string, char from, String to) {
int i = -1;
while ((i = string.indexOf(from, i + 1)) >= 0) {
if (i == 0) {
string = to + string.substring(1);
} else if (i == string.length()) {
string = string.substring(0, i) + to;
} else {
string = string.substring(0, i) + to + string.substring(i + 1);
}
}
return string;
}

public static String camel(String name) {
StringBuffer b = new StringBuffer(name.length());
StringTokenizer t = new StringTokenizer(name);
b.append(t.nextToken());
while (t.hasMoreTokens()) {
String token = t.nextToken();
b.append(token.substring(0, 1).toUpperCase()); // replace spaces with camelCase
b.append(token.substring(1));
}
return b.toString();
}

public String[] getArgs() {
return Arrays.copyOf(args, args.length);
}
}
15 changes: 8 additions & 7 deletions src/fit/Dispatcher.java
Expand Up @@ -39,7 +39,6 @@ String d(long scale) {
}
}


public Dispatcher(FixtureListener listener) {
counts = new Counts();
summary = new HashMap<String, Object>();
Expand All @@ -50,7 +49,6 @@ public Dispatcher() {
this(new NullFixtureListener());
}


public static void setForcedAbort(boolean state) {
forcedAbort = state;
} //Semaphores
Expand All @@ -60,7 +58,6 @@ public static boolean aborting() {
return forcedAbort;
} //Semaphores


public void doTables(Parse tables) {
summary.put("run date", new Date());
summary.put("run elapsed time", new RunTime());
Expand All @@ -84,7 +81,7 @@ private void processTable(Parse table) {
ignore(heading); //Semaphores: ignore on failed lock
} else if (heading != null) {
try {
Fixture fixture = getLinkedFixtureWithArgs(table);
BaseFixture fixture = getLinkedFixtureWithArgs(table);
fixture.doTable(table);
} catch (Throwable e) {
exception(heading, e);
Expand All @@ -99,21 +96,25 @@ private void ignore(Parse cell) {
counts.ignores++;
}

private Fixture getLinkedFixtureWithArgs(Parse tables) throws Throwable {
private BaseFixture getLinkedFixtureWithArgs(Parse tables) throws Throwable {
Parse header = tables.at(0, 0, 0);
Fixture fixture = Fixture.loadFixture(header.text());
BaseFixture fixture = loadFixture(header.text());
fixture.counts = counts;
fixture.summary = summary;
fixture.getArgsForTable(tables);
return fixture;
}

public static BaseFixture loadFixture(String fixtureName) throws Throwable {
return FixtureLoader.instance().disgraceThenLoad(fixtureName);
}

public void exception(Parse cell, Throwable exception) {
while (exception.getClass().equals(InvocationTargetException.class)) {
exception = ((InvocationTargetException) exception).getTargetException();
}
if (isFriendlyException(exception)) {
cell.addToBody("<hr/>" + Fixture.label(exception.getMessage()));
cell.addToBody("<hr/>" + BaseFixture.label(exception.getMessage()));
} else {
final StringWriter buf = new StringWriter();
exception.printStackTrace(new PrintWriter(buf));
Expand Down

0 comments on commit ecd0b8d

Please sign in to comment.