Skip to content

Commit c5c1d04

Browse files
authored
Merge pull request #338 from scijava/integrate-tests-from-271
SingletonServiceTest: extend with tests from #271.
2 parents b46eeb0 + 5e4a79b commit c5c1d04

File tree

1 file changed

+172
-2
lines changed

1 file changed

+172
-2
lines changed

src/test/java/org/scijava/plugin/SingletonServiceTest.java

Lines changed: 172 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,195 @@
3434

3535
import static org.junit.Assert.assertEquals;
3636
import static org.junit.Assert.assertFalse;
37+
import static org.junit.Assert.assertNotNull;
38+
import static org.junit.Assert.assertNotSame;
39+
import static org.junit.Assert.assertNull;
3740
import static org.junit.Assert.assertTrue;
3841

3942
import java.util.List;
4043

44+
import org.junit.After;
45+
import org.junit.Before;
4146
import org.junit.Test;
4247
import org.scijava.Context;
48+
import org.scijava.convert.AbstractConverter;
49+
import org.scijava.convert.ConvertService;
50+
import org.scijava.convert.Converter;
51+
import org.scijava.plugin.event.PluginsAddedEvent;
52+
import org.scijava.plugin.event.PluginsRemovedEvent;
4353

4454
/**
4555
* Tests for the {@link SingletonService}
4656
*
4757
* @author Gabriel Einsdorf KNIME GmbH
58+
* @author Stefan Helfrich KNIME GmbH
4859
*/
4960
public class SingletonServiceTest {
5061

62+
private PluginService pluginService;
63+
private ConvertService convertService;
64+
65+
@Before
66+
public void setUp() {
67+
final Context context = new Context(PluginService.class,
68+
ConvertService.class);
69+
pluginService = context.service(PluginService.class);
70+
convertService = context.service(ConvertService.class);
71+
}
72+
73+
@After
74+
public void tearDown() {
75+
pluginService.context().dispose();
76+
}
77+
78+
/**
79+
* Tests that the {@link AbstractSingletonService} properly handles
80+
* {@link PluginsAddedEvent}s originating from the {@link PluginService}.
81+
*/
82+
@Test
83+
public void testSingletonServicePluginsAddedHandling() {
84+
@SuppressWarnings("rawtypes")
85+
final PluginInfo<Converter> converterInfo = new PluginInfo<>(
86+
FoodConverter.class, Converter.class);
87+
88+
pluginService.addPlugin(converterInfo);
89+
90+
assertNotNull(pluginService.getPlugin(FoodConverter.class));
91+
assertTrue(convertService.supports(new Apple() {}, Peach.class));
92+
}
93+
94+
/**
95+
* Tests that the {@link AbstractSingletonService} properly handles
96+
* {@link PluginsAddedEvent}s that replace an instance.
97+
*/
98+
@Test
99+
public void testSingletonServicePluginsAddedHandlingDuplicates() {
100+
@SuppressWarnings("rawtypes")
101+
final PluginInfo<Converter> converterInfo = new PluginInfo<>(
102+
FoodConverter.class, Converter.class);
103+
104+
pluginService.addPlugin(converterInfo);
105+
final FoodConverter firstInstance = convertService.getInstance(
106+
FoodConverter.class);
107+
108+
pluginService.addPlugin(converterInfo);
109+
final FoodConverter secondInstance = convertService.getInstance(
110+
FoodConverter.class);
111+
112+
assertNotSame(firstInstance, secondInstance);
113+
assertTrue(convertService.supports(new Apple() {}, Peach.class));
114+
}
115+
116+
/**
117+
* Tests that the {@link AbstractSingletonService} properly handles
118+
* {@link PluginsRemovedEvent}s originating from the {@link PluginService}.
119+
*/
120+
@Test
121+
public void testSingletonServiceManuallyAddedPluginsRemovedHandling() {
122+
@SuppressWarnings("rawtypes")
123+
final PluginInfo<Converter> converterInfo = new PluginInfo<>(
124+
FoodConverter.class, Converter.class);
125+
126+
pluginService.addPlugin(converterInfo);
127+
128+
// De-register DummyStringConverter
129+
pluginService.removePlugin(converterInfo);
130+
131+
assertNull(pluginService.getPlugin(FoodConverter.class));
132+
assertFalse(convertService.supports(new Apple() {}, Peach.class));
133+
}
134+
135+
/**
136+
* Tests that the {@link AbstractSingletonService} properly handles
137+
* {@link PluginsRemovedEvent}s originating from the {@link PluginService}.
138+
*/
139+
@Test
140+
public void testSingletonServiceCompileTimePluginsRemovedHandling() {
141+
final PluginInfo<SciJavaPlugin> pluginInfo = pluginService.getPlugin(
142+
DiscoveredFoodConverter.class);
143+
144+
// De-register DiscoveredFoodConverter
145+
pluginService.removePlugin(pluginInfo);
146+
147+
assertNull(pluginService.getPlugin(DiscoveredFoodConverter.class));
148+
assertFalse(convertService.supports(new Orange() {}, Peach.class));
149+
}
150+
151+
/**
152+
* Dummy {@link Converter}.
153+
*/
154+
public static class FoodConverter extends AbstractConverter<Apple, Peach> {
155+
156+
@Override
157+
public <T> T convert(final Object src, final Class<T> dest) {
158+
return null;
159+
}
160+
161+
@Override
162+
public Class<Peach> getOutputType() {
163+
return Peach.class;
164+
}
165+
166+
@Override
167+
public Class<Apple> getInputType() {
168+
return Apple.class;
169+
}
170+
}
171+
172+
/**
173+
* Dummy {@link Converter} that is added automatically.
174+
*/
175+
@Plugin(type = Converter.class)
176+
public static class DiscoveredFoodConverter extends
177+
AbstractConverter<Orange, Peach>
178+
{
179+
180+
@Override
181+
public <T> T convert(final Object src, final Class<T> dest) {
182+
return null;
183+
}
184+
185+
@Override
186+
public Class<Peach> getOutputType() {
187+
return Peach.class;
188+
}
189+
190+
@Override
191+
public Class<Orange> getInputType() {
192+
return Orange.class;
193+
}
194+
}
195+
196+
/**
197+
* Type interface for conversion
198+
*/
199+
public interface Apple {
200+
// NB
201+
}
202+
203+
/**
204+
* Type interface for conversion
205+
*/
206+
public interface Orange {
207+
// NB
208+
}
209+
210+
/**
211+
* Type interface for conversion
212+
*/
213+
public interface Peach {
214+
// NB
215+
}
216+
217+
/**
218+
* Tests that plugins are added to and removed from the correct singleton
219+
* service
220+
*/
51221
@Test
52222
public void testListenToRemove() {
53223

54-
final Context ctx = new Context(PluginService.class, DummySingletonService.class,
55-
DummySingletonService2.class);
224+
final Context ctx = new Context(PluginService.class,
225+
DummySingletonService.class, DummySingletonService2.class);
56226

57227
final DummySingletonService dss = ctx.getService(
58228
DummySingletonService.class);

0 commit comments

Comments
 (0)