1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.cli;
19
20 import com.beust.jcommander.Parameters;
21 import org.apache.any23.Any23OnlineTestBase;
22
23 import java.util.Arrays;
24 import java.util.Locale;
25 import java.util.Objects;
26
27 import static java.lang.String.format;
28 import static org.junit.Assert.assertEquals;
29
30
31
32
33
34
35 public abstract class ToolTestBase extends Any23OnlineTestBase {
36
37 public static final String TOOL_RUN_METHOD = "run";
38
39 private final Class<? extends Tool> toolClazz;
40
41 protected ToolTestBase(Class<? extends Tool> tool) {
42 toolClazz = Objects.requireNonNull(tool, "Tool class cannot be null.");
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56 protected int runTool(String... args) throws Exception {
57 final String commandName = toolClazz.getAnnotation(Parameters.class).commandNames()[0];
58
59 final String[] enhancedArgs = new String[args.length + 1];
60 enhancedArgs[0] = commandName;
61 System.arraycopy(args, 0, enhancedArgs, 1, args.length);
62
63 return new ToolRunner().execute(true, enhancedArgs);
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77 protected int runTool(String args) throws Exception {
78 return runTool(args.split(" "));
79 }
80
81
82
83
84
85
86
87
88
89
90 protected void runToolCheckExit0(String... args) throws Exception {
91 assertEquals(format(Locale.ROOT, "Unexpected exit code for tool [%s] invoked with %s",
92 toolClazz.getSimpleName(), Arrays.asList(args)), 0, runTool(args));
93 }
94
95 }