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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.server.ClassesSerializableTest;
  31. public class BeanPropertySetTest {
  32. @Test
  33. public void testSerializeDeserialize_propertySet() throws Exception {
  34. PropertySet<Person> originalPropertySet = BeanPropertySet
  35. .get(Person.class);
  36. PropertySet<Person> deserializedPropertySet = ClassesSerializableTest
  37. .serializeAndDeserialize(originalPropertySet);
  38. Assert.assertSame(
  39. "Deserialized instance should be the same as the original",
  40. originalPropertySet, deserializedPropertySet);
  41. }
  42. @Test
  43. public void testSerializeDeserialize_propertySet_cacheCleared()
  44. throws Exception {
  45. PropertySet<Person> originalPropertySet = BeanPropertySet
  46. .get(Person.class);
  47. ByteArrayOutputStream bs = new ByteArrayOutputStream();
  48. ObjectOutputStream out = new ObjectOutputStream(bs);
  49. out.writeObject(originalPropertySet);
  50. byte[] data = bs.toByteArray();
  51. // Simulate deserializing into a different JVM by clearing the instance
  52. // map
  53. Field instancesField = BeanPropertySet.class
  54. .getDeclaredField("instances");
  55. instancesField.setAccessible(true);
  56. Map<?, ?> instances = (Map<?, ?>) instancesField.get(null);
  57. instances.clear();
  58. ObjectInputStream in = new ObjectInputStream(
  59. new ByteArrayInputStream(data));
  60. PropertySet<Person> deserializedPropertySet = (PropertySet<Person>) in
  61. .readObject();
  62. Assert.assertSame(
  63. "Deserialized instance should be the same as in the cache",
  64. BeanPropertySet.get(Person.class), deserializedPropertySet);
  65. Assert.assertNotSame(
  66. "Deserialized instance should not be the same as the original",
  67. originalPropertySet, deserializedPropertySet);
  68. }
  69. @Test
  70. public void testSerializeDeserialize_propertyDefinition() throws Exception {
  71. PropertyDefinition<Person, ?> definition = BeanPropertySet
  72. .get(Person.class).getProperty("born")
  73. .orElseThrow(RuntimeException::new);
  74. PropertyDefinition<Person, ?> deserializedDefinition = ClassesSerializableTest
  75. .serializeAndDeserialize(definition);
  76. ValueProvider<Person, ?> getter = deserializedDefinition.getGetter();
  77. Person person = new Person("Milennial", 2000);
  78. Integer age = (Integer) getter.apply(person);
  79. Assert.assertEquals("Deserialized definition should be functional",
  80. Integer.valueOf(2000), age);
  81. Assert.assertSame(
  82. "Deserialized instance should be the same as in the cache",
  83. BeanPropertySet.get(Person.class).getProperty("born")
  84. .orElseThrow(RuntimeException::new),
  85. deserializedDefinition);
  86. }
  87. @Test
  88. public void properties() {
  89. PropertySet<Person> propertySet = BeanPropertySet.get(Person.class);
  90. Set<String> propertyNames = propertySet.getProperties()
  91. .map(PropertyDefinition::getName).collect(Collectors.toSet());
  92. Assert.assertEquals(new HashSet<>(Arrays.asList("name", "born")),
  93. propertyNames);
  94. }
  95. }