001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fit;
004:
005: import java.text.ParseException;
006: import java.util.Date;
007: import fitnesse.testutil.AbstractRegex;
008:
009: public class FixtureTest extends AbstractRegex {
010: static class HasParseMethod {
011: public static Object parse(String s) {
012: return s + " found";
013: }
014: }
015:
016: static class HasNoParseMethod {
017: }
018:
019: public void testHasParseMethod() throws Exception {
020: assertTrue(Fixture.hasParseMethod(HasParseMethod.class));
021: assertFalse(Fixture.hasParseMethod(HasNoParseMethod.class));
022: }
023:
024: public void testCallParseMethod() throws Exception {
025: Object o = Fixture.callParseMethod(HasParseMethod.class,
026: "target");
027: assertTrue(o instanceof String);
028: String s = (String) o;
029: assertEquals("target found", s);
030: }
031:
032: public void testObjectWithParseMethod() throws Exception {
033: Fixture f = new Fixture();
034: Object o = f.parse("target", HasParseMethod.class);
035: assertTrue(o instanceof String);
036: assertEquals("target found", (String) o);
037:
038: try {
039: f.parse("target", HasNoParseMethod.class);
040: fail();
041: } catch (Exception e) {
042: assertTrue(e.getMessage().startsWith("Could not parse"));
043: }
044: }
045:
046: public void testScientificDouble() throws Exception {
047: Fixture f = new Fixture();
048: Object o = f.parse("13.4", ScientificDouble.class);
049: assertTrue(o instanceof ScientificDouble);
050: assertEquals(new ScientificDouble(13.4), o);
051: }
052:
053: public void testRelationalMatching() throws Exception {
054: final String[][] table = {
055: { "fitnesse.fixtures.ColumnFixtureTestFixture" },
056: { "input", "output?" }, { "1", "_>0" } };
057: Parse page = executeFixture(table);
058: String colTwoResult = page.at(0, 2, 1).body;
059: assertTrue(colTwoResult.indexOf("<b>1</b>>0") != -1);
060: String colTwoTag = page.at(0, 2, 1).tag;
061: assertTrue(colTwoTag.indexOf("pass") != -1);
062: }
063:
064: public void testNullAndBlankStrings() throws Exception {
065: Fixture fixture = new Fixture();
066: assertNull(fixture.parse("null", String.class));
067: assertEquals("", fixture.parse("blank", String.class));
068:
069: TypeAdapter adapter = new TypeAdapter();
070: assertEquals("null", adapter.toString((String) null));
071: assertEquals("blank", adapter.toString(""));
072: }
073:
074: public void testEscape() {
075: String junk = "!@#$%^*()_-+={}|[]\\:\";',./?`";
076: assertEquals(junk, Fixture.escape(junk));
077: assertEquals("", Fixture.escape(""));
078: assertEquals("<", Fixture.escape("<"));
079: assertEquals("<<", Fixture.escape("<<"));
080: assertEquals("x<", Fixture.escape("x<"));
081: assertEquals("&", Fixture.escape("&"));
082: assertEquals("<&<", Fixture.escape("<&<"));
083: assertEquals("&<&", Fixture.escape("&<&"));
084: assertEquals("a < b && c < d", Fixture
085: .escape("a < b && c < d"));
086: }
087:
088: public void testFixtureArguments() throws Exception {
089: String prefix = "<table><tr><td>fit.Fixture</td>";
090: String suffix = "</tr></table>";
091: Fixture f = new Fixture();
092:
093: Parse table = new Parse(prefix + "<td>1</td>" + suffix);
094: f.getArgsForTable(table);
095: String[] args = f.getArgs();
096: assertEquals(1, args.length);
097: assertEquals("1", args[0]);
098:
099: table = new Parse(prefix + "" + suffix);
100: f.getArgsForTable(table);
101: args = f.getArgs();
102: assertEquals(0, args.length);
103:
104: table = new Parse(prefix + "<td>1</td><td>2</td>" + suffix);
105: f.getArgsForTable(table);
106: args = f.getArgs();
107: assertEquals(2, args.length);
108: assertEquals("1", args[0]);
109: assertEquals("2", args[1]);
110: }
111:
112: public void testParseDate() throws Exception {
113: Fixture f = new Fixture();
114: Object o = f.parse("1/2/2004", Date.class);
115: assertEquals(java.util.Date.class, o.getClass());
116: }
117:
118: public static Parse executeFixture(String[][] table)
119: throws ParseException {
120: String pageString = makeFixtureTable(table);
121: Parse page = new Parse(pageString);
122: Fixture fixture = new Fixture();
123: fixture.doTables(page);
124: return page;
125: }
126:
127: public void testCanChangeFriendlyExceptions() throws Exception {
128: Fixture fixture = new Fixture() {
129: public boolean isFriendlyException(Throwable exception) {
130: return true;
131: }
132: };
133:
134: Parse cell = new Parse("td", "", null, null);
135: fixture.exception(cell, new NullPointerException(
136: "gobble gobble"));
137: assertSubString("gobble gobble", cell.body);
138: assertNotSubString("Exception", cell.body);
139: }
140:
141: private static String makeFixtureTable(String table[][]) {
142: StringBuffer buf = new StringBuffer();
143: buf.append("<table>\n");
144: for (int ri = 0; ri < table.length; ri++) {
145: buf.append(" <tr>");
146: String[] row = table[ri];
147: for (int ci = 0; ci < row.length; ci++) {
148: String cell = row[ci];
149: buf.append("<td>").append(cell).append("</td>");
150: }
151: buf.append("</tr>\n");
152: }
153: buf.append("</table>\n");
154: return buf.toString();
155: }
156: }
157:
158: class FixtureOne extends Fixture {
159: }
160:
161: class FixtureTwo extends Fixture {
162: }
163:
164: class TheThirdFixture extends Fixture {
165: }
|