Created
October 13, 2012 10:34
-
-
Save anonymous/3884099 to your computer and use it in GitHub Desktop.
AutoBean polymorphism test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| import com.google.web.bindery.autobean.shared.AutoBean; | |
| import com.google.web.bindery.autobean.shared.AutoBeanCodex; | |
| import com.google.web.bindery.autobean.shared.AutoBeanFactory; | |
| import com.google.web.bindery.autobean.shared.AutoBeanUtils; | |
| import com.google.web.bindery.autobean.vm.AutoBeanFactorySource; | |
| import junit.framework.TestCase; | |
| public class Test extends TestCase { | |
| // A default Base bean | |
| // D1 and D2 extends from Base | |
| interface Base { | |
| Base concreteType(); | |
| } | |
| interface D1 extends Base { | |
| String getField(); | |
| void setField(String field); | |
| } | |
| interface D2 extends Base { | |
| int getField(); | |
| void setField(int field); | |
| } | |
| // A bean that wraps a List<Base> | |
| public interface TestBean { | |
| List<Base> getList(); | |
| void setList(List<Base> list); | |
| } | |
| // The factory used to obtain autobeans | |
| public interface Factory extends AutoBeanFactory { | |
| AutoBean<TestBean> testBean(); | |
| AutoBean<D1> d1(); | |
| AutoBean<D2> d2(); | |
| } | |
| // The actual test | |
| public void testAutobean() { | |
| Factory factory = AutoBeanFactorySource.create(Factory.class); | |
| AutoBean<TestBean> autoBean = factory.testBean(); | |
| TestBean bean = autoBean.as(); | |
| bean.setList(new ArrayList<Base>()); | |
| D1 d1 = factory.d1().as(); | |
| d1.setField("string"); | |
| D2 d2 = factory.d2().as(); | |
| d2.setField(123456); | |
| bean.getList().add(d1); | |
| bean.getList().add(d2); | |
| // Encoding the built autobean | |
| String payload = AutoBeanCodex.encode(autoBean).getPayload(); | |
| // Decoding a new autobean using the previous payload | |
| // and obtaining a new payload | |
| // (This could have happened on client-side, for instance) | |
| AutoBean<TestBean> decodedAutoBean = | |
| AutoBeanCodex.decode(factory, TestBean.class, payload); | |
| String payload2 = AutoBeanCodex.encode(decodedAutoBean).getPayload(); | |
| // Fail | |
| assertTrue(AutoBeanUtils.deepEquals(autoBean, decodedAutoBean)); | |
| // Fail expected:<{"list":[{["field":"string"},{"field":123456]}]}> | |
| // but was:<{"list":[{[},{]}]}> | |
| assertEquals(payload, payload2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment