KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > expectation > ReturnValues


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

3 package org.jmock.expectation;
4
5 import java.util.Collection JavaDoc;
6 import java.util.Vector JavaDoc;
7 import junit.framework.AssertionFailedError;
8
9
10 /**
11  * Sequence values as required by MockMaker
12  * This is a generic class that should have been introduced to the mockobjects code stream instead of
13  * being separately included in org.mockobjects.
14  * It is possibly similar to a ReturnObjectList?
15  */

16 public class ReturnValues
17 {
18     private String JavaDoc myName;
19     protected Vector JavaDoc myContents = new Vector JavaDoc();
20     private boolean myKeepUsingLastReturnValue = false;
21
22     public ReturnValues() {
23         this("Generate me with a useful name!", true);
24     }
25
26     public ReturnValues( String JavaDoc name, boolean keepUsingLastReturnValue ) {
27         myName = name;
28         myKeepUsingLastReturnValue = keepUsingLastReturnValue;
29     }
30
31     public ReturnValues( boolean keepUsingLastReturnValue ) {
32         this("Generate me with a useful name!", keepUsingLastReturnValue);
33     }
34
35     public void add( Object JavaDoc element ) {
36         myContents.addElement(element);
37     }
38
39     public void addAll( Collection JavaDoc returnValues ) {
40         myContents.addAll(returnValues);
41     }
42
43     public Object JavaDoc getNext() {
44         if (myContents.isEmpty()) {
45             throw new AssertionFailedError(getClass().getName() + "[" + myName + "] was not setup with enough values");
46         }
47         return pop();
48     }
49
50     public boolean isEmpty() {
51         return myContents.size() == 0;
52     }
53
54     protected Object JavaDoc pop() {
55         Object JavaDoc result = myContents.firstElement();
56         boolean shouldNotRemoveElement = myContents.size() == 1 && myKeepUsingLastReturnValue;
57         if (!shouldNotRemoveElement) {
58             myContents.removeElementAt(0);
59         }
60         return result;
61     }
62 }
63
Popular Tags