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

mockit.internal.faking.FakeStates Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external APIs; JUnit (4 & 5) and TestNG test runners are supported. It also contains an advanced code coverage tool.

The newest version!
/*
 * Copyright (c) 2006 JMockit developers
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.faking;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import mockit.internal.util.ClassLoad;

import org.checkerframework.checker.index.qual.NonNegative;

/**
 * Holds state associated with fake class containing {@linkplain mockit.Mock annotated fakes}.
 */
public final class FakeStates {
    private static final Pattern SPACE = Pattern.compile(" ");

    /**
     * For each fake instance and each @Mock method containing the Invocation parameter, a
     * runtime state will be kept here.
     */
    @NonNull
    private final Map> fakesToFakeStates;
    @NonNull
    private final Map> startupFakesToFakeStates;

    public FakeStates() {
        startupFakesToFakeStates = new IdentityHashMap<>(2);
        fakesToFakeStates = new IdentityHashMap<>(8);
    }

    void addStartupFakeAndItsFakeStates(@NonNull Object fake, @NonNull List fakeStates) {
        startupFakesToFakeStates.put(fake, fakeStates);
    }

    void addFakeAndItsFakeStates(@NonNull Object fake, @NonNull List fakeStates) {
        fakesToFakeStates.put(fake, fakeStates);
    }

    public void copyFakeStates(@NonNull Object previousFake, @NonNull Object newFake) {
        List fakeStates = fakesToFakeStates.get(previousFake);

        if (fakeStates != null) {
            List copiedFakeStates = new ArrayList<>(fakeStates.size());

            for (FakeState fakeState : fakeStates) {
                copiedFakeStates.add(new FakeState(fakeState));
            }

            fakesToFakeStates.put(newFake, copiedFakeStates);
        }
    }

    public void removeClassState(@NonNull Class redefinedClass,
            @Nullable String internalNameForOneOrMoreFakeClasses) {
        removeFakeStates(redefinedClass);

        if (internalNameForOneOrMoreFakeClasses != null) {
            if (internalNameForOneOrMoreFakeClasses.indexOf(' ') < 0) {
                removeFakeStates(internalNameForOneOrMoreFakeClasses);
            } else {
                String[] fakeClassesInternalNames = SPACE.split(internalNameForOneOrMoreFakeClasses);

                for (String fakeClassInternalName : fakeClassesInternalNames) {
                    removeFakeStates(fakeClassInternalName);
                }
            }
        }
    }

    private void removeFakeStates(@NonNull Class redefinedClass) {
        Iterator> itr = fakesToFakeStates.values().iterator();

        while (itr.hasNext()) {
            List fakeStates = itr.next();
            FakeState fakeState = fakeStates.get(0);

            if (fakeState.getRealClass() == redefinedClass) {
                fakeStates.clear();
                itr.remove();
            }
        }
    }

    private void removeFakeStates(@NonNull String fakeClassInternalName) {
        Class fakeClass = ClassLoad.loadClass(fakeClassInternalName.replace('/', '.'));
        Iterator>> itr = fakesToFakeStates.entrySet().iterator();

        while (itr.hasNext()) {
            Entry> fakeAndFakeStates = itr.next();
            Object fake = fakeAndFakeStates.getKey();

            if (fake.getClass() == fakeClass) {
                itr.remove();
            }
        }
    }

    public boolean updateFakeState(@NonNull Object fake, @NonNegative int fakeStateIndex) {
        FakeState fakeState = getFakeState(fake, fakeStateIndex);
        return fakeState.update();
    }

    @NonNull
    FakeState getFakeState(@NonNull Object fake, @NonNegative int fakeStateIndex) {
        List fakeStates = startupFakesToFakeStates.get(fake);

        if (fakeStates == null) {
            fakeStates = fakesToFakeStates.get(fake);
        }

        FakeState fakeState = fakeStates.get(fakeStateIndex);
        assert fakeState != null;
        return fakeState;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy