You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2008/12/20 23:03:33 UTC
svn commit: r728353 - in /wicket/trunk/wicket/src:
main/java/org/apache/wicket/Localizer.java
test/java/org/apache/wicket/LocalizerTest.java
Author: jdonnerstag
Date: Sat Dec 20 14:03:33 2008
New Revision: 728353
URL: http://svn.apache.org/viewvc?rev=728353&view=rev
Log:
wicket-1851: Default lookup keys don't work when UseDefaultOnMissingResource is false
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java
wicket/trunk/wicket/src/test/java/org/apache/wicket/LocalizerTest.java
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java?rev=728353&r1=728352&r2=728353&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java Sat Dec 20 14:03:33 2008
@@ -159,10 +159,16 @@
}
/**
- * Get the localized string using all of the supplied parameters. This method is left public to
- * allow developers full control over string resource loading. However, it is recommended that
- * one of the other convenience methods in the class are used as they handle all of the work
- * related to obtaining the current user locale and style information.
+ * This is similar to {@link #getString(String, Component, IModel, String)} except that the
+ * resource settings are ignored. This allows to to code something like
+ *
+ * <pre>
+ * String option = getLocalizer().getStringIgnoreSettings(getId() + ".null", this, "");
+ * if (Strings.isEmpty(option))
+ * {
+ * option = getLocalizer().getString("null", this, CHOOSE_ONE);
+ * }
+ * </pre>
*
* @param key
* The key to obtain the resource for
@@ -173,14 +179,10 @@
* @param defaultValue
* The default value (optional)
* @return The string resource
- * @throws MissingResourceException
- * If resource not found and configuration dictates that exception should be thrown
*/
- public String getString(final String key, final Component component, final IModel<?> model,
- final String defaultValue) throws MissingResourceException
+ public String getStringIgnoreSettings(final String key, final Component component,
+ final IModel<?> model, final String defaultValue)
{
- final IResourceSettings resourceSettings = Application.get().getResourceSettings();
-
boolean addedToPage = false;
if (component != null)
{
@@ -199,9 +201,8 @@
}
}
-
String cacheKey = null;
- String string = null;
+ String value = null;
// If this component is not yet added to page we do not want to check
// cache as we can generate an invalid cache key
@@ -213,20 +214,22 @@
// Value not found are cached as well (value = null)
if ((cacheKey != null) && cache.containsKey(cacheKey))
{
- string = getFromCache(cacheKey);
+ value = getFromCache(cacheKey);
}
else
{
// Iterate over all registered string resource loaders until the
// property has been found
-
- Iterator<IStringResourceLoader> iter = resourceSettings.getStringResourceLoaders()
+ Iterator<IStringResourceLoader> iter = Application.get()
+ .getResourceSettings()
+ .getStringResourceLoaders()
.iterator();
+
while (iter.hasNext())
{
IStringResourceLoader loader = iter.next();
- string = loader.loadStringResource(component, key);
- if (string != null)
+ value = loader.loadStringResource(component, key);
+ if (value != null)
{
break;
}
@@ -235,37 +238,79 @@
// Cache the result incl null if not found
if (cacheKey != null)
{
- putIntoCache(cacheKey, string);
+ putIntoCache(cacheKey, value);
}
}
- if ((string == null) && (defaultValue != null))
+ if (value == null)
+ {
+ value = defaultValue;
+ }
+
+ // If a property value has been found, or a default value was given,
+ // than replace the placeholder and we are done
+ if (value != null)
+ {
+ return substitutePropertyExpressions(component, value, model);
+ }
+
+ return null;
+ }
+
+ /**
+ * Get the localized string using all of the supplied parameters. This method is left public to
+ * allow developers full control over string resource loading. However, it is recommended that
+ * one of the other convenience methods in the class are used as they handle all of the work
+ * related to obtaining the current user locale and style information.
+ *
+ * @param key
+ * The key to obtain the resource for
+ * @param component
+ * The component to get the resource for (optional)
+ * @param model
+ * The model to use for substitutions in the strings (optional)
+ * @param defaultValue
+ * The default value (optional)
+ * @return The string resource
+ * @throws MissingResourceException
+ * If resource not found and configuration dictates that exception should be thrown
+ */
+ public String getString(final String key, final Component component, final IModel<?> model,
+ final String defaultValue) throws MissingResourceException
+ {
+ final IResourceSettings resourceSettings = Application.get().getResourceSettings();
+
+ String value = getStringIgnoreSettings(key, component, model, null);
+ if ((value == null) && (defaultValue != null))
{
// Resource not found, so handle missing resources based on
// application configuration and try the default value
if (resourceSettings.getUseDefaultOnMissingResource())
{
- string = defaultValue;
+ value = defaultValue;
}
}
// If a property value has been found, or a default value was given,
// than replace the placeholder and we are done
- if (string != null)
+ if (value != null)
{
- return substitutePropertyExpressions(component, string, model);
+ return substitutePropertyExpressions(component, value, model);
}
if (resourceSettings.getThrowExceptionOnMissingResource())
{
- AppendingStringBuffer message = new AppendingStringBuffer("Unable to find property: '" +
- key + "'");
+ AppendingStringBuffer message = new AppendingStringBuffer("Unable to find property: '");
+ message.append(key);
+ message.append("'");
+
if (component != null)
{
message.append(" for component: ");
message.append(component.getPageRelativePath());
message.append(" [class=").append(component.getClass().getName()).append("]");
}
+
throw new MissingResourceException(message.toString(), (component != null
? component.getClass().getName() : ""), key);
}
Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/LocalizerTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/LocalizerTest.java?rev=728353&r1=728352&r2=728353&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/LocalizerTest.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/LocalizerTest.java Sat Dec 20 14:03:33 2008
@@ -22,6 +22,7 @@
import java.util.MissingResourceException;
import junit.framework.Assert;
+
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
@@ -32,6 +33,7 @@
import org.apache.wicket.resource.DummyApplication;
import org.apache.wicket.resource.loader.ComponentStringResourceLoader;
import org.apache.wicket.settings.IResourceSettings;
+import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.tester.WicketTester;
import org.apache.wicket.util.value.ValueMap;
@@ -209,8 +211,54 @@
"DEFAULT {user}"));
Assert.assertEquals("Expected string should be returned", "DEFAULT juergen",
- localizer.getString("test.substituteDoesNotExist", null,
- new PropertyModel<String>(model, null), "DEFAULT ${user}"));
+ localizer.getString("test.substituteDoesNotExist", null, new PropertyModel<String>(
+ model, null), "DEFAULT ${user}"));
+ }
+
+ /**
+ * See https://issues.apache.org/jira/browse/WICKET-1851
+ */
+ public void test_1851_1()
+ {
+ MyMockPage page = new MyMockPage();
+
+ tester.getApplication().getResourceSettings().setThrowExceptionOnMissingResource(false);
+ tester.getApplication().getResourceSettings().setUseDefaultOnMissingResource(false);
+
+ String option = localizer.getStringIgnoreSettings("dummy.null", page.drop1, null, "default");
+ assertEquals(option, "default");
+
+ option = localizer.getStringIgnoreSettings("dummy.null", page.drop1, null, null);
+ assertNull(option);
+ if (Strings.isEmpty(option))
+ {
+ option = localizer.getString("null", page.drop1, "CHOOSE_ONE");
+ }
+ assertEquals(option, "value 1");
+
+ tester.getApplication().getResourceSettings().setThrowExceptionOnMissingResource(false);
+ tester.getApplication().getResourceSettings().setUseDefaultOnMissingResource(false);
+
+ option = localizer.getString("dummy.null", page.drop1, null, "default");
+ assertEquals(option, "[Warning: Property for 'dummy.null' not found]");
+
+ tester.getApplication().getResourceSettings().setThrowExceptionOnMissingResource(true);
+ tester.getApplication().getResourceSettings().setUseDefaultOnMissingResource(true);
+
+ option = localizer.getString("dummy.null", page.drop1, null, "default");
+ assertEquals(option, "default");
+
+ try
+ {
+ localizer.getString("dummy.null", page.drop1, null, null);
+ assertTrue("Expected an exception to happen", false);
+ }
+ catch (MissingResourceException ex)
+ {
+ assertEquals(
+ ex.getMessage(),
+ "Unable to find property: 'dummy.null' for component: form:drop1 [class=org.apache.wicket.markup.html.form.DropDownChoice]");
+ }
}
/**