| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.test; |
| 16 | |
| 17 | import org.jmock.core.Constraint; |
| 18 | |
| 19 | /** |
| 20 | * jMock <code>Constraint</code> that just retains the value passed to it, and |
| 21 | * optionnally evaluates another <code>Constraint</code> on that value. |
| 22 | * |
| 23 | * @author Jean-Francois Poilpret |
| 24 | */ |
| 25 | public class SaveConstraint implements Constraint |
| 26 | { |
| 27 | /** |
| 28 | * Constructor creating a constraint to save the evaluated argument without |
| 29 | * any additional check on that value. |
| 30 | */ |
| 31 | public SaveConstraint() |
| 32 | { |
| 33 | this(null); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Constructor creating a constraint to save the evaluated argument and |
| 38 | * evaluating any additional check on that value. |
| 39 | * @param inner the <code>Constraint</code> to be evaluated on the value |
| 40 | */ |
| 41 | public SaveConstraint(Constraint inner) |
| 42 | { |
| 43 | _inner = inner; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Return the last value saved in this constraint. |
| 48 | * @return the last value saved in this constraint, or <code>null</code> if |
| 49 | * no value was saved |
| 50 | */ |
| 51 | public Object getArgument() |
| 52 | { |
| 53 | return _arg; |
| 54 | } |
| 55 | |
| 56 | public boolean eval(Object o) |
| 57 | { |
| 58 | // Save value |
| 59 | _arg = o; |
| 60 | if (_inner != null) |
| 61 | { |
| 62 | return _inner.eval(o); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public StringBuffer describeTo(StringBuffer buffer) |
| 71 | { |
| 72 | if (_inner != null) |
| 73 | { |
| 74 | return _inner.describeTo(buffer); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | return buffer.append("save"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | protected final Constraint _inner; |
| 83 | protected Object _arg; |
| 84 | } |