Skip to content

Commit 0d3ce4c

Browse files
committed
Add unit test for injection of context subclasses
This will help prevent regressions relating to the bug-fix in fbd6485.
1 parent fbd6485 commit 0d3ce4c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/test/java/org/scijava/ContextInjectionTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ public void testNonContextualContextParameters() {
133133
}
134134
}
135135

136+
/**
137+
* Tests that subclasses of {@link Context} are injected properly in the
138+
* relevant circumstances.
139+
*/
140+
@Test
141+
public void testContextSubclassInjection() {
142+
final Context c = new Context(true);
143+
final FooContext foo = new FooContext();
144+
final BarContext bar = new BarContext();
145+
146+
final ContextSubclassParameters cspPlain = new ContextSubclassParameters();
147+
c.inject(cspPlain);
148+
assertSame(c, cspPlain.c);
149+
assertNull(cspPlain.foo);
150+
assertNull(cspPlain.bar);
151+
152+
final ContextSubclassParameters cspFoo = new ContextSubclassParameters();
153+
foo.inject(cspFoo);
154+
assertNull(cspFoo.o);
155+
assertSame(foo, cspFoo.c);
156+
assertSame(foo, cspFoo.foo);
157+
assertNull(cspFoo.bar);
158+
159+
final ContextSubclassParameters cspBar = new ContextSubclassParameters();
160+
bar.inject(cspBar);
161+
assertNull(cspBar.o);
162+
assertSame(bar, cspBar.c);
163+
assertNull(cspBar.foo);
164+
assertSame(bar, cspBar.bar);
165+
}
166+
136167
/**
137168
* Tests that event subscription works properly for objects which extend
138169
* {@link AbstractContextual}.
@@ -214,6 +245,41 @@ public static class NeedsContext {
214245

215246
}
216247

248+
/** An object that only wants matching {@link Context} types injected. */
249+
public static class ContextSubclassParameters {
250+
251+
@Parameter
252+
private Object o;
253+
254+
@Parameter
255+
private Context c;
256+
257+
@Parameter
258+
private FooContext foo;
259+
260+
@Parameter
261+
private BarContext bar;
262+
263+
}
264+
265+
/** A simple {@link Context} subclass. */
266+
public static class FooContext extends Context {
267+
268+
public FooContext() {
269+
super(true);
270+
}
271+
272+
}
273+
274+
/** Another simple {@link Context} subclass. */
275+
public static class BarContext extends Context {
276+
277+
public BarContext() {
278+
super(true);
279+
}
280+
281+
}
282+
217283
/**
218284
* An object that subscribes to {@link SciJavaEvent}s and extends
219285
* {@link AbstractContextual}.

0 commit comments

Comments
 (0)