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

Commit d0abe2c

Browse files
committed
Update Config Adapters to run as expected, added tests.
1 parent 03513f3 commit d0abe2c

File tree

5 files changed

+79
-23
lines changed

5 files changed

+79
-23
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,11 @@ private void discoverModules() throws IOException {
142142

143143
// Attaches config adapter and loads in the defaults.
144144
config.attachModulesConfig(m);
145+
config.saveAdapterDefaults();
145146

146147
// Load what we have in config into our discovered modules.
147148
try {
148-
config.getConfigAdapter().getNode().forEach((k, v) -> {
149-
discoveredModules.get(k).setStatus(v);
150-
});
149+
config.getConfigAdapter().getNode().forEach((k, v) -> discoveredModules.get(k).setStatus(v));
151150
} catch (ObjectMappingException e) {
152151
Logger.getLogger("QuickStart").warning("Could not load modules config, falling back to defaults.");
153152
e.printStackTrace();
@@ -293,6 +292,12 @@ public void loadModules(boolean failOnOneError) throws QuickStartModuleLoaderExc
293292
throw new QuickStartModuleLoaderException.Enabling(null, "No modules were enabled.", null);
294293
}
295294

295+
try {
296+
config.saveAdapterDefaults();
297+
} catch (IOException e) {
298+
e.printStackTrace();
299+
}
300+
296301
currentPhase = ConstructionPhase.ENABLED;
297302
}
298303

src/main/java/uk/co/drnaylor/quickstart/config/AbstractAdaptableConfig.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,13 @@ public final void attachConfigAdapter(String module, AbstractConfigAdapter<?> co
9191
throw new IllegalArgumentException();
9292
}
9393

94-
configAdapter.attachConfig(module.toLowerCase(), this, () -> node.getNode(module.toLowerCase()), n -> node.setValue(n), nodeCreator);
94+
configAdapter.attachConfig(
95+
module.toLowerCase(),
96+
this,
97+
() -> nodeCreator.get().setValue(node.getNode(module.toLowerCase())),
98+
n -> node.getNode(module.toLowerCase()).setValue(n),
99+
nodeCreator);
95100
moduleConfigAdapters.put(module.toLowerCase(), configAdapter);
96-
saveAdapterDefaults();
97101
}
98102

99103
/**
@@ -105,9 +109,19 @@ public void save() throws IOException {
105109
loader.save(node);
106110
}
107111

108-
protected void saveAdapterDefaults() throws IOException {
112+
public void saveAdapterDefaults() throws IOException {
109113
CommentedConfigurationNode n = SimpleCommentedConfigurationNode.root();
110-
moduleConfigAdapters.forEach((k, v) -> n.getNode(k.toLowerCase()).setValue(v.getDefaults()));
114+
moduleConfigAdapters.forEach((k, v) -> {
115+
// Configurate does something I wasn't expecting. If we set a single value with a key on a node, it seems
116+
// to be set as the root - which causes havoc! So, we get the parent if it exists, because that's the
117+
// actual null node we're interested in.
118+
ConfigurationNode cn = v.getDefaults();
119+
if (cn.getParent() != null) {
120+
cn = cn.getParent();
121+
}
122+
123+
n.getNode(k.toLowerCase()).setValue(cn);
124+
});
111125

112126
node.mergeValuesFrom(n);
113127
save();

src/test/java/uk/co/drnaylor/quickstart/tests/scaffolding/FakeLoaderTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88
import ninja.leaping.configurate.SimpleConfigurationNode;
99
import ninja.leaping.configurate.loader.ConfigurationLoader;
1010
import org.junit.Before;
11+
import uk.co.drnaylor.quickstart.ModuleContainer;
12+
import uk.co.drnaylor.quickstart.exceptions.QuickStartModuleDiscoveryException;
1113

1214
import static org.mockito.Mockito.mock;
1315
import static org.mockito.Mockito.when;
1416

1517
public class FakeLoaderTests {
1618

1719
protected ConfigurationLoader<ConfigurationNode> loader;
20+
protected ConfigurationNode n = SimpleConfigurationNode.root();
1821

1922
@Before
2023
@SuppressWarnings("unchecked")
2124
public void beforeTests() throws Exception {
2225
loader = (ConfigurationLoader<ConfigurationNode>)mock(ConfigurationLoader.class);
2326
when(loader.createEmptyNode()).thenReturn(SimpleConfigurationNode.root());
24-
when(loader.load()).thenReturn(SimpleConfigurationNode.root());
27+
when(loader.load()).thenReturn(n);
28+
}
29+
30+
protected ModuleContainer getContainer(String p) throws QuickStartModuleDiscoveryException {
31+
return ModuleContainer.builder().setConfigurationLoader(loader).setPackageToScan(p).build();
2532
}
2633
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 ninja.leaping.configurate.ConfigurationNode;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import uk.co.drnaylor.quickstart.ModuleContainer;
11+
import uk.co.drnaylor.quickstart.tests.config.adapters.SimpleWithDefault;
12+
import uk.co.drnaylor.quickstart.tests.scaffolding.FakeLoaderTests;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
public class ModuleConfigurationTests extends FakeLoaderTests {
17+
18+
@Test
19+
public void testThatMergedDefaultsFromConfigAdapterArePresent() throws Exception {
20+
// When we load these modules...
21+
ModuleContainer mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.adapterstest");
22+
mc.loadModules(true);
23+
24+
SimpleWithDefault s2 = mc.getConfigAdapterForModule("moduletwo", SimpleWithDefault.class);
25+
assertEquals(s2.getNode().getNode("test").getString(), "test");
26+
}
27+
28+
@Test
29+
public void testThatUpdatingConfigAdapterPutsValueInCorrectPlace() throws Exception {
30+
// When we load these modules...
31+
ModuleContainer mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.adapterstest");
32+
mc.loadModules(true);
33+
34+
SimpleWithDefault s2 = mc.getConfigAdapterForModule("moduletwo", SimpleWithDefault.class);
35+
ConfigurationNode newNode = s2.getNode();
36+
newNode.getNode("newnode").setValue("result");
37+
38+
// This should not update the current node yet
39+
Assert.assertNotEquals("result", n.getNode("moduletwo", "newnode").getString());
40+
s2.setNode(newNode);
41+
42+
// Now it should have done.
43+
Assert.assertEquals("result", n.getNode("moduletwo", "newnode").getString());
44+
}
45+
}

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@
1313
import uk.co.drnaylor.quickstart.tests.config.adapters.SimpleWithDefault;
1414
import uk.co.drnaylor.quickstart.tests.scaffolding.FakeLoaderTests;
1515

16-
import static org.junit.Assert.assertEquals;
1716
import static org.junit.Assert.assertNotNull;
1817

1918
public class ModuleContainerConstructionTests extends FakeLoaderTests {
2019

21-
private ModuleContainer getContainer(String p) throws QuickStartModuleDiscoveryException {
22-
return ModuleContainer.builder().setConfigurationLoader(loader).setPackageToScan(p).build();
23-
}
24-
2520
@Test(expected = QuickStartModuleLoaderException.Construction.class)
2621
public void testThatUnconstructableModulesThrow() throws QuickStartModuleDiscoveryException, QuickStartModuleLoaderException.Enabling, QuickStartModuleLoaderException.Construction {
2722
getContainer("uk.co.drnaylor.quickstart.tests.modules.exceptions.construction").loadModules(true);
@@ -57,14 +52,4 @@ public void testThatConfigAdaptersGetRegisteredOnConstruction() throws Exception
5752
assertNotNull(s);
5853
assertNotNull(s2);
5954
}
60-
61-
@Test
62-
public void testThatMergedDefaultsFromConfigAdapterArePresent() throws Exception {
63-
// When we load these modules...
64-
ModuleContainer mc = getContainer("uk.co.drnaylor.quickstart.tests.modules.adapterstest");
65-
mc.loadModules(true);
66-
67-
SimpleWithDefault s2 = mc.getConfigAdapterForModule("moduletwo", SimpleWithDefault.class);
68-
assertEquals(s2.getNode().getNode("test").getString(), "test");
69-
}
7055
}

0 commit comments

Comments
 (0)