6
6
import static org .junit .Assert .assertTrue ;
7
7
8
8
import java .util .ArrayList ;
9
+ import java .util .HashMap ;
9
10
10
11
import org .junit .Before ;
11
12
import org .junit .BeforeClass ;
14
15
import org .knime .core .data .DataRow ;
15
16
import org .knime .core .data .DataTableSpec ;
16
17
import org .knime .core .data .DataType ;
18
+ import org .knime .core .data .MissingCell ;
17
19
import org .knime .core .data .RowKey ;
18
20
import org .knime .core .data .def .BooleanCell ;
19
21
import org .knime .core .data .def .DefaultRow ;
20
22
import org .knime .core .data .def .IntCell ;
21
23
import org .knime .core .data .def .LongCell ;
22
24
import org .knime .core .data .def .StringCell ;
23
25
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 ;
24
29
import org .knime .scijava .commands .CellOutput ;
25
30
import org .knime .scijava .commands .SciJavaGateway ;
26
31
import org .knime .scijava .commands .module .NodeModule ;
27
32
import org .knime .scijava .commands .module .NodeModuleService ;
33
+ import org .knime .scijava .commands .settings .models .SettingsModelColumnSelection ;
28
34
import org .scijava .Context ;
29
- import org .scijava .ItemIO ;
30
- import org .scijava .command .Command ;
31
35
import org .scijava .command .CommandInfo ;
32
- import org .scijava .command .CommandService ;
33
36
import org .scijava .plugin .Parameter ;
34
37
35
38
/**
40
43
*/
41
44
public class NodeModuleTest {
42
45
43
- private static Context context ;
44
-
45
- @ Parameter
46
- CommandService m_commandService ;
46
+ static Context context ;
47
47
48
48
@ Parameter
49
49
NodeModuleService m_nodeModuleService ;
50
50
51
51
// 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" } ;
54
54
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 };
58
59
59
60
@ BeforeClass
60
61
public static void setUpOnce () {
@@ -66,102 +67,99 @@ public void setUp() {
66
67
context .inject (this );
67
68
}
68
69
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
+ }
72
86
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 ,
74
88
NodeLogger .getLogger (NodeModuleTest .class ));
75
- assertNotNull ( commandModule );
89
+ }
76
90
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" ));
79
101
80
- ArrayList <DataCell []> cells = new ArrayList <>();
81
- CellOutput cellOutput = new CellOutput () {
102
+ final ArrayList <DataCell []> cells = new ArrayList <>();
103
+ final CellOutput cellOutput = new CellOutput () {
82
104
83
- ArrayList <DataCell []> m_cells = cells ;
105
+ final ArrayList <DataCell []> m_cells = cells ;
84
106
85
107
@ Override
86
108
public void push (DataCell [] cells ) throws InterruptedException {
87
109
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 ());
89
116
90
117
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 ());
95
120
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 ());
99
122
100
123
m_cells .add (cells );
101
124
}
102
125
};
103
- commandModule .run (m_testRow , cellOutput , null );
126
+
127
+ nodeModule .run (testRow , cellOutput , null );
104
128
105
129
assertFalse ("No cells were pushed" , cells .isEmpty ());
106
130
}
107
131
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 );
165
152
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 ());
166
164
}
167
165
}
0 commit comments