01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.unit;
07:
08: import java.io.ByteArrayOutputStream;
09: import java.io.PrintStream;
10: import java.lang.reflect.Method;
11:
12: import org.h2.test.TestBase;
13: import org.h2.tools.DeleteDbFiles;
14: import org.h2.util.StringUtils;
15:
16: /**
17: * Tests the sample apps.
18: */
19: public class TestSampleApps extends TestBase {
20:
21: public void test() throws Exception {
22: testApp(org.h2.samples.Compact.class, null,
23: "Compacting...\nDone.");
24: testApp(org.h2.samples.CsvSample.class, null,
25: "NAME: Bob Meier\n" + "EMAIL: bob.meier@abcde.abc\n"
26: + "PHONE: +41123456789\n\n"
27: + "NAME: John Jones\n"
28: + "EMAIL: john.jones@abcde.abc\n"
29: + "PHONE: +41976543210\n");
30: testApp(
31: org.h2.samples.Function.class,
32: null,
33: "2 is prime\n3 is prime\n5 is prime\n7 is prime\n11 is prime\n13 is prime\n17 is prime\n19 is prime");
34: testApp(org.h2.samples.SecurePassword.class, null, "Joe");
35: // TODO test ShowProgress (percent numbers are hardware specific)
36: // TODO test ShutdownServer (server needs to be started in a separate
37: // process)
38: testApp(org.h2.samples.TriggerSample.class, null,
39: "The sum is 20.00");
40:
41: // tools
42: testApp(
43: org.h2.tools.ChangePassword.class,
44: new String[] { "-?" },
45: "java org.h2.tools.ChangePassword [-dir <dir>] "
46: + "[-db <database>] [-cipher <cipher>] [-decrypt <pwd>] [-encrypt <pwd>] [-quiet]\n"
47: + "See also http://h2database.com/javadoc/org/h2/tools/ChangePassword.html");
48: testApp(
49: org.h2.tools.ChangePassword.class,
50: null,
51: "java org.h2.tools.ChangePassword [-dir <dir>] "
52: + "[-db <database>] [-cipher <cipher>] [-decrypt <pwd>] [-encrypt <pwd>] [-quiet]\n"
53: + "See also http://h2database.com/javadoc/org/h2/tools/ChangePassword.html");
54: testApp(
55: org.h2.tools.DeleteDbFiles.class,
56: new String[] { "-?" },
57: "java org.h2.tools.DeleteDbFiles [-dir <dir>] [-db <database>] [-quiet]\n"
58: + "See also http://h2database.com/javadoc/org/h2/tools/DeleteDbFiles.html");
59: }
60:
61: private void testApp(Class clazz, String[] args, String expected)
62: throws Exception {
63: DeleteDbFiles.execute("data", "test", true);
64: Method m = clazz.getMethod("main",
65: new Class[] { String[].class });
66: PrintStream oldOut = System.out, oldErr = System.err;
67: ByteArrayOutputStream buff = new ByteArrayOutputStream();
68: PrintStream out = new PrintStream(buff, false, "UTF-8");
69: System.setOut(out);
70: System.setErr(out);
71: try {
72: m.invoke(null, new Object[] { args });
73: } catch (Throwable e) {
74: TestBase.logError("error", e);
75: }
76: out.flush();
77: System.setOut(oldOut);
78: System.setErr(oldErr);
79: String s = new String(buff.toByteArray(), "UTF-8");
80: s = StringUtils.replaceAll(s, "\r\n", "\n");
81: check(s.trim(), expected.trim());
82: }
83: }
|