forked from joeferner/node-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance asyncOptions to provide control over sync & async method vari…
…ants. Generalizes asyncOptions to allow changing the suffix used for sync and async style methods, and also allow any of them to be omitted.
- Loading branch information
Showing
11 changed files
with
481 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// testAllThreeSuffix.js | ||
|
||
// All three variants have non-empty suffix, i.e a suffix is required for any variant. | ||
|
||
var java = require("../"); | ||
var assert = require("assert"); | ||
var _ = require('lodash'); | ||
|
||
java.asyncOptions = { | ||
syncSuffix: "Sync", | ||
asyncSuffix: "Async", | ||
promiseSuffix: 'Promise', | ||
promisify: require('when/node').lift // https://github.com/cujojs/when | ||
}; | ||
|
||
module.exports = { | ||
testAPI: function(test) { | ||
test.expect(6); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
test.ok(arrayList); | ||
test.ok(java.instanceOf(arrayList, "java.util.ArrayList")); | ||
|
||
var api = _.functions(arrayList); | ||
test.ok(_.includes(api, 'addSync'), 'Expected `addSync` to be present, but it is NOT.'); | ||
test.ok(_.includes(api, 'addAsync'), 'Expected `addAsync` to be present, but it is NOT.'); | ||
test.ok(_.includes(api, 'addPromise'), 'Expected addPromise to be present, but it is NOT.'); | ||
test.ok(!_.includes(api, 'add'), 'Expected add to NOT be present, but it is.'); | ||
test.done(); | ||
}, | ||
|
||
testSyncCalls: function(test) { | ||
test.expect(1); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.addSync("hello"); | ||
arrayList.addSync("world"); | ||
test.strictEqual(arrayList.sizeSync(), 2); | ||
test.done(); | ||
}, | ||
|
||
testAsyncCalls: function(test) { | ||
test.expect(4); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.addAsync("hello", function(err, result) { | ||
test.ifError(err); | ||
arrayList.addAsync("world", function(err, result) { | ||
test.ifError(err); | ||
arrayList.sizeAsync(function(err, size) { | ||
test.ifError(err); | ||
test.strictEqual(size, 2); | ||
test.done(); | ||
}); | ||
}); | ||
}); | ||
}, | ||
|
||
testPromiseCalls: function(test) { | ||
test.expect(1); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.addPromise("hello") | ||
.then(function () { return arrayList.addPromise("world"); }) | ||
.then(function () { return arrayList.sizePromise(); }) | ||
.then(function (size) { | ||
test.strictEqual(size, 2); | ||
test.done(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// testAsyncSuffixSyncDefault.js | ||
|
||
// Use "Async" for the asyncSuffix, and "" for the syncSuffix. | ||
|
||
var java = require("../"); | ||
var assert = require("assert"); | ||
var _ = require('lodash'); | ||
|
||
java.asyncOptions = { | ||
syncSuffix: "", | ||
asyncSuffix: "Async" | ||
}; | ||
|
||
module.exports = { | ||
testAPI: function(test) { | ||
test.expect(5); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
test.ok(arrayList); | ||
test.ok(java.instanceOf(arrayList, "java.util.ArrayList")); | ||
|
||
var api = _.functions(arrayList); | ||
test.ok(_.includes(api, 'addAsync'), 'Expected `addAsync` to be present, but it is NOT.'); | ||
test.ok(_.includes(api, 'add'), 'Expected `add` to be present, but it is NOT.'); | ||
test.ok(!_.includes(api, 'addPromise'), 'Expected addPromise to NOT be present, but it is.'); | ||
test.done(); | ||
}, | ||
|
||
testSyncCalls: function(test) { | ||
test.expect(1); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.add("hello"); | ||
arrayList.add("world"); | ||
test.strictEqual(arrayList.size(), 2); | ||
test.done(); | ||
}, | ||
|
||
testAsyncCalls: function(test) { | ||
test.expect(4); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.addAsync("hello", function(err, result) { | ||
test.ifError(err); | ||
arrayList.addAsync("world", function(err, result) { | ||
test.ifError(err); | ||
arrayList.sizeAsync(function(err, size) { | ||
test.ifError(err); | ||
test.strictEqual(size, 2); | ||
test.done(); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// testDefacto.js | ||
|
||
// In the defacto case, the developer sets asyncOptions, but specifies the defacto standard behavior. | ||
|
||
var _ = require('lodash'); | ||
var java = require("../"); | ||
var nodeunit = require("nodeunit"); | ||
|
||
java.asyncOptions = { | ||
syncSuffix: "Sync", | ||
asyncSuffix: "" | ||
}; | ||
|
||
module.exports = { | ||
testAPI: function(test) { | ||
test.expect(5); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
test.ok(arrayList); | ||
test.ok(java.instanceOf(arrayList, "java.util.ArrayList")); | ||
|
||
var api = _.functions(arrayList); | ||
test.ok(_.includes(api, 'addSync'), 'Expected `addSync` to be present, but it is NOT.'); | ||
test.ok(_.includes(api, 'add'), 'Expected `add` to be present, but it is NOT.'); | ||
test.ok(!_.includes(api, 'addPromise'), 'Expected addPromise to NOT be present, but it is.'); | ||
test.done(); | ||
}, | ||
|
||
testSyncCalls: function(test) { | ||
test.expect(1); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.addSync("hello"); | ||
arrayList.addSync("world"); | ||
test.strictEqual(arrayList.sizeSync(), 2); | ||
test.done(); | ||
}, | ||
|
||
testAsyncCalls: function(test) { | ||
test.expect(4); | ||
var arrayList = java.newInstanceSync("java.util.ArrayList"); | ||
arrayList.add("hello", function(err, result) { | ||
test.ifError(err); | ||
arrayList.add("world", function(err, result) { | ||
test.ifError(err); | ||
arrayList.size(function(err, size) { | ||
test.ifError(err); | ||
test.strictEqual(size, 2); | ||
test.done(); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.