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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Optional;
  26. import java.util.Set;
  27. import java.util.stream.Collectors;
  28. import org.junit.Assert;
  29. import org.junit.Test;
  30. import com.vaadin.data.provider.bov.Person;
  31. import com.vaadin.tests.data.bean.Address;
  32. import com.vaadin.tests.data.bean.Country;
  33. import com.vaadin.tests.data.bean.FatherAndSon;
  34. import com.vaadin.tests.data.bean.Sex;
  35. import com.vaadin.tests.server.ClassesSerializableTest;
  36. public class BeanPropertySetTest {
  37. @Test
  38. public void testSerializeDeserialize_propertySet() throws Exception {
  39. PropertySet<Person> originalPropertySet = BeanPropertySet
  40. .get(Person.class);
  41. PropertySet<Person> deserializedPropertySet = ClassesSerializableTest
  42. .serializeAndDeserialize(originalPropertySet);
  43. Assert.assertSame(
  44. "Deserialized instance should be the same as the original",
  45. originalPropertySet, deserializedPropertySet);
  46. }
  47. @Test
  48. public void testSerializeDeserialize_propertySet_cacheCleared()
  49. throws Exception {
  50. PropertySet<Person> originalPropertySet = BeanPropertySet
  51. .get(Person.class);
  52. ByteArrayOutputStream bs = new ByteArrayOutputStream();
  53. ObjectOutputStream out = new ObjectOutputStream(bs);
  54. out.writeObject(originalPropertySet);
  55. byte[] data = bs.toByteArray();
  56. // Simulate deserializing into a different JVM by clearing the instance
  57. // map
  58. Field instancesField = BeanPropertySet.class
  59. .getDeclaredField("instances");
  60. instancesField.setAccessible(true);
  61. Map<?, ?> instances = (Map<?, ?>) instancesField.get(null);
  62. instances.clear();
  63. ObjectInputStream in = new ObjectInputStream(
  64. new ByteArrayInputStream(data));
  65. PropertySet<Person> deserializedPropertySet = (PropertySet<Person>) in
  66. .readObject();
  67. Assert.assertSame(
  68. "Deserialized instance should be the same as in the cache",
  69. BeanPropertySet.get(Person.class), deserializedPropertySet);
  70. Assert.assertNotSame(
  71. "Deserialized instance should not be the same as the original",
  72. originalPropertySet, deserializedPropertySet);
  73. }
  74. @Test
  75. public void testSerializeDeserialize_propertyDefinition() throws Exception {
  76. PropertyDefinition<Person, ?> definition = BeanPropertySet
  77. .get(Person.class).getProperty("born")
  78. .orElseThrow(RuntimeException::new);
  79. PropertyDefinition<Person, ?> deserializedDefinition = ClassesSerializableTest
  80. .serializeAndDeserialize(definition);
  81. ValueProvider<Person, ?> getter = deserializedDefinition.getGetter();
  82. Person person = new Person("Milennial", 2000);
  83. Integer age = (Integer) getter.apply(person);
  84. Assert.assertEquals("Deserialized definition should be functional",
  85. Integer.valueOf(2000), age);
  86. Assert.assertSame(
  87. "Deserialized instance should be the same as in the cache",
  88. BeanPropertySet.get(Person.class).getProperty("born")
  89. .orElseThrow(RuntimeException::new),
  90. deserializedDefinition);
  91. }
  92. @Test
  93. public void testSerializeDeserialize_nestedPropertyDefinition()
  94. throws Exception {
  95. PropertyDefinition<com.vaadin.tests.data.bean.Person, ?> definition = BeanPropertySet
  96. .get(com.vaadin.tests.data.bean.Person.class)
  97. .getProperty("address.postalCode")
  98. .orElseThrow(RuntimeException::new);
  99. PropertyDefinition<com.vaadin.tests.data.bean.Person, ?> deserializedDefinition = ClassesSerializableTest
  100. .serializeAndDeserialize(definition);
  101. ValueProvider<com.vaadin.tests.data.bean.Person, ?> getter = deserializedDefinition
  102. .getGetter();
  103. Address address = new Address("Ruukinkatu 2-4", 20540, "Turku",
  104. Country.FINLAND);
  105. com.vaadin.tests.data.bean.Person person = new com.vaadin.tests.data.bean.Person(
  106. "Jon", "Doe", "jon.doe@vaadin.com", 32, Sex.MALE, address);
  107. Integer postalCode = (Integer) getter.apply(person);
  108. Assert.assertEquals("Deserialized definition should be functional",
  109. address.getPostalCode(), postalCode);
  110. Assert.assertSame(
  111. "Deserialized instance should be the same as in the cache",
  112. BeanPropertySet.get(com.vaadin.tests.data.bean.Person.class)
  113. .getProperty("address.postalCode").orElseThrow(
  114. RuntimeException::new),
  115. deserializedDefinition);
  116. }
  117. @Test
  118. public void nestedPropertyDefinition_samePropertyNameOnMultipleLevels()
  119. throws Exception {
  120. PropertyDefinition<FatherAndSon, ?> definition = BeanPropertySet
  121. .get(FatherAndSon.class).getProperty("father.father.firstName")
  122. .orElseThrow(RuntimeException::new);
  123. ValueProvider<FatherAndSon, ?> getter = definition.getGetter();
  124. FatherAndSon grandFather = new FatherAndSon("Grand Old Jon", "Doe",
  125. null, null);
  126. FatherAndSon father = new FatherAndSon("Old Jon", "Doe", grandFather,
  127. null);
  128. FatherAndSon son = new FatherAndSon("Jon", "Doe", father, null);
  129. String firstName = (String) getter.apply(son);
  130. Assert.assertEquals(grandFather.getFirstName(), firstName);
  131. }
  132. @Test(expected = NullPointerException.class)
  133. public void nestedPropertyDefinition_propertyChainBroken()
  134. throws Exception {
  135. PropertyDefinition<FatherAndSon, ?> definition = BeanPropertySet
  136. .get(FatherAndSon.class).getProperty("father.firstName")
  137. .orElseThrow(RuntimeException::new);
  138. ValueProvider<FatherAndSon, ?> getter = definition.getGetter();
  139. getter.apply(new FatherAndSon("Jon", "Doe", null, null));
  140. }
  141. @Test(expected = IllegalArgumentException.class)
  142. public void nestedPropertyDefinition_invalidPropertyNameInChain()
  143. throws Exception {
  144. BeanPropertySet.get(FatherAndSon.class)
  145. .getProperty("grandfather.firstName");
  146. }
  147. @Test(expected = IllegalArgumentException.class)
  148. public void nestedPropertyDefinition_invalidPropertyNameAtChainEnd()
  149. throws Exception {
  150. BeanPropertySet.get(FatherAndSon.class).getProperty("father.age");
  151. }
  152. @Test
  153. public void properties() {
  154. PropertySet<Person> propertySet = BeanPropertySet.get(Person.class);
  155. Set<String> propertyNames = propertySet.getProperties()
  156. .map(PropertyDefinition::getName).collect(Collectors.toSet());
  157. Assert.assertEquals(new HashSet<>(Arrays.asList("name", "born")),
  158. propertyNames);
  159. }
  160. }