Skip to content

Commit 6ea385c

Browse files
committed
Add tests for NodeModule and DefaultNodeModuleService
Signed-off-by: Squareys <[email protected]>
1 parent 2bd9815 commit 6ea385c

File tree

3 files changed

+230
-88
lines changed

3 files changed

+230
-88
lines changed

org.knime.scijava.commands.testing/src/org/knime/scijava/commands/testing/NodeModuleTest.java

+86-88
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.Assert.assertTrue;
77

88
import java.util.ArrayList;
9+
import java.util.HashMap;
910

1011
import org.junit.Before;
1112
import org.junit.BeforeClass;
@@ -14,22 +15,24 @@
1415
import org.knime.core.data.DataRow;
1516
import org.knime.core.data.DataTableSpec;
1617
import org.knime.core.data.DataType;
18+
import org.knime.core.data.MissingCell;
1719
import org.knime.core.data.RowKey;
1820
import org.knime.core.data.def.BooleanCell;
1921
import org.knime.core.data.def.DefaultRow;
2022
import org.knime.core.data.def.IntCell;
2123
import org.knime.core.data.def.LongCell;
2224
import org.knime.core.data.def.StringCell;
2325
import org.knime.core.node.NodeLogger;
26+
import org.knime.core.node.defaultnodesettings.SettingsModel;
27+
import org.knime.core.node.defaultnodesettings.SettingsModelBoolean;
28+
import org.knime.core.node.defaultnodesettings.SettingsModelString;
2429
import org.knime.scijava.commands.CellOutput;
2530
import org.knime.scijava.commands.SciJavaGateway;
2631
import org.knime.scijava.commands.module.NodeModule;
2732
import org.knime.scijava.commands.module.NodeModuleService;
33+
import org.knime.scijava.commands.settings.models.SettingsModelColumnSelection;
2834
import org.scijava.Context;
29-
import org.scijava.ItemIO;
30-
import org.scijava.command.Command;
3135
import org.scijava.command.CommandInfo;
32-
import org.scijava.command.CommandService;
3336
import org.scijava.plugin.Parameter;
3437

3538
/**
@@ -40,21 +43,19 @@
4043
*/
4144
public class NodeModuleTest {
4245

43-
private static Context context;
44-
45-
@Parameter
46-
CommandService m_commandService;
46+
static Context context;
4747

4848
@Parameter
4949
NodeModuleService m_nodeModuleService;
5050

5151
// Create the test table
52-
private static final DataRow m_testRow = new DefaultRow(new RowKey("TestRow001"), BooleanCell.TRUE, new IntCell(42),
53-
new IntCell(420), new IntCell(42000), new LongCell(4200000), new StringCell("KNIME"), new StringCell(" "));
52+
final String[] inputNames = new String[] { "b", "i", "l", "str" };
53+
final String[] outputNames = new String[] { "ob", "oi", "ol", "ostr" };
5454

55-
private static final DataTableSpec m_spec = new DataTableSpec(new String[] { "b", "by", "s", "i", "l", "str", "c" },
56-
new DataType[] { BooleanCell.TYPE, IntCell.TYPE, IntCell.TYPE, IntCell.TYPE, LongCell.TYPE, StringCell.TYPE,
57-
StringCell.TYPE });
55+
final DataType[] inputDataTypes = new DataType[] { BooleanCell.TYPE, IntCell.TYPE, LongCell.TYPE, StringCell.TYPE };
56+
final DataTableSpec m_spec = new DataTableSpec(new String[] { "b", "i", "l", "str" }, inputDataTypes);
57+
final DataType[] expectedOutputDataTypes = new DataType[] { BooleanCell.TYPE, IntCell.TYPE, LongCell.TYPE,
58+
StringCell.TYPE };
5859

5960
@BeforeClass
6061
public static void setUpOnce() {
@@ -66,102 +67,99 @@ public void setUp() {
6667
context.inject(this);
6768
}
6869

69-
@Test
70-
public void testModuleExecution() throws Exception {
71-
assertNotNull(m_commandService);
70+
/**
71+
* Create a NodeModule
72+
* @param emptyTest Whether to have the module check for correct input values
73+
* @return the created Module
74+
*/
75+
private NodeModule createNodeModule(boolean emptyTest) {
76+
final HashMap<String, SettingsModel> settings = new HashMap<String, SettingsModel>();
77+
for (final String name : inputNames) {
78+
settings.put(name, new SettingsModelColumnSelection(name, name));
79+
}
80+
81+
settings.put("emptyTest", new SettingsModelBoolean("emptyTest", emptyTest));
82+
83+
for (int i = 0; i < outputNames.length; ++i) {
84+
settings.put(outputNames[i], new SettingsModelString(outputNames[i], expectedOutputDataTypes[i].getName()));
85+
}
7286

73-
NodeModule commandModule = m_nodeModuleService.createNodeModule(new CommandInfo(MyCommand.class), null, m_spec,
87+
return m_nodeModuleService.createNodeModule(new CommandInfo(ScijavaTestCommand.class), settings, m_spec,
7488
NodeLogger.getLogger(NodeModuleTest.class));
75-
assertNotNull(commandModule);
89+
}
7690

77-
// assertFalse("Command was cancelled: " +
78-
// commandModule.getCancelReason(), commandModule.isCanceled());
91+
@Test
92+
public void testExecution() throws Exception {
93+
final NodeModule nodeModule = createNodeModule(false);
94+
95+
final DataRow testRow = new DefaultRow( //
96+
new RowKey("TestRow001"), //
97+
BooleanCell.TRUE, //
98+
new IntCell(42000), //
99+
new LongCell(4200000), //
100+
new StringCell("KNIME"));
79101

80-
ArrayList<DataCell[]> cells = new ArrayList<>();
81-
CellOutput cellOutput = new CellOutput() {
102+
final ArrayList<DataCell[]> cells = new ArrayList<>();
103+
final CellOutput cellOutput = new CellOutput() {
82104

83-
ArrayList<DataCell[]> m_cells = cells;
105+
final ArrayList<DataCell[]> m_cells = cells;
84106

85107
@Override
86108
public void push(DataCell[] cells) throws InterruptedException {
87109
assertNotNull(cells);
88-
assertEquals(cells.length, 7);
110+
assertEquals("Unexpected amout of cells pushed.", 4, cells.length);
111+
112+
assertEquals("Unexpected type for pushed cell.", BooleanCell.class, cells[0].getClass());
113+
assertEquals("Unexpected type for pushed cell.", IntCell.class, cells[1].getClass());
114+
assertEquals("Unexpected type for pushed cell.", LongCell.class, cells[2].getClass());
115+
assertEquals("Unexpected type for pushed cell.", StringCell.class, cells[3].getClass());
89116

90117
assertTrue("Boolean output was not extracted correctly!", ((BooleanCell) cells[0]).getBooleanValue());
91-
assertEquals("Byte output was not extracted correctly!", 42, ((IntCell) cells[1]).getIntValue());
92-
assertEquals("Short output was not extracted correctly!", 420, ((IntCell) cells[2]).getIntValue());
93-
assertEquals("Integer output was not extracted correctly!", 42000, ((IntCell) cells[3]).getIntValue());
94-
assertEquals("Long output was not extracted correctly!", 4200000, ((LongCell) cells[4]).getLongValue());
118+
assertEquals("Integer output was not extracted correctly!", 42000, ((IntCell) cells[1]).getIntValue());
119+
assertEquals("Long output was not extracted correctly!", 4200000, ((LongCell) cells[2]).getLongValue());
95120
assertEquals("String output was not extracted correctly!", "KNIME",
96-
((StringCell) cells[5]).getStringValue());
97-
assertEquals("Character output was not extracted correctly!", " ",
98-
((StringCell) cells[6]).getStringValue());
121+
((StringCell) cells[3]).getStringValue());
99122

100123
m_cells.add(cells);
101124
}
102125
};
103-
commandModule.run(m_testRow, cellOutput, null);
126+
127+
nodeModule.run(testRow, cellOutput, null);
104128

105129
assertFalse("No cells were pushed", cells.isEmpty());
106130
}
107131

108-
/**
109-
* Test command which verifies that inputs have been filled by the
110-
* preprocessor and feeds them directly back in the outputs which can then
111-
* be collected into a data row.
112-
*
113-
* @author Jonathan Hale
114-
*/
115-
public static class MyCommand implements Command {
116-
117-
@Parameter(type = ItemIO.INPUT)
118-
Boolean b;
119-
@Parameter(type = ItemIO.INPUT)
120-
Byte by;
121-
@Parameter(type = ItemIO.INPUT)
122-
Short s;
123-
@Parameter(type = ItemIO.INPUT)
124-
Integer i;
125-
@Parameter(type = ItemIO.INPUT)
126-
Long l;
127-
@Parameter(type = ItemIO.INPUT)
128-
String str;
129-
@Parameter(type = ItemIO.INPUT)
130-
Character c;
131-
132-
@Parameter(type = ItemIO.OUTPUT)
133-
Boolean ob;
134-
@Parameter(type = ItemIO.OUTPUT)
135-
Byte oby;
136-
@Parameter(type = ItemIO.OUTPUT)
137-
Short os;
138-
@Parameter(type = ItemIO.OUTPUT)
139-
Integer oi;
140-
@Parameter(type = ItemIO.OUTPUT)
141-
Long ol;
142-
@Parameter(type = ItemIO.OUTPUT)
143-
String ostr;
144-
@Parameter(type = ItemIO.OUTPUT)
145-
Character oc;
146-
147-
@Override
148-
public void run() {
149-
assertTrue("Boolean input was not filled correctly!", b);
150-
assertEquals("Byte input was not filled correctly!", new Byte((byte) 42), by);
151-
assertEquals("Short input was not filled correctly!", new Short((short) 420), s);
152-
assertEquals("Integer input was not filled correctly!", new Integer(42000), i);
153-
assertEquals("Long input was not filled correctly!", new Long(4200000), l);
154-
assertEquals("String input was not filled correctly!", "KNIME", str);
155-
assertEquals("Character input was not filled correctly!", new Character(' '), c);
156-
157-
ob = b;
158-
oby = by;
159-
os = s;
160-
oi = i;
161-
ol = l;
162-
ostr = str;
163-
oc = c;
164-
}
132+
@Test
133+
public void testMissings() throws Exception {
134+
final NodeModule nodeModule = createNodeModule(true);
135+
136+
final DataRow emptyRow = new DefaultRow( //
137+
new RowKey("TestRow001"), //
138+
new MissingCell("Nothing here."), //
139+
new MissingCell("Nothing here either."), //
140+
new MissingCell("Full of nihilism."), //
141+
new MissingCell("Void."));
142+
143+
final ArrayList<DataCell[]> cells = new ArrayList<>();
144+
final CellOutput cellOutput = new CellOutput() {
145+
146+
final ArrayList<DataCell[]> m_cells = cells;
147+
148+
@Override
149+
public void push(DataCell[] cells) throws InterruptedException {
150+
assertNotNull(cells);
151+
assertEquals("Unexpected amout of cells pushed.", 4, cells.length);
165152

153+
for (DataCell cell : cells) {
154+
assertEquals("Unexpected type for pushed cell.", MissingCell.class, cell.getClass());
155+
}
156+
157+
m_cells.add(cells);
158+
}
159+
};
160+
161+
nodeModule.run(emptyRow, cellOutput, null);
162+
163+
assertFalse("No cells were pushed", cells.isEmpty());
166164
}
167165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.knime.scijava.commands.testing;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.knime.scijava.commands.StyleHook;
7+
import org.scijava.ItemIO;
8+
import org.scijava.command.Command;
9+
import org.scijava.plugin.Parameter;
10+
11+
/**
12+
* Test command which verifies that inputs have been filled by the preprocessor
13+
* and feeds them directly back in the outputs which can then be collected into
14+
* a data row.
15+
*
16+
* @author Jonathan Hale
17+
*/
18+
public class ScijavaTestCommand implements Command {
19+
20+
@Parameter(type = ItemIO.INPUT)
21+
Boolean emptyTest;
22+
23+
@Parameter(type = ItemIO.INPUT, style = StyleHook.COLUMNSELECTION)
24+
Boolean b;
25+
@Parameter(type = ItemIO.INPUT, style = StyleHook.COLUMNSELECTION)
26+
Integer i;
27+
@Parameter(type = ItemIO.INPUT, style = StyleHook.COLUMNSELECTION)
28+
Long l;
29+
@Parameter(type = ItemIO.INPUT, style = StyleHook.COLUMNSELECTION)
30+
String str;
31+
32+
@Parameter(type = ItemIO.OUTPUT)
33+
Boolean ob;
34+
@Parameter(type = ItemIO.OUTPUT)
35+
Integer oi;
36+
@Parameter(type = ItemIO.OUTPUT)
37+
Long ol;
38+
@Parameter(type = ItemIO.OUTPUT)
39+
String ostr;
40+
41+
@Override
42+
public void run() {
43+
if (!emptyTest) {
44+
assertTrue("Boolean input was not filled correctly!", b);
45+
assertEquals("Integer input was not filled correctly!", new Integer(42000), i);
46+
assertEquals("Long input was not filled correctly!", new Long(4200000), l);
47+
assertEquals("String input was not filled correctly!", "KNIME", str);
48+
} else {
49+
assertEquals("Integer input was not filled correctly!", null, b);
50+
assertEquals("Integer input was not filled correctly!", null, i);
51+
assertEquals("Long input was not filled correctly!", null, l);
52+
assertEquals("String input was not filled correctly!", null, str);
53+
}
54+
55+
ob = b;
56+
oi = i;
57+
ol = l;
58+
ostr = str;
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.knime.scijava.commands.testing.services;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotNull;
6+
7+
import java.util.HashMap;
8+
9+
import org.junit.Before;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
import org.knime.core.data.DataRow;
13+
import org.knime.core.data.DataTableSpec;
14+
import org.knime.core.data.DataType;
15+
import org.knime.core.data.RowKey;
16+
import org.knime.core.data.def.BooleanCell;
17+
import org.knime.core.data.def.DefaultRow;
18+
import org.knime.core.data.def.IntCell;
19+
import org.knime.core.data.def.LongCell;
20+
import org.knime.core.data.def.StringCell;
21+
import org.knime.core.node.defaultnodesettings.SettingsModel;
22+
import org.knime.core.node.defaultnodesettings.SettingsModelString;
23+
import org.knime.scijava.commands.SciJavaGateway;
24+
import org.knime.scijava.commands.module.NodeModuleService;
25+
import org.knime.scijava.commands.settings.models.SettingsModelColumnSelection;
26+
import org.knime.scijava.commands.testing.ScijavaTestCommand;
27+
import org.scijava.Context;
28+
import org.scijava.command.CommandInfo;
29+
import org.scijava.plugin.Parameter;
30+
31+
/**
32+
* Test for {@link NodeModuleService}.
33+
*
34+
* @author Jonathan Hale (University of Konstanz)
35+
*/
36+
public class DefaultNodeModuleServiceTest {
37+
38+
private static Context context;
39+
40+
@Parameter
41+
NodeModuleService m_nodeModuleService;
42+
43+
private final DataTableSpec inSpec = new DataTableSpec(new String[] { "b", "i", "l", "str" },
44+
new DataType[] { BooleanCell.TYPE, IntCell.TYPE, LongCell.TYPE, StringCell.TYPE });
45+
46+
@BeforeClass
47+
public static void setUpOnce() {
48+
context = SciJavaGateway.get().getGlobalContext();
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
context.inject(this);
54+
assertNotNull(m_nodeModuleService);
55+
}
56+
57+
@Test
58+
public void testCreateOutSpec() throws Exception {
59+
/*
60+
* Create settings which just map input columns to equally named module
61+
* items.
62+
*/
63+
final HashMap<String, SettingsModel> settings = new HashMap<String, SettingsModel>();
64+
for (final String name : new String[] { "b", "i", "l", "str" }) {
65+
settings.put(name, new SettingsModelColumnSelection(name, name));
66+
}
67+
68+
String[] outNames = new String[] { "ob", "oi", "ol", "ostr" };
69+
DataType[] outTypes = new DataType[] { BooleanCell.TYPE, IntCell.TYPE, LongCell.TYPE, StringCell.TYPE };
70+
for (int i = 0; i < outNames.length; ++i) {
71+
settings.put(outNames[i], new SettingsModelString(outNames[i], outTypes[i].getName()));
72+
}
73+
74+
// Produce output column spec
75+
DataTableSpec outSpec = m_nodeModuleService.createOutSpec(new CommandInfo(ScijavaTestCommand.class), settings,
76+
inSpec);
77+
78+
assertArrayEquals("Names for output columns are incorrect.", outNames, outSpec.getColumnNames());
79+
for (int i = 0; i < outNames.length; ++i) {
80+
assertEquals("Type for column " + i + " is incorrect.", outTypes[i], outSpec.getColumnSpec(i).getType());
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)