Skip to content

Package: EMFMocking

EMFMocking

nameinstructionbranchcomplexitylinemethod
eMock(Class)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
eMock(Class, EMockSettings)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
eMock(Class, String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
eSetContainer(EObject, EObject)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
initEMocks(Object)
M: 17 C: 17
50%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 5
71%
M: 0 C: 1
100%
initEMocks(Object, Class)
M: 5 C: 30
86%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 5
83%
M: 0 C: 1
100%
inject(Object, Field)
M: 7 C: 45
87%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 12
86%
M: 0 C: 1
100%
withESettings()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.test.common.spi;
15:
16: import static org.junit.Assert.fail;
17: import static org.mockito.Mockito.when;
18:
19: import java.lang.reflect.Field;
20:
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.InternalEObject;
23: import org.mockito.MockitoAnnotations;
24:
25: /**
26: * Utilities for injection of Mockito mocks with {@link EMock @EMock} fields
27: * and for <em>ad hoc</em> configuration of mocks for EMF models.
28: *
29: * @since 1.22
30: */
31: public final class EMFMocking {
32:
33:         /**
34:          * Not instantiable by clients.
35:          */
36:         private EMFMocking() {
37:                 super();
38:         }
39:
40:         /**
41:          * Inject mocks into an {@code object}.
42:          *
43:          * @param object an object to inject
44:          */
45:         public static void initEMocks(Object object) {
46:                 MockitoAnnotations.initMocks(object);
47:
48:•                for (Class<?> classs = object.getClass(); classs != Object.class; classs = classs.getSuperclass()) {
49:                         try {
50:                                 initEMocks(object, classs);
51:                         } catch (final IllegalAccessException e) {
52:                                 e.printStackTrace();
53:                                 fail(String.format("Failed to inject mocks in %s: %s", object, e.getMessage()));
54:                         }
55:                 }
56:         }
57:
58:         public static <T extends EObject> T eMock(Class<T> type) {
59:                 return eMock(type, withESettings());
60:         }
61:
62:         public static <T extends EObject> T eMock(Class<T> type, String name) {
63:                 return eMock(type, withESettings().name(name));
64:         }
65:
66:         public static <T extends EObject> T eMock(Class<T> type, EMockSettings settings) {
67:                 return ((EMockSettingsImpl) settings).make(type);
68:         }
69:
70:         public static EMockSettings withESettings() {
71:                 return new EMockSettingsImpl();
72:         }
73:
74:         public static <T extends EObject> T eSetContainer(T mock, EObject container) {
75:                 when(mock.eContainer()).thenReturn(container);
76:                 when(((InternalEObject) mock).eInternalContainer()).thenReturn((InternalEObject) container);
77:                 return mock;
78:         }
79:
80:         private static void initEMocks(Object testInstance, Class<?> testClass) throws IllegalAccessException {
81:•                for (final Field next : testClass.getDeclaredFields()) {
82:•                        if (next.isAnnotationPresent(EMock.class)) {
83:•                                if (!EObject.class.isAssignableFrom(next.getType())) {
84:                                         throw new IllegalArgumentException("Cannot use @EMock annotation on field of non-EObject type"); //$NON-NLS-1$
85:                                 }
86:
87:                                 inject(testInstance, next);
88:                         }
89:                 }
90:         }
91:
92:         private static void inject(Object owner, Field field) throws IllegalAccessException {
93:                 final EMock emock = field.getAnnotation(EMock.class);
94:                 EMockSettings settings = withESettings();
95:
96:•                if (!emock.name().trim().isEmpty()) {
97:                         settings = settings.name(emock.name().trim());
98:                 } else {
99:                         settings = settings.name(field.getName());
100:                 }
101:                 settings = settings.defaultAnswer(emock.answer().get());
102:
103:                 final EObject mock = eMock(field.getType().asSubclass(EObject.class), settings);
104:
105:                 final boolean wasAccessible = field.isAccessible();
106:                 field.setAccessible(true);
107:
108:                 try {
109:                         field.set(owner, mock);
110:                 } finally {
111:                         field.setAccessible(wasAccessible);
112:                 }
113:         }
114:
115: }