3434import static org .junit .Assert .assertNull ;
3535import static org .junit .Assert .assertSame ;
3636
37+ import java .security .GeneralSecurityException ;
38+
39+ import org .junit .Assert ;
3740import org .junit .Test ;
3841import org .scijava .Context ;
42+ import org .scijava .prefs .PrefService ;
3943
4044/**
4145 * Tests {@link ModuleService}.
@@ -74,6 +78,38 @@ 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 entries for the test
88+ final PrefService prefService = context .getService (PrefService .class );
89+ prefService .clear ("persistInteger" );
90+ prefService .clear ("persistDouble" );
91+
92+ final ModuleInfo info = new FooModuleInfo ();
93+ final ModuleItem <Double > doubleItem = (ModuleItem <Double >) info .getInput (
94+ "double1" );
95+ final ModuleItem <Integer > integerItem = (ModuleItem <Integer >) info .getInput (
96+ "integer1" );
97+
98+ // save ModuleItem for which getInitializer() returns "testInitializer"
99+ moduleService .save (doubleItem , 5d );
100+
101+ // verify that the item is not persisted
102+ String persistKey = doubleItem .getPersistKey ();
103+ Assert .assertNull (prefService .get (persistKey ));
104+
105+ // save ModuleItem for which getInitializer() returns null
106+ moduleService .save (integerItem , 5 );
107+
108+ // verify that the item is persisted
109+ persistKey = integerItem .getPersistKey ();
110+ Assert .assertEquals (5 , prefService .getInt (persistKey , 0 ));
111+ }
112+
77113 /** A sample module for testing the module service. */
78114 public static class FooModule extends AbstractModule {
79115
@@ -115,16 +151,16 @@ public Module createModule() throws ModuleException {
115151
116152 @ Override
117153 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 );
154+ addInput ("string" , String .class , true , null , null );
155+ addInput ("float" , Float .class , false , null , null );
156+ addInput ("integer1" , Integer .class , true , "persistInteger" , null );
157+ addInput ("integer2" , Integer .class , true , null , null );
158+ addInput ("double1" , Double .class , false , "persistDouble" , "testInitializer" );
159+ addInput ("double2" , Double .class , true , null , null );
124160 }
125161
126162 private <T > void addInput (final String name , final Class <T > type ,
127- final boolean autoFill )
163+ final boolean autoFill , final String persistKey , final String initializer )
128164 {
129165 registerInput (new AbstractModuleItem <T >(this ) {
130166
@@ -143,6 +179,16 @@ public boolean isAutoFill() {
143179 return autoFill ;
144180 }
145181
182+ @ Override
183+ public String getPersistKey () {
184+ return persistKey ;
185+ }
186+
187+ @ Override
188+ public String getInitializer () {
189+ return initializer ;
190+ }
191+
146192 });
147193 }
148194
0 commit comments