Skip to content

Commit 34b1672

Browse files
committed
Add test for persistance behavior of ModuleItems
1 parent 9bcebd9 commit 34b1672

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

src/test/java/org/scijava/module/ModuleServiceTest.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@
3434
import static org.junit.Assert.assertNull;
3535
import static org.junit.Assert.assertSame;
3636

37+
import java.security.GeneralSecurityException;
38+
39+
import org.junit.Assert;
3740
import org.junit.Test;
3841
import org.scijava.Context;
42+
import org.scijava.prefs.PrefService;
3943

4044
/**
4145
* Tests {@link ModuleService}.
@@ -74,6 +78,37 @@ public void testGetSingleInput() throws ModuleException {
7478
assertSame(info.getInput("double2"), singleDouble);
7579
}
7680

81+
@SuppressWarnings("unchecked")
82+
@Test
83+
public void testPersistingWithInitialize() {
84+
final Context context = new Context(ModuleService.class, PrefService.class);
85+
final ModuleService moduleService = context.getService(ModuleService.class);
86+
87+
// reset the PrefService
88+
final PrefService prefService = context.getService(PrefService.class);
89+
prefService.clearAll();
90+
91+
final ModuleInfo info = new FooModuleInfo();
92+
final ModuleItem<Double> doubleItem = (ModuleItem<Double>) info.getInput(
93+
"double1");
94+
final ModuleItem<Integer> integerItem = (ModuleItem<Integer>) info.getInput(
95+
"integer1");
96+
97+
// save ModuleItem for which getInitializer() returns "testInitializer"
98+
moduleService.save(doubleItem, 5d);
99+
100+
// verify that the item is not persisted
101+
String persistKey = doubleItem.getPersistKey();
102+
Assert.assertNull(prefService.get(persistKey));
103+
104+
// save ModuleItem for which getInitializer() returns null
105+
moduleService.save(integerItem, 5);
106+
107+
// verify that the item is persisted
108+
persistKey = integerItem.getPersistKey();
109+
Assert.assertEquals(5, prefService.getInt(persistKey, 0));
110+
}
111+
77112
/** A sample module for testing the module service. */
78113
public static class FooModule extends AbstractModule {
79114

@@ -115,16 +150,16 @@ public Module createModule() throws ModuleException {
115150

116151
@Override
117152
protected void parseParameters() {
118-
addInput("string", String.class, true);
119-
addInput("float", Float.class, false);
120-
addInput("integer1", Integer.class, true);
121-
addInput("integer2", Integer.class, true);
122-
addInput("double1", Double.class, false);
123-
addInput("double2", Double.class, true);
153+
addInput("string", String.class, true, null, null);
154+
addInput("float", Float.class, false, null, null);
155+
addInput("integer1", Integer.class, true, "persistInteger", null);
156+
addInput("integer2", Integer.class, true, null, null);
157+
addInput("double1", Double.class, false, "persistDouble", "testInitializer");
158+
addInput("double2", Double.class, true, null, null);
124159
}
125160

126161
private <T> void addInput(final String name, final Class<T> type,
127-
final boolean autoFill)
162+
final boolean autoFill, final String persistKey, final String initializer)
128163
{
129164
registerInput(new AbstractModuleItem<T>(this) {
130165

@@ -143,6 +178,16 @@ public boolean isAutoFill() {
143178
return autoFill;
144179
}
145180

181+
@Override
182+
public String getPersistKey() {
183+
return persistKey;
184+
}
185+
186+
@Override
187+
public String getInitializer() {
188+
return initializer;
189+
}
190+
146191
});
147192
}
148193

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy