1
1
package org .scijava .batch ;
2
2
3
+ import static org .junit .Assert .assertEquals ;
3
4
import static org .junit .Assert .assertNotNull ;
4
5
6
+ import java .io .File ;
7
+ import java .io .StringReader ;
8
+ import java .util .HashMap ;
9
+ import java .util .concurrent .ExecutionException ;
10
+
11
+ import net .imagej .table .Table ;
12
+
5
13
import org .junit .After ;
14
+ import org .junit .Before ;
6
15
import org .junit .Test ;
7
16
import org .scijava .Context ;
17
+ import org .scijava .command .CommandInfo ;
18
+ import org .scijava .command .CommandService ;
19
+ import org .scijava .module .Module ;
20
+ import org .scijava .module .ModuleService ;
21
+ import org .scijava .script .ScriptInfo ;
22
+ import org .scijava .service .SciJavaService ;
8
23
9
24
public class BatchServiceTest {
10
-
25
+
11
26
private Context context ;
12
27
28
+ @ Before
29
+ public void initialize () {
30
+ context = new Context (SciJavaService .class );
31
+ }
32
+
13
33
@ After
14
34
public void disposeContext () {
15
35
if (context != null ) {
@@ -20,9 +40,54 @@ public void disposeContext() {
20
40
21
41
@ Test
22
42
public void testContext () {
23
- context = new Context (BatchService .class );
24
- final BatchService batchService =
25
- context .getService (BatchService .class );
43
+ final BatchService batchService = context
44
+ .getService (BatchService .class );
26
45
assertNotNull (batchService );
27
46
}
47
+
48
+ @ Test
49
+ public void testModuleBatchProcessor () {
50
+ String script = "" //
51
+ + "#@ File input\n " //
52
+ + "#@output result\n " //
53
+ + "" //
54
+ + "result = input" ;
55
+ StringReader scriptReader = new StringReader (script );
56
+ ScriptInfo scriptInfo = new ScriptInfo (context , "Foo.groovy" ,
57
+ scriptReader );
58
+
59
+ assertEquals ("Wrong script language" , "Groovy" , scriptInfo
60
+ .getLanguage ().getLanguageName ());
61
+
62
+ File [] files = new File [3 ];
63
+ files [0 ] = new File ("foo.txt" );
64
+ files [1 ] = new File ("bar.txt" );
65
+ files [2 ] = new File ("quo.txt" );
66
+
67
+ HashMap <String , Object > inputMap = new HashMap <>();
68
+ inputMap .put ("moduleInfo" , scriptInfo );
69
+ inputMap .put ("inputChoice" , "input" );
70
+ inputMap .put ("inputFileList" , files );
71
+ inputMap .put ("outputFolder" , null );
72
+ ModuleService moduleService = context .getService (ModuleService .class );
73
+ CommandService commandService = context
74
+ .getService (CommandService .class );
75
+ CommandInfo commandInfo = commandService
76
+ .getCommand (ModuleBatchProcessor .class );
77
+ Module module = moduleService .createModule (commandInfo );
78
+ try {
79
+ module = moduleService .run (module , true , inputMap ).get ();
80
+ } catch (InterruptedException | ExecutionException exc ) {
81
+ // TODO Auto-generated catch block
82
+ exc .printStackTrace ();
83
+ }
84
+ Table <?, ?> outputs = (Table <?, ?>) module .getOutput ("outputTable" );
85
+
86
+ assertEquals ("Wrong number of output columns" , 1 ,
87
+ outputs .getColumnCount ());
88
+ assertEquals ("Wrong number of output rows" , 3 , outputs .getRowCount ());
89
+ assertEquals ("Wrong column header" , "result" ,
90
+ outputs .getColumnHeader (0 ));
91
+ assertEquals ("Wrong file name" , "quo.txt" , outputs .getRowHeader (2 ));
92
+ }
28
93
}
0 commit comments