You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by pf...@apache.org on 2005/06/05 01:53:21 UTC
cvs commit: jakarta-tapestry/framework/src/test/org/apache/tapestry/form FormComponentContributorTestCase.java
pferraro 2005/06/04 16:53:21
Modified: framework/src/java/org/apache/tapestry/valid
ValidationStrings.properties
Added: framework/src/java/org/apache/tapestry/form
FormComponentContributor.java Form.js
AbstractFormComponentContributor.java
framework/src/java/org/apache/tapestry/form/translator
NumberTranslator.js NumberTranslator.java
Translator.java StringTranslator.java
FormatTranslator.java DateTranslator.java
AbstractTranslator.java
framework/src/java/org/apache/tapestry/valid
ValidationStrings.java
framework/src/test/org/apache/tapestry/form/translator
TestNumberTranslator.java TestStringTranslator.java
TestDateTranslator.java TranslatorTestCase.java
framework/src/test/org/apache/tapestry/form
FormComponentContributorTestCase.java
Log:
Added Translator interface and implementations for dates, numbers, and strings.
Revision Changes Path
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/FormComponentContributor.java
Index: FormComponentContributor.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
/**
* Interface for objects that contribute client-side events back to a form
*
* @author Paul Ferraro
* @since 4.0
*/
public interface FormComponentContributor
{
/**
* Invoked by a form component after it finishes rendering its tag (but before
* the tag is closed) to allow this object to contribute to the component's
* rendering process. Typically used by Validators and Translators to add
* javascript methods to the form's submit event handler.
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field);
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/Form.js
Index: Form.js
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function handle_invalid_field(field, message)
{
focus(field);
window.alert(message);
return false;
}
function focus(field)
{
field.focus();
if (field.select)
{
field.select();
}
}
function trim(field)
{
field.value = field.value.replace(/^\s+/g, '').replace(/\s+$/g, '');
return true;
}
function require(field, message)
{
if (field.value && (field.value.length == 0))
{
return handle_invalid_field(field, message)
}
return true
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/AbstractFormComponentContributor.java
Index: AbstractFormComponentContributor.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form;
import org.apache.hivemind.Resource;
import org.apache.hivemind.util.ClasspathResource;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.TapestryUtils;
/**
* Abstract {@link FormComponentContributor} implementation that adds an optional static
* javscript method reference to the page.
*
* @author Paul Ferraro
* @since 4.0
*/
public abstract class AbstractFormComponentContributor implements FormComponentContributor
{
private String _script = defaultScript();
/**
* Defines the default JavaScript file used by this contributor
*/
protected String defaultScript()
{
return null;
}
public String getScript()
{
return _script;
}
public void setScript(String script)
{
_script = script;
}
/**
* @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, org.apache.tapestry.form.IFormComponent)
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field)
{
if (_script != null)
{
Resource script = new ClasspathResource(cycle.getEngine().getClassResolver(), _script);
TapestryUtils.getPageRenderSupport(cycle, field).addExternalScript(script);
}
}
/**
* Helper method that adds the specified submit handler to to the specified form.
*/
protected void addSubmitHandler(IForm form, String handler)
{
form.addEventHandler(FormEventType.SUBMIT, handler);
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/NumberTranslator.js
Index: NumberTranslator.js
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function validate_number(field, message)
{
if (isNaN(field.value))
{
return handle_invalid_field(field, message)
}
return true
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/NumberTranslator.java
Index: NumberTranslator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.util.Locale;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
/**
* A {@link java.text.DecimalFormat}-based {@link Translator} implementation.
*
* @author Paul Ferraro
* @since 4.0
*/
public class NumberTranslator extends FormatTranslator
{
/**
* @see org.apache.tapestry.form.AbstractFormComponentContributor#defaultScript()
*/
protected String defaultScript()
{
return "/org/apache/tapestry/form/translator/DecimalTranslator.js";
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
*/
protected String defaultPattern()
{
return "#";
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
*/
protected Format getFormat(Locale locale)
{
return getDecimalFormat(locale);
}
public DecimalFormat getDecimalFormat(Locale locale)
{
return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale));
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
*/
protected String getMessageKey()
{
return ValidationStrings.INVALID_NUMBER;
}
/**
* @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale, java.lang.String)
*/
protected Object[] getMessageParameters(Locale locale, String label)
{
String pattern = getDecimalFormat(locale).toLocalizedPattern();
return new Object[] { label, pattern };
}
/**
* @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, org.apache.tapestry.form.IFormComponent)
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field)
{
super.renderContribution(writer, cycle, field);
String message = buildMessage(field, getMessageKey());
IForm form = field.getForm();
addSubmitHandler(form, "validate_number(document." + form.getName() + "." + field.getName() + ",'" + message + "')");
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
*/
protected ValidationConstraint getConstraint()
{
return ValidationConstraint.NUMBER_FORMAT;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/Translator.java
Index: Translator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.FormComponentContributor;
import org.apache.tapestry.valid.ValidatorException;
/**
* Interface used by {@link ValidatableField}s to both format an object as text and
* translate submitted text into an appropriate object for a given field.
*
* @author Paul Ferraro
* @since 4.0
*/
public interface Translator extends FormComponentContributor
{
/**
* Invoked during rendering to format an object (which may be null) into a
* text value (which should not be null) appropriate for the specified field.
*/
public String format(IFormComponent field, Object object);
/**
* Invoked during rewind to parse a submitted input value into an object suitable
* for the specified component.
* @throws ValidatorException if the specified text could not be parsed into an object.
*/
public Object parse(IFormComponent field, String value) throws ValidatorException;
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/StringTranslator.java
Index: StringTranslator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import org.apache.tapestry.form.IFormComponent;
/**
* A trivial {@link Translator} implementation. By default, empty text submissions
* are interpretted as null.
*
* @author Paul Ferraro
* @since 4.0
*/
public class StringTranslator extends AbstractTranslator
{
private String _empty = null;
/**
* @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent, java.lang.String)
*/
protected Object parseText(IFormComponent field, String text)
{
return text;
}
/**
* @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
protected String formatObject(IFormComponent field, Object object)
{
return object.toString();
}
public Object getEmpty()
{
return _empty;
}
public void setEmpty(String empty)
{
_empty = empty;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/FormatTranslator.java
Index: FormatTranslator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import java.text.Format;
import java.text.ParseException;
import java.util.Locale;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;
/**
* Abstract {@link Translator} implementation for {@link java.text.Format}-based
* translators.
*
* @author Paul Ferraro
* @since 4.0
*/
public abstract class FormatTranslator extends AbstractTranslator
{
private String _pattern = defaultPattern();
protected abstract String defaultPattern();
/**
* @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
protected String formatObject(IFormComponent field, Object object)
{
Format format = getFormat(field.getPage().getLocale());
return format.format(object);
}
/**
* @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent, java.lang.String)
*/
protected Object parseText(IFormComponent field, String text) throws ValidatorException
{
Format format = getFormat(field.getPage().getLocale());
try
{
return format.parseObject(text);
}
catch (ParseException e)
{
throw new ValidatorException(buildMessage(field, getMessageKey()), getConstraint());
}
}
protected abstract ValidationConstraint getConstraint();
protected abstract Format getFormat(Locale locale);
protected abstract String getMessageKey();
public String getPattern()
{
return _pattern;
}
public void setPattern(String pattern)
{
_pattern = pattern;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/DateTranslator.java
Index: DateTranslator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import java.text.DateFormatSymbols;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Locale;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
/**
* A {@link java.text.SimpleDateFormat}-based {@link Translator} implementation.
*
* @author Paul Ferraro
* @since 4.0
*/
public class DateTranslator extends FormatTranslator
{
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
*/
protected String defaultPattern()
{
return "MM/dd/yyyy";
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
*/
protected Format getFormat(Locale locale)
{
return getDateFormat(locale);
}
public SimpleDateFormat getDateFormat(Locale locale)
{
return new SimpleDateFormat(getPattern(), new DateFormatSymbols(locale));
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
*/
protected String getMessageKey()
{
return ValidationStrings.INVALID_DATE;
}
/**
* @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale, java.lang.String)
*/
protected Object[] getMessageParameters(Locale locale, String label)
{
String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale);
return new Object[] { label, pattern };
}
/**
* @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
*/
protected ValidationConstraint getConstraint()
{
return ValidationConstraint.DATE_FORMAT;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/translator/AbstractTranslator.java
Index: AbstractTranslator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import java.text.MessageFormat;
import java.util.Locale;
import org.apache.hivemind.HiveMind;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.AbstractFormComponentContributor;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* Abstract {@link Translator} implementation that provides default behavior for
* trimming, null object, and empty text handling.
*
* @author Paul Ferraro
* @since 4.0
*/
public abstract class AbstractTranslator extends AbstractFormComponentContributor implements Translator
{
private boolean _trim;
private String _message;
/**
* @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
public String format(IFormComponent field, Object object)
{
return (object != null) ? formatObject(field, object) : "";
}
/**
* @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent, java.lang.String)
*/
public Object parse(IFormComponent field, String text) throws ValidatorException
{
String value = _trim ? text.trim() : text;
return HiveMind.isBlank(value) ? getEmpty() : parseText(field, value);
}
protected abstract String formatObject(IFormComponent field, Object object);
protected abstract Object parseText(IFormComponent field, String text) throws ValidatorException;
protected Object getEmpty()
{
return null;
}
protected String buildMessage(IFormComponent field, String key)
{
Locale locale = field.getPage().getLocale();
String pattern = (_message == null) ? ValidationStrings.getMessagePattern(key, locale) : _message;
String name = field.getDisplayName();
return MessageFormat.format(pattern, getMessageParameters(locale, name));
}
protected Object[] getMessageParameters(Locale locale, String label)
{
return new Object[] { label };
}
/**
* @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle, org.apache.tapestry.form.IFormComponent)
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field)
{
super.renderContribution(writer, cycle, field);
if (_trim)
{
IForm form = field.getForm();
addSubmitHandler(form, "trim(document." + form.getName() + "." + field.getName() + ")");
}
}
public boolean isTrim()
{
return _trim;
}
public void setTrim(boolean trim)
{
this._trim = trim;
}
public String getMessage()
{
return _message;
}
public void setMessage(String message)
{
_message = message;
}
}
1.3 +14 -0 jakarta-tapestry/framework/src/java/org/apache/tapestry/valid/ValidationStrings.properties
Index: ValidationStrings.properties
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/valid/ValidationStrings.properties,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ValidationStrings.properties 6 Jan 2005 02:17:23 -0000 1.2
+++ ValidationStrings.properties 4 Jun 2005 23:53:21 -0000 1.3
@@ -34,3 +34,17 @@
invalid-url-format = Invalid URL.
disallowed-protocol = Disallowed protocol - protocol must be {0}.
+
+### 4.0 ###
+
+select-field-is-required=You must select a value for {0}.
+file-field-is-required=You must select a file to upload.
+
+field-too-long=You must enter no more than {0} characters for {1}.
+
+date-too-small={0} may not be earlier than {1,date}.
+date-too-large={0} may not be later than {1,date}.
+
+regex-mismatch={0} is invalid.
+
+invalid-field-equality={0} must be {1,choice,0#different from|1#the same as} {2}.
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/valid/ValidationStrings.java
Index: ValidationStrings.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.valid;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Constants used for accessing validation message patterns.
*
* @author Paul Ferraro
*/
public final class ValidationStrings
{
public static final String REQUIRED_TEXT_FIELD = "field-is-required";
public static final String REQUIRED_SELECT_FIELD = "select-field-is-required";
public static final String REQUIRED_FILE_FIELD = "file-field-is-required";
public static final String INVALID_DATE = "invalid-date-format";
public static final String INVALID_NUMBER = "invalid-numeric-format";
public static final String INVALID_EMAIL = "invalid-email-format";
public static final String REGEX_MISMATCH = "regex-mismatch";
public static final String VALUE_TOO_SHORT = "field-too-short";
public static final String VALUE_TOO_LONG = "field-too-long";
public static final String VALUE_TOO_SMALL = "number-too-small";
public static final String VALUE_TOO_LARGE = "number-too-large";
public static final String DATE_TOO_EARLY = "date-too-small";
public static final String DATE_TOO_LATE = "date-too-large";
public static final String INVALID_FIELD_EQUALITY = "invalid-field-equality";
private static final String RESOURCE_BUNDLE = ValidationStrings.class.getName();
/**
* Fetches the appropriate validation message pattern from the appropriate localized resource.
* This method should be called with the locale of the current request.
*/
public static String getMessagePattern(String key, Locale locale)
{
return ResourceBundle.getBundle(RESOURCE_BUNDLE, locale).getString(key);
}
private ValidationStrings()
{
// Disable construction
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestNumberTranslator.java
Index: TestNumberTranslator.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import java.util.Locale;
import org.apache.tapestry.form.FormEventType;
import org.apache.tapestry.valid.ValidatorException;
public class TestNumberTranslator extends TranslatorTestCase
{
private NumberTranslator _translator = new NumberTranslator();
public void testDefaultFormat()
{
testFormat(new Integer(10), "10");
}
public void testCustomFormat()
{
_translator.setPattern("$#0.00");
testFormat(new Integer(10), "$10.00");
}
public void testFormat(Number number, String expected)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
replay();
String result = _translator.format(_component, number);
assertEquals(expected, result);
verify();
}
public void testNullFormat()
{
replay();
String result = _translator.format(_component, null);
assertEquals("", result);
verify();
}
public void testDefaultParse()
{
testParse("0.1", new Double(0.1));
}
public void testCustomParse()
{
_translator.setPattern("#%");
testParse("10%", new Double(0.1));
}
public void testTrimmedParse()
{
_translator.setTrim(true);
testParse(" 100 ", new Long(100));
}
private void testParse(String number, Number expected)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
replay();
try
{
Number result = (Number) _translator.parse(_component, number);
assertEquals(expected, result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testFailedParseDefaultMessage()
{
testFailedParse("Field Name must be a numeric value.");
}
public void testFailedParseCustomMessage()
{
String message = "Field Name is an invalid number.";
_translator.setMessage(message);
testFailedParse(message);
}
private void testFailedParse(String message)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Name");
replay();
try
{
System.out.println(_translator.parse(_component, "Bad-Number"));
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
}
finally
{
verify();
}
}
public void testContributeFormEvents()
{
addScript("/org/apache/tapestry/form/translator/NumberTranslator.js");
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
_form.addEventHandler(FormEventType.SUBMIT, "validate_number(document.formName.fieldName,'Field Label must be a numeric value.')");
_formControl.setVoidCallable();
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
public void testMessageContributeFormEvents()
{
_translator.setMessage("You entered a bunk value for {0}. I should look like {1}.");
addScript("/org/apache/tapestry/form/translator/NumberTranslator.js");
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
_form.addEventHandler(FormEventType.SUBMIT, "validate_number(document.formName.fieldName,'You entered a bunk value for Field Label. I should look like #.')");
_formControl.setVoidCallable();
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
public void testTrimContributeFormEvents()
{
_translator.setTrim(true);
trim();
addScript("/org/apache/tapestry/form/translator/NumberTranslator.js");
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
_form.addEventHandler(FormEventType.SUBMIT, "validate_number(document.formName.fieldName,'Field Label must be a numeric value.')");
_formControl.setVoidCallable();
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestStringTranslator.java
Index: TestStringTranslator.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import org.apache.tapestry.valid.ValidatorException;
/**
* Test case for {@link StringTranslator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestStringTranslator extends TranslatorTestCase
{
private StringTranslator _translator = new StringTranslator();
public void testFormat()
{
replay();
String result = _translator.format(_component, "Test this");
assertEquals("Test this", result);
verify();
}
public void testNullFormat()
{
replay();
String result = _translator.format(_component, null);
assertEquals("", result);
verify();
}
public void testParse()
{
replay();
try
{
String result = (String) _translator.parse(_component, "Test this");
assertEquals("Test this", result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testTrimmedParse()
{
_translator.setTrim(true);
replay();
try
{
String result = (String) _translator.parse(_component, " Test this ");
assertEquals("Test this", result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testEmptyParse()
{
replay();
try
{
String result = (String) _translator.parse(_component, "");
assertEquals(null, result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testCustomEmptyParse()
{
_translator.setEmpty("");
replay();
try
{
String result = (String) _translator.parse(_component, "");
assertEquals("", result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testContributeFormEvents()
{
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
public void testTrimContributeFormEvents()
{
_translator.setTrim(true);
trim();
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestDateTranslator.java
Index: TestDateTranslator.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.tapestry.valid.ValidatorException;
/**
* Test case for {@link DateTranslator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestDateTranslator extends TranslatorTestCase
{
private DateTranslator _translator = new DateTranslator();
private Calendar _calendar = Calendar.getInstance();
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
_calendar.clear();
}
private Date buildDate(int year, int month, int day)
{
_calendar.set(Calendar.YEAR, year);
_calendar.set(Calendar.MONTH, month);
_calendar.set(Calendar.DATE, day);
return _calendar.getTime();
}
public void testDefaultFormat()
{
testFormat(buildDate(1976, Calendar.OCTOBER, 29), "10/29/1976");
}
public void testCustomFormat()
{
_translator.setPattern("yyyy-MM-dd");
testFormat(buildDate(1976, Calendar.OCTOBER, 29), "1976-10-29");
}
public void testFormat(Date date, String expected)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
replay();
String result = _translator.format(_component, date);
assertEquals(expected, result);
verify();
}
public void testNullFormat()
{
replay();
String result = _translator.format(_component, null);
assertEquals("", result);
verify();
}
public void testDefaultParse()
{
testParse("10/29/1976", buildDate(1976, Calendar.OCTOBER, 29));
}
public void testCustomParse()
{
_translator.setPattern("yyyy-MM-dd");
testParse("1976-10-29", buildDate(1976, Calendar.OCTOBER, 29));
}
public void testTrimmedParse()
{
_translator.setTrim(true);
testParse(" 10/29/1976 ", buildDate(1976, Calendar.OCTOBER, 29));
}
public void testEmptyParse()
{
replay();
try
{
Date result = (Date) _translator.parse(_component, "");
assertEquals(null, result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
private void testParse(String date, Date expected)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
replay();
try
{
Date result = (Date) _translator.parse(_component, date);
assertEquals(expected, result);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testFailedParseDefaultMessage()
{
testFailedParse("Invalid date format for Field Name. Format is MM/DD/YYYY.");
}
public void testFailedParseCustomMessage()
{
String message = "Field Name is an invalid date.";
_translator.setMessage(message);
testFailedParse(message);
}
private void testFailedParse(String message)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Name");
replay();
try
{
System.out.println(_translator.parse(_component, "Bad-Date"));
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
}
finally
{
verify();
}
}
public void testContributeFormEvents()
{
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
public void testTrimContributeFormEvents()
{
_translator.setTrim(true);
trim();
replay();
_translator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TranslatorTestCase.java
Index: TranslatorTestCase.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form.translator;
import org.apache.tapestry.form.FormEventType;
import org.apache.tapestry.form.FormComponentContributorTestCase;
/**
* Abstract test case for {@link Translator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public abstract class TranslatorTestCase extends FormComponentContributorTestCase
{
protected void trim()
{
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
_form.addEventHandler(FormEventType.SUBMIT, "trim(document.formName.fieldName)");
_formControl.setVoidCallable();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/FormComponentContributorTestCase.java
Index: FormComponentContributorTestCase.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.form;
import org.apache.tapestry.IEngine;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.PageRenderSupport;
import org.apache.tapestry.junit.TapestryTestCase;
import org.easymock.MockControl;
/**
* Abstract test case for {@link FormComponentContributor}.
*
* @author Paul Ferraro
* @since 4.0
*/
public abstract class FormComponentContributorTestCase extends TapestryTestCase
{
protected MockControl _componentControl = MockControl.createControl(IFormComponent.class);
protected IFormComponent _component = (IFormComponent) _componentControl.getMock();
protected MockControl _pageControl = MockControl.createControl(IPage.class);
protected IPage _page = (IPage) _pageControl.getMock();
protected MockControl _cycleControl = MockControl.createControl(IRequestCycle.class);
protected IRequestCycle _cycle = (IRequestCycle) _cycleControl.getMock();
protected MockControl _formControl = MockControl.createControl(IForm.class);
protected IForm _form = (IForm) _formControl.getMock();
protected MockControl _engineControl = MockControl.createControl(IEngine.class);
protected IEngine _engine = (IEngine) _engineControl.getMock();
protected MockControl _pageRenderSupportControl = MockControl.createControl(PageRenderSupport.class);
protected PageRenderSupport _pageRenderSupport = (PageRenderSupport) _pageRenderSupportControl.getMock();
/**
* @see org.apache.hivemind.test.HiveMindTestCase#tearDown()
*/
protected void tearDown() throws Exception
{
_componentControl.reset();
_pageControl.reset();
_cycleControl.reset();
_formControl.reset();
_engineControl.reset();
super.tearDown();
}
protected void replay()
{
_componentControl.replay();
_pageControl.replay();
_cycleControl.replay();
_formControl.replay();
_engineControl.replay();
}
protected void verify()
{
_componentControl.verify();
_pageControl.verify();
_cycleControl.verify();
_formControl.verify();
_engineControl.verify();
}
protected void addScript(String script)
{
_cycle.getEngine();
_cycleControl.setReturnValue(_engine);
_engine.getClassResolver();
_engineControl.setReturnValue(null);
_cycle.getAttribute("org.apache.tapestry.PageRenderSupport");
_cycleControl.setReturnValue(_pageRenderSupport);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org