Author: jdonnerstag
Date: Wed Jul 6 20:34:11 2011
New Revision: 1143552
URL: http://svn.apache.org/viewvc?rev=1143552&view=rev
Log:
fixed: StringResourceModel doesn't handle correctly resources containing a
single quote
Issue: WICKET-3873
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.properties
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java?rev=1143552&r1=1143551&r2=1143552&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java
Wed Jul 6 20:34:11 2011
@@ -273,7 +273,7 @@ public class StringResourceModel extends
* to take place on either the resource key or the actual resource
strings.
* <p>
* The parameters parameter is also optional and is used for
substitutions.
- *
+ *
* @param resourceKey
* The resource key for this string resource
* @param component
@@ -468,12 +468,19 @@ public class StringResourceModel extends
}
}
+ // Escape all single quotes outside {..}
+ if (value.indexOf('\'') != -1)
+ {
+ value = escapeQuotes(value);
+ }
+
if (model != null)
{
// First escape all substitute
properties so that message format doesn't try to
// parse that.
value = Strings.replaceAll(value, "${",
"$'{'").toString();
}
+
// Apply the parameters
final MessageFormat format = new
MessageFormat(value, component != null
? component.getLocale() : locale);
@@ -494,6 +501,39 @@ public class StringResourceModel extends
}
/**
+ * Replace "'" with "''" outside of "{..}"
+ *
+ * @param value
+ * @return escaped message format
+ */
+ private String escapeQuotes(final String value)
+ {
+ StringBuilder newValue = new StringBuilder(value.length() + 10);
+ int count = 0;
+ for (int i = 0; i < value.length(); i++)
+ {
+ char ch = value.charAt(i);
+ if (ch == '{')
+ {
+ count += 1;
+ }
+ else if (ch == '}')
+ {
+ count -= 1;
+ }
+
+ newValue.append(ch);
+ if ((ch == '\'') && (count == 0))
+ {
+ // Escape "'"
+ newValue.append(ch);
+ }
+ }
+
+ return newValue.toString();
+ }
+
+ /**
* Sets the localizer that is being used by this string resource model.
This method is provided
* to allow the default application localizer to be overridden if
required.
*
@@ -514,7 +554,7 @@ public class StringResourceModel extends
@Override
public String toString()
{
- StringBuilder sb = new StringBuilder("StringResourceModel[");
+ StringBuilder sb = new StringBuilder("StringResourceModel[");
sb.append("key:");
sb.append(resourceKey);
sb.append(",default:");
Modified:
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java?rev=1143552&r1=1143551&r2=1143552&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java
(original)
+++
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java
Wed Jul 6 20:34:11 2011
@@ -27,6 +27,7 @@ import org.apache.wicket.WicketTestCase;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.MockPage;
import org.apache.wicket.resource.loader.BundleStringResourceLoader;
+import org.junit.Test;
/**
* Test cases for the <code>StringResourceModel</code> class.
@@ -66,11 +67,9 @@ public class StringResourceModelTest ext
}
- /**
- *
- *
- */
- public void testGetSimpleResource()
+ /** */
+ @Test
+ public void getSimpleResource()
{
StringResourceModel model = new
StringResourceModel("simple.text", page, null);
Assert.assertEquals("Text should be as expected", "Simple
text", model.getString());
@@ -78,28 +77,16 @@ public class StringResourceModelTest ext
// Assert.assertEquals("Text should be as expected", "Simple text",
model.toString());
}
- /**
- *
- *
- */
- public void testNullResourceKey()
+ /** */
+ @Test(expected = IllegalArgumentException.class)
+ public void nullResourceKey()
{
- try
- {
- new StringResourceModel(null, page, null);
- Assert.fail("IllegalArgumentException expected");
- }
- catch (IllegalArgumentException e)
- {
- // Expected result
- }
+ new StringResourceModel(null, page, null);
}
- /**
- *
- *
- */
- public void testGetSimpleResourceWithKeySubstitution()
+ /** */
+ @Test
+ public void getSimpleResourceWithKeySubstitution()
{
StringResourceModel model = new
StringResourceModel("weather.${currentStatus}", page,
wsModel);
@@ -110,11 +97,9 @@ public class StringResourceModelTest ext
model.getString());
}
- /**
- *
- *
- */
- public void testGetPropertySubstitutedResource()
+ /** */
+ @Test
+ public void getPropertySubstitutedResource()
{
tester.getSession().setLocale(Locale.ENGLISH);
StringResourceModel model = new
StringResourceModel("weather.message", page, wsModel);
@@ -129,11 +114,9 @@ public class StringResourceModelTest ext
model.getString());
}
- /**
- *
- *
- */
- public void testSubstitutedPropertyAndParameterResource()
+ /** */
+ @Test
+ public void substitutedPropertyAndParameterResource()
{
StringResourceModel model = new
StringResourceModel("weather.mixed", page, wsModel,
new PropertyModel<Double>(wsModel,
"currentTemperature"), new PropertyModel<String>(
@@ -151,11 +134,9 @@ public class StringResourceModelTest ext
Assert.assertEquals("Text should be as expected", expected,
model.getString());
}
- /**
- *
- *
- */
- public void testSubstitutionParametersResource()
+ /** */
+ @Test
+ public void substitutionParametersResource()
{
Calendar cal = Calendar.getInstance();
cal.set(2004, Calendar.OCTOBER, 15, 13, 21);
@@ -173,45 +154,41 @@ public class StringResourceModelTest ext
Assert.assertEquals("Text should be as expected", expected,
model.getString());
}
-
- /**
- * @throws Exception
- */
- public void testSubstitutionParametersResourceWithSingleQuote() throws
Exception
+ /** */
+ @Test
+ public void substitutionParametersResourceWithSingleQuote()
{
tester.getSession().setLocale(Locale.ENGLISH);
StringResourceModel model = new
StringResourceModel("with.quote", page, null, 10, 20);
assertEquals("2010.00", model.getString());
-
}
-
- /**
- *
- */
- public void testSetObject()
+ /** */
+ @Test
+ public void textResourceWithSubstitutionAndSingleQuote()
{
- try
- {
- StringResourceModel model = new
StringResourceModel("simple.text", page, null);
- model.setObject("Some value");
- Assert.fail("UnsupportedOperationException expected");
- }
- catch (Exception e)
- {
- if (!(e instanceof UnsupportedOperationException ||
e.getCause() instanceof UnsupportedOperationException))
- {
- Assert.fail("UnsupportedOperationException
expected");
- }
- // Expected result
- }
+ tester.getSession().setLocale(Locale.ENGLISH);
+
+ StringResourceModel model = new
StringResourceModel("with.quote.and.no.substitution", page,
+ null, (Object[])null);
+ assertEquals("Let's play in the rain!", model.getString());
+
+ model = new StringResourceModel("with.quote.substitution",
page, null,
+ new Object[] { "rain!" });
+ assertEquals("Let's play in the rain!", model.getString());
}
+ /** */
+ @Test(expected = UnsupportedOperationException.class)
+ public void setObject()
+ {
+ StringResourceModel model = new
StringResourceModel("simple.text", page, null);
+ model.setObject("Some value");
+ }
- /**
- * @throws Exception
- */
- public void testDetachAttachDetachableModel() throws Exception
+ /** */
+ @Test
+ public void detachAttachDetachableModel()
{
IModel<WeatherStation> wsDetachModel = new
LoadableDetachableModel<WeatherStation>()
{
@@ -225,11 +202,11 @@ public class StringResourceModelTest ext
};
+
StringResourceModel model = new
StringResourceModel("simple.text", page, wsDetachModel);
model.getObject();
Assert.assertNotNull(model.getLocalizer());
model.detach();
-
}
/**
@@ -293,5 +270,4 @@ public class StringResourceModelTest ext
return name;
}
}
-
}
Modified:
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.properties
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.properties?rev=1143552&r1=1143551&r2=1143552&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.properties
(original)
+++
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.properties
Wed Jul 6 20:34:11 2011
@@ -23,3 +23,5 @@ weather.message=Weather station "${name}
weather.mixed=Weather station "${name}" reports that the temperature is {0} {1}
weather.detail=The report for {0,date,medium}, shows the temperature as
{2,number,###.##} {3} and the weather to be {1}
with.quote={0,choice,-1#n/a|-1<'{1}{0,number,#,##0.00}'}
+with.quote.substitution=Let's play in the {0}
+with.quote.and.no.substitution=Let's play in the rain!
\ No newline at end of file