001: /*
002: * WbIncludeTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.wbcommands;
013:
014: import java.io.File;
015: import java.io.Writer;
016: import java.sql.ResultSet;
017: import java.sql.Statement;
018: import java.util.ArrayList;
019: import java.util.List;
020: import junit.framework.*;
021: import workbench.TestUtil;
022: import workbench.WbTestCase;
023: import workbench.db.ConnectionMgr;
024: import workbench.db.WbConnection;
025: import workbench.sql.DefaultStatementRunner;
026: import workbench.sql.StatementRunnerResult;
027: import workbench.util.EncodingUtil;
028:
029: /**
030: *
031: * @author support@sql-workbench.net
032: */
033: public class WbIncludeTest extends WbTestCase {
034: private TestUtil util;
035: private DefaultStatementRunner runner;
036:
037: public WbIncludeTest(String testName) {
038: super (testName);
039: util = getTestUtil(testName);
040: }
041:
042: public void setUp() throws Exception {
043: super .setUp();
044: util.emptyBaseDirectory();
045: runner = util.createConnectedStatementRunner();
046: }
047:
048: public void testAlternateInclude() {
049: try {
050: WbConnection con = runner.getConnection();
051:
052: Statement stmt = con.createStatement();
053: stmt
054: .execute("create table include_test (file_name varchar(100))");
055: con.commit();
056:
057: String encoding = "ISO-8859-1";
058: File scriptFile = new File(util.getBaseDir(), "test.sql");
059:
060: Writer w = EncodingUtil.createWriter(scriptFile, encoding,
061: false);
062: w.write("insert into include_test (file_name) values ('"
063: + scriptFile.getAbsolutePath() + "');\n");
064: w.write("commit;\n");
065: w.close();
066:
067: String sql = "-- comment\n\n@test.sql\n";
068: runner.runStatement(sql, -1, -1);
069: StatementRunnerResult result = runner.getResult();
070: assertEquals("Statement not executed", true, result
071: .isSuccess());
072:
073: ResultSet rs = stmt
074: .executeQuery("select count(*) from include_test");
075:
076: if (rs.next()) {
077: int count = rs.getInt(1);
078: assertEquals("Rows not inserted", 1, count);
079: } else {
080: fail("Select failed");
081: }
082: rs.close();
083: } catch (Exception e) {
084: e.printStackTrace();
085: fail(e.getMessage());
086: } finally {
087: ConnectionMgr.getInstance().disconnectAll();
088: }
089: }
090:
091: public void testNestedInclude() throws Exception {
092: try {
093: WbConnection con = runner.getConnection();
094:
095: File subdir1 = new File(util.getBaseDir(), "subdir1");
096: subdir1.mkdir();
097:
098: File include1 = new File(subdir1, "include1.sql");
099:
100: Statement stmt = con.createStatement();
101: stmt
102: .execute("create table include_test (file_name varchar(100))");
103: con.commit();
104:
105: String encoding = "ISO-8859-1";
106: Writer w = EncodingUtil.createWriter(include1, encoding,
107: false);
108: w.write("insert into include_test (file_name) values ('"
109: + include1.getAbsolutePath() + "');\n");
110: w.write("commit;\n");
111: w.close();
112:
113: File main = new File(util.getBaseDir(), "main.sql");
114: w = EncodingUtil.createWriter(main, encoding, false);
115: w.write("insert into include_test (file_name) values ('"
116: + main.getAbsolutePath() + "');\n");
117: w.write("commit;\n");
118: w.write("@./" + subdir1.getName() + "/"
119: + include1.getName() + "\n");
120: w.close();
121:
122: runner.runStatement("wbinclude -file='"
123: + main.getAbsolutePath() + "';\n", -1, -1);
124: StatementRunnerResult result = runner.getResult();
125: assertEquals("Runner not successful", true, result
126: .isSuccess());
127:
128: ResultSet rs = stmt
129: .executeQuery("select * from include_test");
130: List files = new ArrayList();
131: while (rs.next()) {
132: files.add(rs.getString(1));
133: }
134: rs.close();
135: assertEquals("Not enough values retrieved", 2, files.size());
136: assertEquals("Main file not run", true, files.contains(main
137: .getAbsolutePath()));
138: assertEquals("Second file not run", true, files
139: .contains(include1.getAbsolutePath()));
140: stmt.close();
141:
142: } catch (Exception e) {
143: e.printStackTrace();
144: fail(e.getMessage());
145: } finally {
146: ConnectionMgr.getInstance().disconnectAll();
147: }
148: }
149:
150: }
|