Skip to content

Commit

Permalink
Move LocationFactory to plugins to allow overriding it
Browse files Browse the repository at this point in the history
  • Loading branch information
DerFrZocker committed Jul 20, 2023
1 parent cf8b820 commit be64f3f
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 58 deletions.
Expand Up @@ -11,7 +11,7 @@

import org.mockito.MockedConstruction;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.invocation.Location;
import org.mockito.plugins.MockMaker;

Expand All @@ -21,7 +21,7 @@ public final class MockedConstructionImpl<T> implements MockedConstruction<T> {

private boolean closed;

private final Location location = LocationFactory.create();
private final Location location = Plugins.getLocationFactory().create();

protected MockedConstructionImpl(MockMaker.ConstructionMockControl<T> control) {
this.control = control;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mockito/internal/MockedStaticImpl.java
Expand Up @@ -17,7 +17,7 @@
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.listeners.VerificationStartedNotifier;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.stubbing.InvocationContainerImpl;
Expand All @@ -35,7 +35,7 @@ public final class MockedStaticImpl<T> implements MockedStatic<T> {

private boolean closed;

private final Location location = LocationFactory.create();
private final Location location = Plugins.getLocationFactory().create();

protected MockedStaticImpl(MockMaker.StaticMockControl<T> control) {
this.control = control;
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.mockito.plugins.AnnotationEngine;
import org.mockito.plugins.DoNotMockEnforcer;
import org.mockito.plugins.InstantiatorProvider2;
import org.mockito.plugins.LocationFactory;
import org.mockito.plugins.MemberAccessor;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.MockitoLogger;
Expand Down Expand Up @@ -63,6 +64,9 @@ public class DefaultMockitoPlugins implements MockitoPlugins {
DEFAULT_PLUGINS.put(
DoNotMockEnforcer.class.getName(),
"org.mockito.internal.configuration.DefaultDoNotMockEnforcer");
DEFAULT_PLUGINS.put(
LocationFactory.class.getName(),
"org.mockito.internal.debugging.DefaultLocationFactory");

MOCK_MAKER_ALIASES.add(INLINE_ALIAS);
MOCK_MAKER_ALIASES.add(PROXY_ALIAS);
Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.mockito.plugins.AnnotationEngine;
import org.mockito.plugins.DoNotMockEnforcer;
import org.mockito.plugins.InstantiatorProvider2;
import org.mockito.plugins.LocationFactory;
import org.mockito.plugins.MemberAccessor;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.MockResolver;
Expand Down Expand Up @@ -49,6 +50,9 @@ class PluginRegistry {
private final DoNotMockEnforcer doNotMockEnforcer =
new PluginLoader(pluginSwitch).loadPlugin(DoNotMockEnforcer.class);

private final LocationFactory locationFactory =
new PluginLoader(pluginSwitch).loadPlugin(LocationFactory.class);

PluginRegistry() {
instantiatorProvider =
new PluginLoader(pluginSwitch).loadPlugin(InstantiatorProvider2.class);
Expand Down Expand Up @@ -123,6 +127,16 @@ DoNotMockEnforcer getDoNotMockEnforcer() {
return doNotMockEnforcer;
}

/**
* Returns the location factory available for the current runtime.
*
* <p> Returns {@link org.mockito.internal.debugging.DefaultLocationFactory} if no
* {@link LocationFactory} extension exists or is visible in the current classpath.</p>
*/
LocationFactory getLocationFactory() {
return locationFactory;
}

/**
* Returns a list of available mock resolvers if any.
*
Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.mockito.plugins.AnnotationEngine;
import org.mockito.plugins.DoNotMockEnforcer;
import org.mockito.plugins.InstantiatorProvider2;
import org.mockito.plugins.LocationFactory;
import org.mockito.plugins.MemberAccessor;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.MockResolver;
Expand Down Expand Up @@ -105,5 +106,15 @@ public static DoNotMockEnforcer getDoNotMockEnforcer() {
return registry.getDoNotMockEnforcer();
}

/**
* Returns the {@link LocationFactory} available for the current runtime.
*
* <p> Returns {@link org.mockito.internal.debugging.DefaultLocationFactory} if no
* {@link LocationFactory} extension exists or is visible in the current classpath.</p>
*/
public static LocationFactory getLocationFactory() {
return registry.getLocationFactory();
}

private Plugins() {}
}
Expand Up @@ -51,7 +51,6 @@
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.internal.invocation.SerializableMethod;
Expand Down Expand Up @@ -132,7 +131,11 @@ public Callable<?> handle(Object instance, Method origin, Object[] arguments) th
}
return new ReturnValueWrapper(
interceptor.doIntercept(
instance, origin, arguments, realMethod, LocationFactory.create(true)));
instance,
origin,
arguments,
realMethod,
Plugins.getLocationFactory().create(true)));
}

@Override
Expand All @@ -150,7 +153,7 @@ public Callable<?> handleStatic(Class<?> type, Method origin, Object[] arguments
origin,
arguments,
new StaticMethodCall(selfCallInfo, type, origin, arguments),
LocationFactory.create(true)));
Plugins.getLocationFactory().create(true)));
}

@Override
Expand Down
Expand Up @@ -22,7 +22,7 @@
import net.bytebuddy.implementation.bind.annotation.StubValue;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.invocation.Location;
import org.mockito.invocation.MockHandler;
Expand Down Expand Up @@ -53,7 +53,8 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo

Object doIntercept(Object mock, Method invokedMethod, Object[] arguments, RealMethod realMethod)
throws Throwable {
return doIntercept(mock, invokedMethod, arguments, realMethod, LocationFactory.create());
return doIntercept(
mock, invokedMethod, arguments, realMethod, Plugins.getLocationFactory().create());
}

Object doIntercept(
Expand Down
Expand Up @@ -5,7 +5,7 @@
package org.mockito.internal.creation.proxy;

import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.internal.util.Platform;
import org.mockito.invocation.MockHandler;
Expand Down Expand Up @@ -158,7 +158,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
args,
realMethod,
settings,
LocationFactory.create()));
Plugins.getLocationFactory().create()));
}
}

Expand Down
Expand Up @@ -5,17 +5,16 @@
package org.mockito.internal.debugging;

import org.mockito.invocation.Location;
import org.mockito.plugins.LocationFactory;

public final class LocationFactory {
private static final Factory factory = createLocationFactory();
public final class DefaultLocationFactory implements LocationFactory {
private final Factory factory = createLocationFactory();

private LocationFactory() {}

public static Location create() {
public Location create() {
return create(false);
}

public static Location create(boolean inline) {
public Location create(boolean inline) {
return factory.create(inline);
}

Expand All @@ -29,7 +28,7 @@ private static Factory createLocationFactory() {
// available, in this case we have to fallback to Java 8 style of stack
// traversing.
Class.forName("java.lang.StackWalker");
return new DefaultLocationFactory();
return new StackWalkerLocationFactory();
} catch (ClassNotFoundException e) {
return new Java8LocationFactory();
}
Expand All @@ -42,7 +41,7 @@ public Location create(boolean inline) {
}
}

private static final class DefaultLocationFactory implements Factory {
private static final class StackWalkerLocationFactory implements Factory {
@Override
public Location create(boolean inline) {
return new LocationImpl(inline);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/mockito/internal/debugging/Localized.java
Expand Up @@ -4,6 +4,7 @@
*/
package org.mockito.internal.debugging;

import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.invocation.Location;

public class Localized<T> {
Expand All @@ -13,7 +14,7 @@ public class Localized<T> {

public Localized(T object) {
this.object = object;
location = LocationFactory.create();
location = Plugins.getLocationFactory().create();
}

public T getObject() {
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/org/mockito/internal/exceptions/Reporter.java
Expand Up @@ -42,7 +42,7 @@
import org.mockito.exceptions.verification.TooManyActualInvocations;
import org.mockito.exceptions.verification.VerificationInOrderFailure;
import org.mockito.exceptions.verification.WantedButNotInvoked;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.exceptions.util.ScenarioPrinter;
import org.mockito.internal.junit.ExceptionFactory;
import org.mockito.internal.matchers.LocalizedMatcher;
Expand Down Expand Up @@ -106,7 +106,7 @@ public static MockitoException incorrectUseOfApi() {
return new MockitoException(
join(
"Incorrect use of API detected here:",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"",
"You probably stored a reference to OngoingStubbing returned by when() and called stubbing methods like thenReturn() on this reference more than once.",
"Examples of correct usage:",
Expand Down Expand Up @@ -284,7 +284,7 @@ public static MockitoException incorrectUseOfAdditionalMatchers(
"Invalid use of argument matchers inside additional matcher "
+ additionalMatcherName
+ " !",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"",
expectedSubMatchersCount
+ " sub matchers expected, "
Expand Down Expand Up @@ -317,7 +317,7 @@ public static MockitoException reportNoSubMatchersFound(String additionalMatcher
return new InvalidUseOfMatchersException(
join(
"No matchers found for additional matcher " + additionalMatcherName,
LocationFactory.create(),
Plugins.getLocationFactory().create(),
""));
}

Expand Down Expand Up @@ -348,7 +348,7 @@ public static AssertionError argumentsAreDifferent(
.append("Argument(s) are different! Wanted:\n")
.append(wanted)
.append("\n")
.append(LocationFactory.create())
.append(Plugins.getLocationFactory().create())
.append("\n")
.append("Actual invocations have different arguments");

Expand Down Expand Up @@ -419,7 +419,11 @@ public static MockitoAssertionError wantedButNotInvoked(
}

private static String createWantedButNotInvokedMessage(DescribedInvocation wanted) {
return join("Wanted but not invoked:", wanted.toString(), LocationFactory.create(), "");
return join(
"Wanted but not invoked:",
wanted.toString(),
Plugins.getLocationFactory().create(),
"");
}

public static MockitoAssertionError wantedButNotInvokedInOrder(
Expand All @@ -429,7 +433,7 @@ public static MockitoAssertionError wantedButNotInvokedInOrder(
"Verification in order failure",
"Wanted but not invoked:",
wanted.toString(),
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"Wanted anywhere AFTER following interaction:",
previous.toString(),
previous.getLocation(),
Expand All @@ -454,7 +458,7 @@ private static String createTooManyInvocationsMessage(
return join(
wanted.toString(),
"Wanted " + pluralize(wantedCount) + ":",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"But was " + pluralize(actualCount) + ":",
createAllLocationsMessage(invocations),
"");
Expand All @@ -467,7 +471,7 @@ public static MockitoAssertionError neverWantedButInvoked(
join(
wanted.toString(),
"Never wanted here:",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"But invoked here:",
createAllLocationsArgsMessage(invocations)));
}
Expand Down Expand Up @@ -517,7 +521,7 @@ private static String createTooFewInvocationsMessage(
"Wanted "
+ discrepancy.getPluralizedWantedCount()
+ (discrepancy.getWantedCount() == 0 ? "." : ":"),
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"But was "
+ discrepancy.getPluralizedActualCount()
+ (discrepancy.getActualCount() == 0 ? "." : ":"),
Expand Down Expand Up @@ -550,7 +554,7 @@ public static MockitoAssertionError noMoreInteractionsWanted(
return new NoInteractionsWanted(
join(
"No interactions wanted here:",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"But found this interaction on mock '"
+ MockUtil.getMockName(undesired.getMock())
+ "':",
Expand All @@ -562,7 +566,7 @@ public static MockitoAssertionError noMoreInteractionsWantedInOrder(Invocation u
return new VerificationInOrderFailure(
join(
"No interactions wanted here:",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"But found this interaction on mock '"
+ MockUtil.getMockName(undesired.getMock())
+ "':",
Expand All @@ -581,7 +585,7 @@ public static MockitoAssertionError noInteractionsWanted(
return new NoInteractionsWanted(
join(
"No interactions wanted here:",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"But found these interactions on mock '"
+ MockUtil.getMockName(mock)
+ "':",
Expand Down Expand Up @@ -699,7 +703,7 @@ public static MockitoException smartNullPointerException(String invocation, Loca
return new SmartNullPointerException(
join(
"You have a NullPointerException here:",
LocationFactory.create(),
Plugins.getLocationFactory().create(),
"because this method call was *not* stubbed correctly:",
location,
invocation,
Expand Down
Expand Up @@ -7,8 +7,8 @@
import java.lang.reflect.Method;
import java.util.concurrent.Callable;

import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.creation.DelegatingMethod;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.invocation.mockref.MockWeakReference;
import org.mockito.internal.progress.SequenceNumber;
import org.mockito.invocation.Invocation;
Expand Down Expand Up @@ -71,7 +71,12 @@ private static InterceptedInvocation createInvocation(
RealMethod realMethod,
MockCreationSettings settings) {
return createInvocation(
mock, invokedMethod, arguments, realMethod, settings, LocationFactory.create());
mock,
invokedMethod,
arguments,
realMethod,
settings,
Plugins.getLocationFactory().create());
}

private static MockitoMethod createMockitoMethod(Method method, MockCreationSettings settings) {
Expand Down

0 comments on commit be64f3f

Please sign in to comment.