1 /*------------------------------------------------------------------------------
  2 Name:      TestJdbcAccess.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Test JDBC plugin
  6 ------------------------------------------------------------------------------*/
  7 package org.xmlBlaster.test.jdbc;
  8 
  9 import java.util.logging.Logger;
 10 import java.util.logging.Level;
 11 import org.xmlBlaster.util.Global;
 12 import org.xmlBlaster.util.XmlBlasterException;
 13 import org.xmlBlaster.client.qos.ConnectQos;
 14 import org.xmlBlaster.client.qos.DisconnectQos;
 15 import org.xmlBlaster.client.I_XmlBlasterAccess;
 16 import org.xmlBlaster.client.key.GetKey;
 17 import org.xmlBlaster.client.qos.GetQos;
 18 import org.xmlBlaster.client.XmlDbMessageWrapper;
 19 import org.xmlBlaster.util.MsgUnit;
 20 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 21 import org.xmlBlaster.test.Util;
 22 
 23 import junit.framework.*;
 24 
 25 import java.util.Vector;
 26 
 27 
 28 /**
 29  * This client tests the <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/engine.service.rdbms.html">
 30  * JDBC plugin framework</a> with get() invocations. 
 31  * <p />
 32  * We start our own xmlBlaster server in a thread.
 33  * This client may be invoked multiple time on the same xmlBlaster server,
 34  * as it cleans up everything after his tests are done.
 35  * <p>
 36  * Invoke examples:<br />
 37  * <pre>
 38  *    java junit.textui.TestRunner org.xmlBlaster.test.jdbc.TestJdbcAccess
 39  *    java junit.swingui.TestRunner -noloading org.xmlBlaster.test.jdbc.TestJdbcAccess
 40  * </pre>
 41  */
 42 public class TestJdbcAccess extends TestCase
 43 {
 44    private static String ME = "TestJdbcAccess";
 45    private final Global glob;
 46    private static Logger log = Logger.getLogger(TestJdbcAccess.class.getName());
 47 
 48    private I_XmlBlasterAccess con = null;
 49    private String name;
 50    private String passwd = "secret";
 51    private EmbeddedXmlBlaster serverThread;
 52    private int serverPort = 7615;
 53 
 54    private XmlDbMessageWrapper wrap = null;
 55 
 56    /**
 57     * Constructs the TestJdbcAccess object. 
 58     * <p />
 59     * @param testName   The name used in the test suite
 60     */
 61    public TestJdbcAccess(String testName) {
 62       super(testName);
 63       this.glob = Global.instance();
 64 
 65       this.name = testName; // name to login to xmlBlaster
 66    }
 67 
 68    /**
 69     * Sets up the fixture.
 70     * <p />
 71     * We start an own xmlBlaster server in a separate thread,
 72     * it is configured to load the tinySQL JDBC driver to test SQL access (with dBase files)
 73     * <p />
 74     * Then we connect as a client
 75     */
 76    protected void setUp()
 77    {
 78       // We register here the demo plugin with xmlBlaster server, supplying an argument to the plugin
 79       Vector argsVec = Util.getOtherServerPortVec(serverPort);
 80       String tmp = glob.getProperty().get("JdbcDriver.drivers", (String)null);
 81       if (tmp == null || tmp.indexOf("ORG.as220.tinySQL.dbfFileDriver") < 0) {
 82          argsVec.add("-JdbcDriver.drivers");
 83          argsVec.add("ORG.as220.tinySQL.dbfFileDriver");
 84       }
 85       glob.init((String[])argsVec.toArray(new String[argsVec.size()]));
 86 
 87       serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 88       log.info("XmlBlaster is ready for testing JDBC access");
 89 
 90       try {
 91          log.info("Connecting ...");
 92          con = glob.getXmlBlasterAccess();
 93          ConnectQos qos = new ConnectQos(glob, name, passwd);
 94          con.connect(qos, null); // Login to xmlBlaster
 95       }
 96       catch (Exception e) {
 97          Thread.currentThread().dumpStack();
 98          log.severe("Can't connect to xmlBlaster: " + e.toString());
 99       }
100 
101       wrap = new XmlDbMessageWrapper(glob, "joe", "secret", "jdbc:dbfFile:.");
102       wrap.initUpdate(true, "DROP TABLE IF EXISTS cars");
103       String result = invokeSyncQuery(wrap, 1, null);
104    }
105 
106    /**
107     * Tears down the fixture.
108     * <p />
109     * cleaning up .... erase() the previous message OID and logout
110     */
111    protected void tearDown()
112    {
113       if (wrap != null) {
114          wrap.initUpdate(true, "DROP TABLE IF EXISTS cars");
115          String result = invokeSyncQuery(wrap, 1, null);
116          wrap = null;
117       }
118 
119       con.disconnect(null);
120       con=null;
121 
122       try { Thread.sleep(100L); } catch( InterruptedException i) {}
123       EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
124       this.serverThread = null;
125 
126 
127       // reset to default server port (necessary if other tests follow in the same JVM).
128       Util.resetPorts();
129    }
130 
131    /**
132     * Create a RDBMS table, fill some data and destroy it again. 
133     * We use the tinySQL dBase JDBC driver for testing.
134     */
135    public void testQueries()
136    {
137       log.info("######## Start testQueries()");
138       String request = "CREATE TABLE cars (name CHAR(25), id NUMERIC(4,0))";
139       log.info("*** REQUEST=" + request);
140       wrap.initUpdate(true, request);
141       String result = invokeSyncQuery(wrap, 1, null);
142    
143       String[] brands = { "Fiat", "Audi", "BMW", "Porsche", "Mercedes", "Renault", "Citroen" };
144       for (int ii=0; ii<brands.length; ii++) {
145          request = "INSERT INTO cars (name, id) VALUES('" + brands[ii] + "', " + (ii+1) + ")";
146          log.info("*** REQUEST=" + request);
147          wrap.initUpdate(true, request);
148          result = invokeSyncQuery(wrap, 1, null);
149       }
150 
151       request = "SELECT * from cars";
152       log.info("*** REQUEST=" + request);
153       wrap.initQuery(100, true, request);
154       result = invokeSyncQuery(wrap, 1, "BMW");
155       log.info("Successful retrieved cars, dump ommitted to not disturb JUNIT test report generation");
156       log.fine("Retrieved cars:\n" + result);
157    }
158 
159    /**
160     * get() blocks until the SQL query is finished ...
161     */
162    private String invokeSyncQuery(XmlDbMessageWrapper wrap, int numResultRowsExpected, String token) {
163       try {
164          if (log.isLoggable(Level.FINE)) log.fine("Sending command string:\n" + wrap.toXml()); // Junit report does not like it
165          GetKey key = new GetKey(glob, "__sys__jdbc");
166          key.wrap(wrap.toXml());
167          GetQos qos = new GetQos(glob);
168          MsgUnit[] msgUnitArr = con.get(key.toXml(), qos.toXml());
169          if (msgUnitArr.length > 0) {
170             String result = new String(msgUnitArr[0].getContent());
171             if (log.isLoggable(Level.FINE)) log.fine(result);
172             if (token != null && result.indexOf(token) < 0)
173                fail("Token " + token + " not found in result");
174          }
175          else {
176             log.info("No results for your query");
177          }
178          assertEquals("Wrong number of results", numResultRowsExpected, msgUnitArr.length);
179          return new String(msgUnitArr[0].getContent());
180       }
181       catch (Exception e) { 
182          fail("Query failed: " + e.toString());
183          return "";
184       }
185    }
186 
187    /**
188     * Invoke: java org.xmlBlaster.test.jdbc.TestJdbcAccess
189     * @deprecated Use the TestRunner from the testsuite to run it
190     */
191    public static void main(String args[]) {
192       Global glob = Global.instance();
193       if (glob.init(args) != 0) {
194          System.err.println("Init failed");
195          System.exit(1);
196       }
197       TestJdbcAccess test = new TestJdbcAccess("TestJdbcAccess");
198       test.setUp();
199       test.testQueries();
200       test.tearDown();
201    }
202 }


syntax highlighted by Code2HTML, v. 0.9.1