001: /*
002: * WbDefineVarTest.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.FileWriter;
016: import java.io.PrintWriter;
017: import junit.framework.*;
018: import workbench.TestUtil;
019: import workbench.db.ConnectionMgr;
020: import workbench.db.WbConnection;
021: import workbench.sql.DefaultStatementRunner;
022: import workbench.sql.SqlCommand;
023: import workbench.sql.StatementRunnerResult;
024: import workbench.sql.VariablePool;
025: import workbench.util.StringUtil;
026:
027: /**
028: *
029: * @author support@sql-workbench.net
030: */
031: public class WbDefineVarTest extends TestCase {
032:
033: public WbDefineVarTest(String testName) {
034: super (testName);
035: }
036:
037: public void testExecute() throws Exception {
038: try {
039: TestUtil util = new TestUtil(getClass().getName()
040: + "_testExecute");
041: util.prepareEnvironment();
042: DefaultStatementRunner runner = util
043: .createConnectedStatementRunner();
044: WbConnection con = runner.getConnection();
045:
046: String sql = "--define some vars\nwbvardef theanswer = 42";
047: SqlCommand command = runner.getCommandToUse(sql);
048: assertTrue(command instanceof WbDefineVar);
049: runner.runStatement(sql, -1, -1);
050:
051: String varValue = VariablePool.getInstance()
052: .getParameterValue("theanswer");
053: assertEquals("Wrong variable defined", "42", varValue);
054:
055: sql = "--remove the variable\nWbVarDelete theanswer";
056: command = runner.getCommandToUse(sql);
057: assertTrue(command instanceof WbRemoveVar);
058: runner.runStatement(sql, -1, -1);
059:
060: varValue = VariablePool.getInstance().getParameterValue(
061: "theanswer");
062: assertEquals("Variable not deleted", true, StringUtil
063: .isEmptyString(varValue));
064:
065: StatementRunnerResult result = null;
066: runner.runStatement(
067: "\n\n--do it\ncreate table vartest (nr integer)",
068: -1, -1);
069: result = runner.getResult();
070: assertEquals(result.getMessageBuffer().toString(), true,
071: result.isSuccess());
072:
073: runner
074: .runStatement(
075: "-- new entry\ninsert into vartest (nr) values (7)",
076: -1, -1);
077: result = runner.getResult();
078: assertEquals(result.getMessageBuffer().toString(), true,
079: result.isSuccess());
080:
081: runner.runStatement("commit", -1, -1);
082: result = runner.getResult();
083: assertEquals(result.getMessageBuffer().toString(), true,
084: result.isSuccess());
085:
086: sql = "--define some vars\nwbvardef theanswer = @\"select nr from vartest\"";
087: runner.runStatement(sql, -1, -1);
088: varValue = VariablePool.getInstance().getParameterValue(
089: "theanswer");
090: assertEquals("SQL Variable not set", "7", varValue);
091:
092: sql = "--define some vars\nwbvardef theanswer = \"@select nr from vartest\"";
093: runner.runStatement(sql, -1, -1);
094: varValue = VariablePool.getInstance().getParameterValue(
095: "theanswer");
096: assertEquals("SQL Variable not set", "7", varValue);
097:
098: File f = new File(util.getBaseDir(), "vardef.props");
099:
100: PrintWriter pw = new PrintWriter(new FileWriter(f));
101: pw.println("lastname=Dent");
102: pw.println("firstname=Arthur");
103: pw.close();
104:
105: sql = "--define some vars\nwbvardef -file=this_will_not_exist.blafile";
106: runner.runStatement(sql, -1, -1);
107: result = runner.getResult();
108: assertEquals("Invalid file not detected", false, result
109: .isSuccess());
110:
111: sql = "--define some vars\nwbvardef -file='"
112: + f.getAbsolutePath() + "'";
113: runner.runStatement(sql, -1, -1);
114: result = runner.getResult();
115: assertEquals("File not processed", true, result.isSuccess());
116:
117: varValue = VariablePool.getInstance().getParameterValue(
118: "lastname");
119: assertEquals("SQL Variable not set", "Dent", varValue);
120:
121: sql = "-- remove a variable \n " + WbRemoveVar.VERB
122: + " lastname";
123: runner.runStatement(sql, -1, -1);
124: result = runner.getResult();
125: assertEquals("Error deleting variable", true, result
126: .isSuccess());
127:
128: varValue = VariablePool.getInstance().getParameterValue(
129: "lastname");
130: assertEquals("SQL Variable still available ", true,
131: StringUtil.isEmptyString(varValue));
132:
133: sql = "WbVardef var5=' a '";
134: runner.runStatement(sql, -1, -1);
135: varValue = VariablePool.getInstance().getParameterValue(
136: "var5");
137: result = runner.getResult();
138: assertEquals(result.getMessageBuffer().toString(), true,
139: result.isSuccess());
140: assertEquals("Quoted spaces trimmed", " a ", varValue);
141:
142: sql = "WbVardef var5=";
143: runner.runStatement(sql, -1, -1);
144: varValue = VariablePool.getInstance().getParameterValue(
145: "var5");
146: assertNull("Variable not deleted", varValue);
147: } catch (Exception e) {
148: e.printStackTrace();
149: fail("Error when defining variable");
150: } finally {
151: ConnectionMgr.getInstance().disconnectAll();
152: }
153: }
154:
155: }
|