1
2
3
4 package org.melati.app.test;
5
6 import java.io.BufferedReader;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.InputStreamReader;
10
11 import org.melati.app.PoemApp;
12 import org.melati.app.UnhandledExceptionException;
13 import org.melati.util.UnexpectedExceptionException;
14 import org.melati.util.test.StringInputStream;
15
16 import junit.framework.TestCase;
17
18
19
20
21
22 public class PoemAppTest extends TestCase {
23
24
25
26
27 public PoemAppTest(String name) {
28 super(name);
29 }
30
31
32
33
34
35 protected void setUp() throws Exception {
36 super.setUp();
37 }
38
39
40
41
42
43 protected void tearDown() throws Exception {
44 super.tearDown();
45 }
46
47
48
49
50 public void testRun() throws Exception {
51 String fileName = "ttt.tmp";
52 String[] args = { "appjunit", "user", "1", "display", "-o", fileName };
53 PoemApp.main(args);
54 String output = "";
55 File fileIn = new File(fileName);
56 BufferedReader in = new BufferedReader(
57 new InputStreamReader(
58 new FileInputStream(fileIn)));
59 while (in.ready()) {
60 output += in.readLine();
61 output += "\n";
62 }
63 in.close();
64 fileIn.delete();
65 assertEquals("You are : _guest_\n" +
66 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
67 "Your Table was : user (from the data structure definition)\n" +
68 "Your Troid was : 1\n" +
69 "Your Method was : display\n" +
70 "System Users\n" +
71 "============\n" +
72 " Melati guest user\n"+
73 " Melati database administrator\n" , output);
74 }
75
76
77
78
79 public void testRunNoMethod() throws Exception {
80 String fileName = "ttt.tmp";
81 String[] args = { "appjunit", "user", "1", "-o", fileName };
82 PoemApp.main(args);
83 String output = "";
84 File fileIn = new File(fileName);
85 BufferedReader in = new BufferedReader(
86 new InputStreamReader(
87 new FileInputStream(fileIn)));
88 while (in.ready()) {
89 output += in.readLine();
90 output += "\n";
91 }
92 in.close();
93 fileIn.delete();
94 assertEquals("You are : _guest_\n" +
95 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
96 "Your Table was : user (from the data structure definition)\n" +
97 "Your Troid was : 1\n" +
98 "Your Method was : null\n" +
99 "System Users\n" +
100 "============\n" +
101 " Melati guest user\n"+
102 " Melati database administrator\n" , output);
103 }
104
105
106
107 public void testRunTableMethod() throws Exception {
108 String fileName = "ttt.tmp";
109 String[] args = { "appjunit", "user", "method", "-o", fileName };
110 PoemApp.main(args);
111 String output = "";
112 File fileIn = new File(fileName);
113 BufferedReader in = new BufferedReader(
114 new InputStreamReader(
115 new FileInputStream(fileIn)));
116 while (in.ready()) {
117 output += in.readLine();
118 output += "\n";
119 }
120 in.close();
121 fileIn.delete();
122 assertEquals("You are : _guest_\n" +
123 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
124 "Your Table was : user (from the data structure definition)\n" +
125 "Your Troid was : null\n" +
126 "Your Method was : method\n" +
127 "System Users\n" +
128 "============\n" +
129 " Melati guest user\n"+
130 " Melati database administrator\n" , output);
131 }
132
133
134
135
136 public void testRunNoTroid() throws Exception {
137 String fileName = "ttt.tmp";
138 String[] args = { "appjunit", "user", "-o", fileName };
139 PoemApp.main(args);
140 String output = "";
141 File fileIn = new File(fileName);
142 BufferedReader in = new BufferedReader(
143 new InputStreamReader(
144 new FileInputStream(fileIn)));
145 while (in.ready()) {
146 output += in.readLine();
147 output += "\n";
148 }
149 in.close();
150 fileIn.delete();
151 assertEquals("You are : _guest_\n" +
152 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
153 "Your Table was : null\n" +
154 "Your Troid was : null\n" +
155 "Your Method was : user\n" +
156 "System Users\n" +
157 "============\n" +
158 " Melati guest user\n"+
159 " Melati database administrator\n" , output);
160 }
161
162
163
164 public void testRunNoTable() throws Exception {
165 String fileName = "ttt.tmp";
166 String[] args = { "appjunit", "-o", fileName };
167 PoemApp.main(args);
168 String output = "";
169 File fileIn = new File(fileName);
170 BufferedReader in = new BufferedReader(
171 new InputStreamReader(
172 new FileInputStream(fileIn)));
173 while (in.ready()) {
174 output += in.readLine();
175 output += "\n";
176 }
177 in.close();
178 fileIn.delete();
179 assertEquals("You are : _guest_\n" +
180 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
181 "Your Table was : null\n" +
182 "Your Troid was : null\n" +
183 "Your Method was : null\n" +
184 "System Users\n" +
185 "============\n" +
186 " Melati guest user\n"+
187 " Melati database administrator\n" , output);
188 }
189
190
191
192
193
194
195 public void testArgumentHandling() throws Exception {
196 String fileName = "ttt.tmp";
197 String[] args = { "appjunit", "user", "one", "display", "-o", fileName };
198 try {
199 PoemApp.main(args);
200 fail("Should have blown up");
201 } catch (UnexpectedExceptionException e) {
202 assertEquals("An exception occurred in a context where it was very unexpected:\n" +
203 "Arguments `user one display' have wrong form:\n" +
204 "java.lang.NumberFormatException: For input string: \"one\"",e.getMessage());
205 e = null;
206 }
207 File fileIn = new File(fileName);
208 fileIn.delete();
209 }
210
211
212
213
214
215 public void testPrePoemSessionThowing() throws Exception {
216 String fileName = "ttt.tmp";
217 String[] args = { "appjunit", "user", "1", "display", "-o", fileName };
218 try {
219 ThrowingPoemApp.main(args);
220 fail("Should have blown up");
221 } catch (RuntimeException e) {
222 assertEquals("An exception occurred in a context where it was very unexpected:\n" +
223 "Bang!",e.getMessage());
224 e = null;
225 }
226 File fileIn = new File(fileName);
227 fileIn.delete();
228 }
229
230
231
232
233 public void testAccess() throws Exception {
234 String fileName = "ttt.tmp";
235 String[] args = { "user", "1", "display", "-o", fileName };
236 ProtectedPoemApp it = new ProtectedPoemApp();
237 it.setInput(new StringInputStream("_administrator_\nFIXME\n"));
238 it.run(args);
239 String output = "";
240 File fileIn = new File(fileName);
241 BufferedReader in = new BufferedReader(
242 new InputStreamReader(
243 new FileInputStream(fileIn)));
244 while (in.ready()) {
245 output += in.readLine();
246 output += "\n";
247 }
248 in.close();
249 fileIn.delete();
250 assertEquals("You are : _administrator_\n" +
251 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
252 "Your Table was : user (from the data structure definition)\n" +
253 "Your Troid was : 1\n" +
254 "Your Method was : display\n" +
255 "System Users\n" +
256 "============\n" +
257 " Melati guest user\n"+
258 " Melati database administrator\n" , output);
259 }
260
261
262
263
264 public void testAccessPrompted() throws Exception {
265 String fileName = "ttt.tmp";
266 String[] args = { "user", "1", "display", "-o", fileName };
267 ProtectedPoemApp it = new ProtectedPoemApp();
268 it.setInput(new StringInputStream("bad\nwrong\nbad\nwrong\n_administrator_\nFIXME\n"));
269 it.run(args);
270 String output = "";
271 File fileIn = new File(fileName);
272 BufferedReader in = new BufferedReader(
273 new InputStreamReader(
274 new FileInputStream(fileIn)));
275 while (in.ready()) {
276 output += in.readLine();
277 output += "\n";
278 }
279 in.close();
280 fileIn.delete();
281 assertEquals("You are : _administrator_\n" +
282 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
283 "Your Table was : user (from the data structure definition)\n" +
284 "Your Troid was : 1\n" +
285 "Your Method was : display\n" +
286 "System Users\n" +
287 "============\n" +
288 " Melati guest user\n"+
289 " Melati database administrator\n" , output);
290 }
291
292
293
294 public void testAccessPromptedThreeWrongResponses() throws Exception {
295 String fileName = "ttt.tmp";
296 String[] args = { "user", "1", "display", "-o", fileName };
297 ProtectedPoemApp it = new ProtectedPoemApp();
298 it.setInput(new StringInputStream("bad\nwrong\nbad\nwrong\nbad\nwrong\n_administrator_\nFIXME\n"));
299 try {
300 it.run(args);
301 fail("Should have bombed");
302 } catch (UnhandledExceptionException e) {
303 assertEquals("You need the capability _administer_ to write the object tableInfo/0 but your access token _guest_ doesn\'t confer it"
304 , e.subException.getMessage());
305 e = null;
306 }
307 File fileIn = new File(fileName);
308 fileIn.delete();
309 }
310
311
312
313 public void testAccessWrongUsernameInArgs() throws Exception {
314 String fileName = "ttt.tmp";
315 String[] args = { "user", "1", "display", "-o", fileName,
316 "-u", "unk", "-p","unk" };
317 ProtectedPoemApp it = new ProtectedPoemApp();
318 try {
319 it.run(args);
320 fail("Should have bombed");
321 } catch (UnhandledExceptionException e) {
322 assertEquals("You need the capability _administer_ to write the object tableInfo/0 but your access token _guest_ doesn\'t confer it"
323 , e.subException.getMessage());
324 e = null;
325 }
326 File fileIn = new File(fileName);
327 fileIn.delete();
328 }
329
330
331
332
333 public void testAccessWrongPasswordInArgs() throws Exception {
334 String fileName = "ttt.tmp";
335 String[] args = { "user", "1", "display", "-o", fileName,
336 "-u", "_administrator_", "-p","unk" };
337 ProtectedPoemApp it = new ProtectedPoemApp();
338 try {
339 it.run(args);
340 fail("Should have bombed");
341 } catch (UnhandledExceptionException e) {
342 assertEquals("You need the capability _administer_ to write the object tableInfo/0 but your access token _guest_ doesn\'t confer it"
343 , e.subException.getMessage());
344 e = null;
345 }
346 File fileIn = new File(fileName);
347 fileIn.delete();
348 }
349
350
351
352
353 public void testArgumentNames() throws Exception {
354 String fileName = "ttt.tmp";
355 String[] args = { "user", "1", "display", "-o", fileName,
356 "-u", "_administrator_", "-p", "FIXME", };
357 ProtectedPoemApp it = new ProtectedPoemApp();
358 it.run(args);
359 String output = "";
360 File fileIn = new File(fileName);
361 BufferedReader in = new BufferedReader(
362 new InputStreamReader(
363 new FileInputStream(fileIn)));
364 while (in.ready()) {
365 output += in.readLine();
366 output += "\n";
367 }
368 in.close();
369 fileIn.delete();
370 assertEquals("You are : _administrator_\n" +
371 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
372 "Your Table was : user (from the data structure definition)\n" +
373 "Your Troid was : 1\n" +
374 "Your Method was : display\n" +
375 "System Users\n" +
376 "============\n" +
377 " Melati guest user\n"+
378 " Melati database administrator\n" , output);
379 }
380
381
382
383
384 public void testArgumentNames2() throws Exception {
385 String fileName = "ttt.tmp";
386 String[] args = { "user", "1", "display",
387 "-o", fileName, "-user", "_administrator_", "-pass", "FIXME", };
388 ProtectedPoemApp.main(args);
389 String output = "";
390 File fileIn = new File(fileName);
391 BufferedReader in = new BufferedReader(
392 new InputStreamReader(
393 new FileInputStream(fileIn)));
394 while (in.ready()) {
395 output += in.readLine();
396 output += "\n";
397 }
398 in.close();
399 fileIn.delete();
400 assertEquals("You are : _administrator_\n" +
401 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
402 "Your Table was : user (from the data structure definition)\n" +
403 "Your Troid was : 1\n" +
404 "Your Method was : display\n" +
405 "System Users\n" +
406 "============\n" +
407 " Melati guest user\n"+
408 " Melati database administrator\n" , output);
409 }
410
411
412
413 public void testArgumentNames3() throws Exception {
414 String fileName = "ttt.tmp";
415 String[] args = { "user", "1", "display",
416 "-o", fileName, "-username", "_administrator_", "-password", "FIXME", };
417 ProtectedPoemApp.main(args);
418 String output = "";
419 File fileIn = new File(fileName);
420 BufferedReader in = new BufferedReader(
421 new InputStreamReader(
422 new FileInputStream(fileIn)));
423 while (in.ready()) {
424 output += in.readLine();
425 output += "\n";
426 }
427 in.close();
428 fileIn.delete();
429 assertEquals("You are : _administrator_\n" +
430 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
431 "Your Table was : user (from the data structure definition)\n" +
432 "Your Troid was : 1\n" +
433 "Your Method was : display\n" +
434 "System Users\n" +
435 "============\n" +
436 " Melati guest user\n"+
437 " Melati database administrator\n" , output);
438 }
439
440
441
442
443 public void testArgumentNames4() throws Exception {
444 String fileName = "ttt.tmp";
445 String[] args = { "user", "1", "display",
446 "-o", fileName, "--username", "_administrator_", "--password", "FIXME", };
447 ProtectedPoemApp.main(args);
448 String output = "";
449 File fileIn = new File(fileName);
450 BufferedReader in = new BufferedReader(
451 new InputStreamReader(
452 new FileInputStream(fileIn)));
453 while (in.ready()) {
454 output += in.readLine();
455 output += "\n";
456 }
457 in.close();
458 fileIn.delete();
459 assertEquals("You are : _administrator_\n" +
460 "Your Database was: jdbc:hsqldb:mem:appjunit\n" +
461 "Your Table was : user (from the data structure definition)\n" +
462 "Your Troid was : 1\n" +
463 "Your Method was : display\n" +
464 "System Users\n" +
465 "============\n" +
466 " Melati guest user\n"+
467 " Melati database administrator\n" , output);
468 }
469
470
471 }
472