| 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 java.util.HashMap; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.jmock.cglib.MockObjectTestCase; |
| 21 | import org.jmock.core.Constraint; |
| 22 | import org.jmock.core.Stub; |
| 23 | import org.jmock.core.constraint.IsNull; |
| 24 | import org.jmock.core.stub.DefaultResultStub; |
| 25 | |
| 26 | /** |
| 27 | * Utility class that test cases can inherit in order to have better support |
| 28 | * (additional "syntactic sugar") for jMock usage. |
| 29 | * |
| 30 | * @author Jean-Francois Poilpret |
| 31 | */ |
| 32 | public class HivemindUnitTestCase extends MockObjectTestCase |
| 33 | { |
| 34 | /** |
| 35 | * Overridden to reset the list of arguments values retained by the |
| 36 | * <code>save</code> methods. |
| 37 | */ |
| 38 | //CSOFF: IllegalThrowsCheck |
| 39 | public void runBare() throws Throwable |
| 40 | { |
| 41 | _savedArgs = new HashMap<String, SaveConstraint>(); |
| 42 | super.runBare(); |
| 43 | } |
| 44 | //CSON: IllegalThrowsCheck |
| 45 | |
| 46 | /** |
| 47 | * Return the value of an argument provided to a <code>Mock</code> object's |
| 48 | * method and saved thanks to a <code>SaveConstraint</code>. |
| 49 | * @param name name under which the argument was saved |
| 50 | * @return last value saved for this argument, or <code>null</code> if there |
| 51 | * was no argument saved under this name |
| 52 | */ |
| 53 | protected Object getArgument(String name) |
| 54 | { |
| 55 | SaveConstraint constraint = _savedArgs.get(name); |
| 56 | if (constraint != null) |
| 57 | { |
| 58 | return constraint.getArgument(); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates a <code>Constraint</code> that is used in the description of the |
| 68 | * expected argument to mocked method, and that needs to be saved under a |
| 69 | * name for later use in the same test case. |
| 70 | * @param name name under which the argument's value will be saved |
| 71 | * @return a Constraint that can be used in the <code>with</code> method |
| 72 | */ |
| 73 | protected Constraint save(String name) |
| 74 | { |
| 75 | return save(name, null); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates a <code>Constraint</code> that is used in the description of the |
| 80 | * expected argument to mocked method, and that needs to be saved under a |
| 81 | * name for later use in the same test case. |
| 82 | * @param name name under which the argument's value will be saved |
| 83 | * @param inner a real constraint that will be checked by jMock on the |
| 84 | * argument's value |
| 85 | * @return a Constraint that can be used in the <code>with</code> method |
| 86 | */ |
| 87 | protected Constraint save(String name, Constraint inner) |
| 88 | { |
| 89 | SaveConstraint outer = new SaveConstraint(inner); |
| 90 | _savedArgs.put(name, outer); |
| 91 | return outer; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Syntactic sugar to create an <code>IsNull</code> Constraint. |
| 96 | */ |
| 97 | protected Constraint isnull() |
| 98 | { |
| 99 | return new IsNull(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns a <code>Stub</code> that produces a default return value. This |
| 104 | * is simply syntactic sugar around <code>new DefaultResultStub()</code>. |
| 105 | * @return stub returning a default value for a mocked method |
| 106 | */ |
| 107 | protected Stub defaultReturn() |
| 108 | { |
| 109 | return new DefaultResultStub(); |
| 110 | } |
| 111 | |
| 112 | private Map<String, SaveConstraint> _savedArgs; |
| 113 | } |