}
@Test
public void testMapWrapper() throws Exception
{
MapWrapper wrapper = new MapWrapper();
wrapper.setCallContext(new CallContext());
// Construct a map with two elements, foo:aaaaa and bar:zzzzz
Element e = DocumentFactory.getInstance().createElement("map");
Element child = e.addElement("element");
child.addElement("k").addElement("str").addText("foo");
child.addElement("v").addElement("str").addText("aaaaa");
child = e.addElement("element");
child.addElement("k").addElement("str").addText("bar");
child.addElement("v").addElement("str").addText("zzzzz");
wrapper.setElement(e);
// Conversion tests
Map m = (Map) wrapper.convert(Map.class);
assert m.containsKey("foo");
assert m.containsKey("bar");
assert "aaaaa".equals(m.get("foo"));
assert "zzzzz".equals(m.get("bar"));
m = (Map) wrapper.convert(HashMap.class);
assert m.containsKey("foo");
assert m.containsKey("bar");
assert "aaaaa".equals(m.get("foo"));
assert "zzzzz".equals(m.get("bar"));
Type t = RemotingTest.class.getMethod("dummyMap").getGenericReturnType();
m = (Map) wrapper.convert(t);
assert m.containsKey("foo");
assert m.containsKey("bar");
assert "aaaaa".equals(m.get("foo"));
assert "zzzzz".equals(m.get("bar"));
try
{
// This should throw a ConversionException
wrapper.convert(InvalidClass.class);
assert false;
}
catch (ConversionException ex)
{
}
try
{
// This should throw a ConversionException also
wrapper.convert(InvalidMap.class);
assert false;
}
catch (ConversionException ex)
{
}
byte[] expected = ("<map><element><k><str>foo</str></k><v><str>aaaaa</str></v></element>"
+ "<element><k><str>bar</str></k><v><str>zzzzz</str></v></element></map>")
.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream();
wrapper.marshal(out);
assertEquals(expected, out.toByteArray());
// test conversionScore() method
assert ConversionScore.exact == wrapper.conversionScore(Map.class);
assert ConversionScore.exact == wrapper.conversionScore(HashMap.class);
assert ConversionScore.compatible == wrapper
.conversionScore(Object.class);
assert ConversionScore.nomatch == wrapper
.conversionScore(InvalidClass.class);
}