• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.configuration.injection;
6 
7 import static org.mockito.internal.util.Checks.checkItemsNotNull;
8 import static org.mockito.internal.util.Checks.checkNotNull;
9 import static org.mockito.internal.util.collections.Sets.newMockSafeHashSet;
10 
11 import java.lang.reflect.Field;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.Set;
15 
16 /**
17  * Internal injection configuration utility.
18  *
19  * <p>Allow the user of this class to configure the way the injection of mocks will happen.
20  */
21 public final class MockInjection {
22 
23     /**
24      * Create a new configuration setup for a field
25      *
26      *
27      * @param field Field needing mock injection
28      * @param ofInstance Instance owning the <code>field</code>
29      * @return New configuration builder
30      */
onField(Field field, Object ofInstance)31     public static OngoingMockInjection onField(Field field, Object ofInstance) {
32         return new OngoingMockInjection(field, ofInstance);
33     }
34 
35     /**
36      * Create a new configuration setup for fields
37      *
38      *
39      * @param fields Fields needing mock injection
40      * @param ofInstance Instance owning the <code>field</code>
41      * @return New configuration builder
42      */
onFields(Set<Field> fields, Object ofInstance)43     public static OngoingMockInjection onFields(Set<Field> fields, Object ofInstance) {
44         return new OngoingMockInjection(fields, ofInstance);
45     }
46 
47     /**
48      * Ongoing configuration of the mock injector.
49      */
50     public static class OngoingMockInjection {
51         private final Set<Field> fields = new HashSet<>();
52         private final Set<Object> mocks = newMockSafeHashSet();
53         private final Object fieldOwner;
54         private final MockInjectionStrategy injectionStrategies = MockInjectionStrategy.nop();
55         private final MockInjectionStrategy postInjectionStrategies = MockInjectionStrategy.nop();
56 
OngoingMockInjection(Field field, Object fieldOwner)57         private OngoingMockInjection(Field field, Object fieldOwner) {
58             this(Collections.singleton(field), fieldOwner);
59         }
60 
OngoingMockInjection(Set<Field> fields, Object fieldOwner)61         private OngoingMockInjection(Set<Field> fields, Object fieldOwner) {
62             this.fieldOwner = checkNotNull(fieldOwner, "fieldOwner");
63             this.fields.addAll(checkItemsNotNull(fields, "fields"));
64         }
65 
withMocks(Set<Object> mocks)66         public OngoingMockInjection withMocks(Set<Object> mocks) {
67             this.mocks.addAll(checkNotNull(mocks, "mocks"));
68             return this;
69         }
70 
tryConstructorInjection()71         public OngoingMockInjection tryConstructorInjection() {
72             injectionStrategies.thenTry(new ConstructorInjection());
73             return this;
74         }
75 
tryPropertyOrFieldInjection()76         public OngoingMockInjection tryPropertyOrFieldInjection() {
77             injectionStrategies.thenTry(new PropertyAndSetterInjection());
78             return this;
79         }
80 
handleSpyAnnotation()81         public OngoingMockInjection handleSpyAnnotation() {
82             postInjectionStrategies.thenTry(new SpyOnInjectedFieldsHandler());
83             return this;
84         }
85 
apply()86         public void apply() {
87             for (Field field : fields) {
88                 injectionStrategies.process(field, fieldOwner, mocks);
89                 postInjectionStrategies.process(field, fieldOwner, mocks);
90             }
91         }
92     }
93 
MockInjection()94     private MockInjection() {}
95 }
96