1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package au.csiro.netcdf;
21
22 import java.io.BufferedWriter;
23 import java.io.ByteArrayOutputStream;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.security.Permission;
28
29 import junit.framework.TestCase;
30 import au.csiro.netcdf.cli.NetCDFTool;
31
32
33
34
35
36
37
38
39
40 public class TestCommandLineRunner extends TestCase
41 {
42
43
44
45 private final String dummyCSVFilename = "dummy.csv";
46
47
48
49
50 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
51
52
53
54
55 private SecurityManager securityManager;
56
57
58
59
60
61
62
63 @Override
64 protected void setUp() throws Exception
65 {
66 super.setUp();
67
68
69 this.createDummyCSVFile();
70
71
72 System.setOut(new PrintStream(outContent));
73
74
75 securityManager = System.getSecurityManager();
76 System.setSecurityManager(new NoExitSecurityManager());
77 }
78
79
80
81
82
83
84 @Override
85 protected void tearDown() throws Exception
86 {
87 super.tearDown();
88
89 System.setOut(null);
90
91 System.setSecurityManager(securityManager);
92 }
93
94
95
96
97 public final void testMainMethodInvalidCommand()
98 {
99 try
100 {
101 NetCDFTool.main(new String[]{"invalidCommandName","-invalidOption","invalidValue"});
102 }
103 catch(ExitException ee)
104 {
105 ee.printStackTrace();
106 assertTrue("Wrong system exit status code", ee.status == 0);
107 }
108 }
109
110
111
112
113 public final void testMainMethodValidCommandInvalidArgs()
114 {
115 try
116 {
117 NetCDFTool.main(new String[]{NcCSVExtract.EXTRACT_COMMAND_NAME});
118 }
119 catch(ExitException ee)
120 {
121 ee.printStackTrace();
122 assertTrue("Wrong system exit status code", ee.status == 1);
123 }
124 }
125
126
127
128
129
130 public final void testMainMethodValidCommandValidArgsInvalidContents() throws IOException
131 {
132 try
133 {
134 NetCDFTool.main(new String[]{NcCSVExtract.EXTRACT_COMMAND_NAME,
135 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
136 "-"+NcCSVExtract.COLUMN_INDEX, "0"
137 }
138 catch(ExitException ee)
139 {
140 ee.printStackTrace();
141 assertTrue("Wrong system exit status code", ee.status == 2);
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154
155 protected static class ExitException extends SecurityException {
156 private static final long serialVersionUID = -1982617086752946683L;
157 public final int status;
158
159 public ExitException(int status) {
160 super("System exiting");
161 this.status = status;
162 }
163 }
164
165
166
167
168
169
170
171
172
173
174 private static class NoExitSecurityManager extends SecurityManager {
175 @Override
176 public void checkPermission(Permission perm) {
177
178 }
179
180 @Override
181 public void checkPermission(Permission perm, Object context) {
182
183 }
184
185 @Override
186 public void checkExit(int status) {
187 super.checkExit(status);
188 throw new ExitException(status);
189 }
190 }
191
192
193
194
195 private void createDummyCSVFile()
196 {
197 try
198 {
199 FileWriter fstream = new FileWriter(this.dummyCSVFilename);
200 BufferedWriter out = new BufferedWriter(fstream);
201 out.close();
202 }
203 catch (Exception e)
204 {
205
206 e.printStackTrace();
207 fail("Failed trying to create a dummy csv file for testing the extract command.");
208 }
209 }
210 }