KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > expectation > ReturnObjectBagTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.expectation;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.expectation.AssertMo;
8 import org.jmock.expectation.ReturnObjectBag;
9
10
11 public class ReturnObjectBagTest extends TestCase
12 {
13     private ReturnObjectBag bag;
14     private static final String JavaDoc KEY1 = "key1";
15     private static final String JavaDoc KEY2 = "key2";
16     private static final short SHORT_KEY1 = 1;
17     private static final short SHORT_KEY2 = 2;
18     private static final String JavaDoc VALUE_ONE = "one";
19     private static final String JavaDoc VALUE_TWO = "two";
20
21     protected void setUp() throws Exception JavaDoc {
22         super.setUp();
23         bag = new ReturnObjectBag(getName());
24     }
25
26     public void testLeftoverObjectFails() {
27         bag.putObjectToReturn(KEY1, VALUE_ONE);
28
29         AssertMo.assertVerifyFails(bag);
30     }
31
32     public void testEmptyList() {
33         bag.verify();
34     }
35
36     public void testReturnSucceeds() {
37         bag.putObjectToReturn(KEY1, VALUE_ONE);
38         bag.putObjectToReturn(KEY2, VALUE_TWO);
39
40         assertEquals("Should be first result", VALUE_ONE, bag.getNextReturnObject(KEY1));
41         assertEquals("Should be second result", VALUE_TWO, bag.getNextReturnObject(KEY2));
42         bag.verify();
43     }
44
45     public void testReturnInt() {
46         bag.putObjectToReturn(KEY1, 1);
47
48         assertEquals("Should be 1", 1, bag.getNextReturnInt(KEY1));
49         bag.verify();
50     }
51
52     public void testReturnBoolean() {
53         bag.putObjectToReturn(KEY1, true);
54
55         assertEquals("Should be true", true, bag.getNextReturnBoolean(KEY1));
56         bag.verify();
57     }
58
59     public void testShortKey() {
60         bag.putObjectToReturn(SHORT_KEY1, VALUE_ONE);
61         bag.putObjectToReturn(SHORT_KEY2, VALUE_TWO);
62
63         assertEquals("Should be first result", VALUE_ONE, bag.getNextReturnObject(SHORT_KEY1));
64         assertEquals("Should be second result", VALUE_TWO, bag.getNextReturnObject(SHORT_KEY2));
65         bag.verify();
66     }
67
68     public void testNoListForKey() {
69         try {
70             bag.getNextReturnObject(KEY1);
71             fail("AssertionFiledError not thrown");
72         }
73         catch (AssertionFailedError e) {
74             assertEquals(getName() + " does not contain key1", e.getMessage());
75         }
76     }
77
78     public void testNullKey() {
79         bag.putObjectToReturn(null, VALUE_ONE);
80         assertEquals(VALUE_ONE, bag.getNextReturnObject(null));
81     }
82
83     public void testTooManyReturns() {
84         bag.putObjectToReturn(KEY1, VALUE_ONE);
85         bag.getNextReturnObject(KEY1);
86         try {
87             bag.getNextReturnObject(KEY1);
88             fail("AssertionFiledError not thrown");
89         }
90         catch (AssertionFailedError e) {
91             assertEquals(getName() + ".key1 has run out of objects.", e.getMessage());
92         }
93     }
94 }
95
Popular Tags