-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathModuleServiceTest.java
195 lines (158 loc) · 5.94 KB
/
ModuleServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.module;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import org.junit.Test;
import org.scijava.Context;
import org.scijava.prefs.PrefService;
/**
* Tests {@link ModuleService}.
*
* @author Curtis Rueden
*/
public class ModuleServiceTest {
@Test
public void testGetSingleInput() throws ModuleException {
final Context context = new Context(ModuleService.class);
final ModuleService moduleService = context.getService(ModuleService.class);
final ModuleInfo info = new FooModuleInfo();
final Module module = info.createModule();
// verify single string input is detected
final ModuleItem<String> singleString =
moduleService.getSingleInput(module, String.class);
assertSame(info.getInput("string"), singleString);
// check that non-autofilled inputs are not detected
final ModuleItem<Float> singleFloat =
moduleService.getSingleInput(module, Float.class);
assertNull(singleFloat);
// verify that multiple inputs of the same type are not detected
final ModuleItem<Integer> singleInteger =
moduleService.getSingleInput(module, Integer.class);
assertNull(singleInteger);
// verify that single input is detected if there are
// non-autofilled inputs of the same kind too
final ModuleItem<Double> singleDouble =
moduleService.getSingleInput(module, Double.class);
assertSame(info.getInput("double2"), singleDouble);
}
@SuppressWarnings("unchecked")
@Test
public void testPersistingWithInitialize() {
final Context context = new Context(ModuleService.class, PrefService.class);
final ModuleService moduleService = context.getService(ModuleService.class);
// reset the PrefService entries for the test
final PrefService prefService = context.getService(PrefService.class);
prefService.clear("persistInteger");
prefService.clear("persistDouble");
final ModuleInfo info = new FooModuleInfo();
final ModuleItem<Double> doubleItem = (ModuleItem<Double>) info.getInput(
"double1");
final ModuleItem<Integer> integerItem = (ModuleItem<Integer>) info.getInput(
"integer1");
// save ModuleItem for which getInitializer() returns "testInitializer"
moduleService.save(doubleItem, 5d);
// verify that the item is not persisted
String persistKey = doubleItem.getPersistKey();
assertNull(prefService.get(persistKey));
// save ModuleItem for which getInitializer() returns null
moduleService.save(integerItem, 5);
// verify that the item is persisted
persistKey = integerItem.getPersistKey();
assertEquals(5, prefService.getInt(persistKey, 0));
}
/** A sample module for testing the module service. */
public static class FooModule extends AbstractModule {
private final FooModuleInfo info;
public FooModule(final FooModuleInfo info) {
this.info = info;
}
@Override
public ModuleInfo getInfo() {
return info;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
/** {@link ModuleInfo} implementation for the {@link FooModule}. */
public static class FooModuleInfo extends AbstractModuleInfo {
@Override
public String getDelegateClassName() {
return FooModule.class.getName();
}
@Override
public Class<?> loadDelegateClass() throws ClassNotFoundException {
return FooModule.class;
}
@Override
public Module createModule() throws ModuleException {
return new FooModule(this);
}
@Override
protected void parseParameters() {
addInput("string", String.class, true, null, null);
addInput("float", Float.class, false, null, null);
addInput("integer1", Integer.class, true, "persistInteger", null);
addInput("integer2", Integer.class, true, null, null);
addInput("double1", Double.class, false, "persistDouble", "testInitializer");
addInput("double2", Double.class, true, null, null);
}
private <T> void addInput(final String name, final Class<T> type,
final boolean autoFill, final String persistKey, final String initializer)
{
registerInput(new AbstractModuleItem<T>(this) {
@Override
public String getName() {
return name;
}
@Override
public Class<T> getType() {
return type;
}
@Override
public boolean isAutoFill() {
return autoFill;
}
@Override
public String getPersistKey() {
return persistKey;
}
@Override
public String getInitializer() {
return initializer;
}
});
}
}
}