|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ReturnValue.java | 100% | 100% | 100% | 100% |
|
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 | 12 |
public ReturnValue(String name) {
|
39 | 12 |
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 | 14 |
public Object getValue() {
|
48 | 14 |
AssertMo.assertNotNull("The return value \"" + name + "\" has not been set.", value); |
49 |
|
|
50 | 12 |
if (value instanceof Null) { |
51 | 2 |
return null; |
52 |
} |
|
53 |
|
|
54 | 10 |
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 | 10 |
public void setValue(Object value) { |
61 | 10 |
if (value == null) { |
62 | 2 |
this.value = Null.NULL;
|
63 |
} else {
|
|
64 | 8 |
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 | 2 |
public void setValue(boolean value) { |
73 | 2 |
setValue(new Boolean(value));
|
74 |
} |
|
75 |
|
|
76 |
/**
|
|
77 |
* @return the current value converted to a boolean
|
|
78 |
*/
|
|
79 | 2 |
public boolean getBooleanValue() { |
80 | 2 |
return ((Boolean) getValue()).booleanValue();
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* @return the current value converted to an int
|
|
85 |
*/
|
|
86 | 4 |
public int getIntValue() { |
87 | 4 |
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 | 2 |
public void setValue(int value) { |
95 | 2 |
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 | 2 |
public void setValue(long value) { |
103 | 2 |
setValue(new Long(value));
|
104 |
} |
|
105 |
|
|
106 |
/**
|
|
107 |
* @return the current value converted to an long
|
|
108 |
*/
|
|
109 | 2 |
public long getLongValue() { |
110 | 2 |
return ((Number) getValue()).longValue();
|
111 |
} |
|
112 |
|
|
113 |
|
|
114 |
} |
|