19
19
import android .content .Context ;
20
20
21
21
import com .optimizely .ab .android .datafile_handler .DatafileHandler ;
22
+ import com .optimizely .ab .android .event_handler .DefaultEventHandler ;
22
23
import com .optimizely .ab .android .shared .DatafileConfig ;
23
24
import com .optimizely .ab .android .user_profile .DefaultUserProfileService ;
24
25
import com .optimizely .ab .bucketing .UserProfileService ;
28
29
import com .optimizely .ab .event .EventProcessor ;
29
30
import com .optimizely .ab .notification .NotificationCenter ;
30
31
32
+ import org .junit .Before ;
31
33
import org .junit .Test ;
32
34
import org .junit .runner .RunWith ;
33
35
import org .mockito .runners .MockitoJUnitRunner ;
36
+ import org .powermock .api .mockito .PowerMockito ;
37
+ import org .powermock .core .classloader .annotations .PowerMockIgnore ;
34
38
import org .powermock .core .classloader .annotations .PrepareForTest ;
35
39
import org .powermock .modules .junit4 .PowerMockRunner ;
36
40
import org .slf4j .Logger ;
54
58
import static org .mockito .Mockito .never ;
55
59
import static org .mockito .Mockito .verify ;
56
60
import static org .mockito .Mockito .when ;
61
+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
57
62
import static org .powermock .api .mockito .PowerMockito .verifyNew ;
58
63
import static org .powermock .api .mockito .PowerMockito .whenNew ;
59
64
60
65
61
66
@ RunWith (PowerMockRunner .class )
62
- @ PrepareForTest ({OptimizelyManager .class , BatchEventProcessor .class })
67
+ @ PowerMockIgnore ("jdk.internal.reflect.*" )
68
+ @ PrepareForTest ({OptimizelyManager .class , BatchEventProcessor .class , DefaultEventHandler .class })
63
69
public class OptimizelyManagerIntervalTest {
64
70
65
71
private Logger logger ;
72
+ private Context mockContext ;
73
+ private DefaultEventHandler mockEventHandler ;
66
74
67
- // DatafileDownloadInterval
75
+ @ Before
76
+ public void setup () throws Exception {
77
+ mockContext = mock (Context .class );
78
+ when (mockContext .getApplicationContext ()).thenReturn (mockContext );
68
79
69
- @ Test
70
- public void testBuildWithDatafileDownloadInterval () throws Exception {
71
80
whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
81
+ whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
72
82
73
- Context appContext = mock (Context .class );
74
- when (appContext .getApplicationContext ()).thenReturn (appContext );
83
+ mockEventHandler = mock (DefaultEventHandler .class );
84
+ mockStatic (DefaultEventHandler .class );
85
+ when (DefaultEventHandler .getInstance (any ())).thenReturn (mockEventHandler );
86
+ }
87
+
88
+ // DatafileDownloadInterval
75
89
90
+ @ Test
91
+ public void testBuildWithDatafileDownloadInterval () throws Exception {
76
92
long goodNumber = 27 ;
77
93
OptimizelyManager manager = OptimizelyManager .builder ("1" )
78
94
.withLogger (logger )
79
95
.withDatafileDownloadInterval (goodNumber , TimeUnit .MINUTES )
80
- .build (appContext );
96
+ .build (mockContext );
81
97
82
98
verifyNew (OptimizelyManager .class ).withArguments (anyString (),
83
99
anyString (),
@@ -96,16 +112,11 @@ public void testBuildWithDatafileDownloadInterval() throws Exception {
96
112
97
113
@ Test
98
114
public void testBuildWithDatafileDownloadIntervalDeprecated () throws Exception {
99
- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
100
-
101
- Context appContext = mock (Context .class );
102
- when (appContext .getApplicationContext ()).thenReturn (appContext );
103
-
104
115
long goodNumber = 1234L ;
105
116
OptimizelyManager manager = OptimizelyManager .builder ("1" )
106
117
.withLogger (logger )
107
118
.withDatafileDownloadInterval (goodNumber ) // deprecated
108
- .build (appContext );
119
+ .build (mockContext );
109
120
110
121
verifyNew (OptimizelyManager .class ).withArguments (anyString (),
111
122
anyString (),
@@ -124,17 +135,11 @@ public void testBuildWithDatafileDownloadIntervalDeprecated() throws Exception {
124
135
125
136
@ Test
126
137
public void testBuildWithEventDispatchInterval () throws Exception {
127
- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
128
- whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
129
-
130
- Context appContext = mock (Context .class );
131
- when (appContext .getApplicationContext ()).thenReturn (appContext );
132
-
133
138
long goodNumber = 100L ;
134
139
OptimizelyManager manager = OptimizelyManager .builder ("1" )
135
140
.withLogger (logger )
136
141
.withEventDispatchInterval (goodNumber , TimeUnit .SECONDS )
137
- .build (appContext );
142
+ .build (mockContext );
138
143
139
144
verifyNew (BatchEventProcessor .class ).withArguments (any (BlockingQueue .class ),
140
145
any (EventHandler .class ),
@@ -145,14 +150,16 @@ public void testBuildWithEventDispatchInterval() throws Exception {
145
150
any (NotificationCenter .class ),
146
151
any (Object .class ));
147
152
153
+ verify (mockEventHandler ).setDispatchInterval (-1L ); // default
154
+
148
155
verifyNew (OptimizelyManager .class ).withArguments (anyString (),
149
156
anyString (),
150
157
any (DatafileConfig .class ),
151
158
any (Logger .class ),
152
159
anyLong (),
153
160
any (DatafileHandler .class ),
154
161
any (ErrorHandler .class ),
155
- eq (-1L ), // milliseconds
162
+ eq (-1L ), // default
156
163
any (EventHandler .class ),
157
164
any (EventProcessor .class ),
158
165
any (UserProfileService .class ),
@@ -162,19 +169,14 @@ public void testBuildWithEventDispatchInterval() throws Exception {
162
169
163
170
@ Test
164
171
public void testBuildWithEventDispatchRetryInterval () throws Exception {
165
- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
166
- whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
167
-
168
- Context appContext = mock (Context .class );
169
- when (appContext .getApplicationContext ()).thenReturn (appContext );
170
-
171
172
long goodNumber = 100L ;
172
- long defaultEventFlushInterval = 30L ;
173
+ TimeUnit timeUnit = TimeUnit .MINUTES ;
174
+ long defaultEventFlushInterval = 30L ; // seconds
173
175
174
176
OptimizelyManager manager = OptimizelyManager .builder ("1" )
175
177
.withLogger (logger )
176
- .withEventDispatchRetryInterval (goodNumber , TimeUnit . MINUTES )
177
- .build (appContext );
178
+ .withEventDispatchRetryInterval (goodNumber , timeUnit )
179
+ .build (mockContext );
178
180
179
181
verifyNew (BatchEventProcessor .class ).withArguments (any (BlockingQueue .class ),
180
182
any (EventHandler .class ),
@@ -185,6 +187,8 @@ public void testBuildWithEventDispatchRetryInterval() throws Exception {
185
187
any (NotificationCenter .class ),
186
188
any (Object .class ));
187
189
190
+ verify (mockEventHandler ).setDispatchInterval (timeUnit .toMillis (goodNumber )); // milli-seconds
191
+
188
192
verifyNew (OptimizelyManager .class ).withArguments (anyString (),
189
193
anyString (),
190
194
any (DatafileConfig .class ),
@@ -202,17 +206,11 @@ public void testBuildWithEventDispatchRetryInterval() throws Exception {
202
206
203
207
@ Test
204
208
public void testBuildWithEventDispatchIntervalDeprecated () throws Exception {
205
- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
206
- whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
207
-
208
- Context appContext = mock (Context .class );
209
- when (appContext .getApplicationContext ()).thenReturn (appContext );
210
-
211
209
long goodNumber = 1234L ;
212
210
OptimizelyManager manager = OptimizelyManager .builder ("1" )
213
211
.withLogger (logger )
214
212
.withEventDispatchInterval (goodNumber ) // deprecated
215
- .build (appContext );
213
+ .build (mockContext );
216
214
217
215
verifyNew (BatchEventProcessor .class ).withArguments (any (BlockingQueue .class ),
218
216
any (EventHandler .class ),
@@ -223,14 +221,16 @@ public void testBuildWithEventDispatchIntervalDeprecated() throws Exception {
223
221
any (NotificationCenter .class ),
224
222
any (Object .class ));
225
223
224
+ verify (mockEventHandler ).setDispatchInterval (-1L ); // deprecated api not change default retryInterval
225
+
226
226
verifyNew (OptimizelyManager .class ).withArguments (anyString (),
227
227
anyString (),
228
228
any (DatafileConfig .class ),
229
229
any (Logger .class ),
230
230
anyLong (),
231
231
any (DatafileHandler .class ),
232
232
any (ErrorHandler .class ),
233
- eq (goodNumber ), // milliseconds
233
+ eq (- 1L ), // deprecated api not change default retryInterval
234
234
any (EventHandler .class ),
235
235
any (EventProcessor .class ),
236
236
any (UserProfileService .class ),
0 commit comments