Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit b5ffda6

Browse files
committed
More tests for module loading.
1 parent a9465ac commit b5ffda6

File tree

7 files changed

+191
-2
lines changed

7 files changed

+191
-2
lines changed

src/main/java/uk/co/drnaylor/quickstart/ModuleContainer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ private void discoverModules() throws IOException, QuickStartModuleDiscoveryExce
145145

146146
// Load what we have in config into our discovered modules.
147147
try {
148-
config.getConfigAdapter().getNode().forEach((k, v) -> discoveredModules.get(k).setStatus(v));
148+
config.getConfigAdapter().getNode().forEach((k, v) -> {
149+
try {
150+
discoveredModules.get(k).setStatus(v);
151+
} catch (IllegalStateException ex) {
152+
Logger.getLogger("QuickStart").warning("A mandatory module can't have its status changed by config. Falling back to FORCELOAD for " + k);
153+
}
154+
});
149155
} catch (ObjectMappingException e) {
150156
Logger.getLogger("QuickStart").warning("Could not load modules config, falling back to defaults.");
151157
e.printStackTrace();
@@ -382,7 +388,7 @@ public ModuleContainer build() throws QuickStartModuleDiscoveryException {
382388
}
383389
}
384390

385-
enum ModuleStatusTristate {
391+
public enum ModuleStatusTristate {
386392
ENABLE(k -> k.getValue().getStatus() != LoadingStatus.DISABLED),
387393
DISABLE(k -> k.getValue().getStatus() == LoadingStatus.DISABLED),
388394
ALL(k -> true);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart.tests.modules.modulestates;
6+
7+
import uk.co.drnaylor.quickstart.Module;
8+
import uk.co.drnaylor.quickstart.annotations.ModuleData;
9+
import uk.co.drnaylor.quickstart.enums.LoadingStatus;
10+
11+
@ModuleData(id = "dis", name = "disabled", status = LoadingStatus.DISABLED)
12+
public class DisabledModule implements Module {
13+
@Override
14+
public void onEnable() {
15+
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart.tests.modules.modulestates;
6+
7+
import uk.co.drnaylor.quickstart.Module;
8+
import uk.co.drnaylor.quickstart.annotations.ModuleData;
9+
10+
@ModuleData(id = "en", name = "enabled")
11+
public class EnabledModule implements Module {
12+
13+
@Override
14+
public void onEnable() {
15+
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart.tests.modules.modulestates;
6+
7+
import uk.co.drnaylor.quickstart.Module;
8+
import uk.co.drnaylor.quickstart.annotations.ModuleData;
9+
import uk.co.drnaylor.quickstart.enums.LoadingStatus;
10+
11+
@ModuleData(id = "fl", name = "forceload", status = LoadingStatus.FORCELOAD)
12+
public class ForceLoadedModule implements Module {
13+
@Override
14+
public void onEnable() {
15+
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart.tests.modules.modulestates;
6+
7+
import uk.co.drnaylor.quickstart.Module;
8+
import uk.co.drnaylor.quickstart.annotations.ModuleData;
9+
10+
@ModuleData(id = "man", name = "mandat", isRequired = true)
11+
public class MandatoryModule implements Module {
12+
@Override
13+
public void onEnable() {
14+
15+
}
16+
}

src/test/java/uk/co/drnaylor/quickstart/tests/tests/ModuleContainerConstructionTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package uk.co.drnaylor.quickstart.tests.tests;
66

7+
import org.junit.Assert;
78
import org.junit.Rule;
89
import org.junit.Test;
910
import org.junit.rules.ExpectedException;
@@ -15,6 +16,8 @@
1516
import uk.co.drnaylor.quickstart.tests.config.adapters.SimpleWithDefault;
1617
import uk.co.drnaylor.quickstart.tests.scaffolding.FakeLoaderTests;
1718

19+
import java.util.Set;
20+
1821
import static org.junit.Assert.assertNotNull;
1922

2023
public class ModuleContainerConstructionTests extends FakeLoaderTests {
@@ -65,4 +68,16 @@ public void testThatConfigAdaptersGetRegisteredOnConstruction() throws Exception
6568
assertNotNull(s);
6669
assertNotNull(s2);
6770
}
71+
72+
@Test
73+
public void testThatModulesAreLoadedAsExpected() throws Exception {
74+
// When we load these modules...
75+
ModuleContainer mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.modulestates");
76+
mc.loadModules(true);
77+
78+
Set<String> ss = mc.getModules(ModuleContainer.ModuleStatusTristate.ENABLE);
79+
Assert.assertTrue(ss.contains("man"));
80+
Assert.assertTrue(ss.contains("fl"));
81+
Assert.assertFalse(ss.contains("dis"));
82+
}
6883
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart.tests.tests;
6+
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import uk.co.drnaylor.quickstart.ModuleContainer;
11+
import uk.co.drnaylor.quickstart.config.ModulesConfigAdapter;
12+
import uk.co.drnaylor.quickstart.exceptions.UndisableableModuleException;
13+
import uk.co.drnaylor.quickstart.tests.scaffolding.FakeLoaderTests;
14+
15+
public class ModuleManagementTests extends FakeLoaderTests {
16+
17+
private ModuleContainer mc;
18+
19+
@Before
20+
@Override
21+
public void beforeTests() throws Exception {
22+
super.beforeTests();
23+
24+
mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.modulestates");
25+
}
26+
27+
@Test
28+
public void checkEnabledModuleCanBeDisabled() throws Exception {
29+
mc.disableModule("en");
30+
mc.loadModules(true);
31+
32+
checkEnabled("fl", "man");
33+
checkDisabled("en", "dis");
34+
}
35+
36+
@Test
37+
public void checkDisabledModuleCanPointlesslyBeDisabled() throws Exception {
38+
mc.disableModule("dis");
39+
mc.loadModules(true);
40+
41+
checkEnabled("en", "fl", "man");
42+
checkDisabled("dis");
43+
}
44+
45+
@Test(expected = UndisableableModuleException.class)
46+
public void checkForceLoadedModuleCannotBeDisabled() throws Exception {
47+
mc.disableModule("fl");
48+
}
49+
50+
@Test(expected = UndisableableModuleException.class)
51+
public void checkMandatoryModulesCannotBeDisabled() throws Exception {
52+
mc.disableModule("man");
53+
}
54+
55+
@Test
56+
public void checkForceLoadedByDefaultButActuallyJustEnabledCanActuallyBeDisabled() throws Exception {
57+
// Force enabled.
58+
n.getNode(ModulesConfigAdapter.modulesKey, "fl").setValue("enabled");
59+
60+
// Rebuild.
61+
mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.modulestates");
62+
63+
mc.disableModule("fl");
64+
}
65+
66+
@Test
67+
public void checkMandatoryModuleCannotBeDisabledEvenIfSomeoneTriesCreatingAConfigEntryForItThatSaysDisabled() throws Exception {
68+
// Force enabled.
69+
n.getNode(ModulesConfigAdapter.modulesKey, "man").setValue("disabled");
70+
71+
// Rebuild.
72+
mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.modulestates");
73+
74+
mc.loadModules(true);
75+
76+
checkEnabled("en", "fl", "man");
77+
checkDisabled("dis");
78+
}
79+
80+
@Test(expected = UndisableableModuleException.class)
81+
public void checkMandatoryModuleCannotBeDisabledEvenIfSomeoneTriesCreatingAConfigEntryForItThatSaysEnabledAndThenTriesToDisableItAnyway() throws Exception {
82+
// Force enabled.
83+
n.getNode(ModulesConfigAdapter.modulesKey, "man").setValue("enabled");
84+
85+
// Rebuild.
86+
mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.modulestates");
87+
mc.disableModule("man");
88+
}
89+
90+
private void checkEnabled(String... ids) throws Exception {
91+
for (String c : ids) {
92+
Assert.assertTrue(mc.getModules(ModuleContainer.ModuleStatusTristate.ENABLE).contains(c));
93+
}
94+
}
95+
96+
private void checkDisabled(String... ids) throws Exception {
97+
for (String c : ids) {
98+
Assert.assertFalse(mc.getModules(ModuleContainer.ModuleStatusTristate.ENABLE).contains(c));
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)