Skip to content

Commit 49dcea4

Browse files
committed
Fix update tests related checkstyle
1 parent 26b6bf1 commit 49dcea4

File tree

6 files changed

+126
-10
lines changed

6 files changed

+126
-10
lines changed

src/test/java/org/hydev/mcpm/client/database/MockInstaller.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,32 @@
1313
import java.util.List;
1414
import java.util.Set;
1515

16+
/**
17+
* Mock installer utility that keeps track of plugins that were requested to be installed.
18+
*/
1619
public class MockInstaller implements InstallBoundary {
1720
private final List<PluginModel> plugins;
1821
private final PluginTracker tracker;
1922
private final boolean defaultResult;
2023
private final Set<String> requested = new HashSet<>();
2124

25+
/**
26+
* Creates a new MockInstaller object that always succeeds (when it is not already installed).
27+
*
28+
* @param plugins A list of plugins to lookup plugin related information about (to pass to PluginTracker).
29+
* @param tracker A plugin tracker to query whether a plugin is installed.
30+
*/
2231
public MockInstaller(List<PluginModel> plugins, PluginTracker tracker) {
2332
this(plugins, tracker, true);
2433
}
2534

35+
/**
36+
* Creates a new MockInstaller object.
37+
*
38+
* @param plugins A list of plugins to lookup plugin related information about (to pass to PluginTracker).
39+
* @param tracker A plugin tracker to query whether a plugin is installed.
40+
* @param defaultResult Determines what the installer should return when installPlugin is invoked.
41+
*/
2642
public MockInstaller(List<PluginModel> plugins, PluginTracker tracker, boolean defaultResult) {
2743
this.plugins = plugins;
2844
this.tracker = tracker;
@@ -53,6 +69,11 @@ public boolean installPlugin(InstallInput installInput, InstallResultPresenter r
5369
return defaultResult;
5470
}
5571

72+
/**
73+
* Returns a list of all plugin names that were passed to installPlugin (and succeeded).
74+
*
75+
* @return A list of plugin names.
76+
*/
5677
Set<String> getRequested() {
5778
return new HashSet<>(requested);
5879
}

src/test/java/org/hydev/mcpm/client/database/MockPluginTracker.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import java.util.List;
99
import java.util.Map;
1010

11+
/**
12+
* Mock implementation of the PluginTracker interface.
13+
*
14+
* @param entries A list of all entries in the lockfile.
15+
* @param installed A list of all "Installed" PluginYml files (to be returned in listInstalled).
16+
*/
1117
public record MockPluginTracker(
1218
Map<String, Boolean> entries,
1319
List<PluginYml> installed

src/test/java/org/hydev/mcpm/client/database/PluginMockFactory.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,18 @@ public static PluginModel model(long id, String name, String description, List<S
175175
* @return List of plugins for testing.
176176
*/
177177
public static @Unmodifiable List<PluginModel> generateTestPlugins() {
178-
String[] descriptions = {"WorldGuard lets you and players guard areas " +
179-
"of land against griefers and undesirables as well " +
180-
"as tweak and disable various gameplay features of Minecraft.",
181-
"Multiverse was created at the dawn of Bukkit multiworld support. " +
182-
"It has since then grown into a complete world management " +
183-
"solution including special treatment of your nether worlds with " +
184-
"Multiverse NetherPortals.",
185-
"Create futuristic holograms to display text and items to players!"
186-
};
178+
var worldGuardDescription = "WorldGuard lets you and players guard areas " +
179+
"of land against griefers and undesirables as well " +
180+
"as tweak and disable various gameplay features of Minecraft.";
181+
182+
var multiverseDescription = "Multiverse was created at the dawn of Bukkit multiworld support. " +
183+
"It has since then grown into a complete world management " +
184+
"solution including special treatment of your nether worlds with " +
185+
"Multiverse NetherPortals.";
186+
187+
var hologramDescription = "Create futuristic holograms to display text and items to players!";
188+
189+
String[] descriptions = { worldGuardDescription, multiverseDescription, hologramDescription };
187190

188191
return List.of(
189192
PluginMockFactory.model(1),
@@ -193,7 +196,7 @@ public static PluginModel model(long id, String name, String description, List<S
193196
createCommand(descriptions[0], List.of("/god", "/ungod"))),
194197
PluginMockFactory.model(3, "Multiverse-Core", descriptions[1],
195198
List.of("1.19.2", "1.19.1"),
196-
createCommand(descriptions[1],List.of("/mvlist", "/mvl"))),
199+
createCommand(descriptions[1], List.of("/mvlist", "/mvl"))),
197200
PluginMockFactory.model(4, "Holographic Displays", descriptions[2],
198201
List.of("1.19.2", "1.19.1", "1.19"),
199202
createCommand(descriptions[2], List.of("/hd", "/ungod")))

src/test/java/org/hydev/mcpm/client/database/SilentInstallPresenter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.hydev.mcpm.client.commands.presenters.InstallResultPresenter;
44
import org.hydev.mcpm.client.installer.InstallResult;
55

6+
/**
7+
* Mock Install Presenter that does nothing on invocation.
8+
*/
69
public class SilentInstallPresenter implements InstallResultPresenter {
710
@Override
811
public void displayResult(InstallResult installResult, String name) {

src/test/java/org/hydev/mcpm/client/database/UpdateInteractorTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525

26+
/**
27+
* Test methods related to the Update plugins use case.
28+
* To accomplish this, we use mock installers, plugin trackers and databases.
29+
*/
2630
public class UpdateInteractorTest {
2731
private static final InstallResultPresenter installPresenter = new SilentInstallPresenter();
2832

@@ -32,6 +36,15 @@ private record MockPackage(
3236
MockInstaller mockInstaller
3337
) { }
3438

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+
*/
3548
private static MockPackage interactor(
3649
List<PluginModel> plugins,
3750
Map<Long, String> installedVersions,
@@ -66,6 +79,11 @@ private static MockPackage interactor(
6679
);
6780
}
6881

82+
/**
83+
* Default plugins in a database for utility.
84+
*
85+
* @return A list of mock PluginModel objects.
86+
*/
6987
private static List<PluginModel> defaultPlugins() {
7088
return List.of(
7189
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() {
7593
);
7694
}
7795

96+
/**
97+
* Returns an interactor with an empty database and empty plugin tracker.
98+
*
99+
* @return A MockPackage object.
100+
*/
78101
private static MockPackage emptyInteractor() {
79102
return interactor(List.of(), Map.of(), true, false);
80103
}
81104

105+
/**
106+
* Returns an interactor with an empty database but one installed plugin (JedCore).
107+
*
108+
* @return A MockPackage object.
109+
*/
82110
private static MockPackage emptyWithInstalledInteractor() {
83111
return interactor(
84112
defaultPlugins(),
@@ -87,6 +115,11 @@ private static MockPackage emptyWithInstalledInteractor() {
87115
);
88116
}
89117

118+
/**
119+
* Returns an interactor with all installed plugins marked as up-to-date.
120+
*
121+
* @return A MockPackage object.
122+
*/
90123
private static MockPackage upToDateInteractor() {
91124
return interactor(defaultPlugins(), Map.of(
92125
2L, "version-3",
@@ -95,6 +128,11 @@ private static MockPackage upToDateInteractor() {
95128
), true, false);
96129
}
97130

131+
/**
132+
* Returns an interactor with one installed plugin marked as out-of-date.
133+
*
134+
* @return A MockPackage object.
135+
*/
98136
private static MockPackage oneOldInteractor() {
99137
return interactor(defaultPlugins(), Map.of(
100138
2L, "version-2",
@@ -103,6 +141,11 @@ private static MockPackage oneOldInteractor() {
103141
), true, false);
104142
}
105143

144+
/**
145+
* Returns an interactor with one out-of-date plugin, with a strictly failing mock installer.
146+
*
147+
* @return A MockPackage object.
148+
*/
106149
private static MockPackage failingOneOldInteractor() {
107150
return interactor(defaultPlugins(), Map.of(
108151
2L, "version-2",
@@ -111,6 +154,11 @@ private static MockPackage failingOneOldInteractor() {
111154
), false, false);
112155
}
113156

157+
/**
158+
* Returns an interactor with all installed plugins marked as out-of-date.
159+
*
160+
* @return A MockPackage object.
161+
*/
114162
private static MockPackage outOfDateInteractor() {
115163
return interactor(defaultPlugins(), Map.of(
116164
2L, "version-2",
@@ -119,18 +167,27 @@ private static MockPackage outOfDateInteractor() {
119167
), true, false);
120168
}
121169

170+
/**
171+
* Asserts if the outcome is not UP_TO_DATE.
172+
*/
122173
private static void assertUpToDate(UpdateOutcome outcome, String version) {
123174
assertEquals(outcome.state(), UpdateOutcome.State.UP_TO_DATE);
124175
assertEquals(outcome.initialVersion(), version);
125176
assertNull(outcome.destinationVersion());
126177
}
127178

179+
/**
180+
* Asserts if the outcome is not UPDATED.
181+
*/
128182
private static void assertUpdated(UpdateOutcome outcome, String version, String destination) {
129183
assertEquals(outcome.state(), UpdateOutcome.State.UPDATED);
130184
assertEquals(outcome.initialVersion(), version);
131185
assertEquals(outcome.destinationVersion(), destination);
132186
}
133187

188+
/**
189+
* Tests that update does nothing on an empty database/plugin tracker.
190+
*/
134191
@Test
135192
void testEmptyDatabase() {
136193
var mock = emptyInteractor();
@@ -143,6 +200,9 @@ void testEmptyDatabase() {
143200
assertTrue(result.outcomes().isEmpty());
144201
}
145202

203+
/**
204+
* Tests that update correctly marks a plugin that does not exist in the database as MISMATCHED.
205+
*/
146206
@Test
147207
void testMismatchedPlugin() {
148208
var mock = emptyWithInstalledInteractor();
@@ -155,6 +215,9 @@ void testMismatchedPlugin() {
155215
assertEquals(result.outcomes().get("JedCore").state(), UpdateOutcome.State.MISMATCHED);
156216
}
157217

218+
/**
219+
* Tests that update correctly marks a given plugin that is not in the plugin tracker as NOT_INSTALLED.
220+
*/
158221
@Test
159222
void testMismatchedName() {
160223
var mock = emptyInteractor();
@@ -167,6 +230,9 @@ void testMismatchedName() {
167230
assertEquals(result.outcomes().get("JedCore").state(), UpdateOutcome.State.NOT_INSTALLED);
168231
}
169232

233+
/**
234+
* Tests that update correctly marks all up-to-date plugins as UP_TO_DATE.
235+
*/
170236
@Test
171237
void testAllUpToDate() {
172238
var mock = upToDateInteractor();
@@ -182,6 +248,9 @@ void testAllUpToDate() {
182248
assertUpToDate(result.outcomes().get("TpProtect"), "9.99");
183249
}
184250

251+
/**
252+
* Tests that update correctly marks a specific given plugin as UP_TO_DATE.
253+
*/
185254
@Test
186255
void testOneUpToDate() {
187256
var mock = upToDateInteractor();
@@ -195,6 +264,9 @@ void testOneUpToDate() {
195264
assertUpToDate(result.outcomes().get("Apples+"), "1.2");
196265
}
197266

267+
/**
268+
* Tests that update correctly marks all installed plugins as OUT_OF_DATE.
269+
*/
198270
@Test
199271
void testAllOutOfDate() {
200272
var mock = outOfDateInteractor();
@@ -210,6 +282,9 @@ void testAllOutOfDate() {
210282
assertUpdated(result.outcomes().get("TpProtect"), "0.0.3", "9.99");
211283
}
212284

285+
/**
286+
* Tests that update correctly marks a given plugin as OUT_OF_DATE.
287+
*/
213288
@Test
214289
void testOneOutOfDate() {
215290
var mock = outOfDateInteractor();
@@ -223,6 +298,9 @@ void testOneOutOfDate() {
223298
assertUpdated(result.outcomes().get("Apples+"), "1.0", "1.2");
224299
}
225300

301+
/**
302+
* Tests that update correctly marks a mixture of OUT_OF_DATE and UP_TO_DATE plugins.
303+
*/
226304
@Test
227305
void testAllMixed() {
228306
var mock = oneOldInteractor();
@@ -238,6 +316,9 @@ void testAllMixed() {
238316
assertUpToDate(result.outcomes().get("TpProtect"), "9.99");
239317
}
240318

319+
/**
320+
* Tests that update correctly conveys a NETWORK_ERROR on an installation failure.
321+
*/
241322
@Test
242323
void testFailingInstaller() {
243324
var mock = failingOneOldInteractor();

src/test/java/org/hydev/mcpm/utils/GeneralUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.hydev.mcpm.utils;
22

3+
import org.junit.jupiter.api.Tag;
34
import org.junit.jupiter.api.Test;
45

56
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -19,6 +20,7 @@ void makeUrl()
1920
}
2021

2122
@Test
23+
@Tag("IntegrationTest")
2224
void safeSleep()
2325
{
2426
long time = System.currentTimeMillis();

0 commit comments

Comments
 (0)