@Test
public void testStringWrapper() throws Exception
{
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+ "!@#$%^&*()_+-=`~[]{}|;:\"',./<>?\\ ";
StringWrapper wrapper = new StringWrapper();
wrapper.setElement(createElement("str", value));
assert value.equals(wrapper.convert(String.class));
assert value
.equals(((StringBuilder) wrapper.convert(StringBuilder.class))
.toString());
assert value.equals(((StringBuffer) wrapper.convert(StringBuffer.class))
.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
wrapper.marshal(out);
byte[] expected = ("<str>"
+ URLEncoder.encode(value, StringWrapper.DEFAULT_ENCODING) + "</str>")
.replace("+", "%20").getBytes();
assertEquals(expected, out.toByteArray());
value = "123";
wrapper.setElement(createElement("str", value));
assert Integer.valueOf(value).equals(wrapper.convert(Integer.class));
assert Integer.valueOf(value).equals(wrapper.convert(Integer.TYPE));
assert Long.valueOf(value).equals(wrapper.convert(Long.class));
assert Long.valueOf(value).equals(wrapper.convert(Long.TYPE));
assert Short.valueOf(value).equals(wrapper.convert(Short.class));
assert Short.valueOf(value).equals(wrapper.convert(Short.TYPE));
assert Byte.valueOf(value).equals(wrapper.convert(Byte.class));
assert Byte.valueOf(value).equals(wrapper.convert(Byte.TYPE));
value = "123.4567";
wrapper.setElement(createElement("str", value));
assert Double.valueOf(value).equals(wrapper.convert(Double.class));
assert Double.valueOf(value).equals(wrapper.convert(Double.TYPE));
assert Float.valueOf(value).equals(wrapper.convert(Float.class));
assert Float.valueOf(value).equals(wrapper.convert(Float.TYPE));
wrapper.setElement(createElement("str", Boolean.toString(true)));
assert (Boolean) wrapper.convert(Boolean.class);
assert (Boolean) wrapper.convert(Boolean.TYPE);
wrapper.setElement(createElement("str", Boolean.toString(false)));
assert !(Boolean) wrapper.convert(Boolean.class);
assert !(Boolean) wrapper.convert(Boolean.TYPE);
try
{
// Attempt an invalid conversion
wrapper.convert(Integer.class);
assert false;
}
catch (ConversionException ex)
{
}
value = "A";
wrapper.setElement(createElement("str", value));
assert Character.valueOf(value.charAt(0)).equals(
wrapper.convert(Character.class));
assert Character.valueOf(value.charAt(0)).equals(
wrapper.convert(Character.TYPE));
value = "green";
wrapper.setElement(createElement("str", value));
assert TestEnum.valueOf(value).equals(wrapper.convert(TestEnum.class));
try
{
wrapper.setElement(createElement("str", "foo"));
// Attempt an invalid conversion
wrapper.convert(InvalidClass.class);
assert false;
}
catch (ConversionException ex)
{
}
// Test conversionScore() method
wrapper = new StringWrapper();
assert ConversionScore.exact == wrapper.conversionScore(String.class);
assert ConversionScore.exact == wrapper
.conversionScore(StringBuffer.class);
assert ConversionScore.exact == wrapper
.conversionScore(StringBuffer.class);
assert ConversionScore.compatible == wrapper
.conversionScore(Integer.class);
assert ConversionScore.compatible == wrapper
.conversionScore(Integer.TYPE);
assert ConversionScore.compatible == wrapper.conversionScore(Long.class);
assert ConversionScore.compatible == wrapper.conversionScore(Long.TYPE);
assert ConversionScore.compatible == wrapper.conversionScore(Short.class);
assert ConversionScore.compatible == wrapper.conversionScore(Short.TYPE);
assert ConversionScore.compatible == wrapper
.conversionScore(Boolean.class);
assert ConversionScore.compatible == wrapper
.conversionScore(Boolean.TYPE);
assert ConversionScore.compatible == wrapper.conversionScore(Float.class);
assert ConversionScore.compatible == wrapper.conversionScore(Float.TYPE);
assert ConversionScore.compatible == wrapper
.conversionScore(Character.class);
assert ConversionScore.compatible == wrapper
.conversionScore(Character.TYPE);
assert ConversionScore.compatible == wrapper.conversionScore(Byte.class);
assert ConversionScore.compatible == wrapper.conversionScore(Byte.TYPE);
wrapper.setElement(createElement("str", "green"));
assert ConversionScore.compatible == wrapper
.conversionScore(TestEnum.class);
wrapper.setElement(createElement("str", "foo"));
assert ConversionScore.nomatch == wrapper.conversionScore(TestEnum.class);
assert ConversionScore.nomatch == wrapper
.conversionScore(InvalidClass.class);
}