23
23
import static org .junit .jupiter .api .Assertions .assertTrue ;
24
24
import static org .junit .jupiter .api .Assertions .assertEquals ;
25
25
26
+ /**
27
+ * Test methods related to the Update plugins use case.
28
+ * To accomplish this, we use mock installers, plugin trackers and databases.
29
+ */
26
30
public class UpdateInteractorTest {
27
31
private static final InstallResultPresenter installPresenter = new SilentInstallPresenter ();
28
32
@@ -32,6 +36,15 @@ private record MockPackage(
32
36
MockInstaller mockInstaller
33
37
) { }
34
38
39
+ /**
40
+ * Create an interactor with provided paramters.
41
+ *
42
+ * @param plugins A repository of all plugins. Will be passed to database unless emptyDatabase is true.
43
+ * @param installedVersions Maps from plugin id to versions of plugins that are "installed" in the mock tracker.
44
+ * @param installerSucceeds Determines the return value of the mock installer (false for failure).
45
+ * @param emptyDatabase If true, regardless of the value of plugins, the database will contain no plugins.
46
+ * @return A MockPackage object, consisting of an UpdateBoundary, PluginTracker and Installer.
47
+ */
35
48
private static MockPackage interactor (
36
49
List <PluginModel > plugins ,
37
50
Map <Long , String > installedVersions ,
@@ -66,6 +79,11 @@ private static MockPackage interactor(
66
79
);
67
80
}
68
81
82
+ /**
83
+ * Default plugins in a database for utility.
84
+ *
85
+ * @return A list of mock PluginModel objects.
86
+ */
69
87
private static List <PluginModel > defaultPlugins () {
70
88
return List .of (
71
89
PluginMockFactory .model (1 , "TpProtect" , List .of ("0.0.0.0.1" , "0.0.3" , "9.99" )),
@@ -75,10 +93,20 @@ private static List<PluginModel> defaultPlugins() {
75
93
);
76
94
}
77
95
96
+ /**
97
+ * Returns an interactor with an empty database and empty plugin tracker.
98
+ *
99
+ * @return A MockPackage object.
100
+ */
78
101
private static MockPackage emptyInteractor () {
79
102
return interactor (List .of (), Map .of (), true , false );
80
103
}
81
104
105
+ /**
106
+ * Returns an interactor with an empty database but one installed plugin (JedCore).
107
+ *
108
+ * @return A MockPackage object.
109
+ */
82
110
private static MockPackage emptyWithInstalledInteractor () {
83
111
return interactor (
84
112
defaultPlugins (),
@@ -87,6 +115,11 @@ private static MockPackage emptyWithInstalledInteractor() {
87
115
);
88
116
}
89
117
118
+ /**
119
+ * Returns an interactor with all installed plugins marked as up-to-date.
120
+ *
121
+ * @return A MockPackage object.
122
+ */
90
123
private static MockPackage upToDateInteractor () {
91
124
return interactor (defaultPlugins (), Map .of (
92
125
2L , "version-3" ,
@@ -95,6 +128,11 @@ private static MockPackage upToDateInteractor() {
95
128
), true , false );
96
129
}
97
130
131
+ /**
132
+ * Returns an interactor with one installed plugin marked as out-of-date.
133
+ *
134
+ * @return A MockPackage object.
135
+ */
98
136
private static MockPackage oneOldInteractor () {
99
137
return interactor (defaultPlugins (), Map .of (
100
138
2L , "version-2" ,
@@ -103,6 +141,11 @@ private static MockPackage oneOldInteractor() {
103
141
), true , false );
104
142
}
105
143
144
+ /**
145
+ * Returns an interactor with one out-of-date plugin, with a strictly failing mock installer.
146
+ *
147
+ * @return A MockPackage object.
148
+ */
106
149
private static MockPackage failingOneOldInteractor () {
107
150
return interactor (defaultPlugins (), Map .of (
108
151
2L , "version-2" ,
@@ -111,6 +154,11 @@ private static MockPackage failingOneOldInteractor() {
111
154
), false , false );
112
155
}
113
156
157
+ /**
158
+ * Returns an interactor with all installed plugins marked as out-of-date.
159
+ *
160
+ * @return A MockPackage object.
161
+ */
114
162
private static MockPackage outOfDateInteractor () {
115
163
return interactor (defaultPlugins (), Map .of (
116
164
2L , "version-2" ,
@@ -119,18 +167,27 @@ private static MockPackage outOfDateInteractor() {
119
167
), true , false );
120
168
}
121
169
170
+ /**
171
+ * Asserts if the outcome is not UP_TO_DATE.
172
+ */
122
173
private static void assertUpToDate (UpdateOutcome outcome , String version ) {
123
174
assertEquals (outcome .state (), UpdateOutcome .State .UP_TO_DATE );
124
175
assertEquals (outcome .initialVersion (), version );
125
176
assertNull (outcome .destinationVersion ());
126
177
}
127
178
179
+ /**
180
+ * Asserts if the outcome is not UPDATED.
181
+ */
128
182
private static void assertUpdated (UpdateOutcome outcome , String version , String destination ) {
129
183
assertEquals (outcome .state (), UpdateOutcome .State .UPDATED );
130
184
assertEquals (outcome .initialVersion (), version );
131
185
assertEquals (outcome .destinationVersion (), destination );
132
186
}
133
187
188
+ /**
189
+ * Tests that update does nothing on an empty database/plugin tracker.
190
+ */
134
191
@ Test
135
192
void testEmptyDatabase () {
136
193
var mock = emptyInteractor ();
@@ -143,6 +200,9 @@ void testEmptyDatabase() {
143
200
assertTrue (result .outcomes ().isEmpty ());
144
201
}
145
202
203
+ /**
204
+ * Tests that update correctly marks a plugin that does not exist in the database as MISMATCHED.
205
+ */
146
206
@ Test
147
207
void testMismatchedPlugin () {
148
208
var mock = emptyWithInstalledInteractor ();
@@ -155,6 +215,9 @@ void testMismatchedPlugin() {
155
215
assertEquals (result .outcomes ().get ("JedCore" ).state (), UpdateOutcome .State .MISMATCHED );
156
216
}
157
217
218
+ /**
219
+ * Tests that update correctly marks a given plugin that is not in the plugin tracker as NOT_INSTALLED.
220
+ */
158
221
@ Test
159
222
void testMismatchedName () {
160
223
var mock = emptyInteractor ();
@@ -167,6 +230,9 @@ void testMismatchedName() {
167
230
assertEquals (result .outcomes ().get ("JedCore" ).state (), UpdateOutcome .State .NOT_INSTALLED );
168
231
}
169
232
233
+ /**
234
+ * Tests that update correctly marks all up-to-date plugins as UP_TO_DATE.
235
+ */
170
236
@ Test
171
237
void testAllUpToDate () {
172
238
var mock = upToDateInteractor ();
@@ -182,6 +248,9 @@ void testAllUpToDate() {
182
248
assertUpToDate (result .outcomes ().get ("TpProtect" ), "9.99" );
183
249
}
184
250
251
+ /**
252
+ * Tests that update correctly marks a specific given plugin as UP_TO_DATE.
253
+ */
185
254
@ Test
186
255
void testOneUpToDate () {
187
256
var mock = upToDateInteractor ();
@@ -195,6 +264,9 @@ void testOneUpToDate() {
195
264
assertUpToDate (result .outcomes ().get ("Apples+" ), "1.2" );
196
265
}
197
266
267
+ /**
268
+ * Tests that update correctly marks all installed plugins as OUT_OF_DATE.
269
+ */
198
270
@ Test
199
271
void testAllOutOfDate () {
200
272
var mock = outOfDateInteractor ();
@@ -210,6 +282,9 @@ void testAllOutOfDate() {
210
282
assertUpdated (result .outcomes ().get ("TpProtect" ), "0.0.3" , "9.99" );
211
283
}
212
284
285
+ /**
286
+ * Tests that update correctly marks a given plugin as OUT_OF_DATE.
287
+ */
213
288
@ Test
214
289
void testOneOutOfDate () {
215
290
var mock = outOfDateInteractor ();
@@ -223,6 +298,9 @@ void testOneOutOfDate() {
223
298
assertUpdated (result .outcomes ().get ("Apples+" ), "1.0" , "1.2" );
224
299
}
225
300
301
+ /**
302
+ * Tests that update correctly marks a mixture of OUT_OF_DATE and UP_TO_DATE plugins.
303
+ */
226
304
@ Test
227
305
void testAllMixed () {
228
306
var mock = oneOldInteractor ();
@@ -238,6 +316,9 @@ void testAllMixed() {
238
316
assertUpToDate (result .outcomes ().get ("TpProtect" ), "9.99" );
239
317
}
240
318
319
+ /**
320
+ * Tests that update correctly conveys a NETWORK_ERROR on an installation failure.
321
+ */
241
322
@ Test
242
323
void testFailingInstaller () {
243
324
var mock = failingOneOldInteractor ();
0 commit comments