1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dx.mockito.inline; 18 19 import android.util.Log; 20 21 import org.mockito.invocation.MockHandler; 22 import org.mockito.mock.MockCreationSettings; 23 import org.mockito.plugins.MockMaker; 24 25 import java.lang.reflect.InvocationTargetException; 26 import java.util.ArrayList; 27 28 /** 29 * Multiplexes multiple mock makers 30 */ 31 public final class MockMakerMultiplexer implements MockMaker { 32 private static final String LOG_TAG = MockMakerMultiplexer.class.getSimpleName(); 33 private final static MockMaker[] MOCK_MAKERS; 34 35 static { 36 String[] potentialMockMakers = new String[] { 37 "com.android.dx.mockito.inline.InlineStaticMockMaker", 38 InlineDexmakerMockMaker.class.getName() 39 }; 40 41 ArrayList<MockMaker> mockMakers = new ArrayList<>(); 42 for (String potentialMockMaker : potentialMockMakers) { 43 try { 44 Class<? extends MockMaker> mockMakerClass = (Class<? extends MockMaker>) 45 Class.forName(potentialMockMaker); 46 mockMakers.add(mockMakerClass.getDeclaredConstructor().newInstance()); 47 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException 48 | NoSuchMethodException | InvocationTargetException e) { 49 if (potentialMockMaker.equals(InlineDexmakerMockMaker.class.getName())) { 50 Log.e(LOG_TAG, "Could not init mockmaker " + potentialMockMaker, e); 51 } else { 52 // Additional mock makers might not be loaded 53 Log.e(LOG_TAG, "Could not init mockmaker " + potentialMockMaker); 54 } 55 } 56 } 57 58 MOCK_MAKERS = mockMakers.toArray(new MockMaker[]{}); 59 } 60 61 @Override createMock(MockCreationSettings<T> settings, MockHandler handler)62 public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) { 63 for (MockMaker mockMaker : MOCK_MAKERS) { 64 T mock = mockMaker.createMock(settings, handler); 65 66 if (mock != null) { 67 return mock; 68 } 69 } 70 71 return null; 72 } 73 74 @Override getHandler(Object mock)75 public MockHandler getHandler(Object mock) { 76 for (MockMaker mockMaker : MOCK_MAKERS) { 77 MockHandler handler = mockMaker.getHandler(mock); 78 79 if (handler != null) { 80 return handler; 81 } 82 } 83 84 return null; 85 } 86 87 @Override resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings)88 public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { 89 for (MockMaker mockMaker : MOCK_MAKERS) { 90 mockMaker.resetMock(mock, newHandler, settings); 91 } 92 } 93 94 @Override isTypeMockable(Class<?> type)95 public TypeMockability isTypeMockable(Class<?> type) { 96 for (MockMaker mockMaker : MOCK_MAKERS) { 97 TypeMockability mockability = mockMaker.isTypeMockable(type); 98 99 if (mockability != null) { 100 return mockability; 101 } 102 } 103 104 return null; 105 } 106 } 107