001// Copyright 2004, 2005 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.hivemind.test; 016 017import org.easymock.AbstractMatcher; 018 019/** 020 * @author Howard M. Lewis Ship 021 * @since 1.1 022 */ 023public class AggregateArgumentsMatcher extends AbstractMatcher 024{ 025 private ArgumentMatcher[] _matchers; 026 027 private ArgumentMatcher _defaultMatcher = new EqualsMatcher(); 028 029 /** 030 * Aggregates the individual matchers. Each matcher is matched against the argument in the same 031 * position. Null matchers, or arguments outside the array range, are handled by a default 032 * instance (of {@link EqualsMatcher}). This makes it easy to provide special argument matchers 033 * for particular arguments. 034 */ 035 public AggregateArgumentsMatcher(ArgumentMatcher[] matchers) 036 { 037 _matchers = matchers; 038 } 039 040 /** 041 * Convienice for just a single matcher. 042 */ 043 public AggregateArgumentsMatcher(ArgumentMatcher matcher) 044 { 045 this(new ArgumentMatcher[] 046 { matcher }); 047 } 048 049 public boolean matches(Object[] expected, Object[] actual) 050 { 051 for (int i = 0; i < expected.length; i++) 052 { 053 if (!matches(i, expected[i], actual[i])) 054 return false; 055 } 056 057 return true; 058 } 059 060 private boolean matches(int argumentIndex, Object expected, Object actual) 061 { 062 if (expected == actual) 063 return true; 064 065 // If one is null, but both aren't null (previous check) then a non-match. 066 067 if (expected == null || actual == null) 068 return false; 069 070 ArgumentMatcher am = getArgumentMatcher(argumentIndex); 071 072 return am.compareArguments(expected, actual); 073 } 074 075 private ArgumentMatcher getArgumentMatcher(int argumentIndex) 076 { 077 if (argumentIndex >= _matchers.length) 078 return _defaultMatcher; 079 080 ArgumentMatcher result = _matchers[argumentIndex]; 081 082 if (result == null) 083 result = _defaultMatcher; 084 085 return result; 086 } 087}