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 fitnesse.testutil.AbstractRegex;
006: import fit.exception.*;
007:
008: public class BindingTest extends AbstractRegex {
009: private TestFixture fixture;
010:
011: private Parse cell1;
012:
013: private Parse cell2;
014:
015: private Parse cell3;
016:
017: private Parse cell4;
018:
019: protected void setUp() throws Exception {
020: fixture = new TestFixture();
021: Parse table = new Parse(
022: "<table><tr><td>123</td><td>321</td><td>abc</td><td></td></tr></table>");
023: cell1 = table.parts.parts;
024: cell2 = table.parts.parts.more;
025: cell3 = table.parts.parts.more.more;
026: cell4 = table.parts.parts.more.more.more;
027: }
028:
029: public void testConstruction() throws Throwable {
030: assertEquals(Binding.QueryBinding.class, Binding.create(
031: fixture, "intMethod()").getClass());
032: assertEquals(Binding.QueryBinding.class, Binding.create(
033: fixture, "intMethod?").getClass());
034: assertEquals(Binding.QueryBinding.class, Binding.create(
035: fixture, "intMethod!").getClass());
036: assertEquals(Binding.SetBinding.class, Binding.create(fixture,
037: "intField").getClass());
038: assertEquals(Binding.RecallBinding.class, Binding.create(
039: fixture, "intField=").getClass());
040: assertEquals(Binding.SaveBinding.class, Binding.create(fixture,
041: "=intMethod()").getClass());
042: assertEquals(Binding.SaveBinding.class, Binding.create(fixture,
043: "=intField").getClass());
044: }
045:
046: public static class TestFixture extends Fixture {
047: public int intField = 0;
048:
049: public int intMethod() {
050: return intField;
051: }
052: }
053:
054: public void testQueryBinding() throws Throwable {
055: Binding binding = Binding.create(fixture, "intMethod()");
056: binding.doCell(fixture, cell1);
057: assertEquals(1, fixture.counts.wrong);
058:
059: fixture.intField = 321;
060: binding.doCell(fixture, cell2);
061: assertEquals(1, fixture.counts.right);
062: }
063:
064: public void testSetBinding() throws Throwable {
065: Binding binding = Binding.create(fixture, "intField");
066: binding.doCell(fixture, cell1);
067: assertEquals(123, fixture.intField);
068:
069: binding.doCell(fixture, cell2);
070: assertEquals(321, fixture.intField);
071: }
072:
073: public void testQueryBindingWithBlackCell() throws Throwable {
074: Binding binding = Binding.create(fixture, "intField");
075: binding.doCell(fixture, cell4);
076: assertSubString("0", cell4.text());
077: }
078:
079: public void testSaveBinding() throws Throwable {
080: Binding binding = Binding.create(fixture, "=intMethod()");
081: binding.doCell(fixture, cell1);
082: assertEquals("0", Fixture.getSymbol("123"));
083: assertSubString("123 = 0", cell1.text());
084:
085: fixture.intField = 999;
086: binding.doCell(fixture, cell2);
087: assertEquals("999", Fixture.getSymbol("321"));
088: }
089:
090: public void testRecallBinding() throws Throwable {
091: Binding binding = Binding.create(fixture, "intField=");
092: Fixture.setSymbol("123", "999");
093: binding.doCell(fixture, cell1);
094: assertEquals(999, fixture.intField);
095:
096: binding.doCell(fixture, cell3);
097: assertSubString("No such symbol: abc", cell3.text());
098: }
099:
100: public void testUseOfGracefulNamingForMethods() throws Throwable {
101: checkForMethodBinding("intMethod()", true);
102: checkForMethodBinding("int Method?", true);
103: checkForMethodBinding("int method?", true);
104: checkForMethodBinding("intmethod?", false);
105: checkForMethodBinding("Intmethod?", false);
106: checkForMethodBinding("IntMethod?", false);
107: }
108:
109: public void testUseOfGracefulNamingForFields() throws Throwable {
110: checkForFieldBinding("intField", true);
111: checkForFieldBinding("int Field", true);
112: checkForFieldBinding("int field", true);
113: checkForFieldBinding("intfield", false);
114: checkForFieldBinding("Intfield", false);
115: checkForFieldBinding("IntField", false);
116: }
117:
118: private void checkForMethodBinding(String name, boolean expected)
119: throws Throwable {
120: Binding binding = null;
121: try {
122: binding = Binding.create(fixture, name);
123: } catch (NoSuchMethodFitFailureException e) {
124: assertFalse("method not found", expected);
125: return;
126: }
127: assertTrue("method was found", expected);
128: assertTrue(binding instanceof Binding.QueryBinding);
129: assertEquals("intMethod", binding.adapter.method.getName());
130: }
131:
132: private void checkForFieldBinding(String name, boolean expected)
133: throws Throwable {
134: Binding binding = null;
135: try {
136: binding = Binding.create(fixture, name);
137: } catch (NoSuchFieldFitFailureException e) {
138: assertFalse("field not found", expected);
139: return;
140: }
141: assertTrue("field was found", expected);
142: assertTrue(binding instanceof Binding.SetBinding);
143: assertEquals("intField", binding.adapter.field.getName());
144: }
145: }
|