My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import com.google.gwt.user.client.rpc.AsyncCallback;

import org.easymock.IArgumentMatcher;

import static org.easymock.EasyMock.reportMatcher;

/**
* EasyMock matcher for setting success expectations for GWT
* {@link AsyncCallback}.
*
* This is not a regular matcher, in that it does not verify that the specified
* value is passed to the callback. Instead it calls
* {@link AsyncCallback#onSuccess} with that value.
*
* @param <V> the object type being by the {@link AsyncCallback}
*
* @author nsw (Nicolas Wettstein)
* @author sec (Christopher Semturs)
*/
public class AsyncCallbackSuccessMatcher<V> implements IArgumentMatcher {
private final V returnValue;
private int count;

private AsyncCallbackSuccessMatcher(V value, int count) {
this.returnValue = value;
this.count = count;
}

/**
* Factory method. Creates a matcher returning the specified {@link Throwable}
* and expecting to be called once.
*
* @param value the value to report to the {@link AsyncCallback}
*/
public static <U> AsyncCallback<U> reportSuccess(U value) {
reportMatcher(new AsyncCallbackSuccessMatcher<U>(value, 1));
return null;
}

/**
* Factory method. Creates a matcher returning the specified {@see Throwable}
* and expecting to be called once.
*
* @param value the value to report to the {@link AsyncCallback}
* @param count the number of times this matcher is expected to be applied
*/
public static <U> AsyncCallback<U> reportSuccess(U value, int count) {
reportMatcher(new AsyncCallbackSuccessMatcher<U>(value, count));
return null;
}

@Override
public void appendTo(StringBuffer buffer) {
buffer.append(String.valueOf(returnValue));
if (count <= 0) {
buffer.append(" [callback invoked too often]");
}
}

@SuppressWarnings("unchecked")
@Override
public boolean matches(Object argument) {
if (!(argument instanceof AsyncCallback)) {
return false;
}
if (count > 0) {
count--;
AsyncCallback<V> argObj = (AsyncCallback<V>) argument;
argObj.onSuccess(returnValue);
return true;
} else {
return false;
}
}
}

Change log

r3 by n...@google.com on Aug 12, 2009   Diff
Fix by David Rieber: make it possible to
use AsyncCallbackSuccessMatcher with
methods that take AsyncCallback<Void>

Thanks David!

Go to: 
Project members, sign in to write a code review

Older revisions

r2 by n...@google.com on Jul 30, 2009   Diff
initial checkin
All revisions of this file

File info

Size: 2756 bytes, 89 lines
Powered by Google Project Hosting