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.creation.settings; 6 7 import java.io.Serializable; 8 import java.util.ArrayList; 9 import java.util.LinkedHashSet; 10 import java.util.LinkedList; 11 import java.util.List; 12 import java.util.Set; 13 import java.util.concurrent.CopyOnWriteArrayList; 14 15 import org.mockito.listeners.InvocationListener; 16 import org.mockito.listeners.StubbingLookupListener; 17 import org.mockito.listeners.VerificationStartedListener; 18 import org.mockito.mock.MockCreationSettings; 19 import org.mockito.mock.MockName; 20 import org.mockito.mock.SerializableMode; 21 import org.mockito.stubbing.Answer; 22 23 public class CreationSettings<T> implements MockCreationSettings<T>, Serializable { 24 private static final long serialVersionUID = -6789800638070123629L; 25 26 protected Class<T> typeToMock; 27 protected Set<Class<?>> extraInterfaces = new LinkedHashSet<>(); 28 protected String name; 29 protected Object spiedInstance; 30 protected Answer<Object> defaultAnswer; 31 protected MockName mockName; 32 protected SerializableMode serializableMode = SerializableMode.NONE; 33 protected List<InvocationListener> invocationListeners = new ArrayList<>(); 34 35 // Other listeners in this class may also need concurrency-safe implementation. However, no 36 // issue was reported about it. 37 // If we do it, we need to understand usage patterns and choose the right concurrent 38 // implementation. 39 protected List<StubbingLookupListener> stubbingLookupListeners = new CopyOnWriteArrayList<>(); 40 41 protected List<VerificationStartedListener> verificationStartedListeners = new LinkedList<>(); 42 protected boolean stubOnly; 43 protected boolean stripAnnotations; 44 private boolean useConstructor; 45 private Object outerClassInstance; 46 private Object[] constructorArgs; 47 protected boolean lenient; 48 CreationSettings()49 public CreationSettings() {} 50 51 @SuppressWarnings("unchecked") CreationSettings(CreationSettings copy)52 public CreationSettings(CreationSettings copy) { 53 // TODO can we have a reflection test here? We had a couple of bugs here in the past. 54 this.typeToMock = copy.typeToMock; 55 this.extraInterfaces = copy.extraInterfaces; 56 this.name = copy.name; 57 this.spiedInstance = copy.spiedInstance; 58 this.defaultAnswer = copy.defaultAnswer; 59 this.mockName = copy.mockName; 60 this.serializableMode = copy.serializableMode; 61 this.invocationListeners = copy.invocationListeners; 62 this.stubbingLookupListeners = copy.stubbingLookupListeners; 63 this.verificationStartedListeners = copy.verificationStartedListeners; 64 this.stubOnly = copy.stubOnly; 65 this.useConstructor = copy.isUsingConstructor(); 66 this.outerClassInstance = copy.getOuterClassInstance(); 67 this.constructorArgs = copy.getConstructorArgs(); 68 this.lenient = copy.lenient; 69 this.stripAnnotations = copy.stripAnnotations; 70 } 71 72 @Override getTypeToMock()73 public Class<T> getTypeToMock() { 74 return typeToMock; 75 } 76 setTypeToMock(Class<T> typeToMock)77 public CreationSettings<T> setTypeToMock(Class<T> typeToMock) { 78 this.typeToMock = typeToMock; 79 return this; 80 } 81 82 @Override getExtraInterfaces()83 public Set<Class<?>> getExtraInterfaces() { 84 return extraInterfaces; 85 } 86 setExtraInterfaces(Set<Class<?>> extraInterfaces)87 public CreationSettings<T> setExtraInterfaces(Set<Class<?>> extraInterfaces) { 88 this.extraInterfaces = extraInterfaces; 89 return this; 90 } 91 getName()92 public String getName() { 93 return name; 94 } 95 96 @Override getSpiedInstance()97 public Object getSpiedInstance() { 98 return spiedInstance; 99 } 100 101 @Override getDefaultAnswer()102 public Answer<Object> getDefaultAnswer() { 103 return defaultAnswer; 104 } 105 106 @Override getMockName()107 public MockName getMockName() { 108 return mockName; 109 } 110 setMockName(MockName mockName)111 public CreationSettings<T> setMockName(MockName mockName) { 112 this.mockName = mockName; 113 return this; 114 } 115 116 @Override isSerializable()117 public boolean isSerializable() { 118 return serializableMode != SerializableMode.NONE; 119 } 120 setSerializableMode(SerializableMode serializableMode)121 public CreationSettings<T> setSerializableMode(SerializableMode serializableMode) { 122 this.serializableMode = serializableMode; 123 return this; 124 } 125 126 @Override getSerializableMode()127 public SerializableMode getSerializableMode() { 128 return serializableMode; 129 } 130 131 @Override getInvocationListeners()132 public List<InvocationListener> getInvocationListeners() { 133 return invocationListeners; 134 } 135 136 @Override getVerificationStartedListeners()137 public List<VerificationStartedListener> getVerificationStartedListeners() { 138 return verificationStartedListeners; 139 } 140 141 @Override getStubbingLookupListeners()142 public List<StubbingLookupListener> getStubbingLookupListeners() { 143 return stubbingLookupListeners; 144 } 145 146 @Override isUsingConstructor()147 public boolean isUsingConstructor() { 148 return useConstructor; 149 } 150 151 @Override isStripAnnotations()152 public boolean isStripAnnotations() { 153 return stripAnnotations; 154 } 155 156 @Override getConstructorArgs()157 public Object[] getConstructorArgs() { 158 return constructorArgs; 159 } 160 161 @Override getOuterClassInstance()162 public Object getOuterClassInstance() { 163 return outerClassInstance; 164 } 165 166 @Override isStubOnly()167 public boolean isStubOnly() { 168 return stubOnly; 169 } 170 171 @Override isLenient()172 public boolean isLenient() { 173 return lenient; 174 } 175 } 176