/
MockCreationValidator.java
65 lines (55 loc) · 2.47 KB
/
MockCreationValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import static org.mockito.internal.exceptions.Reporter.cannotMockClass;
import static org.mockito.internal.exceptions.Reporter.extraInterfacesCannotContainMockedType;
import static org.mockito.internal.exceptions.Reporter.mockedTypeIsInconsistentWithDelegatedInstanceType;
import static org.mockito.internal.exceptions.Reporter.mockedTypeIsInconsistentWithSpiedInstanceType;
import static org.mockito.internal.exceptions.Reporter.usingConstructorWithFancySerializable;
import java.util.Collection;
import org.mockito.mock.SerializableMode;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.MockMaker.TypeMockability;
@SuppressWarnings("unchecked")
public class MockCreationValidator {
public void validateType(Class<?> classToMock, Class<? extends MockMaker> mockMaker) {
TypeMockability typeMockability = MockUtil.typeMockabilityOf(classToMock, mockMaker);
if (!typeMockability.mockable()) {
throw cannotMockClass(classToMock, typeMockability.nonMockableReason());
}
}
public void validateExtraInterfaces(
Class<?> classToMock, Collection<Class<?>> 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);
}
}
}