1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.expectation;
3
4
5 /***
6 * <p>The ReturnValue class allows a value to be setup which will then be returned upon a specific
7 * method call. If </code>value.getValue()</code> is called before <code>value.setValue(value)</code>
8 * the ReturnValue will raise an error warning that this value has not been set. If the required
9 * return value is <code>null</code> the return value can be set like this
10 * <code>value.setValue(null)</code> in this case calling <code>value.getValue()</code>
11 * will return null.<p>
12 * <p/>
13 * <p>The advantage of this is provide better information to the user of a mock when
14 * interacting with third party code which may expect certain values to have been set.</p>
15 * <p/>
16 * e.g.
17 * <pre>
18 * private final ReturnValue value = new ReturnValue("value");
19 * <p/>
20 * public void setupValue(Integer value){
21 * value.setValue(value);
22 * }
23 * <p/>
24 * public Integer getValue(){
25 * return (Integer)value.getValue();
26 * }
27 * </pre>
28 *
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 * @throws junit.framework.AssertionFailedError
45 * throw if setValue has not been called
46 */
47 public Object getValue() {
48 AssertMo.assertNotNull("The return value \"" + name + "\" has not been set.", value);
49
50 if (value instanceof Null) {
51 return null;
52 }
53
54 return value;
55 }
56
57 /***
58 * @param value value to be returned by getValue. null can be use to force getValue to return null.
59 */
60 public void setValue(Object value) {
61 if (value == null) {
62 this.value = Null.NULL;
63 } else {
64 this.value = value;
65 }
66 }
67
68 /***
69 * @param value value to be returned by getBooleanValue. Calling getValue after this method will return
70 * a Boolean wrapper around the value.
71 */
72 public void setValue(boolean value) {
73 setValue(new Boolean(value));
74 }
75
76 /***
77 * @return the current value converted to a boolean
78 */
79 public boolean getBooleanValue() {
80 return ((Boolean) getValue()).booleanValue();
81 }
82
83 /***
84 * @return the current value converted to an int
85 */
86 public int getIntValue() {
87 return ((Number) getValue()).intValue();
88 }
89
90 /***
91 * @param value value to be returned by getIntValue. Calling getValue after this method will return
92 * a Integer wrapper around the value.
93 */
94 public void setValue(int value) {
95 setValue(new Integer(value));
96 }
97
98 /***
99 * @param value value to be returned by getLongValue. Calling getValue after this method will return
100 * a Long wrapper around the value.
101 */
102 public void setValue(long value) {
103 setValue(new Long(value));
104 }
105
106 /***
107 * @return the current value converted to an long
108 */
109 public long getLongValue() {
110 return ((Number) getValue()).longValue();
111 }
112
113
114 }
This page was automatically generated by Maven