Enable findbugs in mockito-configuration
[yangtools.git] / common / mockito-configuration / src / main / java / org / mockito / configuration / ArgumentsExtractorVerifier.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.mockito.configuration;
9
10 import java.util.List;
11 import org.mockito.exceptions.base.MockitoException;
12 import org.mockito.internal.invocation.InvocationsFinder;
13 import org.mockito.internal.verification.VerificationModeFactory;
14 import org.mockito.internal.verification.api.VerificationData;
15 import org.mockito.invocation.Invocation;
16 import org.mockito.verification.VerificationMode;
17
18 /**
19  * Verifier that extracts arguments from actual invocation. Useful when deeper validation of arguments is needed.
20  */
21 public class ArgumentsExtractorVerifier implements VerificationMode {
22     private Object[] arguments = null;
23
24     @Override
25     public void verify(final VerificationData data) {
26         List<Invocation> actualInvocations =
27             InvocationsFinder.findInvocations(data.getAllInvocations(), data.getTarget());
28         if (actualInvocations.size() != 1) {
29             throw new MockitoException("This verifier can only be used with 1 invocation, got "
30                     + actualInvocations.size());
31         }
32         Invocation invocation = actualInvocations.get(0);
33         arguments = invocation.getArguments();
34         invocation.markVerified();
35     }
36
37     @Override
38     public VerificationMode description(final String description) {
39         return VerificationModeFactory.description(this, description);
40     }
41
42     public Object[] getArguments() {
43         return arguments == null ? null : arguments.clone();
44     }
45 }