1 5 package org.h2.test.unit; 6 7 import java.io.ByteArrayOutputStream ; 8 import java.io.PrintStream ; 9 import java.lang.reflect.Method ; 10 11 import org.h2.test.TestBase; 12 import org.h2.tools.DeleteDbFiles; 13 import org.h2.util.StringUtils; 14 15 public class TestSampleApps extends TestBase { 16 17 public void test() throws Exception { 18 testApp(org.h2.samples.Compact.class, null, "Compacting...\nDone."); 19 testApp(org.h2.samples.CsvSample.class, null, 20 "NAME: Bob Meier\n" 21 + "EMAIL: bob.meier@abcde.fgh\n" 22 +"PHONE: +41123456789\n\n" 23 +"NAME: John Jones\n" 24 +"EMAIL: johnjones@abcde.fgh\n" 25 +"PHONE: +41976543210\n"); 26 testApp(org.h2.samples.Function.class, null, 27 "2 is prime\n3 is prime\n5 is prime\n7 is prime\n11 is prime\n13 is prime\n17 is prime\n19 is prime"); 28 testApp(org.h2.samples.SecurePassword.class, null, "Hello"); 29 testApp(org.h2.samples.TriggerSample.class, null, "The sum is 20.00"); 32 33 testApp(org.h2.tools.ChangePassword.class, new String []{"-?"}, "java org.h2.tools.ChangePassword [-dir <dir>] " 35 + "[-db <database>] [-cipher <cipher>] [-decrypt <pwd>] [-encrypt <pwd>] [-quiet]"); 36 testApp(org.h2.tools.ChangePassword.class, null, "java org.h2.tools.ChangePassword [-dir <dir>] " 37 + "[-db <database>] [-cipher <cipher>] [-decrypt <pwd>] [-encrypt <pwd>] [-quiet]"); 38 testApp(org.h2.tools.DeleteDbFiles.class, new String []{"-?"}, "java org.h2.tools.DeleteDbFiles [-dir <dir>] [-db <database>] [-quiet]"); 39 } 40 41 private void testApp(Class clazz, String [] args, String expected) throws Exception { 42 DeleteDbFiles.execute("data", "test", true); 43 Method m = clazz.getMethod("main", new Class []{String [].class}); 44 PrintStream oldOut = System.out, oldErr = System.err; 45 ByteArrayOutputStream buff = new ByteArrayOutputStream (); 46 PrintStream out = new PrintStream (buff, false, "UTF-8"); 47 System.setOut(out); 48 System.setErr(out); 49 try { 50 m.invoke(null, new Object []{args}); 51 } catch(Throwable e) { 52 TestBase.logError("error", e); 53 } 54 out.flush(); 55 System.setOut(oldOut); 56 System.setErr(oldErr); 57 String s = new String (buff.toByteArray(), "UTF-8"); 58 s = StringUtils.replaceAll(s, "\r\n", "\n"); 59 check(s.trim(), expected.trim()); 60 } 61 } 62 | Popular Tags |