| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Capturer |
|
| 0.0;0 |
| 1 | // Copyright 2006 Howard M. Lewis Ship |
|
| 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 | /** |
|
| 16 | * |
|
| 17 | */ |
|
| 18 | package com.javaforge.tapestry.testng; |
|
| 19 | ||
| 20 | import org.easymock.IArgumentMatcher; |
|
| 21 | ||
| 22 | import static org.easymock.EasyMock.reportMatcher; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * An EasyMock 2.0 argument matcher that captures a method argument value. This allows an object |
|
| 26 | * created inside a test method to be interrogated after the method completes, even when the object |
|
| 27 | * is not a return value, but is merely passed as a parameter to a mock object. |
|
| 28 | * |
|
| 29 | * @author Howard M. Lewis Ship |
|
| 30 | * @param <T> |
|
| 31 | * the type of object to capture |
|
| 32 | */ |
|
| 33 | public final class Capturer<T> implements IArgumentMatcher |
|
| 34 | { |
|
| 35 | private final Class<T> _matchType; |
|
| 36 | ||
| 37 | private T _captured; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * Creates a new Capturer for the given type. Because of Generics syntax, it is easier to use |
|
| 41 | * the static method. |
|
| 42 | */ |
|
| 43 | public Capturer(Class<T> matchType) |
|
| 44 | 9 | { |
| 45 | 9 | _matchType = matchType; |
| 46 | 9 | } |
| 47 | ||
| 48 | /** |
|
| 49 | * Factory method that invokes the normal constructor in a way that keeps Java Generics happy. |
|
| 50 | */ |
|
| 51 | public static <T> Capturer<T> newCapturer(Class<T> matchType) |
|
| 52 | { |
|
| 53 | 9 | return new Capturer<T>(matchType); |
| 54 | } |
|
| 55 | ||
| 56 | public void appendTo(StringBuffer buffer) |
|
| 57 | { |
|
| 58 | 9 | buffer.append(String.format("capture(%s)", _matchType.getName())); |
| 59 | 9 | } |
| 60 | ||
| 61 | public boolean matches(Object parameter) |
|
| 62 | { |
|
| 63 | 15 | boolean result = _matchType.isInstance(parameter); |
| 64 | ||
| 65 | 15 | if (result) |
| 66 | 3 | _captured = _matchType.cast(parameter); |
| 67 | ||
| 68 | 15 | return result; |
| 69 | } |
|
| 70 | ||
| 71 | /** Returns the method argument value previously captured. */ |
|
| 72 | public T getCaptured() |
|
| 73 | { |
|
| 74 | 9 | return _captured; |
| 75 | } |
|
| 76 | ||
| 77 | /** |
|
| 78 | * Useage (with static imports): |
|
| 79 | * <p> |
|
| 80 | * Capturer<Type> c = newCapturer(Type.class); |
|
| 81 | * <p> |
|
| 82 | * mock.someMethod(capture(c)); |
|
| 83 | * <p> . . . |
|
| 84 | * <p> |
|
| 85 | * c.getCaptured().getXXX() |
|
| 86 | * <p> |
|
| 87 | * The interrogation of the captured argument should occur after the test subject has invoked |
|
| 88 | * the method on the mock; the best time for this is typically after invoking |
|
| 89 | * {@link TestBase#verify()}. |
|
| 90 | * <p> |
|
| 91 | * Remember that when you use an argument matcher for one argument of a method invocation, you |
|
| 92 | * must use argument matchers for <em>all</em> arguments of the method invocation. |
|
| 93 | */ |
|
| 94 | public static <T> T capture(Capturer<T> capturer) |
|
| 95 | { |
|
| 96 | 9 | reportMatcher(capturer); |
| 97 | ||
| 98 | 9 | return null; |
| 99 | } |
|
| 100 | ||
| 101 | } |