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,37 @@ 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
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
+
77
112
/** A sample module for testing the module service. */
78
113
public static class FooModule extends AbstractModule {
79
114
@@ -115,16 +150,16 @@ public Module createModule() throws ModuleException {
115
150
116
151
@ Override
117
152
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 );
124
159
}
125
160
126
161
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 )
128
163
{
129
164
registerInput (new AbstractModuleItem <T >(this ) {
130
165
@@ -143,6 +178,16 @@ public boolean isAutoFill() {
143
178
return autoFill ;
144
179
}
145
180
181
+ @ Override
182
+ public String getPersistKey () {
183
+ return persistKey ;
184
+ }
185
+
186
+ @ Override
187
+ public String getInitializer () {
188
+ return initializer ;
189
+ }
190
+
146
191
});
147
192
}
148
193
0 commit comments