01: /*
02: * WbManagerTest.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench;
13:
14: import java.io.File;
15: import java.io.PrintWriter;
16: import java.sql.ResultSet;
17: import java.sql.Statement;
18: import junit.framework.TestCase;
19: import workbench.db.ConnectionMgr;
20: import workbench.db.WbConnection;
21: import workbench.log.LogMgr;
22: import workbench.util.EncodingUtil;
23: import workbench.util.WbFile;
24:
25: /**
26: *
27: * @author support@sql-workbench.net
28: */
29: public class WbManagerTest extends TestCase {
30:
31: public WbManagerTest(String testName) {
32: super (testName);
33: }
34:
35: private static final String UMLAUTS = "\u00f6\u00e4\u00fc";
36:
37: private String createScript(String basedir) {
38: File f = new File(basedir, "batch_script.sql");
39: PrintWriter w = null;
40: try {
41: w = new PrintWriter(EncodingUtil.createWriter(f, "UTF8",
42: false));
43: w
44: .println("create table batch_test (nr integer, name varchar(100));\n");
45: w.println("insert into batch_test (nr, name) values (1, '"
46: + UMLAUTS + "');\n");
47: w.println("commit;\n");
48: w.close();
49:
50: } catch (Exception e) {
51: e.printStackTrace();
52: }
53: return f.getAbsolutePath();
54: }
55:
56: public void testBatchMode() {
57: TestUtil util = new TestUtil(getName());
58: WbFile logfile = new WbFile(util.getBaseDir(),
59: "junit_wb_test.log");
60: try {
61: System.setProperty("workbench.system.doexit", "false");
62:
63: File db = new File(util.getBaseDir(), getName());
64: String script = createScript(util.getBaseDir());
65: String args[] = { "-embedded ", "-nosettings ",
66: "-configdir=" + util.getBaseDir(),
67: "-url='jdbc:h2:" + db.getAbsolutePath() + "'",
68: "-user=sa",
69: "-logfile='" + logfile.getFullPath() + "'",
70: "-driver=org.h2.Driver ",
71: "-script='" + script + "'", "-encoding=UTF8" };
72: WbManager.main(args);
73: WbConnection con = util.getConnection(db);
74: Statement stmt = con.createStatement();
75: ResultSet rs = stmt
76: .executeQuery("select nr, name from batch_test");
77: if (rs.next()) {
78: int nr = rs.getInt(1);
79: String name = rs.getString(2);
80: assertEquals("Wrong id retrieved", 1, nr);
81: assertEquals("Wrong name retrieved", UMLAUTS, name);
82: }
83: rs.close();
84: stmt.close();
85: assertTrue(logfile.exists());
86: } catch (Exception e) {
87: e.printStackTrace();
88: fail(e.getMessage());
89: } finally {
90: ConnectionMgr.getInstance().disconnectAll();
91: LogMgr.shutdown();
92: logfile.delete();
93: }
94: }
95:
96: }
|