All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.mockito.internal.MockitoCore Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package com.fitbur.mockito.internal;

import static com.fitbur.mockito.exceptions.Reporter.missingMethodInvocation;
import static com.fitbur.mockito.exceptions.Reporter.mocksHaveToBePassedToVerifyNoMoreInteractions;
import static com.fitbur.mockito.exceptions.Reporter.mocksHaveToBePassedWhenCreatingInOrder;
import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedToVerify;
import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedToVerifyNoMoreInteractions;
import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedWhenCreatingInOrder;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedToVerify;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedToVerifyNoMoreInteractions;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedWhenCreatingInOrder;
import static com.fitbur.mockito.internal.verification.VerificationModeFactory.noMoreInteractions;

import java.util.Arrays;
import java.util.List;

import com.fitbur.mockito.InOrder;
import com.fitbur.mockito.MockSettings;
import com.fitbur.mockito.MockingDetails;
import com.fitbur.mockito.exceptions.misusing.NotAMockException;
import com.fitbur.mockito.internal.creation.MockSettingsImpl;
import com.fitbur.mockito.internal.invocation.finder.VerifiableInvocationsFinder;
import com.fitbur.mockito.internal.progress.MockingProgress;
import com.fitbur.mockito.internal.progress.ThreadSafeMockingProgress;
import com.fitbur.mockito.internal.stubbing.InvocationContainer;
import com.fitbur.mockito.internal.stubbing.OngoingStubbingImpl;
import com.fitbur.mockito.internal.stubbing.StubberImpl;
import com.fitbur.mockito.internal.util.DefaultMockingDetails;
import com.fitbur.mockito.internal.util.MockUtil;
import com.fitbur.mockito.internal.verification.MockAwareVerificationMode;
import com.fitbur.mockito.internal.verification.VerificationDataImpl;
import com.fitbur.mockito.internal.verification.VerificationModeFactory;
import com.fitbur.mockito.internal.verification.api.InOrderContext;
import com.fitbur.mockito.internal.verification.api.VerificationDataInOrder;
import com.fitbur.mockito.internal.verification.api.VerificationDataInOrderImpl;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.mock.MockCreationSettings;
import com.fitbur.mockito.stubbing.OngoingStubbing;
import com.fitbur.mockito.stubbing.Stubber;
import com.fitbur.mockito.verification.VerificationMode;

@SuppressWarnings("unchecked")
public class MockitoCore {

    private final MockUtil mockUtil = new MockUtil();
    private final MockingProgress mockingProgress = new ThreadSafeMockingProgress();

    public boolean isTypeMockable(Class typeToMock) {
        return mockUtil.typeMockabilityOf(typeToMock).mockable();
    }

    public  T mock(Class typeToMock, MockSettings settings) {
        if (!MockSettingsImpl.class.isInstance(settings)) {
            throw new IllegalArgumentException(
                    "Unexpected implementation of '" + settings.getClass().getCanonicalName() + "'\n"
                    + "At the moment, you cannot provide your own implementations of that class.");
        }
        MockSettingsImpl impl = MockSettingsImpl.class.cast(settings);
        MockCreationSettings creationSettings = impl.confirm(typeToMock);
        T mock = mockUtil.createMock(creationSettings);
        mockingProgress.mockingStarted(mock, typeToMock);
        return mock;
    }

    public  OngoingStubbing when(T methodCall) {
        mockingProgress.stubbingStarted();
        @SuppressWarnings("unchecked")
        OngoingStubbing stubbing = (OngoingStubbing) mockingProgress.pullOngoingStubbing();
        if (stubbing == null) {
            mockingProgress.reset();
            throw missingMethodInvocation();
        }
        return stubbing;
    }
    
    public  T verify(T mock, VerificationMode mode) {
        if (mock == null) {
            throw nullPassedToVerify();
        } 
        if (!mockUtil.isMock(mock)) {
            throw notAMockPassedToVerify(mock.getClass());
        }
        VerificationMode actualMode = this.mockingProgress.maybeVerifyLazily(mode);
        mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode));
        return mock;
    }
    
    public  void reset(T ... mocks) {
        mockingProgress.validateState();
        mockingProgress.reset();
        mockingProgress.resetOngoingStubbing();
        
        for (T m : mocks) {
            mockUtil.resetMock(m);
        }
    }

    public  void clearInvocations(T ... mocks) {
        mockingProgress.validateState();
        mockingProgress.reset();
        mockingProgress.resetOngoingStubbing();

        for (T m : mocks) {
            mockUtil.getMockHandler(m).getInvocationContainer().clearInvocations();
        }
    }
    
    public void verifyNoMoreInteractions(Object... mocks) {
        assertMocksNotEmpty(mocks);
        mockingProgress.validateState();
        for (Object mock : mocks) {
            try {
                if (mock == null) {
                    throw nullPassedToVerifyNoMoreInteractions();
                }
                InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer();
                VerificationDataImpl data = new VerificationDataImpl(invocations, null);
                noMoreInteractions().verify(data);
            } catch (NotAMockException e) {
                throw notAMockPassedToVerifyNoMoreInteractions();
            }
        }
    }

    public void verifyNoMoreInteractionsInOrder(List mocks, InOrderContext inOrderContext) {
        mockingProgress.validateState();
        VerifiableInvocationsFinder finder = new VerifiableInvocationsFinder();
        VerificationDataInOrder data = new VerificationDataInOrderImpl(inOrderContext, finder.find(mocks), null);
        VerificationModeFactory.noMoreInteractions().verifyInOrder(data);
    }    
    
    private void assertMocksNotEmpty(Object[] mocks) {
        if (mocks == null || mocks.length == 0) {
            throw mocksHaveToBePassedToVerifyNoMoreInteractions();
        }
    }
    
    public InOrder inOrder(Object... mocks) {
        if (mocks == null || mocks.length == 0) {
            throw mocksHaveToBePassedWhenCreatingInOrder();
        }
        for (Object mock : mocks) {
            if (mock == null) {
                throw nullPassedWhenCreatingInOrder();
            } 
            if (!mockUtil.isMock(mock)) {
                throw notAMockPassedWhenCreatingInOrder();
            }
        }
        return new InOrderImpl(Arrays.asList(mocks));
    }
    
    public Stubber stubber() {
        mockingProgress.stubbingStarted();
        mockingProgress.resetOngoingStubbing();
        return new StubberImpl();
    }

    public void validateMockitoUsage() {
        mockingProgress.validateState();
    }

    /**
     * For testing purposes only. Is not the part of main API.
     * @return last invocation
     */
    public Invocation getLastInvocation() {
        OngoingStubbingImpl ongoingStubbing = ((OngoingStubbingImpl) mockingProgress.pullOngoingStubbing());
        List allInvocations = ongoingStubbing.getRegisteredInvocations();
        return allInvocations.get(allInvocations.size()-1);
    }

    public Object[] ignoreStubs(Object... mocks) {
        for (Object m : mocks) {
            InvocationContainer invocationContainer = new MockUtil().getMockHandler(m).getInvocationContainer();
            List ins = invocationContainer.getInvocations();
            for (Invocation in : ins) {
                if (in.stubInfo() != null) {
                    in.ignoreForVerification();
                }
            }
        }
        return mocks;
    }

    public MockingDetails mockingDetails(Object toInspect) {
        return new DefaultMockingDetails(toInspect, new MockUtil());
    }
}