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

org.mockito.internal.creation.settings.CreationSettings Maven / Gradle / Ivy

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

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import org.mockito.listeners.InvocationListener;
import org.mockito.listeners.StubbingLookupListener;
import org.mockito.listeners.VerificationStartedListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.mock.MockName;
import org.mockito.mock.SerializableMode;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;

public class CreationSettings implements MockCreationSettings, Serializable {
    private static final long serialVersionUID = -6789800638070123629L;

    protected Class typeToMock;
    protected transient Type genericTypeToMock;
    protected Set> extraInterfaces = new LinkedHashSet<>();
    protected String name;
    protected Object spiedInstance;
    protected Answer defaultAnswer;
    protected MockName mockName;
    protected SerializableMode serializableMode = SerializableMode.NONE;
    protected List invocationListeners = new ArrayList<>();

    // Other listeners in this class may also need concurrency-safe implementation. However, no
    // issue was reported about it.
    // If we do it, we need to understand usage patterns and choose the right concurrent
    // implementation.
    protected List stubbingLookupListeners = new CopyOnWriteArrayList<>();

    protected List verificationStartedListeners = new LinkedList<>();
    protected boolean stubOnly;
    protected boolean stripAnnotations;
    private boolean useConstructor;
    private Object outerClassInstance;
    private Object[] constructorArgs;
    protected Strictness strictness = null;
    protected String mockMaker;

    public CreationSettings() {}

    @SuppressWarnings("unchecked")
    public CreationSettings(CreationSettings copy) {
        // TODO can we have a reflection test here? We had a couple of bugs here in the past.
        this.typeToMock = copy.typeToMock;
        this.genericTypeToMock = copy.genericTypeToMock;
        this.extraInterfaces = copy.extraInterfaces;
        this.name = copy.name;
        this.spiedInstance = copy.spiedInstance;
        this.defaultAnswer = copy.defaultAnswer;
        this.mockName = copy.mockName;
        this.serializableMode = copy.serializableMode;
        this.invocationListeners = copy.invocationListeners;
        this.stubbingLookupListeners = copy.stubbingLookupListeners;
        this.verificationStartedListeners = copy.verificationStartedListeners;
        this.stubOnly = copy.stubOnly;
        this.useConstructor = copy.isUsingConstructor();
        this.outerClassInstance = copy.getOuterClassInstance();
        this.constructorArgs = copy.getConstructorArgs();
        this.strictness = copy.strictness;
        this.stripAnnotations = copy.stripAnnotations;
        this.mockMaker = copy.mockMaker;
    }

    @Override
    public Class getTypeToMock() {
        return typeToMock;
    }

    public CreationSettings setTypeToMock(Class typeToMock) {
        this.typeToMock = typeToMock;
        return this;
    }

    public CreationSettings setGenericTypeToMock(Type genericTypeToMock) {
        this.genericTypeToMock = genericTypeToMock;
        return this;
    }

    @Override
    public Set> getExtraInterfaces() {
        return extraInterfaces;
    }

    public CreationSettings setExtraInterfaces(Set> extraInterfaces) {
        this.extraInterfaces = extraInterfaces;
        return this;
    }

    public String getName() {
        return name;
    }

    @Override
    public Object getSpiedInstance() {
        return spiedInstance;
    }

    @Override
    public Answer getDefaultAnswer() {
        return defaultAnswer;
    }

    @Override
    public MockName getMockName() {
        return mockName;
    }

    public CreationSettings setMockName(MockName mockName) {
        this.mockName = mockName;
        return this;
    }

    @Override
    public boolean isSerializable() {
        return serializableMode != SerializableMode.NONE;
    }

    public CreationSettings setSerializableMode(SerializableMode serializableMode) {
        this.serializableMode = serializableMode;
        return this;
    }

    @Override
    public SerializableMode getSerializableMode() {
        return serializableMode;
    }

    @Override
    public List getInvocationListeners() {
        return invocationListeners;
    }

    @Override
    public List getVerificationStartedListeners() {
        return verificationStartedListeners;
    }

    @Override
    public List getStubbingLookupListeners() {
        return stubbingLookupListeners;
    }

    @Override
    public boolean isUsingConstructor() {
        return useConstructor;
    }

    @Override
    public boolean isStripAnnotations() {
        return stripAnnotations;
    }

    @Override
    public Object[] getConstructorArgs() {
        return constructorArgs;
    }

    @Override
    public Object getOuterClassInstance() {
        return outerClassInstance;
    }

    @Override
    public boolean isStubOnly() {
        return stubOnly;
    }

    @Override
    public boolean isLenient() {
        return strictness == Strictness.LENIENT;
    }

    @Override
    public Strictness getStrictness() {
        return strictness;
    }

    @Override
    public String getMockMaker() {
        return mockMaker;
    }

    @Override
    public Type getGenericTypeToMock() {
        return genericTypeToMock;
    }
}