1 /*
2  * Copyright (c) 2016 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.verification;
6 
7 import org.mockito.exceptions.base.MockitoException;
8 import org.mockito.internal.InOrderImpl;
9 import org.mockito.internal.verification.api.VerificationData;
10 import org.mockito.internal.verification.api.VerificationInOrderMode;
11 import org.mockito.verification.VerificationMode;
12 
13 public class VerificationWrapperInOrderWrapper implements VerificationMode {
14     private final VerificationMode delegate;
15 
VerificationWrapperInOrderWrapper(VerificationWrapper<?> verificationWrapper, InOrderImpl inOrder)16     public VerificationWrapperInOrderWrapper(VerificationWrapper<?> verificationWrapper, InOrderImpl inOrder) {
17         VerificationMode verificationMode = verificationWrapper.wrappedVerification;
18 
19         VerificationMode inOrderWrappedVerificationMode = wrapInOrder(verificationWrapper, verificationMode, inOrder);
20 
21         delegate = verificationWrapper.copySelfWithNewVerificationMode(inOrderWrappedVerificationMode);
22     }
23 
24     @Override
verify(VerificationData data)25     public void verify(VerificationData data) {
26         delegate.verify(data);
27     }
28 
29     @Override
description(String description)30     public VerificationMode description(String description) {
31         return VerificationModeFactory.description(this, description);
32     }
33 
wrapInOrder(VerificationWrapper<?> verificationWrapper, VerificationMode verificationMode, InOrderImpl inOrder)34     private VerificationMode wrapInOrder(VerificationWrapper<?> verificationWrapper, VerificationMode verificationMode, InOrderImpl inOrder) {
35         if (verificationMode instanceof VerificationInOrderMode) {
36             final VerificationInOrderMode verificationInOrderMode = (VerificationInOrderMode)verificationMode;
37             return new InOrderWrapper(verificationInOrderMode, inOrder);
38         }
39 
40         if (verificationMode instanceof VerificationOverTimeImpl) {
41             final VerificationOverTimeImpl verificationOverTime = (VerificationOverTimeImpl)verificationMode;
42             if (verificationOverTime.isReturnOnSuccess()) {
43                 return new VerificationOverTimeImpl(verificationOverTime.getPollingPeriodMillis(),
44                         verificationOverTime.getTimer().duration(),
45                         wrapInOrder(verificationWrapper, verificationOverTime.getDelegate(), inOrder),
46                         verificationOverTime.isReturnOnSuccess());
47             }
48         }
49 
50         throw new MockitoException(verificationMode.getClass().getSimpleName() +
51                 " is not implemented to work with InOrder wrapped inside a " +
52                 verificationWrapper.getClass().getSimpleName());
53     }
54 }
55