1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockito.internal; 7 8 import org.mockito.InOrder; 9 import org.mockito.exceptions.base.MockitoException; 10 import org.mockito.internal.verification.InOrderContextImpl; 11 import org.mockito.internal.verification.InOrderWrapper; 12 import org.mockito.internal.verification.VerificationModeFactory; 13 import org.mockito.internal.verification.api.InOrderContext; 14 import org.mockito.internal.verification.api.VerificationInOrderMode; 15 import org.mockito.invocation.Invocation; 16 import org.mockito.verification.VerificationMode; 17 import org.mockito.internal.verification.VerificationWrapper; 18 import org.mockito.internal.verification.VerificationWrapperInOrderWrapper; 19 20 import static org.mockito.internal.exceptions.Reporter.inOrderRequiresFamiliarMock; 21 22 import java.util.LinkedList; 23 import java.util.List; 24 25 /** 26 * Allows verifying in order. This class should not be exposed, hence default access. 27 */ 28 public class InOrderImpl implements InOrder, InOrderContext { 29 30 private final MockitoCore mockitoCore = new MockitoCore(); 31 private final List<Object> mocksToBeVerifiedInOrder = new LinkedList<Object>(); 32 private final InOrderContext inOrderContext = new InOrderContextImpl(); 33 getMocksToBeVerifiedInOrder()34 public List<Object> getMocksToBeVerifiedInOrder() { 35 return mocksToBeVerifiedInOrder; 36 } 37 InOrderImpl(List<?> mocksToBeVerifiedInOrder)38 public InOrderImpl(List<?> mocksToBeVerifiedInOrder) { 39 this.mocksToBeVerifiedInOrder.addAll(mocksToBeVerifiedInOrder); 40 } 41 verify(T mock)42 public <T> T verify(T mock) { 43 return this.verify(mock, VerificationModeFactory.times(1)); 44 } 45 verify(T mock, VerificationMode mode)46 public <T> T verify(T mock, VerificationMode mode) { 47 if (!mocksToBeVerifiedInOrder.contains(mock)) { 48 throw inOrderRequiresFamiliarMock(); 49 } 50 if (mode instanceof VerificationWrapper) { 51 return mockitoCore.verify(mock, new VerificationWrapperInOrderWrapper((VerificationWrapper) mode, this)); 52 } else if (!(mode instanceof VerificationInOrderMode)) { 53 throw new MockitoException(mode.getClass().getSimpleName() + " is not implemented to work with InOrder"); 54 } 55 return mockitoCore.verify(mock, new InOrderWrapper((VerificationInOrderMode) mode, this)); 56 } 57 isVerified(Invocation i)58 public boolean isVerified(Invocation i) { 59 return inOrderContext.isVerified(i); 60 } 61 markVerified(Invocation i)62 public void markVerified(Invocation i) { 63 inOrderContext.markVerified(i); 64 } 65 verifyNoMoreInteractions()66 public void verifyNoMoreInteractions() { 67 mockitoCore.verifyNoMoreInteractionsInOrder(mocksToBeVerifiedInOrder, this); 68 } 69 } 70