001: /*
002: * VariablePoolTest.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;
013:
014: import java.io.File;
015: import java.io.FileWriter;
016: import java.io.PrintWriter;
017: import java.util.Set;
018: import junit.framework.TestCase;
019: import workbench.AppArguments;
020: import workbench.TestUtil;
021: import workbench.TestUtil;
022: import workbench.util.ArgumentParser;
023:
024: /**
025: *
026: * @author support@sql-workbench.net
027: */
028: public class VariablePoolTest extends TestCase {
029:
030: public VariablePoolTest(String testName) {
031: super (testName);
032: }
033:
034: public void testInitFromCommandLine() {
035: try {
036: TestUtil util = new TestUtil(this .getName());
037:
038: ArgumentParser p = new ArgumentParser();
039: p.addArgument(AppArguments.ARG_VARDEF);
040: p.parse("-" + AppArguments.ARG_VARDEF
041: + "='#exportfile=/user/home/test.txt'");
042: VariablePool pool = VariablePool.getInstance();
043: pool.readDefinition(p.getValue(AppArguments.ARG_VARDEF));
044: assertEquals("Wrong parameter retrieved from commandline",
045: "/user/home/test.txt", pool
046: .getParameterValue("exportfile"));
047:
048: File f = new File(util.getBaseDir(), "vars.properties");
049:
050: FileWriter out = new FileWriter(f);
051: out.write("exportfile=/user/home/export.txt\n");
052: out.write("exporttable=person\n");
053: out.close();
054:
055: pool.clear();
056: p.parse("-" + AppArguments.ARG_VARDEF + "='"
057: + f.getAbsolutePath() + "'");
058: pool.readDefinition(p.getValue(AppArguments.ARG_VARDEF));
059: assertEquals("Wrong parameter retrieved from file",
060: "/user/home/export.txt", pool
061: .getParameterValue("exportfile"));
062: assertEquals("Wrong parameter retrieved from file",
063: "person", pool.getParameterValue("exporttable"));
064: } catch (Exception e) {
065: e.printStackTrace();
066: fail(e.getMessage());
067: }
068: }
069:
070: public void testInitFromProperties() {
071: try {
072: System.setProperty(VariablePool.PROP_PREFIX + "testvalue",
073: "value1");
074: System.setProperty(VariablePool.PROP_PREFIX + "myprop",
075: "value2");
076: System.setProperty("someprop.testvalue", "value2");
077: VariablePool pool = VariablePool.getInstance();
078:
079: pool.initFromProperties(System.getProperties());
080: assertEquals("Wrong firstvalue", "value1", pool
081: .getParameterValue("testvalue"));
082: assertEquals("Wrong firstvalue", "value2", pool
083: .getParameterValue("myprop"));
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail(e.getMessage());
087: }
088: }
089:
090: public void testPool() throws Exception {
091: TestUtil util = new TestUtil("testPool");
092: util.prepareEnvironment();
093:
094: VariablePool pool = VariablePool.getInstance();
095:
096: pool.setParameterValue("id", "42");
097:
098: String value = pool.getParameterValue("id");
099: assertEquals("Wrong value stored", "42", value);
100:
101: String sql = "select * from test where id=$[id]";
102: String realSql = pool.replaceAllParameters(sql);
103: assertEquals("Parameter not replaced",
104: "select * from test where id=42", realSql);
105:
106: sql = "select * from test where id=$[?id]";
107: boolean hasPrompt = pool.hasPrompt(sql);
108: assertEquals("Prompt not detected", true, hasPrompt);
109:
110: sql = "select * from test where id=$[&id]";
111: Set vars = pool.getVariablesNeedingPrompt(sql);
112: assertEquals("Prompt not detected", 0, vars.size());
113:
114: pool.removeValue("id");
115: vars = pool.getVariablesNeedingPrompt(sql);
116: assertEquals("Prompt not detected", 1, vars.size());
117: assertEquals("Variable not in prompt pool", true, vars
118: .contains("id"));
119:
120: File f = new File(util.getBaseDir(), "vardef.props");
121:
122: PrintWriter pw = new PrintWriter(new FileWriter(f));
123: pw.println("lastname=Dent");
124: pw.println("firstname=Arthur");
125: pw.close();
126: pool.readFromFile(f.getAbsolutePath(), null);
127:
128: value = pool.getParameterValue("lastname");
129: assertEquals("Lastname not defined", "Dent", value);
130: value = pool.getParameterValue("firstname");
131: assertEquals("Firstname not defined", "Arthur", value);
132:
133: f.delete();
134: }
135: }
|