• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.util;
6 
7 import org.mockito.exceptions.misusing.NotAMockException;
8 import org.mockito.internal.InternalMockHandler;
9 import org.mockito.internal.configuration.ClassPathLoader;
10 import org.mockito.internal.creation.settings.CreationSettings;
11 import org.mockito.internal.handler.MockHandlerFactory;
12 import org.mockito.internal.util.reflection.LenientCopyTool;
13 import org.mockito.invocation.MockHandler;
14 import org.mockito.mock.MockCreationSettings;
15 import org.mockito.mock.MockName;
16 import org.mockito.plugins.MockMaker;
17 
18 import java.lang.reflect.Modifier;
19 
20 @SuppressWarnings("unchecked")
21 public class MockUtil {
22 
23     private static final MockMaker mockMaker = ClassPathLoader.getMockMaker();
24 
isTypeMockable(Class<?> type)25     public boolean isTypeMockable(Class<?> type) {
26       return !type.isPrimitive() && !Modifier.isFinal(type.getModifiers());
27     }
28 
createMock(MockCreationSettings<T> settings)29     public <T> T createMock(MockCreationSettings<T> settings) {
30         MockHandler mockHandler = new MockHandlerFactory().create(settings);
31 
32         T mock = mockMaker.createMock(settings, mockHandler);
33 
34         Object spiedInstance = settings.getSpiedInstance();
35         if (spiedInstance != null) {
36             new LenientCopyTool().copyToMock(spiedInstance, mock);
37         }
38 
39         return mock;
40     }
41 
resetMock(T mock)42     public <T> void resetMock(T mock) {
43         InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock);
44         MockCreationSettings settings = oldHandler.getMockSettings();
45         MockHandler newHandler = new MockHandlerFactory().create(settings);
46 
47         mockMaker.resetMock(mock, newHandler, settings);
48     }
49 
getMockHandler(T mock)50     public <T> InternalMockHandler<T> getMockHandler(T mock) {
51         if (mock == null) {
52             throw new NotAMockException("Argument should be a mock, but is null!");
53         }
54 
55         if (isMockitoMock(mock)) {
56             return (InternalMockHandler) mockMaker.getHandler(mock);
57         } else {
58             throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
59         }
60     }
61 
isMock(Object mock)62     public boolean isMock(Object mock) {
63         // double check to avoid classes that have the same interfaces, could be great to have a custom mockito field in the proxy instead of relying on instance fields
64         return mock instanceof MockitoMock && isMockitoMock(mock);
65     }
66 
isSpy(Object mock)67     public boolean isSpy(Object mock) {
68         return mock instanceof MockitoSpy;
69     }
70 
isMock(Class mockClass)71     public boolean isMock(Class mockClass) {
72         return mockClass != null && MockitoMock.class.isAssignableFrom(mockClass);
73     }
74 
isSpy(Class mockClass)75     public boolean isSpy(Class mockClass) {
76         return mockClass != null && MockitoSpy.class.isAssignableFrom(mockClass);
77     }
78 
isMockitoMock(T mock)79     private <T> boolean isMockitoMock(T mock) {
80         return mockMaker.getHandler(mock) != null;
81     }
82 
getMockName(Object mock)83     public MockName getMockName(Object mock) {
84         return getMockHandler(mock).getMockSettings().getMockName();
85     }
86 
maybeRedefineMockName(Object mock, String newName)87     public void maybeRedefineMockName(Object mock, String newName) {
88         MockName mockName = getMockName(mock);
89         //TODO SF hacky...
90         if (mockName.isDefault() && getMockHandler(mock).getMockSettings() instanceof CreationSettings) {
91             ((CreationSettings) getMockHandler(mock).getMockSettings()).setMockName(new MockNameImpl(newName));
92         }
93     }
94 
getMockSettings(Object mock)95     public MockCreationSettings getMockSettings(Object mock) {
96         return getMockHandler(mock).getMockSettings();
97     }
98 }
99