34
34
import static org .junit .Assert .assertNull ;
35
35
import static org .junit .Assert .assertSame ;
36
36
37
+ import java .security .GeneralSecurityException ;
38
+
39
+ import org .junit .Assert ;
37
40
import org .junit .Test ;
38
41
import org .scijava .Context ;
42
+ import org .scijava .prefs .PrefService ;
39
43
40
44
/**
41
45
* Tests {@link ModuleService}.
@@ -74,6 +78,38 @@ public void testGetSingleInput() throws ModuleException {
74
78
assertSame (info .getInput ("double2" ), singleDouble );
75
79
}
76
80
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
+
77
113
/** A sample module for testing the module service. */
78
114
public static class FooModule extends AbstractModule {
79
115
@@ -115,16 +151,16 @@ public Module createModule() throws ModuleException {
115
151
116
152
@ Override
117
153
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 );
124
160
}
125
161
126
162
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 )
128
164
{
129
165
registerInput (new AbstractModuleItem <T >(this ) {
130
166
@@ -143,6 +179,16 @@ public boolean isAutoFill() {
143
179
return autoFill ;
144
180
}
145
181
182
+ @ Override
183
+ public String getPersistKey () {
184
+ return persistKey ;
185
+ }
186
+
187
+ @ Override
188
+ public String getInitializer () {
189
+ return initializer ;
190
+ }
191
+
146
192
});
147
193
}
148
194
0 commit comments