|
|||||||||||||||||||
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 | |||||||||||||||
AggregateArgumentsMatcher.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
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 org.apache.hivemind.test;
|
|
16 |
|
|
17 |
import org.easymock.AbstractMatcher;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* @author Howard M. Lewis Ship
|
|
21 |
* @since 1.1
|
|
22 |
*/
|
|
23 |
public class AggregateArgumentsMatcher extends AbstractMatcher |
|
24 |
{ |
|
25 |
private ArgumentMatcher[] _matchers;
|
|
26 |
|
|
27 |
private ArgumentMatcher _defaultMatcher = new EqualsMatcher(); |
|
28 |
|
|
29 |
/**
|
|
30 |
* Aggregates the individual matchers. Each matcher is matched against the argument in the same
|
|
31 |
* position. Null matchers, or arguments outside the array range, are handled by a default
|
|
32 |
* instance (of {@link EqualsMatcher}). This makes it easy to provide special argument matchers
|
|
33 |
* for particular arguments.
|
|
34 |
*/
|
|
35 | 12 |
public AggregateArgumentsMatcher(ArgumentMatcher[] matchers)
|
36 |
{ |
|
37 | 12 |
_matchers = matchers; |
38 |
} |
|
39 |
|
|
40 |
/**
|
|
41 |
* Convienice for just a single matcher.
|
|
42 |
*/
|
|
43 | 5 |
public AggregateArgumentsMatcher(ArgumentMatcher matcher)
|
44 |
{ |
|
45 | 5 |
this(new ArgumentMatcher[] |
46 |
{ matcher }); |
|
47 |
} |
|
48 |
|
|
49 | 13 |
public boolean matches(Object[] expected, Object[] actual) |
50 |
{ |
|
51 | 13 |
for (int i = 0; i < expected.length; i++) |
52 |
{ |
|
53 | 27 |
if (!matches(i, expected[i], actual[i]))
|
54 | 2 |
return false; |
55 |
} |
|
56 |
|
|
57 | 11 |
return true; |
58 |
} |
|
59 |
|
|
60 | 27 |
private boolean matches(int argumentIndex, Object expected, Object actual) |
61 |
{ |
|
62 | 27 |
if (expected == actual)
|
63 | 10 |
return true; |
64 |
|
|
65 |
// If one is null, but both aren't null (previous check) then a non-match.
|
|
66 |
|
|
67 | 17 |
if (expected == null || actual == null) |
68 | 1 |
return false; |
69 |
|
|
70 | 16 |
ArgumentMatcher am = getArgumentMatcher(argumentIndex); |
71 |
|
|
72 | 16 |
return am.matches(expected, actual);
|
73 |
} |
|
74 |
|
|
75 | 16 |
private ArgumentMatcher getArgumentMatcher(int argumentIndex) |
76 |
{ |
|
77 | 16 |
if (argumentIndex >= _matchers.length)
|
78 | 1 |
return _defaultMatcher;
|
79 |
|
|
80 | 15 |
ArgumentMatcher result = _matchers[argumentIndex]; |
81 |
|
|
82 | 15 |
if (result == null) |
83 | 5 |
result = _defaultMatcher; |
84 |
|
|
85 | 15 |
return result;
|
86 |
} |
|
87 |
} |
|