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; 6 7 import org.mockito.MockSettings; 8 9 import static org.mockito.internal.exceptions.Reporter.*; 10 import org.mockito.internal.creation.settings.CreationSettings; 11 import org.mockito.internal.debugging.VerboseMockInvocationLogger; 12 import org.mockito.internal.util.MockCreationValidator; 13 import org.mockito.internal.util.MockNameImpl; 14 import org.mockito.listeners.InvocationListener; 15 import org.mockito.mock.MockCreationSettings; 16 import org.mockito.mock.MockName; 17 import org.mockito.mock.SerializableMode; 18 import org.mockito.stubbing.Answer; 19 20 import java.io.Serializable; 21 import java.util.HashSet; 22 import java.util.List; 23 import java.util.Set; 24 25 import static org.mockito.internal.util.collections.Sets.newSet; 26 27 @SuppressWarnings("unchecked") 28 public class MockSettingsImpl<T> extends CreationSettings<T> implements MockSettings, MockCreationSettings<T> { 29 30 private static final long serialVersionUID = 4475297236197939569L; 31 private boolean useConstructor; 32 private Object outerClassInstance; 33 serializable()34 public MockSettings serializable() { 35 return serializable(SerializableMode.BASIC); 36 } 37 serializable(SerializableMode mode)38 public MockSettings serializable(SerializableMode mode) { 39 this.serializableMode = mode; 40 return this; 41 } 42 extraInterfaces(Class<?>.... extraInterfaces)43 public MockSettings extraInterfaces(Class<?>... extraInterfaces) { 44 if (extraInterfaces == null || extraInterfaces.length == 0) { 45 throw extraInterfacesRequiresAtLeastOneInterface(); 46 } 47 48 for (Class<?> i : extraInterfaces) { 49 if (i == null) { 50 throw extraInterfacesDoesNotAcceptNullParameters(); 51 } else if (!i.isInterface()) { 52 throw extraInterfacesAcceptsOnlyInterfaces(i); 53 } 54 } 55 this.extraInterfaces = newSet(extraInterfaces); 56 return this; 57 } 58 getMockName()59 public MockName getMockName() { 60 return mockName; 61 } 62 getExtraInterfaces()63 public Set<Class<?>> getExtraInterfaces() { 64 return extraInterfaces; 65 } 66 getSpiedInstance()67 public Object getSpiedInstance() { 68 return spiedInstance; 69 } 70 name(String name)71 public MockSettings name(String name) { 72 this.name = name; 73 return this; 74 } 75 spiedInstance(Object spiedInstance)76 public MockSettings spiedInstance(Object spiedInstance) { 77 this.spiedInstance = spiedInstance; 78 return this; 79 } 80 defaultAnswer(Answer defaultAnswer)81 public MockSettings defaultAnswer(Answer defaultAnswer) { 82 this.defaultAnswer = defaultAnswer; 83 if (defaultAnswer == null) { 84 throw defaultAnswerDoesNotAcceptNullParameter(); 85 } 86 return this; 87 } 88 getDefaultAnswer()89 public Answer<Object> getDefaultAnswer() { 90 return defaultAnswer; 91 } 92 stubOnly()93 public MockSettingsImpl<T> stubOnly() { 94 this.stubOnly = true; 95 return this; 96 } 97 useConstructor()98 public MockSettings useConstructor() { 99 this.useConstructor = true; 100 return this; 101 } 102 outerInstance(Object outerClassInstance)103 public MockSettings outerInstance(Object outerClassInstance) { 104 this.outerClassInstance = outerClassInstance; 105 return this; 106 } 107 isUsingConstructor()108 public boolean isUsingConstructor() { 109 return useConstructor; 110 } 111 getOuterClassInstance()112 public Object getOuterClassInstance() { 113 return outerClassInstance; 114 } 115 isStubOnly()116 public boolean isStubOnly() { 117 return this.stubOnly; 118 } 119 verboseLogging()120 public MockSettings verboseLogging() { 121 if (!invocationListenersContainsType(VerboseMockInvocationLogger.class)) { 122 invocationListeners(new VerboseMockInvocationLogger()); 123 } 124 return this; 125 } 126 invocationListeners(InvocationListener... listeners)127 public MockSettings invocationListeners(InvocationListener... listeners) { 128 if (listeners == null || listeners.length == 0) { 129 throw invocationListenersRequiresAtLeastOneListener(); 130 } 131 for (InvocationListener listener : listeners) { 132 if (listener == null) { 133 throw invocationListenerDoesNotAcceptNullParameters(); 134 } 135 this.invocationListeners.add(listener); 136 } 137 return this; 138 } 139 invocationListenersContainsType(Class<?> clazz)140 private boolean invocationListenersContainsType(Class<?> clazz) { 141 for (InvocationListener listener : invocationListeners) { 142 if (listener.getClass().equals(clazz)) { 143 return true; 144 } 145 } 146 return false; 147 } 148 getInvocationListeners()149 public List<InvocationListener> getInvocationListeners() { 150 return this.invocationListeners; 151 } 152 hasInvocationListeners()153 public boolean hasInvocationListeners() { 154 return !invocationListeners.isEmpty(); 155 } 156 getTypeToMock()157 public Class<T> getTypeToMock() { 158 return typeToMock; 159 } 160 confirm(Class<T> typeToMock)161 public MockCreationSettings<T> confirm(Class<T> typeToMock) { 162 return validatedSettings(typeToMock, this); 163 } 164 validatedSettings(Class<T> typeToMock, CreationSettings<T> source)165 private static <T> CreationSettings<T> validatedSettings(Class<T> typeToMock, CreationSettings<T> source) { 166 MockCreationValidator validator = new MockCreationValidator(); 167 168 validator.validateType(typeToMock); 169 validator.validateExtraInterfaces(typeToMock, source.getExtraInterfaces()); 170 validator.validateMockedType(typeToMock, source.getSpiedInstance()); 171 172 //TODO SF - add this validation and also add missing coverage 173 // validator.validateDelegatedInstance(classToMock, settings.getDelegatedInstance()); 174 175 validator.validateConstructorUse(source.isUsingConstructor(), source.getSerializableMode()); 176 177 //TODO SF - I don't think we really need CreationSettings type 178 CreationSettings<T> settings = new CreationSettings<T>(source); 179 settings.setMockName(new MockNameImpl(source.getName(), typeToMock)); 180 settings.setTypeToMock(typeToMock); 181 settings.setExtraInterfaces(prepareExtraInterfaces(source)); 182 return settings; 183 } 184 prepareExtraInterfaces(CreationSettings settings)185 private static Set<Class<?>> prepareExtraInterfaces(CreationSettings settings) { 186 Set<Class<?>> interfaces = new HashSet<Class<?>>(settings.getExtraInterfaces()); 187 if(settings.isSerializable()) { 188 interfaces.add(Serializable.class); 189 } 190 return interfaces; 191 } 192 193 } 194 195