001: package net.sf.mockcreator;
002:
003: import net.sf.mockcreator.exceptions.MockException;
004: import net.sf.mockcreator.expectable.MockExpectables;
005: import net.sf.mockcreator.expectable.StructNoEquals;
006:
007: public class BlockTest extends TestCase {
008: MockExpectables mock;
009: MockExpectables mock2;
010:
011: public BlockTest(String name) {
012: super (name);
013: }
014:
015: public void setUp() throws Exception {
016: super .setUp();
017: mock = new MockExpectables();
018: mock2 = new MockExpectables();
019: }
020:
021: public void testExpectInBlock() {
022: MockCore.startBlock();
023: mock.expectMap(new Struct(5, 6, 7, "hello")).returns(
024: new Struct(1, 1, 1, "1"));
025: mock.expectMap(new Struct(1, 2, 3, "bar")).returns(
026: new Struct(2, 2, 2, "2"));
027: MockCore.endBlock();
028:
029: Struct s1 = mock.map(new Struct(1, 2, 3, "bar"));
030: Struct s2 = mock.map(new Struct(5, 6, 7, "hello"));
031:
032: assertEquals(2, s1.id);
033: assertEquals(1, s2.id);
034: }
035:
036: public void testAcceptInBlock() {
037: StructNoEquals s1 = new StructNoEquals();
038: StructNoEquals s2 = new StructNoEquals();
039:
040: MockCore.startBlock();
041: mock.acceptGet(new Integer(1)).returns(s1);
042: mock.acceptGet(new Integer(2)).returns(s2);
043: MockCore.endBlock();
044:
045: assertEquals(s2, mock.get(2));
046: assertEquals(s1, mock.get(1));
047:
048: MockCore.verify();
049: }
050:
051: public void testExpectInBlockTwice() {
052: MockCore.startBlock();
053: mock.expectMap(new Struct(5, 6, 7, "hello")).returns(
054: new Struct(1, 1, 1, "1"));
055: mock.expectMap(new Struct(1, 2, 3, "bar")).returns(
056: new Struct(2, 2, 2, "2"));
057: mock.expectMap(new Struct(5, 6, 7, "hello")).returns(
058: new Struct(1, 1, 1, "1"));
059: MockCore.endBlock();
060:
061: Struct s1 = mock.map(new Struct(1, 2, 3, "bar"));
062: Struct s2 = mock.map(new Struct(5, 6, 7, "hello"));
063: Struct s3 = mock.map(new Struct(5, 6, 7, "hello"));
064:
065: assertEquals(2, s1.id);
066: assertEquals(1, s2.id);
067: assertEquals(1, s3.id);
068: }
069:
070: public void testAcceptInBlockTwice() {
071: MockCore.startBlock();
072: mock.acceptMap(new Struct(5, 6, 7, "hello")).returns(
073: new Struct(1, 1, 1, "1"));
074: mock.acceptMap(new Struct(1, 2, 3, "bar")).returns(
075: new Struct(2, 2, 2, "2"));
076: mock.acceptMap(new Struct(5, 6, 7, "hello")).returns(
077: new Struct(1, 1, 1, "1"));
078: MockCore.endBlock();
079:
080: Struct s1 = mock.map(new Struct(1, 2, 3, "bar"));
081: Struct s2 = mock.map(new Struct(5, 6, 7, "hello"));
082: Struct s3 = mock.map(new Struct(5, 6, 7, "hello"));
083:
084: assertEquals(2, s1.id);
085: assertEquals(1, s2.id);
086: assertEquals(1, s3.id);
087: }
088:
089: public void testExpectTwoBlocks() {
090: MockCore.startBlock();
091: mock.expectMap(new Struct(1, 1, 1, "1")).returns(
092: new Struct(1, 1, 1, "11"));
093: mock.expectMap(new Struct(2, 2, 2, "1")).returns(
094: new Struct(2, 2, 2, "22"));
095: MockCore.endBlock();
096:
097: mock.expectMap(new Struct(3, 3, 3, "3")).returns(
098: new Struct(3, 3, 3, "33"));
099:
100: MockCore.startBlock();
101: mock.expectMap(new Struct(4, 4, 4, "4")).returns(
102: new Struct(4, 4, 4, "44"));
103: mock.expectMap(new Struct(5, 5, 5, "5")).returns(
104: new Struct(5, 5, 5, "55"));
105: MockCore.endBlock();
106:
107: assertEquals(1, mock.map(new Struct(1, 1, 1, "1")).id);
108: assertEquals(2, mock.map(new Struct(2, 2, 2, "2")).id);
109: assertEquals(3, mock.map(new Struct(3, 3, 3, "3")).id);
110: assertEquals(5, mock.map(new Struct(5, 5, 5, "5")).id);
111: assertEquals(4, mock.map(new Struct(4, 4, 4, "4")).id);
112: }
113:
114: public void testExpectTwoBlocksTwoMocks() {
115: MockCore.startBlock();
116: mock.expectMap(new Struct(1, 1, 1, "1")).returns(
117: new Struct(1, 1, 1, "11"));
118: mock.expectMap(new Struct(2, 2, 2, "1")).returns(
119: new Struct(2, 2, 2, "22"));
120: mock2.expectMap(new Struct(22, 2, 2, "2.1")).returns(
121: new Struct(222, 2, 2, "2.22"));
122: MockCore.endBlock();
123:
124: mock.expectMap(new Struct(3, 3, 3, "3")).returns(
125: new Struct(3, 3, 3, "33"));
126: mock2.expectMap(new Struct(23, 3, 3, "2.3")).returns(
127: new Struct(223, 3, 3, "2.33"));
128:
129: MockCore.startBlock();
130: mock2.expectMap(new Struct(24, 4, 4, "2.4")).returns(
131: new Struct(224, 4, 4, "2.44"));
132: mock.expectMap(new Struct(4, 4, 4, "4")).returns(
133: new Struct(4, 4, 4, "44"));
134: mock.expectMap(new Struct(5, 5, 5, "5")).returns(
135: new Struct(5, 5, 5, "55"));
136: MockCore.endBlock();
137:
138: assertEquals(1, mock.map(new Struct(1, 1, 1, "1")).id);
139: assertEquals(2, mock.map(new Struct(2, 2, 2, "2")).id);
140: assertEquals(222, mock2.map(new Struct(22, 2, 2, "2.1")).id);
141:
142: assertEquals(3, mock.map(new Struct(3, 3, 3, "3")).id);
143: assertEquals(223, mock2.map(new Struct(23, 3, 3, "2.3")).id);
144:
145: assertEquals(5, mock.map(new Struct(5, 5, 5, "5")).id);
146: assertEquals(224, mock2.map(new Struct(24, 4, 4, "2.44")).id);
147: assertEquals(4, mock.map(new Struct(4, 4, 4, "4")).id);
148: }
149:
150: public void testTwoMocksOfSameType() {
151: mock.expectMap(new Struct(1, 1, 1, "1")).returns(
152: new Struct(1, 1, 1, "11"));
153: mock2.expectMap(new Struct(2, 2, 2, "2")).returns(
154: new Struct(2, 2, 2, "21"));
155:
156: try {
157: mock2.map(new Struct(1, 1, 1, "1"));
158: fail("was able to call expectation on wrong object");
159: } catch (MockException ex) {
160: }
161: }
162:
163: public void testNewMockAfterSetDummy() {
164: mock.expectZeroOrMoreMap_Struct_returning_Struct().returns(
165: new Struct(1, 1, 1, "11"));
166:
167: assertEquals(1, mock.map((Struct) null).id);
168:
169: mock2 = new MockExpectables();
170:
171: assertEquals(1, mock.map(null).id);
172: }
173:
174: public void testNewMockAfterExpect() {
175: mock.expectMap(null).returns(new Struct(1, 1, 1, "11"));
176: mock.expectMap(null).returns(new Struct(1, 1, 1, "11"));
177:
178: assertEquals(1, mock.map((Struct) null).id);
179:
180: mock2 = new MockExpectables();
181:
182: assertEquals(1, mock.map(null).id);
183: }
184:
185: public void testDoubleStartBlock() {
186: MockCore.startBlock();
187:
188: try {
189: MockCore.startBlock();
190: fail();
191: } catch (MockException ex) {
192: }
193: }
194:
195: public void testEmptyBlock() {
196: MockCore.startBlock();
197:
198: try {
199: MockCore.endBlock();
200: fail();
201: } catch (MockException ex) {
202: }
203: }
204:
205: public void testEndBlock() {
206: try {
207: MockCore.endBlock();
208: fail();
209: } catch (MockException ex) {
210: }
211: }
212: }
|