You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanPropertySetTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.lang.reflect.Field;
  22. import java.util.Arrays;
  23. import java.util.HashSet;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.stream.Collectors;
  27. import org.junit.Assert;
  28. import org.junit.Test;
  29. import com.vaadin.data.provider.bov.Person;
  30. import com.vaadin.tests.data.bean.Address;
  31. import com.vaadin.tests.data.bean.Country;
  32. import com.vaadin.tests.data.bean.FatherAndSon;
  33. import com.vaadin.tests.data.bean.Sex;
  34. import com.vaadin.tests.server.ClassesSerializableTest;
  35. public class BeanPropertySetTest {
  36. @Test
  37. public void testSerializeDeserialize_propertySet() throws Exception {
  38. PropertySet<Person> originalPropertySet = BeanPropertySet
  39. .get(Person.class);
  40. PropertySet<Person> deserializedPropertySet = ClassesSerializableTest
  41. .serializeAndDeserialize(originalPropertySet);
  42. Assert.assertSame(
  43. "Deserialized instance should be the same as the original",
  44. originalPropertySet, deserializedPropertySet);
  45. }
  46. @Test
  47. public void testSerializeDeserialize_propertySet_cacheCleared()
  48. throws Exception {
  49. PropertySet<Person> originalPropertySet = BeanPropertySet
  50. .get(Person.class);
  51. ByteArrayOutputStream bs = new ByteArrayOutputStream();
  52. ObjectOutputStream out = new ObjectOutputStream(bs);
  53. out.writeObject(originalPropertySet);
  54. byte[] data = bs.toByteArray();
  55. // Simulate deserializing into a different JVM by clearing the instance
  56. // map
  57. Field instancesField = BeanPropertySet.class
  58. .getDeclaredField("instances");
  59. instancesField.setAccessible(true);
  60. Map<?, ?> instances = (Map<?, ?>) instancesField.get(null);
  61. instances.clear();
  62. ObjectInputStream in = new ObjectInputStream(
  63. new ByteArrayInputStream(data));
  64. PropertySet<Person> deserializedPropertySet = (PropertySet<Person>) in
  65. .readObject();
  66. Assert.assertSame(
  67. "Deserialized instance should be the same as in the cache",
  68. BeanPropertySet.get(Person.class), deserializedPropertySet);
  69. Assert.assertNotSame(
  70. "Deserialized instance should not be the same as the original",
  71. originalPropertySet, deserializedPropertySet);
  72. }
  73. @Test
  74. public void testSerializeDeserialize_propertyDefinition() throws Exception {
  75. PropertyDefinition<Person, ?> definition = BeanPropertySet
  76. .get(Person.class).getProperty("born")
  77. .orElseThrow(RuntimeException::new);
  78. PropertyDefinition<Person, ?> deserializedDefinition = ClassesSerializableTest
  79. .serializeAndDeserialize(definition);
  80. ValueProvider<Person, ?> getter = deserializedDefinition.getGetter();
  81. Person person = new Person("Milennial", 2000);
  82. Integer age = (Integer) getter.apply(person);
  83. Assert.assertEquals("Deserialized definition should be functional",
  84. Integer.valueOf(2000), age);
  85. Assert.assertSame(
  86. "Deserialized instance should be the same as in the cache",
  87. BeanPropertySet.get(Person.class).getProperty("born")
  88. .orElseThrow(RuntimeException::new),
  89. deserializedDefinition);
  90. }
  91. @Test
  92. public void testSerializeDeserialize_nestedPropertyDefinition()
  93. throws Exception {
  94. PropertyDefinition<com.vaadin.tests.data.bean.Person, ?> definition = BeanPropertySet
  95. .get(com.vaadin.tests.data.bean.Person.class)
  96. .getProperty("address.postalCode")
  97. .orElseThrow(RuntimeException::new);
  98. PropertyDefinition<com.vaadin.tests.data.bean.Person, ?> deserializedDefinition = ClassesSerializableTest
  99. .serializeAndDeserialize(definition);
  100. ValueProvider<com.vaadin.tests.data.bean.Person, ?> getter = deserializedDefinition
  101. .getGetter();
  102. Address address = new Address("Ruukinkatu 2-4", 20540, "Turku",
  103. Country.FINLAND);
  104. com.vaadin.tests.data.bean.Person person = new com.vaadin.tests.data.bean.Person(
  105. "Jon", "Doe", "jon.doe@vaadin.com", 32, Sex.MALE, address);
  106. Integer postalCode = (Integer) getter.apply(person);
  107. Assert.assertEquals("Deserialized definition should be functional",
  108. address.getPostalCode(), postalCode);
  109. Assert.assertSame(
  110. "Deserialized instance should be the same as in the cache",
  111. BeanPropertySet.get(com.vaadin.tests.data.bean.Person.class)
  112. .getProperty("address.postalCode").orElseThrow(
  113. RuntimeException::new),
  114. deserializedDefinition);
  115. }
  116. @Test
  117. public void nestedPropertyDefinition_samePropertyNameOnMultipleLevels()
  118. throws Exception {
  119. PropertyDefinition<FatherAndSon, ?> definition = BeanPropertySet
  120. .get(FatherAndSon.class).getProperty("father.father.firstName")
  121. .orElseThrow(RuntimeException::new);
  122. ValueProvider<FatherAndSon, ?> getter = definition.getGetter();
  123. FatherAndSon grandFather = new FatherAndSon("Grand Old Jon", "Doe",
  124. null, null);
  125. FatherAndSon father = new FatherAndSon("Old Jon", "Doe", grandFather,
  126. null);
  127. FatherAndSon son = new FatherAndSon("Jon", "Doe", father, null);
  128. String firstName = (String) getter.apply(son);
  129. Assert.assertEquals(grandFather.getFirstName(), firstName);
  130. }
  131. @Test(expected = NullPointerException.class)
  132. public void nestedPropertyDefinition_propertyChainBroken()
  133. throws Exception {
  134. PropertyDefinition<FatherAndSon, ?> definition = BeanPropertySet
  135. .get(FatherAndSon.class).getProperty("father.firstName")
  136. .orElseThrow(RuntimeException::new);
  137. ValueProvider<FatherAndSon, ?> getter = definition.getGetter();
  138. getter.apply(new FatherAndSon("Jon", "Doe", null, null));
  139. }
  140. @Test(expected = IllegalArgumentException.class)
  141. public void nestedPropertyDefinition_invalidPropertyNameInChain()
  142. throws Exception {
  143. BeanPropertySet.get(FatherAndSon.class)
  144. .getProperty("grandfather.firstName");
  145. }
  146. @Test(expected = IllegalArgumentException.class)
  147. public void nestedPropertyDefinition_invalidPropertyNameAtChainEnd()
  148. throws Exception {
  149. BeanPropertySet.get(FatherAndSon.class).getProperty("father.age");
  150. }
  151. @Test
  152. public void properties() {
  153. PropertySet<Person> propertySet = BeanPropertySet.get(Person.class);
  154. Set<String> propertyNames = propertySet.getProperties()
  155. .map(PropertyDefinition::getName).collect(Collectors.toSet());
  156. Assert.assertEquals(new HashSet<>(Arrays.asList("name", "born")),
  157. propertyNames);
  158. }
  159. }