Source code: com/mockobjects/ReturnValue.java
1 package com.mockobjects;
2
3 import com.mockobjects.util.AssertMo;
4 import com.mockobjects.util.Null;
5
6 /**
7 * <p>The ReturnValue class allows a value to be setup which will then be returned upon a specific
8 * method 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 *
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 *
17 * e.g.
18 * <pre>
19 * private final ReturnValue value = new ReturnValue("value");
20 *
21 * public void setupValue(Integer value){
22 * value.setValue(value);
23 * }
24 *
25 * public Integer getValue(){
26 * return (Integer)value.getValue();
27 * }
28 * </pre>
29 * @version $Revision: 1.4 $
30 */
31 public class ReturnValue {
32 private final String name;
33 private Object value;
34
35 /**
36 * @param name the name used to identify the ReturnValue when an error is raised
37 */
38 public ReturnValue(String name) {
39 this.name = name;
40 }
41
42 /**
43 * @return the value set using setValue
44 * @exception junit.framework.AssertionFailedError throw if setValue has not been called
45 */
46 public Object getValue() {
47 AssertMo.assertNotNull("The return value \"" + name + "\" has not been set.", value);
48
49 if(value instanceof Null){
50 return null;
51 }
52
53 return value;
54 }
55
56 /**
57 * @param value value to be returned by getValue. null can be use to force getValue to return null.
58 */
59 public void setValue(Object value) {
60 if(value==null){
61 this.value = Null.NULL;
62 }else{
63 this.value = value;
64 }
65 }
66
67 /**
68 * @param value value to be returned by getBooleanValue. Calling getValue after this method will return
69 * a Boolean wrapper around the value.
70 */
71 public void setValue(boolean value){
72 setValue(new Boolean(value));
73 }
74
75 /**
76 * @return the current value converted to a boolean
77 */
78 public boolean getBooleanValue() {
79 return ((Boolean)getValue()).booleanValue();
80 }
81
82 /**
83 * @return the current value converted to an int
84 */
85 public int getIntValue() {
86 return ((Number)getValue()).intValue();
87 }
88
89 /**
90 * @param value value to be returned by getIntValue. Calling getValue after this method will return
91 * a Integer wrapper around the value.
92 */
93 public void setValue(int value) {
94 setValue(new Integer(value));
95 }
96
97 /**
98 * @param value value to be returned by getLongValue. Calling getValue after this method will return
99 * a Long wrapper around the value.
100 */
101 public void setValue(long value) {
102 setValue(new Long(value));
103 }
104
105 /**
106 * @return the current value converted to an long
107 */
108 public long getLongValue() {
109 return ((Number)getValue()).longValue();
110 }
111
112
113 }