KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > TestReturnObjectBag


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