Testing Tools: Even more servlet tests
2 May 2009For today’s servlet test, I decided to take on the LookupTablePropertyJavascriptServlet, which is a rather strange, single-purpose servlet that produces a Javascript file needed for the Look-up Table entry edit page. This servlet uses the values in the global dataType look-up table to produce a script that looks something like this:
// generated by "org.restafarian.core.servlets.impl.LookupTablePropertyJavascriptServlet"
var entryEditConfig = {};
entryEditConfig.property = [];
entryEditConfig.source = {};
entryEditConfig.propertyType = {};
entryEditConfig.propertyType['bool'] = {id: 'bool', description: 'Boolean', inputControl:
'<input type="checkbox" id="{0}"/>'};
entryEditConfig.propertyType['text'] = {id: 'text', description: 'Text', inputControl:
'<textarea id="{0}" cols="60" rows="3"></textarea>'};
entryEditConfig.propertyType['int'] = {id: 'int', description: 'Integer', inputControl:
'<input type="text" id="{0}"/>'};
entryEditConfig.propertyType['char'] = {id: 'char', description: 'Character',
inputControl: '<input type="text" id="{0}"/>'};
entryEditConfig.propertyType['decimal'] = {id: 'decimal', description: 'Decimal',
inputControl: '<input type="text" id="{0}"/>'};
entryEditConfig.propertyType['date'] = {id: 'date', description: 'Date', inputControl:
'<input type="text" size="10" id="{0}"> <img src="/core/images/calendar.gif"
alt="Click to open calendar" id="icon4{0}" style="cursor: pointer;"/>'};
entryEditConfig.propertyType['dateTime'] = {id: 'dateTime', description: 'Date/Time',
inputControl: '<input type="text" size="18" id="{0}"> <img
src="/core/images/calendar.gif" alt="Click to open calendar" id="icon4{0}"
style="cursor: pointer;"/>'};
function getInputControl(propertyIndex) {
var type = entryEditConfig.property[propertyIndex].type;
var html = entryEditConfig.propertyType[type].inputControl;
if (entryEditConfig.property[propertyIndex].source > '') {
html = '<select id="{0}"></select>';
}
html = html.replace(/\{0\}/g, entryEditConfig.property[propertyIndex].tagName);
var size = entryEditConfig.property[propertyIndex].size;
var sizeNbr = parseInt(size);
if (sizeNbr > 0) {
if (size.indexOf('.') != -1) {
var parts = size.split('.');
sizeNbr = parseInt(parts[0]) + parseInt(parts[1]) + 1;
}
var boxSize = parseInt(((sizeNbr + 1)/2) + '');
if (boxSize > 60) {
boxSize = 60;
}
html = html.replace(/\{1\}/g, sizeNbr + '');
html = html.replace(/\{2\}/g, boxSize + '');
}
return html;
}
function populateForm(propertyIndex) {
var type = entryEditConfig.property[propertyIndex].type;
var value = entryEditConfig.property[propertyIndex].value;
var tagName = entryEditConfig.property[propertyIndex].tagName;
var source = entryEditConfig.property[propertyIndex].source;
if (source > '') {
var required = entryEditConfig.property[propertyIndex].inputRequired;
setUpSelectOptions(document.getElementById(tagName), entryEditConfig.source[source], value,
required);
} else {
if (type == 'bool') {
document.getElementById(tagName).checked = (value == 'true');
}
if (type == 'text') {
document.getElementById(tagName).value = value;
}
if (type == 'int') {
document.getElementById(tagName).value = value;
}
if (type == 'char') {
document.getElementById(tagName).value = value;
}
if (type == 'decimal') {
document.getElementById(tagName).value = value;
}
if (type == 'date') {
document.getElementById(tagName).value = value;
Calendar.setup({inputField: tagName, ifFormat: "%m/%d/%Y", showsTime: false, button:
"icon4" + tagName, singleClick: true, step: 1});
}
if (type == 'dateTime') {
document.getElementById(tagName).value = value;
Calendar.setup({inputField: tagName, ifFormat: "%m/%d/%Y %I:%M %p", showsTime: true,
button: "icon4" + tagName, singleClick: true, step: 1});
}
}
}
function setUpSelectOptions(elem, options, value, required) {
var i = 0;
i = parseInt(elem.length);
while (i > 0) {
i--;
elem.options[i] = null;
}
var j = 0;
if (!required) {
elem.options[j] = new Option(' (none)', '');
j++;
}
for (i=0; i<options.length; i++) {
elem.options[j] = new Option(options[i].label, options[i].value);
if (value > '') {
if (elem.options[j].value == value) {
elem.options[j].selected = true;
}
}
j++;
}
if (!(value > '')) {
elem.options[0].selected = true;
}
}
function validateProperty(propertyIndex) {
var name = entryEditConfig.property[propertyIndex].name;
var type = entryEditConfig.property[propertyIndex].type;
var size = entryEditConfig.property[propertyIndex].size;
var label = entryEditConfig.property[propertyIndex].label;
var required = entryEditConfig.property[propertyIndex].inputRequired;
var value = '';
var tagName = entryEditConfig.property[propertyIndex].tagName;
if (type == 'bool') {
value = 'false';
if (document.getElementById(tagName).checked) {
value = 'true';
}
}
if (type == 'text') {
value = document.getElementById(tagName).value;
if (required) {
if (!(value > '')) {
addInputError(label + ' is required');
}
}
}
if (type == 'int') {
value = document.getElementById(tagName).value;
if (required) {
if (!(value > '')) {
addInputError(label + ' is required');
}
}
}
if (type == 'char') {
value = document.getElementById(tagName).value;
if (required) {
if (!(value > '')) {
addInputError(label + ' is required');
}
}
}
if (type == 'decimal') {
value = document.getElementById(tagName).value;
if (required) {
if (!(value > '')) {
addInputError(label + ' is required');
}
}
}
if (type == 'date') {
value = document.getElementById(tagName).value;
if (required) {
if (!(value > '')) {
addInputError(label + ' is required');
}
}
}
if (type == 'dateTime') {
value = document.getElementById(tagName).value;
if (required) {
if (!(value > '')) {
addInputError(label + ' is required');
}
}
}
entryEditConfig.property[propertyIndex].value = value;
}
There are no parameters to pass, so the test is actually pretty simple code:
package org.restafarian.core.servlets.impl;
import org.restafarian.core.test.servlets.BaseServletTestCase;
import org.restafarian.core.test.servlets.GenericSpringTestServlet;
import com.meterware.httpunit.WebResponse;
/**
* <p>This class tests the LookupTablePropertyJavascriptServlet.</p>
*/
public class LookupTablePropertyJavascriptServletTest extends
BaseServletTestCase<GenericSpringTestServlet> {
/**
* <p>Constructs a new LookupTablePropertyJavascriptServletTest.</p>
*/
public LookupTablePropertyJavascriptServletTest() {
super("lookupTablePropertyJavascriptServlet", "propertyType.js");
}
/**
* <p>Tests the servlet's GET method.</p>
*/
public void testServletGet() {
try {
WebResponse webResponse = getGetMethodWebResponse();
assertEquals("Unexpected response code from testing LookupTablePropertyJavascriptServlet
GET method", 200, webResponse.getResponseCode());
assertTrue("Unexpected content returned from testing LookupTablePropertyJavascriptServlet
GET method: " + webResponse.getText(), webResponse.getText().trim().startsWith("//
generated by \"org.restafarian.core.servlets.impl.LookupTablePropertyJavascriptServlet\""));
assertTrue("Unexpected content returned from testing LookupTablePropertyJavascriptServlet
GET method: " + webResponse.getText(), webResponse.getText().trim().endsWith("}"));
} catch (Exception e) {
log.error("Error testing LookupTablePropertyJavascriptServlet GET method; Exception is " +
e, e);
fail("Error testing LookupTablePropertyJavascriptServlet GET method; Exception is " + e);
}
}
}
So far, I just been focused on performing the tests in all of these exercises, but at some point, I am going to have to go back and do a little work to make sure that the database is set up properly for each test. In this case, I need to make sure that the global dataType table is actually defined and has some entries in it. That’s not all that difficult to do; I just haven’t taken the time to add that part of the process to any of these servlet test just yet.
Leave a reply
You must be logged in to post a comment.





