1 /**
2 * Copyright 2010, CSIRO Australia.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * This class is a unit test suite to verify that the {@link NetCDFTool} operates correctly.
34 * <p>
35 * Copyright 2010, CSIRO Australia All rights reserved.
36 *
37 * @author Robert Bridle on 17/03/2010
38 * @version $Revision: 78 $ $Date: 2010-07-24 16:23:13 +1000 (Sat, 24 Jul 2010) $
39 */
40 public class TestCommandLineRunner extends TestCase
41 {
42 /**
43 * A dummy csv file that we can test the commands on.
44 */
45 private final String dummyCSVFilename = "dummy.csv";
46
47 /**
48 * Used to capture standard output
49 */
50 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
51
52 /**
53 * A security manager to stop system exits from stopping the JVM
54 */
55 private SecurityManager securityManager;
56
57
58 /*
59 * (non-Javadoc)
60 *
61 * @see junit.framework.TestCase#setUp()
62 */
63 @Override
64 protected void setUp() throws Exception
65 {
66 super.setUp();
67
68 // create dummy csv file with invalid contents.
69 this.createDummyCSVFile();
70
71 // write standard output to a stream we can examine.
72 System.setOut(new PrintStream(outContent));
73
74 // create out security manager
75 securityManager = System.getSecurityManager();
76 System.setSecurityManager(new NoExitSecurityManager());
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see junit.framework.TestCase#tearDown()
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 * Test execution of the main method with an invalid command.
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 * Test execution of the main method with a valid command but with invalid arguments.
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 * Test execution of the main method with a valid command and valid arguments but invalid contents.
128 * @throws IOException
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 /*this file is known to exist but is an invalid csv file*/,
136 "-"+NcCSVExtract.COLUMN_INDEX, "0"/*could be anything*/});
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 * Exit exception used for catching system exits.
148 * <p>
149 * Copyright 2010, CSIRO Australia
150 * All rights reserved.
151 *
152 * @author Robert Bridle on 18/03/2010
153 * @version $Revision: 78 $ $Date: 2010-07-24 16:23:13 +1000 (Sat, 24 Jul 2010) $
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 * Security manager used for stopping system exits from stopping the JVM.
167 * <p>
168 * Copyright 2010, CSIRO Australia
169 * All rights reserved.
170 *
171 * @author Robert Bridle on 18/03/2010
172 * @version $Revision: 78 $ $Date: 2010-07-24 16:23:13 +1000 (Sat, 24 Jul 2010) $
173 */
174 private static class NoExitSecurityManager extends SecurityManager {
175 @Override
176 public void checkPermission(Permission perm) {
177 // allow anything.
178 }
179
180 @Override
181 public void checkPermission(Permission perm, Object context) {
182 // allow anything.
183 }
184
185 @Override
186 public void checkExit(int status) {
187 super.checkExit(status);
188 throw new ExitException(status);
189 }
190 }
191
192 /**
193 * Makes a dummy csv file with invalid contents, i.e. empty
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 // Catch exception if any
206 e.printStackTrace();
207 fail("Failed trying to create a dummy csv file for testing the extract command.");
208 }
209 }
210 }