Author: niallp Date: Mon Oct 31 07:12:50 2005 New Revision: 329836 URL: http://svn.apache.org/viewcvs?rev=329836&view=rev Log: Fix for Bug 37310 Search the locale 'hierarchy' of formsets for a Form - as a result of Wolfgang Gehner's feedback.
Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml (with props) jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java (with props) Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java?rev=329836&r1=329835&r2=329836&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java Mon Oct 31 07:12:50 2005 @@ -393,27 +393,61 @@ public Form getForm(String language, String country, String variant, String formKey) { + Form form = null; + + // Try language/country/variant String key = this.buildLocale(language, country, variant); - if (key.length() == 0) { - return defaultFormSet.getForm(formKey); + if (key.length() > 0) { + FormSet formSet = (FormSet)hFormSets.get(key); + if (formSet != null) { + form = formSet.getForm(formKey); + } } + String localeKey = key; - FormSet formSet = (FormSet) hFormSets.get(key); - if (formSet == null) { + // Try language/country + if (form == null) { key = buildLocale(language, country, null); - formSet = (FormSet) hFormSets.get(key); + if (key.length() > 0) { + FormSet formSet = (FormSet)hFormSets.get(key); + if (formSet != null) { + form = formSet.getForm(formKey); + } + } } - if (formSet == null) { + // Try language + if (form == null) { key = buildLocale(language, null, null); - formSet = (FormSet) hFormSets.get(key); + if (key.length() > 0) { + FormSet formSet = (FormSet)hFormSets.get(key); + if (formSet != null) { + form = formSet.getForm(formKey); + } + } + } + + // Try default formset + if (form == null) { + form = defaultFormSet.getForm(formKey); + key = "default"; } - if (formSet == null) { - formSet = defaultFormSet; + if (form == null) { + if (log.isWarnEnabled()) { + log.warn("Form '" + formKey + "' not found for locale '" + + localeKey + "'"); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Form '" + formKey + "' found in formset '" + + key + "' for locale '" + localeKey + "'"); + } } - return formSet.getForm(formKey); + + return form; + } /** Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml?rev=329836&view=auto ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml (added) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml Mon Oct 31 07:12:50 2005 @@ -0,0 +1,141 @@ +<!DOCTYPE form-validation PUBLIC + "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN" + "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd"> + +<!-- + Each of the form's in this test config has a field with a property name of + "testProperty" which has a variable named "localeVar" which has the value of + the locale for the Form. + + The "localeVar" is used to check that the form is retrieved from the expected + formset. +--> +<form-validation> + <global> + <validator name="required" + classname="org.apache.commons.validator.TestValidator" + method="validateRequired" + methodParams="java.lang.Object,org.apache.commons.validator.Field" + msg=""/> + </global> + + <!-- default formset --> + <formset> + + <form name="testForm_default"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>default</var-value> + </var> + </field> + </form> + + <form name="testForm_language"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>default</var-value> + </var> + </field> + </form> + + <form name="testForm_language_country"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>default</var-value> + </var> + </field> + </form> + + <form name="testForm_language_country_variant"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>default</var-value> + </var> + </field> + </form> + + </formset> + + <!-- French formset --> + <formset language="fr"> + + <form name="testForm_language"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr</var-value> + </var> + </field> + </form> + + <form name="testForm_language_country"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr</var-value> + </var> + </field> + </form> + + <form name="testForm_language_country_variant"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr</var-value> + </var> + </field> + </form> + + </formset> + + <!-- French formset --> + <formset language="fr" country="FR"> + + <form name="testForm_language_country"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr_FR</var-value> + </var> + </field> + </form> + + <form name="testForm_language_country_variant"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr_FR</var-value> + </var> + </field> + </form> + + </formset> + + <!-- Canadian French formset --> + <formset language="fr" country="CA"> + <form name="testForm_language_country"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr_CA</var-value> + </var> + </field> + </form> + + <form name="testForm_language_country_variant"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr_CA</var-value> + </var> + </field> + </form> + </formset> + + <!-- Candaian French Variant formset --> + <formset language="fr" country="CA" variant="XXX"> + + <form name="testForm_language_country_variant"> + <field property="testProperty" depends="required"> + <var><var-name>localeVar</var-name> + <var-value>fr_CA_XXX</var-value> + </var> + </field> + </form> + + </formset> + +</form-validation> Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest-config.xml ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java?rev=329836&view=auto ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java (added) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java Mon Oct 31 07:12:50 2005 @@ -0,0 +1,246 @@ +/* + * $Id$ + * $Rev$ + * $Date$ + * + * ==================================================================== + * Copyright 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.commons.validator; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Locale; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import org.xml.sax.SAXException; + +/** + * Tests retrieving forms using different Locales. + */ +public class RetrieveFormTest extends TestCase { + + /** + * Resources used for validation tests. + */ + private ValidatorResources resources = null; + + /** + * Prefix for the forms. + */ + private static final String FORM_PREFIX = "testForm_"; + + /** + * Prefix for the forms. + */ + private static final Locale CANADA_FRENCH_XXX = new Locale("fr", "CA", "XXX"); + + /** + * @return a test suite (<code>TestSuite</code>) that includes all methods + * starting with "test" + */ + public static Test suite() { + // All methods starting with "test" will be executed in the test suite. + return new TestSuite(RetrieveFormTest.class); + } + + /** + * Constructor for FormTest. + * @param name + */ + public RetrieveFormTest(String name) { + super(name); + } + + /** + * Load <code>ValidatorResources</code> from multiple xml files. + */ + protected void setUp() throws IOException, SAXException { + InputStream[] streams = + new InputStream[] { + this.getClass().getResourceAsStream( + "RetrieveFormTest-config.xml")}; + + this.resources = new ValidatorResources(streams); + + for (int i = 0; i < streams.length; i++) { + streams[i].close(); + } + } + + /** + * Test a form defined only in the "default" formset. + */ + public void testDefaultForm() throws ValidatorException { + + String formKey = FORM_PREFIX + "default"; + + // *** US locale *** + checkForm(Locale.US, formKey, "default"); + + // *** French locale *** + checkForm(Locale.FRENCH, formKey, "default"); + + // *** France locale *** + checkForm(Locale.FRANCE, formKey, "default"); + + // *** Candian (English) locale *** + checkForm(Locale.CANADA, formKey, "default"); + + // *** Candian French locale *** + checkForm(Locale.CANADA_FRENCH, formKey, "default"); + + // *** Candian French Variant locale *** + checkForm(CANADA_FRENCH_XXX, formKey, "default"); + + } + + /** + * Test a form defined in the "default" formset and formsets + * where just the "language" is specified. + */ + public void testLanguageForm() throws ValidatorException { + + String formKey = FORM_PREFIX + "language"; + + // *** US locale *** + checkForm(Locale.US, formKey, "default"); + + // *** French locale *** + checkForm(Locale.FRENCH, formKey, "fr"); + + // *** France locale *** + checkForm(Locale.FRANCE, formKey, "fr"); + + // *** Candian (English) locale *** + checkForm(Locale.CANADA, formKey, "default"); + + // *** Candian French locale *** + checkForm(Locale.CANADA_FRENCH, formKey, "fr"); + + // *** Candian French Variant locale *** + checkForm(CANADA_FRENCH_XXX, formKey, "fr"); + + } + + /** + * Test a form defined in the "default" formset, formsets + * where just the "language" is specified and formset where + * the language and country are specified. + */ + public void testLanguageCountryForm() throws ValidatorException { + + String formKey = FORM_PREFIX + "language_country"; + + // *** US locale *** + checkForm(Locale.US, formKey, "default"); + + // *** French locale *** + checkForm(Locale.FRENCH, formKey, "fr"); + + // *** France locale *** + checkForm(Locale.FRANCE, formKey, "fr_FR"); + + // *** Candian (English) locale *** + checkForm(Locale.CANADA, formKey, "default"); + + // *** Candian French locale *** + checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA"); + + // *** Candian French Variant locale *** + checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA"); + + } + + /** + * Test a form defined in all the formsets + */ + public void testLanguageCountryVariantForm() throws ValidatorException { + + String formKey = FORM_PREFIX + "language_country_variant"; + + // *** US locale *** + checkForm(Locale.US, formKey, "default"); + + // *** French locale *** + checkForm(Locale.FRENCH, formKey, "fr"); + + // *** France locale *** + checkForm(Locale.FRANCE, formKey, "fr_FR"); + + // *** Candian (English) locale *** + checkForm(Locale.CANADA, formKey, "default"); + + // *** Candian French locale *** + checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA"); + + // *** Candian French Variant locale *** + checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA_XXX"); + + } + + /** + * Test a form not defined + */ + public void testFormNotFound() throws ValidatorException { + + String formKey = "INVALID_NAME"; + + // *** US locale *** + checkFormNotFound(Locale.US, formKey); + + // *** French locale *** + checkFormNotFound(Locale.FRENCH, formKey); + + // *** France locale *** + checkFormNotFound(Locale.FRANCE, formKey); + + // *** Candian (English) locale *** + checkFormNotFound(Locale.CANADA, formKey); + + // *** Candian French locale *** + checkFormNotFound(Locale.CANADA_FRENCH, formKey); + + // *** Candian French Variant locale *** + checkFormNotFound(CANADA_FRENCH_XXX, formKey); + + + } + + private void checkForm(Locale locale, String formKey, String expectedVarValue) { + + // Retrieve the Form + Form testForm = resources.getForm(locale, formKey); + assertNotNull("Form '" +formKey+"' null for locale " + locale, testForm); + + // Validate the expected Form is retrieved by checking the "localeVar" + // value of the field. + Field testField = (Field)testForm.getField("testProperty"); + assertEquals("Incorrect Form '" + formKey + "' for locale '" + locale + "'", + expectedVarValue, + testField.getVarValue("localeVar")); + } + + private void checkFormNotFound(Locale locale, String formKey) { + + // Retrieve the Form + Form testForm = resources.getForm(locale, formKey); + assertNull("Form '" +formKey+"' not null for locale " + locale, testForm); + + } + +} Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/RetrieveFormTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java?rev=329836&r1=329835&r2=329836&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java (original) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java Mon Oct 31 07:12:50 2005 @@ -64,6 +64,7 @@ suite.addTestSuite(ExceptionTest.class); suite.addTest(UrlTest.suite()); suite.addTest(VarTest.suite()); + suite.addTest(RetrieveFormTest.suite()); return suite; } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]