diff --git a/bin/startdbs b/bin/startdbs index 2e0b5bd..3ae336b 100755 --- a/bin/startdbs +++ b/bin/startdbs @@ -1,3 +1,4 @@ #!/bin/bash java -cp drivers\/hsqldb.jar org.hsqldb.server.Server --database.0 file:test\/mydb --dbname.0 xdb >test\/hsqldb.log 2>&1 & -java -Dderby.system.home=test -classpath drivers\/derby.jar:drivers\/derbynet.jar:drivers\/derbytools.jar -jar drivers\/derbyrun.jar server start >test\/derby.log 2>&1 & +java -Dderby.system.home=./test -classpath drivers\/derby.jar:drivers\/derbynet.jar:drivers\/derbytools.jar -jar drivers\/derbyrun.jar server start >test\/derby.log 2>&1 & +sleep 10 diff --git a/bin/stopdbs b/bin/stopdbs index 98d03a2..a4dd5f4 100755 --- a/bin/stopdbs +++ b/bin/stopdbs @@ -1,2 +1,3 @@ #!/bin/bash java -classpath drivers/derby.jar:drivers/derbytools.jar:drivers/derbynet.jar -jar drivers/derbyrun.jar server shutdown >test\/derby.log 2>&1 & +rm -rf test/testdb diff --git a/lib/jdbc.js b/lib/jdbc.js index 719da43..7feed04 100644 --- a/lib/jdbc.js +++ b/lib/jdbc.js @@ -43,15 +43,18 @@ JDBCConn.prototype.initialize = function(config, callback) { var self = this; self._config = config; - if (self._config.properties){ - var Properties = java.import('java.util.Properties'); - var properties = new Properties(); + + var Properties = java.import('java.util.Properties'); + var properties = new Properties(); + + if (self._config.properties) { self._config.properties.forEach(function(prop){ properties.putSync(prop[0], prop[1]); }); - this._props = properties; } + this._props = properties; + if (self._config.user){ if(this._props.getPropertySync('user') === undefined){ this._props.putSync('user', self._config.user); diff --git a/package.json b/package.json index 1ff4ee3..a254271 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "description": "Node Module JDBC wrapper", "main": "index.js", "dependencies": { - "java": "~0.4.4", - "underscore": "~1.7.0", - "promise": "~6.0.1" + "java": "~0.5", + "underscore": "~1.8", + "promise": "~7.0" }, "devDependencies": { - "nodeunit": "~0.9.0" + "nodeunit": "~0.9" }, "scripts": { "pretest": "bin\/startdbs", diff --git a/test/test-derby.js b/test/test-derby.js index b02255b..26f5566 100644 --- a/test/test-derby.js +++ b/test/test-derby.js @@ -9,7 +9,7 @@ jinst.setupClasspath(['./drivers/hsqldb.jar', var config = { drivername: 'org.apache.derby.jdbc.ClientDriver', - url: 'jdbc:derby://localhost:1527/testdb' + url: 'jdbc:derby://localhost:1527/testdb;create=true' }; module.exports = { @@ -20,5 +20,87 @@ module.exports = { test.equal(drivername, 'org.apache.derby.jdbc.ClientDriver'); test.done(); }); + }, + testopen: function(test) { + derbyConn.open(function(err, conn) { + test.expect(2); + test.equal(null, err); + test.ok(conn); + test.done(); + }); + }, + testcreatetable: function(test) { + derbyConn.executeUpdate("CREATE TABLE blah (id int, name varchar(10), date DATE, time TIME, timestamp TIMESTAMP)", function(err, result) { + test.expect(1); + test.equal(null, err); + test.done(); + }); + }, + // testeqinsert: function(test) { + // derbyConn.executeQuery("INSERT INTO blah VALUES (1, 'Jason', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { + // test.expect(2); + // test.equal(null, err); + // test.ok(result); + // test.done(); + // }); + // }, + // testeuinsert: function(test) { + // derbyConn.executeUpdate("INSERT INTO blah VALUES (3, 'Temp', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { + // test.expect(2); + // test.equal(null, err); + // test.ok(result && result == 1); + // test.done(); + // }); + // }, + // testequpdate: function(test) { + // derbyConn.executeQuery("UPDATE blah SET id = 2 WHERE name = 'Jason';", function(err, result) { + // test.expect(2); + // test.equal(null, err); + // test.ok(result); + // test.done(); + // }); + // }, + // testeuupdate: function(test) { + // derbyConn.executeUpdate("UPDATE blah SET id = 4 WHERE name = 'Temp';", function(err, result) { + // test.expect(2); + // test.equal(null, err); + // test.ok(result && result == 1); + // test.done(); + // }); + // }, + // testselect: function(test) { + // derbyConn.executeQuery("SELECT * FROM blah;", function(err, result) { + // test.expect(6); + // test.equal(null, err); + // test.ok(result && result.length == 2); + // test.equal(result[0].NAME, 'Jason'); + // test.ok(result[0].DATE); + // test.ok(result[0].TIME); + // test.ok(result[0].TIMESTAMP); + // test.done(); + // }); + // }, + // testeqdelete: function(test) { + // derbyConn.executeQuery("DELETE FROM blah WHERE id = 2;", function(err, result) { + // test.expect(2); + // test.equal(null, err); + // test.ok(result); + // test.done(); + // }); + // }, + // testeudelete: function(test) { + // derbyConn.executeUpdate("DELETE FROM blah WHERE id = 4;", function(err, result) { + // test.expect(2); + // test.equal(null, err); + // test.ok(result && result == 1); + // test.done(); + // }); + // }, + testdroptable: function(test) { + derbyConn.executeUpdate("DROP TABLE blah", function(err, result) { + test.expect(1); + test.equal(null, err); + test.done(); + }); } }; diff --git a/test/test-hsqldb.js b/test/test-hsqldb.js index 07b0952..24ce972 100644 --- a/test/test-hsqldb.js +++ b/test/test-hsqldb.js @@ -1,45 +1,22 @@ var jinst = require('../lib/jinst.js'); var nodeunit = require('nodeunit'); -var jdbcConn = new ( require('../lib/jdbc.js') ), - jdbcConnWithProps = new ( require('../lib/jdbc.js') ); +var jdbcConn = new ( require('../lib/jdbc.js') ); jinst.setupClasspath(['./drivers/hsqldb.jar', './drivers/derby.jar', './drivers/derbyclient.jar', './drivers/derbytools.jar']); -var configWithUserInUrl = { - drivername: 'org.hsqldb.jdbc.JDBCDriver', - url: 'jdbc:hsqldb:hsql://localhost/xdb;user=SA;password=' -}; - -var configWithUserInConfig = { +var config = { drivername: 'org.hsqldb.jdbc.JDBCDriver', url: 'jdbc:hsqldb:hsql://localhost/xdb', user : 'SA', password: '' }; -var configWithPropertiesInConfig = { - drivername: 'org.hsqldb.jdbc.JDBCDriver', - url: 'jdbc:hsqldb:hsql://localhost/xdb', - properties: [ - ['user', 'SA'], - ['password',''] - ] -}; - module.exports = { testinit: function(test) { - jdbcConn.initialize(configWithUserInUrl, function(err, drivername) { - test.expect(2); - test.equal(null, err); - test.equal(drivername, 'org.hsqldb.jdbc.JDBCDriver'); - test.done(); - }); - }, - testinitwithproperties: function(test) { - jdbcConnWithProps.initialize(configWithPropertiesInConfig, function(err, drivername) { + jdbcConn.initialize(config, function(err, drivername) { test.expect(2); test.equal(null, err); test.equal(drivername, 'org.hsqldb.jdbc.JDBCDriver'); @@ -54,14 +31,6 @@ module.exports = { test.done(); }); }, - testopenwithproperties: function(test) { - jdbcConnWithProps.open(function(err, conn) { - test.expect(2); - test.equal(null, err); - test.ok(conn); - test.done(); - }); - }, testcreatetable: function(test) { jdbcConn.executeQuery("CREATE TABLE blah (id int, name varchar(10), date DATE, time TIME, timestamp TIMESTAMP);", function(err, result) { test.expect(2); @@ -70,14 +39,6 @@ module.exports = { test.done(); }); }, - testcreatetablewithproperties: function(test) { - jdbcConnWithProps.executeQuery("CREATE TABLE blahP (id int, name varchar(10), date DATE, time TIME, timestamp TIMESTAMP);", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result); - test.done(); - }); - }, testeqinsert: function(test) { jdbcConn.executeQuery("INSERT INTO blah VALUES (1, 'Jason', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { test.expect(2); @@ -86,14 +47,6 @@ module.exports = { test.done(); }); }, - testeqinsertwithproperties: function(test) { - jdbcConnWithProps.executeQuery("INSERT INTO blahP VALUES (1, 'Jason', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result); - test.done(); - }); - }, testeuinsert: function(test) { jdbcConn.executeUpdate("INSERT INTO blah VALUES (3, 'Temp', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { test.expect(2); @@ -102,14 +55,6 @@ module.exports = { test.done(); }); }, - testeuinsertwithproperties: function(test) { - jdbcConnWithProps.executeUpdate("INSERT INTO blahP VALUES (3, 'Temp', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result && result == 1); - test.done(); - }); - }, testequpdate: function(test) { jdbcConn.executeQuery("UPDATE blah SET id = 2 WHERE name = 'Jason';", function(err, result) { test.expect(2); @@ -118,14 +63,6 @@ module.exports = { test.done(); }); }, - testequpdatewithproperties: function(test) { - jdbcConnWithProps.executeQuery("UPDATE blahP SET id = 2 WHERE name = 'Jason';", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result); - test.done(); - }); - }, testeuupdate: function(test) { jdbcConn.executeUpdate("UPDATE blah SET id = 4 WHERE name = 'Temp';", function(err, result) { test.expect(2); @@ -134,14 +71,6 @@ module.exports = { test.done(); }); }, - testeuupdatewithproperties: function(test) { - jdbcConnWithProps.executeUpdate("UPDATE blahP SET id = 4 WHERE name = 'Temp';", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result && result == 1); - test.done(); - }); - }, testselect: function(test) { jdbcConn.executeQuery("SELECT * FROM blah;", function(err, result) { test.expect(6); @@ -154,14 +83,6 @@ module.exports = { test.done(); }); }, - testselectwithproperties: function(test) { - jdbcConnWithProps.executeQuery("SELECT * FROM blahP;", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result && result.length == 2); - test.done(); - }); - }, testeqdelete: function(test) { jdbcConn.executeQuery("DELETE FROM blah WHERE id = 2;", function(err, result) { test.expect(2); @@ -170,14 +91,6 @@ module.exports = { test.done(); }); }, - testeqdeletewithproperties: function(test) { - jdbcConnWithProps.executeQuery("DELETE FROM blahP WHERE id = 2;", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result); - test.done(); - }); - }, testeudelete: function(test) { jdbcConn.executeUpdate("DELETE FROM blah WHERE id = 4;", function(err, result) { test.expect(2); @@ -186,14 +99,6 @@ module.exports = { test.done(); }); }, - testeudeletewithproperties: function(test) { - jdbcConnWithProps.executeUpdate("DELETE FROM blahP WHERE id = 4;", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result && result == 1); - test.done(); - }); - }, testdroptable: function(test) { jdbcConn.executeQuery("DROP TABLE blah;", function(err, result) { test.expect(2); @@ -201,13 +106,5 @@ module.exports = { test.ok(result); test.done(); }); - }, - testdroptablewithproperties: function(test) { - jdbcConnWithProps.executeQuery("DROP TABLE blahP;", function(err, result) { - test.expect(2); - test.equal(null, err); - test.ok(result); - test.done(); - }); } }; diff --git a/test/test-properties-config.js b/test/test-properties-config.js new file mode 100644 index 0000000..d124322 --- /dev/null +++ b/test/test-properties-config.js @@ -0,0 +1,112 @@ +var jinst = require('../lib/jinst.js'); +var nodeunit = require('nodeunit'); +var jdbcConn = new ( require('../lib/jdbc.js') ); + +jinst.setupClasspath(['./drivers/hsqldb.jar', + './drivers/derby.jar', + './drivers/derbyclient.jar', + './drivers/derbytools.jar']); + +var config = { + drivername: 'org.hsqldb.jdbc.JDBCDriver', + url: 'jdbc:hsqldb:hsql://localhost/xdb', + properties: [ + ['user', 'SA'], + ['password',''] + ] +}; + +module.exports = { + testinit: function(test) { + jdbcConn.initialize(config, function(err, drivername) { + test.expect(2); + test.equal(null, err); + test.equal(drivername, 'org.hsqldb.jdbc.JDBCDriver'); + test.done(); + }); + }, + testopen: function(test) { + jdbcConn.open(function(err, conn) { + test.expect(2); + test.equal(null, err); + test.ok(conn); + test.done(); + }); + }, + testcreatetable: function(test) { + jdbcConn.executeQuery("CREATE TABLE blahP (id int, name varchar(10), date DATE, time TIME, timestamp TIMESTAMP);", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeqinsert: function(test) { + jdbcConn.executeQuery("INSERT INTO blahP VALUES (1, 'Jason', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeuinsert: function(test) { + jdbcConn.executeUpdate("INSERT INTO blahP VALUES (3, 'Temp', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result && result == 1); + test.done(); + }); + }, + testequpdate: function(test) { + jdbcConn.executeQuery("UPDATE blahP SET id = 2 WHERE name = 'Jason';", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeuupdate: function(test) { + jdbcConn.executeUpdate("UPDATE blahP SET id = 4 WHERE name = 'Temp';", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result && result == 1); + test.done(); + }); + }, + testselect: function(test) { + jdbcConn.executeQuery("SELECT * FROM blahP;", function(err, result) { + test.expect(6); + test.equal(null, err); + test.ok(result && result.length == 2); + test.equal(result[0].NAME, 'Jason'); + test.ok(result[0].DATE); + test.ok(result[0].TIME); + test.ok(result[0].TIMESTAMP); + test.done(); + }); + }, + testeqdelete: function(test) { + jdbcConn.executeQuery("DELETE FROM blahP WHERE id = 2;", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeudelete: function(test) { + jdbcConn.executeUpdate("DELETE FROM blahP WHERE id = 4;", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result && result == 1); + test.done(); + }); + }, + testdroptable: function(test) { + jdbcConn.executeQuery("DROP TABLE blahP;", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + } +}; diff --git a/test/test-user-password-url.js b/test/test-user-password-url.js new file mode 100644 index 0000000..024cf8d --- /dev/null +++ b/test/test-user-password-url.js @@ -0,0 +1,108 @@ +var jinst = require('../lib/jinst.js'); +var nodeunit = require('nodeunit'); +var jdbcConn = new ( require('../lib/jdbc.js') ); + +jinst.setupClasspath(['./drivers/hsqldb.jar', + './drivers/derby.jar', + './drivers/derbyclient.jar', + './drivers/derbytools.jar']); + +var config = { + drivername: 'org.hsqldb.jdbc.JDBCDriver', + url: 'jdbc:hsqldb:hsql://localhost/xdb;user=SA;password=' +}; + +module.exports = { + testinit: function(test) { + jdbcConn.initialize(config, function(err, drivername) { + test.expect(2); + test.equal(null, err); + test.equal(drivername, 'org.hsqldb.jdbc.JDBCDriver'); + test.done(); + }); + }, + testopen: function(test) { + jdbcConn.open(function(err, conn) { + test.expect(2); + test.equal(null, err); + test.ok(conn); + test.done(); + }); + }, + testcreatetable: function(test) { + jdbcConn.executeQuery("CREATE TABLE blahU (id int, name varchar(10), date DATE, time TIME, timestamp TIMESTAMP);", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeqinsert: function(test) { + jdbcConn.executeQuery("INSERT INTO blahU VALUES (1, 'Jason', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeuinsert: function(test) { + jdbcConn.executeUpdate("INSERT INTO blahU VALUES (3, 'Temp', CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP);", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result && result == 1); + test.done(); + }); + }, + testequpdate: function(test) { + jdbcConn.executeQuery("UPDATE blahU SET id = 2 WHERE name = 'Jason';", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeuupdate: function(test) { + jdbcConn.executeUpdate("UPDATE blahU SET id = 4 WHERE name = 'Temp';", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result && result == 1); + test.done(); + }); + }, + testselect: function(test) { + jdbcConn.executeQuery("SELECT * FROM blahU;", function(err, result) { + test.expect(6); + test.equal(null, err); + test.ok(result && result.length == 2); + test.equal(result[0].NAME, 'Jason'); + test.ok(result[0].DATE); + test.ok(result[0].TIME); + test.ok(result[0].TIMESTAMP); + test.done(); + }); + }, + testeqdelete: function(test) { + jdbcConn.executeQuery("DELETE FROM blahU WHERE id = 2;", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + }, + testeudelete: function(test) { + jdbcConn.executeUpdate("DELETE FROM blahU WHERE id = 4;", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result && result == 1); + test.done(); + }); + }, + testdroptable: function(test) { + jdbcConn.executeQuery("DROP TABLE blahU;", function(err, result) { + test.expect(2); + test.equal(null, err); + test.ok(result); + test.done(); + }); + } +};