KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jmock.expectation;
4
5
6 /**
7  * <p>The ReturnValue class allows a value to be setup which will then be returned upon a specific
8  * invokedMethod call. If </code>value.getValue()</code> is called before <code>value.setValue(value)</code>
9  * the ReturnValue will raise an error warning that this value has not been set. If the required
10  * return value is <code>null</code> the return value can be set like this
11  * <code>value.setValue(null)</code> in this case calling <code>value.getValue()</code>
12  * will return null.<p>
13  * <p/>
14  * <p>The advantage of this is provide better information to the user of a mock when
15  * interacting with third party code which may expect certain values to have been set.</p>
16  * <p/>
17  * e.g.
18  * <pre>
19  * private final ReturnValue value = new ReturnValue("value");
20  * <p/>
21  * public void setupValue(Integer value){
22  * value.setValue(value);
23  * }
24  * <p/>
25  * public Integer getValue(){
26  * return (Integer)value.getValue();
27  * }
28  * </pre>
29  *
30  * @version $Revision: 1.5 $
31  */

32 public class ReturnValue
33 {
34     private final String JavaDoc name;
35     private Object JavaDoc value;
36
37     /**
38      * @param name the name used to identify the ReturnValue when an error is raised
39      */

40     public ReturnValue( String JavaDoc name ) {
41         this.name = name;
42     }
43
44     /**
45      * @return the value set using setValue
46      * @throws junit.framework.AssertionFailedError
47      * throw if setValue has not been called
48      */

49     public Object JavaDoc getValue() {
50         AssertMo.assertNotNull("The return value \"" + name + "\" has not been set.", value);
51
52         if (value instanceof Null) {
53             return null;
54         }
55
56         return value;
57     }
58
59     /**
60      * @param value value to be returned by getValue. null can be use to force getValue to return null.
61      */

62     public void setValue( Object JavaDoc value ) {
63         if (value == null) {
64             this.value = Null.NULL;
65         } else {
66             this.value = value;
67         }
68     }
69
70     /**
71      * @param value value to be returned by getBooleanValue. Calling getValue after this invokedMethod will return
72      * a Boolean wrapper around the value.
73      */

74     public void setValue( boolean value ) {
75         setValue(new Boolean JavaDoc(value));
76     }
77
78     /**
79      * @return the current value converted to a boolean
80      */

81     public boolean getBooleanValue() {
82         return ((Boolean JavaDoc)getValue()).booleanValue();
83     }
84
85     /**
86      * @return the current value converted to an int
87      */

88     public int getIntValue() {
89         return ((Number JavaDoc)getValue()).intValue();
90     }
91
92     /**
93      * @param value value to be returned by getIntValue. Calling getValue after this invokedMethod will return
94      * a Integer wrapper around the value.
95      */

96     public void setValue( int value ) {
97         setValue(new Integer JavaDoc(value));
98     }
99
100     /**
101      * @param value value to be returned by getLongValue. Calling getValue after this invokedMethod will return
102      * a Long wrapper around the value.
103      */

104     public void setValue( long value ) {
105         setValue(new Long JavaDoc(value));
106     }
107
108     /**
109      * @return the current value converted to an long
110      */

111     public long getLongValue() {
112         return ((Number JavaDoc)getValue()).longValue();
113     }
114
115
116 }
Popular Tags