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: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
005: // Released under the terms of the GNU General Public License version 2 or later.
006:
007: package fit;
008:
009: import junit.framework.TestCase;
010: import java.util.LinkedList;
011: import java.lang.reflect.Field;
012:
013: public class RowFixtureTest extends TestCase {
014:
015: class BusinessObject {
016: private String[] strs;
017:
018: public BusinessObject(String[] strs) {
019: this .strs = strs;
020: }
021:
022: public String[] getStrings() {
023: return strs;
024: }
025: }
026:
027: public RowFixtureTest(String name) {
028: super (name);
029: }
030:
031: public void testMatch() throws Exception {
032:
033: /*
034: Now back to the bug I found: The problem stems from the fact
035: that java doesn't do deep equality for arrays. Little known to
036: me (I forget easily ;-), java arrays are equal only if they
037: are identical. Unfortunately the 2 sort methods returns a map
038: that is directly keyed on the value of the column without
039: considering this little fact. Conclusion there is a missing
040: and a surplus row where there should be one right row.
041: -- Jacques Morel
042: */
043:
044: RowFixture fixture = new TestRowFixture();
045: TypeAdapter arrayAdapter = TypeAdapter.on(fixture,
046: BusinessObject.class.getMethod("getStrings",
047: new Class[0]));
048: Binding binding = new Binding.QueryBinding();
049: binding.adapter = arrayAdapter;
050: fixture.columnBindings = new Binding[] { binding };
051:
052: LinkedList computed = new LinkedList();
053: computed.add(new BusinessObject(new String[] { "1" }));
054: LinkedList expected = new LinkedList();
055: expected.add(new Parse("tr", "", new Parse("td", "1", null,
056: null), null));
057: fixture.match(expected, computed, 0);
058: assertEquals("right", 1, fixture.counts.right);
059: assertEquals("exceptions", 0, fixture.counts.exceptions);
060: assertEquals("missing", 0, fixture.missing.size());
061: assertEquals("surplus", 0, fixture.surplus.size());
062: }
063:
064: public void testBindColumnToField() throws Exception {
065: RowFixture fixture = new SimpleRowFixture();
066: Parse table = new Parse(
067: "<table><tr><td>field</td></tr></table>");
068: Parse tableHead = table.parts.parts;
069: fixture.bind(tableHead);
070: assertNotNull(fixture.columnBindings[0]);
071: Field field = fixture.columnBindings[0].adapter.field;
072: assertNotNull(field);
073: assertEquals("field", field.getName());
074: assertEquals(int.class, field.getType());
075: }
076:
077: private class SimpleRowFixture extends RowFixture {
078: public Class getTargetClass() // get expected type of row
079: {
080: return SimpleBusinessObject.class;
081: }
082:
083: public Object[] query() throws Exception // get rows to be compared
084: {
085: return new Object[0];
086: }
087: }
088:
089: private class SimpleBusinessObject {
090: public int field;
091: }
092:
093: private class TestRowFixture extends RowFixture {
094: public Object[] query() throws Exception // get rows to be compared
095: {
096: return new Object[0];
097: }
098:
099: public Class getTargetClass() // get expected type of row
100: {
101: return BusinessObject.class;
102: }
103: }
104: }
|