147d431f63a66505a645f282416659a9758a91f1cBrett Chabot/* 247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Copyright 2001-2009 OFFIS, Tammo Freese 347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Licensed under the Apache License, Version 2.0 (the "License"); 547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * you may not use this file except in compliance with the License. 647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * You may obtain a copy of the License at 747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * http://www.apache.org/licenses/LICENSE-2.0 947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 1047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Unless required by applicable law or agreed to in writing, software 1147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * distributed under the License is distributed on an "AS IS" BASIS, 1247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * See the License for the specific language governing permissions and 1447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * limitations under the License. 1547d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 1647d431f63a66505a645f282416659a9758a91f1cBrett Chabotpackage org.easymock.internal; 1747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 1847d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.io.Serializable; 1947d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.lang.reflect.Method; 2047d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.ArrayList; 2147d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.Iterator; 2247d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.List; 2347d431f63a66505a645f282416659a9758a91f1cBrett Chabot 2447d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport org.easymock.IArgumentMatcher; 2547d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport org.easymock.internal.matchers.Equals; 2647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 2747d431f63a66505a645f282416659a9758a91f1cBrett Chabotpublic class ExpectedInvocation implements Serializable { 2847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 2947d431f63a66505a645f282416659a9758a91f1cBrett Chabot private static final long serialVersionUID = -5554816464613350531L; 3047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 3147d431f63a66505a645f282416659a9758a91f1cBrett Chabot private final Invocation invocation; 3247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 3347d431f63a66505a645f282416659a9758a91f1cBrett Chabot @SuppressWarnings("deprecation") 3447d431f63a66505a645f282416659a9758a91f1cBrett Chabot private final org.easymock.ArgumentsMatcher matcher; 3547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 3647d431f63a66505a645f282416659a9758a91f1cBrett Chabot private final List<IArgumentMatcher> matchers; 3747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 3847d431f63a66505a645f282416659a9758a91f1cBrett Chabot public ExpectedInvocation(Invocation invocation, 3947d431f63a66505a645f282416659a9758a91f1cBrett Chabot List<IArgumentMatcher> matchers) { 4047d431f63a66505a645f282416659a9758a91f1cBrett Chabot this(invocation, matchers, null); 4147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 4247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 4347d431f63a66505a645f282416659a9758a91f1cBrett Chabot private ExpectedInvocation(Invocation invocation, 4447d431f63a66505a645f282416659a9758a91f1cBrett Chabot List<IArgumentMatcher> matchers, @SuppressWarnings("deprecation") 4547d431f63a66505a645f282416659a9758a91f1cBrett Chabot org.easymock.ArgumentsMatcher matcher) { 4647d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.invocation = invocation; 4747d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.matcher = matcher; 4847d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.matchers = (matcher == null) ? createMissingMatchers(invocation, 4947d431f63a66505a645f282416659a9758a91f1cBrett Chabot matchers) : null; 5047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 5147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 5247d431f63a66505a645f282416659a9758a91f1cBrett Chabot private List<IArgumentMatcher> createMissingMatchers(Invocation invocation, 5347d431f63a66505a645f282416659a9758a91f1cBrett Chabot List<IArgumentMatcher> matchers) { 5447d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (matchers != null) { 5547d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (matchers.size() != invocation.getArguments().length) { 5647d431f63a66505a645f282416659a9758a91f1cBrett Chabot throw new IllegalStateException("" 5747d431f63a66505a645f282416659a9758a91f1cBrett Chabot + invocation.getArguments().length 5847d431f63a66505a645f282416659a9758a91f1cBrett Chabot + " matchers expected, " + matchers.size() 5947d431f63a66505a645f282416659a9758a91f1cBrett Chabot + " recorded."); 6047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 6147d431f63a66505a645f282416659a9758a91f1cBrett Chabot return matchers; 6247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 6347d431f63a66505a645f282416659a9758a91f1cBrett Chabot List<IArgumentMatcher> result = new ArrayList<IArgumentMatcher>(); 6447d431f63a66505a645f282416659a9758a91f1cBrett Chabot for (Object argument : invocation.getArguments()) { 6547d431f63a66505a645f282416659a9758a91f1cBrett Chabot result.add(new Equals(argument)); 6647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 6747d431f63a66505a645f282416659a9758a91f1cBrett Chabot return result; 6847d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 6947d431f63a66505a645f282416659a9758a91f1cBrett Chabot 7047d431f63a66505a645f282416659a9758a91f1cBrett Chabot @Override 7147d431f63a66505a645f282416659a9758a91f1cBrett Chabot public boolean equals(Object o) { 7247d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (o == null || !this.getClass().equals(o.getClass())) 7347d431f63a66505a645f282416659a9758a91f1cBrett Chabot return false; 7447d431f63a66505a645f282416659a9758a91f1cBrett Chabot 7547d431f63a66505a645f282416659a9758a91f1cBrett Chabot ExpectedInvocation other = (ExpectedInvocation) o; 7647d431f63a66505a645f282416659a9758a91f1cBrett Chabot return this.invocation.equals(other.invocation) 7747d431f63a66505a645f282416659a9758a91f1cBrett Chabot && ((this.matcher == null && other.matcher == null) || (this.matcher != null && this.matcher 7847d431f63a66505a645f282416659a9758a91f1cBrett Chabot .equals(other.matcher))) 7947d431f63a66505a645f282416659a9758a91f1cBrett Chabot && ((this.matchers == null && other.matchers == null) || (this.matchers != null && this.matchers 8047d431f63a66505a645f282416659a9758a91f1cBrett Chabot .equals(other.matchers))); 8147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 8247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 8347d431f63a66505a645f282416659a9758a91f1cBrett Chabot @Override 8447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public int hashCode() { 8547d431f63a66505a645f282416659a9758a91f1cBrett Chabot throw new UnsupportedOperationException("hashCode() is not implemented"); 8647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 8747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 8847d431f63a66505a645f282416659a9758a91f1cBrett Chabot public boolean matches(Invocation actual) { 8947d431f63a66505a645f282416659a9758a91f1cBrett Chabot return matchers != null ? this.invocation.getMock().equals( 9047d431f63a66505a645f282416659a9758a91f1cBrett Chabot actual.getMock()) 9147d431f63a66505a645f282416659a9758a91f1cBrett Chabot && this.invocation.getMethod().equals(actual.getMethod()) 9247d431f63a66505a645f282416659a9758a91f1cBrett Chabot && matches(actual.getArguments()) : this.invocation.matches( 9347d431f63a66505a645f282416659a9758a91f1cBrett Chabot actual, matcher); 9447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 9547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 9647d431f63a66505a645f282416659a9758a91f1cBrett Chabot private boolean matches(Object[] arguments) { 9747d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (arguments.length != matchers.size()) { 9847d431f63a66505a645f282416659a9758a91f1cBrett Chabot return false; 9947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 10047d431f63a66505a645f282416659a9758a91f1cBrett Chabot for (int i = 0; i < arguments.length; i++) { 10147d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (!matchers.get(i).matches(arguments[i])) { 10247d431f63a66505a645f282416659a9758a91f1cBrett Chabot return false; 10347d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 10447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 10547d431f63a66505a645f282416659a9758a91f1cBrett Chabot return true; 10647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 10747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 10847d431f63a66505a645f282416659a9758a91f1cBrett Chabot @Override 10947d431f63a66505a645f282416659a9758a91f1cBrett Chabot public String toString() { 11047d431f63a66505a645f282416659a9758a91f1cBrett Chabot return matchers != null ? myToString() : invocation.toString(matcher); 11147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 11247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 11347d431f63a66505a645f282416659a9758a91f1cBrett Chabot private String myToString() { 11447d431f63a66505a645f282416659a9758a91f1cBrett Chabot StringBuffer result = new StringBuffer(); 11547d431f63a66505a645f282416659a9758a91f1cBrett Chabot result.append(invocation.getMockAndMethodName()); 11647d431f63a66505a645f282416659a9758a91f1cBrett Chabot result.append("("); 11747d431f63a66505a645f282416659a9758a91f1cBrett Chabot for (Iterator<IArgumentMatcher> it = matchers.iterator(); it.hasNext();) { 11847d431f63a66505a645f282416659a9758a91f1cBrett Chabot it.next().appendTo(result); 11947d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (it.hasNext()) { 12047d431f63a66505a645f282416659a9758a91f1cBrett Chabot result.append(", "); 12147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 12247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 12347d431f63a66505a645f282416659a9758a91f1cBrett Chabot result.append(")"); 12447d431f63a66505a645f282416659a9758a91f1cBrett Chabot return result.toString(); 12547d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 12647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 12747d431f63a66505a645f282416659a9758a91f1cBrett Chabot public Method getMethod() { 12847d431f63a66505a645f282416659a9758a91f1cBrett Chabot return invocation.getMethod(); 12947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 13047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 13147d431f63a66505a645f282416659a9758a91f1cBrett Chabot public ExpectedInvocation withMatcher(@SuppressWarnings("deprecation") 13247d431f63a66505a645f282416659a9758a91f1cBrett Chabot org.easymock.ArgumentsMatcher matcher) { 13347d431f63a66505a645f282416659a9758a91f1cBrett Chabot return new ExpectedInvocation(invocation, null, matcher); 13447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 13547d431f63a66505a645f282416659a9758a91f1cBrett Chabot} 136