1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.listeners; 6 7 import org.mockito.MockingDetails; 8 import org.mockito.Mockito; 9 import org.mockito.internal.exceptions.Reporter; 10 import org.mockito.internal.matchers.text.ValuePrinter; 11 import org.mockito.listeners.VerificationStartedEvent; 12 import org.mockito.listeners.VerificationStartedListener; 13 import org.mockito.mock.MockCreationSettings; 14 15 import java.util.List; 16 import java.util.Set; 17 18 public class VerificationStartedNotifier { 19 notifyVerificationStarted(List<VerificationStartedListener> listeners, MockingDetails originalMockingDetails)20 public static Object notifyVerificationStarted(List<VerificationStartedListener> listeners, MockingDetails originalMockingDetails) { 21 if (listeners.isEmpty()) { 22 return originalMockingDetails.getMock(); 23 } 24 VerificationStartedEvent event = new Event(originalMockingDetails); 25 for (VerificationStartedListener listener : listeners) { 26 listener.onVerificationStarted(event); 27 } 28 return event.getMock(); 29 } 30 31 static class Event implements VerificationStartedEvent { 32 private final MockingDetails originalMockingDetails; 33 private Object mock; 34 Event(MockingDetails originalMockingDetails)35 public Event(MockingDetails originalMockingDetails) { 36 this.originalMockingDetails = originalMockingDetails; 37 this.mock = originalMockingDetails.getMock(); 38 } 39 setMock(Object mock)40 public void setMock(Object mock) { 41 if (mock == null) { 42 throw Reporter.methodDoesNotAcceptParameter("VerificationStartedEvent.setMock", "null parameter."); 43 } 44 MockingDetails mockingDetails = Mockito.mockingDetails(mock); 45 if (!mockingDetails.isMock()) { 46 throw Reporter.methodDoesNotAcceptParameter("VerificationStartedEvent.setMock", "parameter which is not a Mockito mock.\n" + 47 " Received parameter: " + ValuePrinter.print(mock) + ".\n "); 48 } 49 MockCreationSettings originalMockSettings = this.originalMockingDetails.getMockCreationSettings(); 50 assertCompatibleTypes(mock, originalMockSettings); 51 this.mock = mock; 52 } 53 getMock()54 public Object getMock() { 55 return mock; 56 } 57 } 58 assertCompatibleTypes(Object mock, MockCreationSettings originalSettings)59 static void assertCompatibleTypes(Object mock, MockCreationSettings originalSettings) { 60 Class originalType = originalSettings.getTypeToMock(); 61 if (!originalType.isInstance(mock)) { 62 throw Reporter.methodDoesNotAcceptParameter("VerificationStartedEvent.setMock", 63 "parameter which is not the same type as the original mock.\n" + 64 " Required type: " + originalType.getName() + "\n" + 65 " Received parameter: " + ValuePrinter.print(mock) + ".\n "); 66 } 67 68 for (Class iface : (Set<Class>) originalSettings.getExtraInterfaces()) { 69 if (!iface.isInstance(mock)) { 70 throw Reporter.methodDoesNotAcceptParameter("VerificationStartedEvent.setMock", 71 "parameter which does not implement all extra interfaces of the original mock.\n" + 72 " Required type: " + originalType.getName() + "\n" + 73 " Required extra interface: " + iface.getName() + "\n" + 74 " Received parameter: " + ValuePrinter.print(mock) + ".\n "); 75 76 } 77 } 78 } 79 } 80