Tuesday, January 31, 2012

Problems with AutoBean Lists

I am having problems modifying a list property on an AutoBean
deserialized from JSON with AutoBeanCodex. When I call
List<>.add(position, value) it appears to be overwriting the value at
that position instead of shifting values right to make room for the
new item.

Here's a JUnit test that demonstrates the behavior I'm seeing:

public class AutoBeanListTest extends GWTTestCase {

@Override
public String getModuleName() {
return "com.xxx.AutoBeanListTest";
}

@Test
public void testDirectInsert() {
MyBeanFactory factory = GWT.create(MyBeanFactory.class);
MyBean bean = factory.myBean().as();
bean.setValues(new ArrayList<String>());
bean.getValues().add("A");
bean.getValues().add("C");

assertEquals(2,bean.getValues().size());

bean.getValues().add(1,"B");

assertEquals(3,bean.getValues().size()); // this one works
assertEquals("A", bean.getValues().get(0));
assertEquals("B", bean.getValues().get(1));
assertEquals("C", bean.getValues().get(2));
}

@Test
public void testSerializeInsert() {
MyBeanFactory factory = GWT.create(MyBeanFactory.class);
MyBean bean = factory.myBean().as();
bean.setValues(new ArrayList<String>());
bean.getValues().add("A");
bean.getValues().add("C");

assertEquals(2,bean.getValues().size());

String json =
AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(bean)).getPayload();
bean = AutoBeanCodex.decode(factory, MyBean.class, json).as();

assertEquals(2,bean.getValues().size());

bean.getValues().add(1,"B");

assertEquals(3,bean.getValues().size()); // this one FAILS
assertEquals("A", bean.getValues().get(0));
assertEquals("B", bean.getValues().get(1));
assertEquals("C", bean.getValues().get(2));
}

public interface MyBeanFactory extends AutoBeanFactory {
AutoBean<MyBean> myBean();
AutoBean<MyBean> myBean(MyBean myBean);
}

public interface MyBean {
List<String> getValues();
void setValues(List<String> values);
}
}

The first test case (which modifies a list that I create and put into
the bean) works as I expect, but the second test (which modifies the
list returned from deserialization) does not. After the insert, the
list still has only two values in it, and the second one has been
replaced by the value I inserted.

Am I doing something wrong? Is this an issue in the underlying code?

I can't help but notice that the SplittableList class uses the same
implementation for set() and add(). That doesn't seem right to me,
but I assume I must be missing something.

James

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment