Skip to content

Commit 6156f22

Browse files
committed
Add regression test for CommandModule cancelation
This validates the bug-fix in 8a65459.
1 parent 8a65459 commit 6156f22

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.command;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.util.concurrent.ExecutionException;
39+
40+
import org.junit.Test;
41+
import org.scijava.Cancelable;
42+
import org.scijava.Context;
43+
import org.scijava.module.Module;
44+
import org.scijava.module.process.AbstractPreprocessorPlugin;
45+
import org.scijava.module.process.PreprocessorPlugin;
46+
import org.scijava.plugin.Plugin;
47+
48+
/** Regression tests for {@link CommandModule}. */
49+
public class CommandModuleTest {
50+
51+
@Test
52+
public void testCancelable() throws InterruptedException, ExecutionException {
53+
final Context context = new Context(CommandService.class);
54+
final CommandService commandService = context.service(CommandService.class);
55+
final CommandModule ice = commandService.run(IceCommand.class, true).get();
56+
final Cancelable c = (Cancelable) ice.getDelegateObject();
57+
assertTrue(ice.isCanceled());
58+
assertTrue(c.isCanceled());
59+
assertEquals("Stop! Collaborate and listen!", ice.getCancelReason());
60+
assertEquals("Stop! Collaborate and listen!", c.getCancelReason());
61+
}
62+
63+
@Test
64+
public void testNotCancelable() throws InterruptedException,
65+
ExecutionException
66+
{
67+
final Context context = new Context(CommandService.class);
68+
final CommandService commandService = context.service(CommandService.class);
69+
final CommandModule fire = commandService.run(FireCommand.class, true).get();
70+
assertFalse(fire.getDelegateObject() instanceof Cancelable);
71+
assertTrue(fire.isCanceled());
72+
assertEquals("NO SINGING!", fire.getCancelReason());
73+
}
74+
75+
// -- Helper classes --
76+
77+
/** A command which implements {@link Cancelable}. */
78+
@Plugin(type = Command.class, initializer = "init")
79+
public static class IceCommand extends ContextCommand {
80+
81+
public void init() {
82+
cancel("Stop! Collaborate and listen!");
83+
}
84+
85+
@Override
86+
public void run() {
87+
throw new IllegalStateException("Unexpected");
88+
}
89+
}
90+
91+
/** A command which does not implement {@link Cancelable}. */
92+
@Plugin(type = Command.class)
93+
public static class FireCommand implements Command {
94+
95+
@Override
96+
public void run() {
97+
throw new IllegalStateException("Unexpected");
98+
}
99+
}
100+
101+
@Plugin(type = PreprocessorPlugin.class)
102+
public static class CommandCanceler extends AbstractPreprocessorPlugin {
103+
104+
@Override
105+
public void process(final Module module) {
106+
final Object command = module.getDelegateObject();
107+
if (command instanceof IceCommand || command instanceof FireCommand) {
108+
// NB: A Monty Python quote which is also a Game of Thrones reference.
109+
// That's right -- we did that.
110+
cancel("NO SINGING!");
111+
}
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)