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