Merge remote-tracking branch 'modules/master' into subtree

this merges the master head of https://github.com/helma-org/apps-modules-mirror into helma
This commit is contained in:
Simon Oberhammer 2012-03-27 11:46:35 +02:00
commit 226552bc24
53 changed files with 12023 additions and 0 deletions

View file

@ -0,0 +1,148 @@
tests = [
"testEquality",
"testSimpleMapping",
"testSimpleCollection",
"testObjectReference",
"testCollectionForReference"
];
function setup() {
}
function testEquality() {
var person = new Person();
root.persons.add(person);
res.commit();
var id = person._id;
app.clearCache();
var person2 = root.persons.get(id);
assertNotNull(person2);
assertTrue(person !== person2);
assertTrue(person._id === person2._id);
assertTrue(person == person2);
person.remove();
}
function testSimpleMapping() {
var data = {
name: "Oliver Stone",
dateOfBirth: new Date(1946, 8, 15, 6, 0, 0),
height: 182
};
var person = new Person();
person.name = data.name;
person.dateOfBirth = data.dateOfBirth;
person.height = data.height;
root.persons.add(person);
var personId = person._id;
res.commit(); // Commit Transaction
app.clearCache(); // Clear cache so that object is refetched
var person = Person.getById(personId);
assertNotNull(person);
assertEqual(person._prototype, "Person");
assertEqual(person._id, personId);
assertEqual(person.name, data.name);
assertEqual(person.height, data.height);
assertEqual(person.dateOfBirth.valueOf(), data.dateOfBirth.valueOf());
res.commit();
person.remove();
}
function testSimpleCollection() {
var data = {
name: "Helma"
};
var orgCount = root.organisations.count();
var org = new Organisation();
org.name = data.name;
root.organisations.add(org);
var orgId = org._id;
assertEqual(orgCount + 1, root.organisations.count());
assertNotEqual(root.organisations.indexOf(org), -1);
// fetch Object via position
assertEqual(root.organisations.get(root.organisations.indexOf(org)), org);
// fetch Object via accessname
assertEqual(root.organisations.get(data.name), org);
// fetch Object via id
assertEqual(root.organisations.getById(orgId), org);
// test list
assertEqual(root.organisations.count(), root.organisations.list().length);
org.remove();
assertEqual(orgCount, root.organisations.count());
assertEqual(root.organisations.indexOf(org), -1);
assertNull(root.organisations.get(data.name));
assertNull(root.organisations.getById(orgId));
}
function testObjectReference() {
var org = new Organisation();
org.name = "Helma";
root.organisations.add(org);
var orgId = org._id;
var person = new Person();
person.name = "Hannes";
person.organisation = org;
root.persons.add(person);
var personId = person._id;
res.commit(); // Commit Transaction
app.clearCache(); // Clear cache so that object is refetched
var org = Organisation.getById(orgId);
var person = Person.getById(personId);
assertEqual(person.organisation, org);
org.remove();
res.commit(); // Commit Transaction
assertNull(person.organisation);
person.remove();
}
function testCollectionForReference() {
var org = new Organisation();
org.name = "Helma";
root.organisations.add(org);
var orgId = org._id;
var personCount = org.persons.count();
var person = new Person();
person.name = "Hannes";
person.organisation = org;
root.persons.add(person);
org.persons.add(person);
assertEqual(personCount + 1, org.persons.count());
assertNotEqual(org.persons.indexOf(person), -1);
org.persons.removeChild(person);
person.remove();
assertEqual(personCount, org.persons.count());
assertEqual(org.persons.indexOf(person), -1);
org.remove();
}
function cleanup() {
}

View file

@ -0,0 +1,183 @@
tests = [
"testSize",
"testMaxSize",
"testAddSmall",
"testAddLarge",
"testRemoveSmall",
"testRemoveLarge",
"testUpdateSmall",
"testUpdateLarge",
"testListSmall",
"testListLarge",
"testOrderLarge",
"testOrderSmall"
];
var helma, ikea;
var small = 3, large = 1234;
function setup() {
ikea = makeOrg('Ikea', large);
helma = makeOrg('Helma', small);
}
function testSize() {
assertEqual(ikea.persons.size(), large);
assertEqual(helma.persons.size(), small);
}
function testMaxSize() {
assertEqual(ikea.range.size(), 100);
assertEqual(helma.range.size(), 0);
var person = ikea.range.get("Person Ikea 0150");
assertNotNull(person);
assertEqual(person, ikea.range.get(50));
assertEqual(person, ikea.persons.get(150));
assertEqual(50, ikea.range.indexOf(person));
assertEqual(150, ikea.persons.indexOf(person));
}
function testAddSmall() {
testAdd(helma, small);
}
function testAddLarge() {
testAdd(ikea, large);
}
// test directly adding to a collection
function testAdd(org, size) {
var person = new Person();
person.name = "TestPerson";
org.persons.add(person);
assertEqual(org.persons.size(), size + 1);
assertEqual(org.persons.indexOf(person), size);
assertEqual(org.persons.contains(person), size);
assertEqual(person.href(), org.persons.href() + "TestPerson/");
// make sure the add has set the back-reference on the person object.
// note that === comparison will return false if the
// collection size exceeds the cache size.
assertTrue(person.organisation == org);
}
function testRemoveSmall() {
testRemove(helma, small);
}
function testRemoveLarge() {
testRemove(ikea, large);
}
// test directly removing from a collection
function testRemove(org, size) {
var person = org.persons.get(org.persons.size() - 1);
person.remove();
assertEqual(org.persons.size(), size);
assertEqual(org.persons.indexOf(person), -1);
assertEqual(org.persons.contains(person), -1);
}
function testUpdateSmall() {
testUpdate(helma, small);
}
function testUpdateLarge() {
testUpdate(ikea, large);
}
// test indirectly adding to and removing form a collection
function testUpdate(org, size) {
var person = new Person();
person.name = "TestPerson";
person.organisation = org;
person.persist();
res.commit();
assertEqual(org.persons.size(), size + 1);
org.persons.prefetchChildren();
assertEqual(org.persons.indexOf(person), size);
assertEqual(org.persons.contains(person), size);
assertEqual(person.href(), org.persons.href() + "TestPerson/");
person.remove();
res.commit();
assertEqual(org.persons.size(), size);
assertEqual(org.persons.indexOf(person), -1);
assertEqual(org.persons.contains(person), -1);
}
function testListSmall() {
testList(helma, small);
}
function testListLarge() {
testList(ikea, large);
}
function testList(org, size) {
function iterate(list, start, length) {
assertEqual(list.length, length);
for (var i = 0; i < length; i++) {
assertEqual(list[i].name, "Person " + org.name + (start + i).format(" 0000"));
}
}
iterate(org.persons.list(), 0, size);
org.persons.invalidate();
iterate(org.persons.list(), 0, size);
org.persons.invalidate();
iterate(org.persons.list(1, size - 2), 1, size - 2);
}
function testOrderLarge() {
testOrder(ikea, ikea.persons.size() - 2);
testOrder(ikea, Math.floor(ikea.persons.size() / 2));
testOrder(ikea, 2);
}
function testOrderSmall() {
testOrder(helma, helma.persons.size() - 1);
testOrder(helma, 1);
testOrder(helma, 0);
}
function testOrder(org, pos) {
var person = new Person();
person.name = "Person " + org.name + pos.format(" 0000") + "B";
person.organisation = org;
root.persons.add(person);
res.commit();
assertEqual(pos + 1, org.persons.indexOf(person));
}
function cleanup() {
var persons = root.persons.list();
for each (var person in persons) {
person.remove();
}
ikea.remove();
helma.remove();
}
function makeOrg(name, size) {
var org = new Organisation();
org.name = name;
root.organisations.add(org);
for (var i = 0; i < size; i++) {
var person = new Person();
person.name = "Person " + name + i.format(" 0000");
person.organisation = org;
root.persons.add(person);
}
res.commit();
return org;
}
// debugging helpers
function dumpDataChange(message) {
res.debug(message + ": ");
dumpDataChangeFor("Person");
dumpDataChangeFor("Organisation");
}
function dumpDataChangeFor(name) {
res.debug(name + ": " + app.__app__.getDbMapping(name).getLastDataChange());
}

View file

@ -0,0 +1,93 @@
tests = [
'testSize',
'testContent',
'testGroupContent',
'testRemove',
'testAdd'
];
var org;
var size = 16;
function setup() {
org = new Organisation();
org.name = "GenericOrg";
var i = 0, person;
function addPerson(collection) {
person = new Person();
person.name = "GenericPerson " + i.format("00");
collection.add(person);
i++;
}
// add first half to both collections of transient parent ...
while (i < 4)
addPerson(org.generic);
while (i < 8)
addPerson(org.groupedGeneric);
root.organisations.add(org);
// ... second half to both collections of persistent parent.
while (i < 12)
addPerson(org.generic);
while (i < size)
addPerson(org.groupedGeneric);
res.commit();
}
function testSize() {
assertEqual(org.generic.size(), size);
org.invalidate();
assertEqual(org.generic.size(), size);
}
function testContent() {
var list = org.generic.list();
assertEqual(list.length, size);
for (var i = 0; i < list.length; i++) {
assertEqual(list[i].name, "GenericPerson " + i.format("00"));
}
}
function testGroupContent() {
var list = org.groupedGeneric.list();
assertEqual(list.length, size);
for (var i = 0; i < list.length; i++) {
assertEqual(list[i].groupname, "GenericPerson " + i.format("00"));
assertEqual(list[i].size(), 1);
assertEqual(list[i].get(0).name, "GenericPerson " + i.format("00"));
}
}
function testRemove() {
var person = org.generic.get(size/2);
org.generic.removeChild(person);
assertEqual(org.generic.size(), size - 1);
res.rollback();
// note: removeChild does not remove the node, nor does it
// unset the constraints between parent and child, so after a rollback
// the object is back in place. While this behaviour is disputable,
// until this is so we test for it.
assertEqual(org.generic.size(), size);
}
function testAdd() {
var person = new Person();
org.generic.add(person);
assertEqual(org.generic.size(), size + 1);
assertEqual(org.groupedGeneric.size(), size);
res.commit();
// note: even after commit the grouped collection must not grow
// since we added a person without a name
assertEqual(org.generic.size(), size + 1);
assertEqual(org.groupedGeneric.size(), size);
}
function cleanup() {
var persons = org.generic.list();
for each (var person in persons) {
person.remove();
}
org.remove();
}

View file

@ -0,0 +1,149 @@
tests = [
"testGroupByAddRemoveCommit",
"testGroupByAddRemoveNoCommit",
"testGroupOrder",
"testGroupTransient"
];
// todo: run with different sizes
var size = 1234;
function setup() {
for (var i = 0; i < size; i++) {
var org = new Organisation();
org.name = "Organisation " + i;
org.country = "CH" + i.format("0000");
// add to different collections
if (i % 2 == 0)
root.organisations.add(org);
else
root.organisationsByCountry.add(org);
}
res.commit();
}
function testGroupByAddRemoveCommit() {
var countryCount = root.organisationsByCountry.count();
var org = new Organisation();
org.country = "AT" + Math.random();
org.name = "Helma" + Math.random();
root.organisations.add(org);
res.commit(); // Commit Transaction
var country = root.organisationsByCountry.get(org.country);
assertEqual(countryCount + 1, root.organisationsByCountry.count());
assertNotNull(country);
assertEqual(country._prototype, "Country");
assertEqual(country._id, org.country);
org.remove();
res.commit(); // Commit Transaction
assertNull(root.organisationsByCountry.get(org.country));
assertEqual(countryCount, root.organisationsByCountry.count());
}
function testGroupByAddRemoveNoCommit() {
var countryCount = root.organisationsByCountry.count();
var org = new Organisation();
org.country = "AT" + Math.random();
org.name = "Helma" + Math.random();
root.organisations.add(org);
root.organisationsByCountry.add(org);
// FIXME HELMABUG: count does not get incremented immediately
assertEqual(countryCount + 1, root.organisationsByCountry.count());
var country = root.organisationsByCountry.get(org.country);
assertNotNull(country);
assertEqual(country._prototype, "Country");
assertEqual(country._id, org.country);
assertEqual(country.count(), 1);
assertEqual(country.get(0), org);
root.organisationsByCountry.removeChild(org);
org.remove();
// FIXME HELMABUG: country is still accessible at this point
// similar to http://helma.org/bugs/show_bug.cgi?id=551
assertNull(root.organisationsByCountry.get(org.country));
assertEqual(countryCount, root.organisationsByCountry.count());
}
function testGroupOrder() {
var org1 = new Organisation();
org1.country = "AT";
org1.name = "Helma" + Math.random();
root.organisations.add(org1);
var org2 = new Organisation();
org2.country = "CH01"; // pre-populated items have countries CH0000..C1234
org2.name = "Helma" + Math.random();
root.organisations.add(org2);
var org3 = new Organisation();
org3.country = "DE";
org3.name = "Helma" + Math.random();
root.organisations.add(org3);
var org4 = new Organisation();
org4.country = org3.country;
org4.name = "Helma" + Math.random();
root.organisations.add(org4);
res.commit();
// make sure that countries and organisations are sorted in decreasing order (as specified in type.properties)
var countries = root.organisationsByCountry.list();
assertEqual(countries.length, size + 3);
for (var i = 0; i < countries.length; i++) {
if (i>0) {
assertTrue(root.organisationsByCountry.get(i-1).groupname >= root.organisationsByCountry.get(i).groupname);
}
for (var j = 0; j < root.organisationsByCountry.get(i); j++) {
if (j > 0) {
assertTrue(root.organisationsByCountry.get(i).get(j-1).groupname >= root.organisationsByCountry.get(i).get(j).groupname);
}
}
}
org1.remove();
org2.remove();
org3.remove();
org4.remove();
}
function testGroupTransient() {
var temp = new Root();
var countryCount = temp.organisationsByCountry.count();
var org = new Organisation();
org.country = "AT" + Math.random();
org.name = "Helma" + Math.random();
temp.organisationsByCountry.add(org);
var country = temp.organisationsByCountry.get(org.country);
assertEqual(countryCount + 1, temp.organisationsByCountry.count());
assertNotNull(country);
assertEqual(country._prototype, "Country");
assertEqual(country.groupname, org.country);
// These don't work as org uses the parent from type.properties
// which is root.organisations. Not sure if this is a bug or not.
// assertEqual(country, org._parent);
// org.remove();
country.removeChild(org);
assertNull(root.organisationsByCountry.get(org.country));
assertEqual(countryCount, temp.organisationsByCountry.count());
}
function cleanup() {
var orgs = root.organisations.list();
for each (var org in orgs) {
org.remove();
}
}

View file

@ -0,0 +1,56 @@
tests = [
"testSimpleParent",
"testFallbackParent",
"testMountpoints"
];
var org;
var person1;
var person2;
function setup() {
org = new Organisation();
org.name = "Helma";
root.organisations.add(org);
person1 = new Person();
person1.name = "Hannes";
person1.organisation = org;
root.persons.add(person1);
person2 = new Person();
person2.name = "Michi";
root.persons.add(person2);
}
function testSimpleParent() {
assertEqual(org.href(), root.organisations.href() + org.name + "/");
assertEqual(root.organisations, org._parent);
assertEqual(root, org._parent._parent);
}
function testFallbackParent() {
assertEqual(person1.href(), person1.organisation.persons.href() + person1.name + "/");
assertEqual(person1.organisation.persons, person1._parent);
assertEqual(person2.href(), root.persons.href() + person2._id + "/");
assertEqual(root.persons, person2._parent);
}
function testMountpoints() {
assertEqual(root.someMountpoint._prototype, "SomeMountpoint");
assertEqual(root.someMountpoint._parent, root);
assertEqual(root.someMountpoint.href(), root.href() + "someMountpoint/");
assertEqual(org.someMountpoint._prototype, "SomeMountpoint");
assertEqual(org.someMountpoint._parent, org);
// FIXME: Helma-Bug ? mountpoints are converted to lower case ?
assertEqual(org.someMountpoint.href(), org.href() + "someMountpoint/");
}
function cleanup() {
org.remove();
person1.remove();
person2.remove();
}

View file

@ -0,0 +1,40 @@
tests = [
"testForward",
"testBackward",
];
function setup() {
var org = new Organisation();
var person = new Person();
org.name = "Acme Hovercraft";
person.name = "Murray Feather";
person.organisation = org;
org.person = person;
person.persist();
res.commit();
}
function testForward() {
app.clearCache();
person = root.persons.get(0);
org = root.organisations.get(0);
assertEqual(person.organisation, org);
assertEqual(person.organisation.name, org.name);
assertEqual("Acme Hovercraft", org.name);
}
function testBackward() {
app.clearCache();
var person = root.persons.get(0);
var org = root.organisations.get(0);
assertEqual(org.person, person);
assertEqual(org.person.name, person.name);
assertEqual("Murray Feather", person.name);
}
function cleanup() {
var person = root.persons.get(0);
var org = root.organisations.get(0);
org.remove();
person.remove();
}

319
modules/test/tests/Skin.js Normal file
View file

@ -0,0 +1,319 @@
var tests = [
"testCommentMacro",
"testDeepResolve",
"testDeepUnhandled",
"testDeep",
"testDeepFail",
"testDeepFailSilent",
"testJavaProp",
"testJavaMissing",
"testJavaMissingSilent",
"testJavaMissingVerbose",
"testUndefinedHandler",
"testJSProp",
"testJSMacro",
"testJSFunction",
"testJSMissing",
"testJSMissingSilent",
"testJSMissingVerbose",
"testDateFilter",
"testNestedRootMacro",
"testNestedParamMacro",
"testNestedResponseMacro",
"testNestedPrefixSuffix",
"testResponseProp",
"testResponseMacro",
"testResponseFunction",
"testResponseMissing",
"testResponseMissingSilent",
"testResponseMissingVerbose",
"testRootProp",
"testRootMacro",
"testRootMissing",
"testRootMissingSilent",
"testRootMissingVerbose",
"testSessionProp",
"testSessionMacro",
"testSessionFunction",
"testSessionMissing",
"testSessionMissingDefault",
"testSessionMissingSilent",
"testSessionMissingVerbose",
"testSessionDeepMissing",
"testSubskins"
];
var setup = function() {
res.handlers.color = new java.awt.Color["(int,int,int)"](0, 255, 0);
res.handlers.file = new java.io.File["(java.lang.String,java.lang.String)"](null, "file");
res.handlers.jsobject = {
banana: "yellow",
kiwi_macro: function() { return this.kiwiColor },
apple: function() {},
kiwiColor: "green"
};
res.data.date = new Date();
res.data.banana = "yellow";
res.data.kiwi_macro = function() { return "green" };
res.data.apple = function() {};
session.data.banana = "yellow";
session.data.kiwi_macro = function() { return "green" };
session.data.apple = function() {};
};
var testCommentMacro = function() {
var skin = createSkin("<% // this.foo bar=<% this.foobar %> FIXME %>ok");
var result = renderSkinAsString(skin);
assertEqual(result, "ok");
}
var testDeepResolve = function() {
res.handlers.deep = {
getMacroHandler: function(name) {
if (name != "foo") return null;
return {
bar_macro: function() {
return "ok";
}
}
}
}
var result = renderSkinAsString(createSkin("<% deep.foo.bar %>"));
assertEqual(result, "ok");
};
var testDeepUnhandled = function() {
res.handlers.deep = {
getMacroHandler: function(name) {
if (name != "foo") return null;
return {
onUnhandledMacro: function(name) {
if (name == "bar") return "ok";
}
}
}
}
var result = renderSkinAsString(createSkin("<% deep.foo.bar %>"));
assertEqual(result, "ok");
};
var testDeep = function() {
res.handlers.deep = {
foo: {
bar: "ok"
}
}
var result = renderSkinAsString(createSkin("<% deep.foo.bar %>"));
assertEqual(result, "ok");
};
var testDeepFail = function() {
var result = renderSkinAsString(createSkin("<% root.foo.bar %>"));
assertStringContains(result, "Unhandled");
var result = renderSkinAsString(createSkin("<% root.foo.bar failmode=silent %>"));
assertEqual(result, "");
};
var testDeepFailSilent = function() {
res.handlers.deep = {
foo: {}
};
var result = renderSkinAsString(createSkin("<% deep.foo.bar %>"));
assertEqual(result, "");
var result = renderSkinAsString(createSkin("<% deep.foo.bar failmode=verbose %>"));
assertStringContains(result, "Unhandled");
};
var testJavaProp = function() {
var result = renderSkinAsString(createSkin("<% color.green %>"));
assertEqual(result, "255");
result = renderSkinAsString(createSkin("<% color.red %>"));
assertEqual(result, "0");
};
var testJavaMissing = function() {
var result = renderSkinAsString(createSkin("<% colo.foo %>"));
assertStringContains(result, "Unhandled");
};
var testJavaMissingSilent = function() {
var result = renderSkinAsString(createSkin("<% color.foo failmode=silent default=ok %>"));
assertEqual(result, "ok");
};
var testJavaMissingVerbose = function() {
var result = renderSkinAsString(createSkin("<% color.foo failmode=verbose %>"));
assertStringContains(result, "Unhandled");
}
var testUndefinedHandler = function() {
var result = renderSkinAsString(createSkin("<% file.parentFile %>"));
assertEqual(result, "");
};
var testJSProp = function() {
var result = renderSkinAsString(createSkin("<% jsobject.banana %>"));
assertEqual(result, "yellow");
};
var testJSMacro = function() {
var result = renderSkinAsString(createSkin("<% jsobject.kiwi %>"));
assertEqual(result, "green");
};
var testJSFunction = function() {
var result = renderSkinAsString(createSkin("<% jsobject.apple failmode=silent %>"));
assertEqual(result, "");
};
var testJSMissing = function() {
var result = renderSkinAsString(createSkin("<% jsobject.papaya %>"));
assertEqual(result, "");
};
var testJSMissingSilent = function() {
var result = renderSkinAsString(createSkin("<% jsobject.papaya failmode=silent %>"));
assertEqual(result, "");
};
var testJSMissingVerbose = function() {
var result = renderSkinAsString(createSkin("<% jsobject.papaya failmode=verbose %>"));
assertStringContains(result, "Unhandled");
};
var testDateFilter = function() {
var result = renderSkinAsString(createSkin("<% root.date | isDate %>"));
assertEqual(result, "true");
};
var testNestedRootMacro = function() {
var skin = "<% echo <% root.date %> | isRootDate %>";
var result = renderSkinAsString(createSkin(skin));
assertEqual(result, "true");
}
var testNestedParamMacro = function() {
var skin = "<% echo <% param.date %> | isDate %>";
var result = renderSkinAsString(createSkin(skin), { date: new Date() });
assertEqual(result, "true");
}
var testNestedResponseMacro = function() {
var skin = "<% echo what=<% response.date %> | isResponseDate %>";
var result = renderSkinAsString(createSkin(skin));
assertEqual(result, "true");
};
var testNestedPrefixSuffix = function() {
var skin = "<% root.macro prefix=<% root.string %> suffix=<% root.macro %> %>";
var result = renderSkinAsString(createSkin(skin));
// root.macro changes suffix to "."
assertEqual(result, "rootroot.");
}
var testResponseProp = function() {
var result = renderSkinAsString(createSkin("<% response.banana %>"));
assertEqual(result, "yellow");
};
var testResponseMacro = function() {
var result = renderSkinAsString(createSkin("<% response.kiwi %>"));
assertEqual(result, "green");
};
var testResponseFunction = function() {
var result = renderSkinAsString(createSkin("<% response.apple failmode=silent %>"));
assertEqual(result, "");
};
var testResponseMissing = function() {
var result = renderSkinAsString(createSkin("<% response.papaya %>"));
assertEqual(result, "");
};
var testResponseMissingSilent = function() {
var result = renderSkinAsString(createSkin("<% response.papaya failmode=silent %>"));
assertEqual(result, "");
};
var testResponseMissingVerbose = function() {
var result = renderSkinAsString(createSkin("<% response.papaya failmode=verbose %>"));
assertStringContains(result, "Unhandled");
};
var testRootProp = function() {
var result = renderSkinAsString(createSkin("<% root.string %>"));
assertEqual(result, "root");
};
var testRootMacro = function() {
var result = renderSkinAsString(createSkin("<% root.macro %>"));
assertEqual(result, "root");
};
var testRootMissing = function() {
var result = renderSkinAsString(createSkin("<% root.undefinedmacro %>"));
assertStringContains(result, "Unhandled");
};
var testRootMissingSilent = function() {
var result = renderSkinAsString(createSkin("<% root.undefinedmacro failmode=silent %>"));
assertEqual(result, "");
};
var testRootMissingVerbose = function() {
var result = renderSkinAsString(createSkin("<% root.undefinedmacro failmode=verbose %>"));
assertStringContains(result, "Unhandled");
};
var testSessionProp = function() {
var result = renderSkinAsString(createSkin("<% session.banana %>"));
assertEqual(result, "yellow");
};
var testSessionMacro = function() {
var result = renderSkinAsString(createSkin("<% session.kiwi %>"));
assertEqual(result, "green");
};
var testSessionFunction = function() {
var result = renderSkinAsString(createSkin("<% session.apple failmode=silent %>"));
assertEqual(result, "");
};
var testSessionMissingDefault = function() {
var result = renderSkinAsString(createSkin("<% session.papaya default=nope %>"));
assertEqual(result, "nope");
};
var testSessionMissing = function() {
var result = renderSkinAsString(createSkin("<% session.papaya %>"));
assertEqual(result, "");
};
var testSessionMissingSilent = function() {
var result = renderSkinAsString(createSkin("<% session.papaya failmode=silent %>"));
assertEqual(result, "");
};
var testSessionMissingVerbose = function() {
var result = renderSkinAsString(createSkin("<% session.papaya failmode=verbose %>"));
assertStringContains(result, "Unhandled");
};
var testSessionDeepMissing = function() {
var result = renderSkinAsString(createSkin("<% session.user.name %>"));
assertStringContains(result, "Unhandled");
// assertEqual(result, "");
};
var testSubskins = function() {
var result = renderSkinAsString("subskins");
assertEqual(result, "mainskin");
result = renderSkinAsString("subskins#subskin1");
assertEqual(result, "subskin1");
result = renderSkinAsString("subskins#subskin2");
assertEqual(result, "subskin2");
};

View file

@ -0,0 +1,68 @@
var tests = [
"testSimple",
"testError",
"testNotFound",
"testRedirect",
"testRedirectNoFollow",
"testMaxResponseSize",
"testLongResponse"
];
app.addRepository("modules/helma/Http.js");
var http = new helma.Http();
var testSimple = function() {
var result = http.getUrl(root.href("hello"));
assertEqual(result.content, "Hello");
assertEqual(result.code, 200);
};
var testError = function() {
var result = http.getUrl(root.href("throwerror"));
assertEqual(result.content, "Error");
assertEqual(result.code, 500);
};
var testNotFound = function() {
var result = http.getUrl(root.href("nonExistingAction"));
assertEqual(result.content, "Not found");
assertEqual(result.code, 404);
};
var testRedirect = function() {
var result = http.getUrl(root.href("redirect"));
assertEqual(result.content, "Hello");
assertEqual(result.code, 200);
};
var testRedirectNoFollow = function() {
http.setFollowRedirects(false);
var result = null;
try {
result = http.getUrl(root.href("redirect"));
} finally {
http.setFollowRedirects(true);
}
assertEqual(result.content, "");
// response codes 302 and 303 are both ok
assertTrue(result.code == 302 || result.code == 303);
};
var testMaxResponseSize = function() {
http.setMaxResponseSize(3);
var error = null;
try {
http.getUrl(root.href("hello"));
} catch (err) {
error = err;
} finally {
http.setMaxResponseSize(null);
}
assertNotNull(error);
};
var testLongResponse = function() {
var result = http.getUrl(root.href("long"));
assertEqual(result.content, makeLongString());
};

View file

@ -0,0 +1,347 @@
var tests = [
"testConstructor",
"testGetDirectory",
"testGetRAMDirectory",
"testCreateIndex",
"testGetReaderWriter",
"testIndexLock",
"testDocument",
"testAddDocuments",
"testSearch"
];
app.addRepository("modules/helma/Search.js");
var indexName = "testindex";
var basePath = java.lang.System.getProperty("java.io.tmpdir");
var index;
/**
* Test preliminaries
*/
var setup = function() {
// check if there is a (leftover) directory with the same name
// in the system's temporary directory - if so throw an exception and stop
var dir = new helma.File(basePath, indexName);
if (dir.exists()) {
throw "There is already a directory '" + dir.getAbsolutePath() +
"', please remove it before trying to run test again";
}
return;
};
/**
* Test the helma.Search constructor to make sure Lucene is loaded
*/
var testConstructor = function() {
// should not throw an exception
var s = new helma.Search();
return;
};
/**
* Test getDirectory method
*/
var testGetDirectory = function() {
var search = new helma.Search();
assertThrows(function() {
search.getDirectory();
});
var dirs = [
new helma.File(basePath, indexName),
new File(basePath, indexName),
new java.io.File(basePath, indexName),
basePath + "/" + indexName
];
var dir, fsDir, dirPath;
for (var i in dirs) {
dir = dirs[i];
if (dir.constructor != String) {
dirPath = dir.getAbsolutePath();
} else {
dirPath = dir;
}
fsDir = search.getDirectory(dir);
assertNotNull(fsDir);
assertEqual(fsDir.getFile().getAbsolutePath(), dirPath);
}
return;
};
/**
* Test getRAMDirectory method
*/
var testGetRAMDirectory = function() {
var search = new helma.Search();
assertThrows(function() {
search.getDirectory();
});
var dirs = [
new helma.File(basePath, indexName),
new File(basePath, indexName),
new java.io.File(basePath, indexName),
basePath + "/" + indexName
];
var dir, ramDir;
for (var i in dirs) {
dir = dirs[i];
ramDir = search.getRAMDirectory(dir);
assertNotNull(ramDir);
}
return;
};
/**
* Test index creation - this method creates a RAMDirectory based
* index for testing and stores it in the global variable "index"
*/
var testCreateIndex = function() {
var search = new helma.Search();
// test creating a file based index
var fsDir = search.getDirectory(new helma.File(basePath, indexName));
index = search.createIndex(fsDir);
assertNotNull(index);
// explicitly test index.create(true)
assertTrue(index.create(true));
// test creating a ram based index
var ramDir = search.getRAMDirectory();
index = search.createIndex(ramDir);
assertNotNull(index);
// explicitly test index.create(true)
assertTrue(index.create(true));
assertEqual(index.constructor, helma.Search.Index);
assertEqual(index.size(), 0);
return;
};
/**
* Test getting index reader, writer and modifier
*/
var testGetReaderWriter = function() {
// test getReader
var reader = index.getReader();
assertNotNull(reader);
reader.close();
// test getWriter
var writer = index.getWriter();
assertNotNull(writer);
writer.close();
return;
};
/**
* Test index locking
*/
var testIndexLock = function() {
// test if the index is correctly locked when opening a writer
var writer = index.getWriter();
assertTrue(index.isLocked());
// test if getWriter() throws an exception when trying to open a second writer
assertThrows(function() {
index.getWriter();
});
writer.close();
assertFalse(index.isLocked());
return;
};
/**
* Test document constructor and methods
*/
var testDocument = function() {
var doc = new helma.Search.Document();
var f;
// test type conversion
f = new helma.Search.Document.Field("id", 1);
assertEqual(f.value.constructor, String);
assertEqual(f.value, "1");
var now = new Date();
f = new helma.Search.Document.Field("createtime", now);
assertEqual(f.dateValue.constructor, Date);
assertEqual(f.dateValue.getTime(), now.getTime() - (now.getTime() % 60000));
// test adding field with default store and index options
doc.addField(new helma.Search.Document.Field("id", 1));
f = doc.getField("id");
assertNotNull(f);
assertTrue(f.isStored());
assertTrue(f.isIndexed());
assertTrue(f.isTokenized());
// test adding date field with changed field options
f = new helma.Search.Document.Field("createtime", new Date(), {
store: "no",
index: "tokenized"
});
doc.addField(f);
f = doc.getField("createtime");
assertNotNull(f);
assertFalse(f.isStored());
assertTrue(f.isIndexed());
assertTrue(f.isTokenized());
// test deprecated way of calling addField()
doc.addField("title", "Just a test", {
"store": false,
"index": true,
"tokenize": false
});
f = doc.getField("title");
assertNotNull(f);
assertFalse(f.isStored());
assertTrue(f.isIndexed());
assertFalse(f.isTokenized());
// test getFields()
var fields = doc.getFields();
assertEqual(fields.length, 3);
assertEqual(fields[0].name, "id");
assertEqual(fields[1].name, "createtime");
assertEqual(fields[2].name, "title");
return;
};
/**
* Test adding documents
*/
var testAddDocuments = function() {
// test addDocument()
var doc = new helma.Search.Document();
doc.addField(new helma.Search.Document.Field("id", 1));
index.addDocument(doc);
assertEqual(index.size(), 1);
// test removeDocument()
index.removeDocument("id", 1);
assertEqual(index.size(), 0);
// test addDocuments() and removeDocuments() with an array
doc = new helma.Search.Document();
doc.addField(new helma.Search.Document.Field("id", 2));
index.addDocuments([doc]);
assertEqual(index.size(), 1);
index.removeDocuments("id", [2]);
assertEqual(index.size(), 0);
// test addDocuments() and removeDocuments() with a Hashtable as argument
var ht = new java.util.Hashtable();
ht.put("doc", doc);
index.addDocuments(ht);
ht = new java.util.Hashtable();
ht.put("id", 1);
ht.put("id", 2);
index.removeDocuments("id", ht);
assertEqual(index.size(), 0);
// test addDocuments() and removeDocuments() with a Vector as argument
var v = new java.util.Vector();
v.add(doc);
index.addDocuments(v);
v = new java.util.Vector();
v.add(1);
v.add(2);
index.removeDocuments("id", v);
assertEqual(index.size(), 0);
// test updateDocument
index.addDocument(doc);
doc = new helma.Search.Document();
doc.addField("id", 2);
index.updateDocument(doc, "id");
assertEqual(index.size(), 1);
// test count()
doc = new helma.Search.Document();
doc.addField("id", 3);
index.addDocument(doc);
assertEqual(index.count("id", 3), 1);
return;
};
/**
* Test searching the index
*/
var testSearch = function() {
// clear the index
index.create();
assertEqual(index.size(), 0);
// populate the index with test content
var names = [
"foo",
"bar",
"baz"
];
var now = new Date();
var doc;
names.forEach(function(name, idx) {
doc = new helma.Search.Document();
doc.addField("id", idx + 1);
doc.addField("parent", idx % 2);
doc.addField("name", name);
doc.addField("timestamp", new Date(now.getTime() - (idx * 1e6)));
index.addDocument(doc);
});
assertEqual(index.size(), 3);
var searcher = index.getSearcher();
assertNotNull(searcher);
assertNull(searcher.sortFields);
assertNotNull(searcher.getSearcher());
assertTrue(searcher.getSearcher() instanceof Packages.org.apache.lucene.search.IndexSearcher);
// test basic search
var q = new helma.Search.TermQuery("id", 1);
assertEqual(searcher.search(q), 1);
assertNotNull(searcher.hits);
assertEqual(searcher.hits.constructor, helma.Search.HitCollection);
assertEqual(searcher.hits.size(), 1);
var hit = searcher.hits.get(0);
assertNotNull(hit);
assertEqual(hit.constructor, helma.Search.Document);
assertEqual(hit.getField("name").constructor, helma.Search.Document.Field);
assertEqual(hit.getField("name").value, "foo");
// test date value conversion
assertEqual(hit.getField("timestamp").value.constructor, String);
assertEqual(hit.getField("timestamp").dateValue.constructor, Date);
// test query filter
var qf = new helma.Search.QueryFilter(new helma.Search.TermQuery("parent", 0));
q = new helma.Search.WildcardQuery("name", "ba*");
assertEqual(searcher.search(q, qf), 1);
assertEqual(searcher.hits.get(0).getField("name").value, names[2]);
// test sorting of hits
searcher.sortBy("id", true);
assertEqual(searcher.search(q), 2);
assertEqual(searcher.hits.get(0).getField("name").value, names[2]);
assertEqual(searcher.hits.get(1).getField("name").value, names[1]);
// test boolean query
q = new helma.Search.BooleanQuery();
q.addTerm("parent", "0");
assertEqual(q.toString(), "[(parent:0)]");
q = new helma.Search.BooleanQuery();
q.addTerm("parent", "0", "and");
assertEqual(q.toString(), "[+(parent:0)]");
q = new helma.Search.BooleanQuery();
q.addTerm("parent", "0", "not");
assertEqual(q.toString(), "[-(parent:0)]");
searcher.close();
return;
};
/**
* Cleanup
*/
var cleanup = function() {
// remove the directory containing the test index
var dir = new helma.File(basePath, indexName);
dir.removeDirectory();
return;
}