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

com.fitbur.mockito.internal.util.MockCreationValidator 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.util;

import static com.fitbur.mockito.exceptions.Reporter.cannotMockClass;
import static com.fitbur.mockito.exceptions.Reporter.extraInterfacesCannotContainMockedType;
import static com.fitbur.mockito.exceptions.Reporter.mockedTypeIsInconsistentWithDelegatedInstanceType;
import static com.fitbur.mockito.exceptions.Reporter.mockedTypeIsInconsistentWithSpiedInstanceType;
import static com.fitbur.mockito.exceptions.Reporter.usingConstructorWithFancySerializable;

import java.util.Collection;

import com.fitbur.mockito.mock.SerializableMode;
import com.fitbur.mockito.plugins.MockMaker.TypeMockability;

@SuppressWarnings("unchecked")
public class MockCreationValidator {

    private final MockUtil mockUtil = new MockUtil();

    public void validateType(Class classToMock) {
        TypeMockability typeMockability = mockUtil.typeMockabilityOf(classToMock);
        if (!typeMockability.mockable()) {
            throw cannotMockClass(classToMock, typeMockability.nonMockableReason());
        }
    }

    public void validateExtraInterfaces(Class classToMock, Collection> extraInterfaces) {
        if (extraInterfaces == null) {
            return;
        }

        for (Class i : extraInterfaces) {
            if (classToMock == i) {
                throw extraInterfacesCannotContainMockedType(classToMock);
            }
        }
    }

    public void validateMockedType(Class classToMock, Object spiedInstance) {
        if (classToMock == null || spiedInstance == null) {
            return;
        }
        if (!classToMock.equals(spiedInstance.getClass())) {
            throw mockedTypeIsInconsistentWithSpiedInstanceType(classToMock, spiedInstance);
        }
    }

    public void validateDelegatedInstance(Class classToMock, Object delegatedInstance) {
        if (classToMock == null || delegatedInstance == null) {
            return;
        }
        if (delegatedInstance.getClass().isAssignableFrom(classToMock)) {
            throw mockedTypeIsInconsistentWithDelegatedInstanceType(classToMock, delegatedInstance);
        }
    }

    public void validateConstructorUse(boolean usingConstructor, SerializableMode mode) {
        if (usingConstructor && mode == SerializableMode.ACROSS_CLASSLOADERS) {
            throw usingConstructorWithFancySerializable(mode);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy