|
22 | 22 | import static org.junit.Assert.*; |
23 | 23 |
|
24 | 24 | import com.esotericsoftware.kryo.Kryo; |
| 25 | +import com.esotericsoftware.kryo.KryoException; |
25 | 26 | import com.esotericsoftware.kryo.KryoTestCase; |
26 | 27 | import com.esotericsoftware.kryo.SerializerFactory.CompatibleFieldSerializerFactory; |
27 | 28 |
|
| 29 | +import java.io.Serializable; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | + |
28 | 34 | import org.apache.commons.lang.builder.EqualsBuilder; |
| 35 | +import org.junit.Rule; |
29 | 36 | import org.junit.Test; |
| 37 | +import org.junit.rules.ExpectedException; |
30 | 38 |
|
31 | 39 | /** @author Nathan Sweet */ |
32 | 40 | public class CompatibleFieldSerializerTest extends KryoTestCase { |
33 | 41 | { |
34 | 42 | supportsCopy = true; |
35 | 43 | } |
36 | 44 |
|
| 45 | + @Rule public ExpectedException exceptionRule = ExpectedException.none(); |
| 46 | + |
37 | 47 | @Test |
38 | 48 | public void testCompatibleFieldSerializer () { |
39 | 49 | testCompatibleFieldSerializer(83, false, false); |
@@ -206,6 +216,55 @@ private void testRemovedField (int length, boolean references, boolean chunked) |
206 | 216 | assertEquals(object1, object2); |
207 | 217 | } |
208 | 218 |
|
| 219 | + @Test |
| 220 | + public void testChangeFieldTypeWithChunkedEncodingEnabled () { |
| 221 | + testChangeFieldType(16, true); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void testChangeFieldTypeWithChunkedEncodingDisabled () { |
| 226 | + exceptionRule.expect(KryoException.class); |
| 227 | + exceptionRule.expectMessage("Read type is incompatible with the field type: String -> Long"); |
| 228 | + |
| 229 | + testChangeFieldType(14, false); |
| 230 | + } |
| 231 | + |
| 232 | + private void testChangeFieldType(int length, boolean chunked) { |
| 233 | + CompatibleFieldSerializer<AnotherClass> serializer = new CompatibleFieldSerializer<>(kryo, AnotherClass.class); |
| 234 | + serializer.getCompatibleFieldSerializerConfig().setChunkedEncoding(chunked); |
| 235 | + kryo.setReferences(false); |
| 236 | + kryo.register(AnotherClass.class, serializer); |
| 237 | + |
| 238 | + roundTrip(length, new AnotherClass("Hacker")); |
| 239 | + |
| 240 | + serializer.getField("value").setValueClass(Long.class); |
| 241 | + |
| 242 | + final AnotherClass o = (AnotherClass)kryo.readClassAndObject(input); |
| 243 | + assertNull(o.value); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void testChangePrimitiveAndWrapperFieldTypes () { |
| 248 | + testChangePrimitiveAndWrapperFieldTypes(26, true); |
| 249 | + testChangePrimitiveAndWrapperFieldTypes(22, false); |
| 250 | + } |
| 251 | + |
| 252 | + private void testChangePrimitiveAndWrapperFieldTypes (int length, boolean chunked) { |
| 253 | + CompatibleFieldSerializer<ClassWithPrimitiveAndWrapper> serializer = new CompatibleFieldSerializer<>(kryo, ClassWithPrimitiveAndWrapper.class); |
| 254 | + serializer.getCompatibleFieldSerializerConfig().setChunkedEncoding(chunked); |
| 255 | + kryo.setReferences(false); |
| 256 | + kryo.register(ClassWithPrimitiveAndWrapper.class, serializer); |
| 257 | + |
| 258 | + roundTrip(length, new ClassWithPrimitiveAndWrapper(1, 1L)); |
| 259 | + |
| 260 | + serializer.getField("primitive").setValueClass(Long.class); |
| 261 | + serializer.getField("wrapper").setValueClass(long.class); |
| 262 | + |
| 263 | + ClassWithPrimitiveAndWrapper o = (ClassWithPrimitiveAndWrapper)kryo.readClassAndObject(input); |
| 264 | + assertEquals(1, o.primitive); |
| 265 | + assertEquals(1L, o.wrapper, 0); |
| 266 | + } |
| 267 | + |
209 | 268 | @Test |
210 | 269 | public void testRemovedFieldFromClassWithManyFields () { |
211 | 270 | testRemovedFieldFromClassWithManyFields(198, false, false, true); |
@@ -379,6 +438,21 @@ private void testExtendedClass (int length, boolean references, boolean chunked) |
379 | 438 | assertEquals(extendedObject, object2); |
380 | 439 | } |
381 | 440 |
|
| 441 | + @Test |
| 442 | + public void testClassWithSuperTypeFields() { |
| 443 | + kryo.setReferences(false); |
| 444 | + kryo.setRegistrationRequired(false); |
| 445 | + |
| 446 | + CompatibleFieldSerializer<ClassWithSuperTypeFields> serializer = new CompatibleFieldSerializer<>(kryo, |
| 447 | + ClassWithSuperTypeFields.class); |
| 448 | + CompatibleFieldSerializer.CompatibleFieldSerializerConfig config = serializer.getCompatibleFieldSerializerConfig(); |
| 449 | + config.setChunkedEncoding(true); |
| 450 | + config.setReadUnknownFieldData(true); |
| 451 | + kryo.register(ClassWithSuperTypeFields.class, serializer); |
| 452 | + |
| 453 | + roundTrip(71, new ClassWithSuperTypeFields("foo", Arrays.asList("bar"), "baz")); |
| 454 | + } |
| 455 | + |
382 | 456 | public static class TestClass { |
383 | 457 | public String text = "something"; |
384 | 458 | public int moo = 120; |
@@ -436,6 +510,26 @@ public boolean equals (Object obj) { |
436 | 510 |
|
437 | 511 | public static class AnotherClass { |
438 | 512 | String value; |
| 513 | + |
| 514 | + public AnotherClass () { |
| 515 | + } |
| 516 | + |
| 517 | + public AnotherClass (String value) { |
| 518 | + this.value = value; |
| 519 | + } |
| 520 | + |
| 521 | + @Override |
| 522 | + public boolean equals (Object o) { |
| 523 | + if (this == o) return true; |
| 524 | + if (o == null || getClass() != o.getClass()) return false; |
| 525 | + final AnotherClass that = (AnotherClass)o; |
| 526 | + return Objects.equals(value, that.value); |
| 527 | + } |
| 528 | + |
| 529 | + @Override |
| 530 | + public int hashCode () { |
| 531 | + return Objects.hash(value); |
| 532 | + } |
439 | 533 | } |
440 | 534 |
|
441 | 535 | public static class ClassWithManyFields { |
@@ -493,4 +587,59 @@ public boolean equals (Object obj) { |
493 | 587 | return false; |
494 | 588 | } |
495 | 589 | } |
| 590 | + |
| 591 | + public static class ClassWithPrimitiveAndWrapper { |
| 592 | + long primitive; |
| 593 | + Long wrapper; |
| 594 | + |
| 595 | + public ClassWithPrimitiveAndWrapper () { |
| 596 | + } |
| 597 | + |
| 598 | + public ClassWithPrimitiveAndWrapper (long primitive, Long wrapper) { |
| 599 | + this.primitive = primitive; |
| 600 | + this.wrapper = wrapper; |
| 601 | + } |
| 602 | + |
| 603 | + @Override |
| 604 | + public boolean equals (Object o) { |
| 605 | + if (this == o) return true; |
| 606 | + if (o == null || getClass() != o.getClass()) return false; |
| 607 | + final ClassWithPrimitiveAndWrapper that = (ClassWithPrimitiveAndWrapper)o; |
| 608 | + return primitive == that.primitive && Objects.equals(wrapper, that.wrapper); |
| 609 | + } |
| 610 | + |
| 611 | + @Override |
| 612 | + public int hashCode () { |
| 613 | + return Objects.hash(primitive, wrapper); |
| 614 | + } |
| 615 | + } |
| 616 | + |
| 617 | + public static class ClassWithSuperTypeFields { |
| 618 | + private Object value; |
| 619 | + private Iterable<?> list; |
| 620 | + private Serializable serializable; |
| 621 | + |
| 622 | + public ClassWithSuperTypeFields () { |
| 623 | + } |
| 624 | + |
| 625 | + public ClassWithSuperTypeFields (Object value, List<?> list, Serializable serializable) { |
| 626 | + this.value = value; |
| 627 | + this.list = list; |
| 628 | + this.serializable = serializable; |
| 629 | + } |
| 630 | + |
| 631 | + @Override |
| 632 | + public boolean equals (Object o) { |
| 633 | + if (this == o) return true; |
| 634 | + if (o == null || getClass() != o.getClass()) return false; |
| 635 | + ClassWithSuperTypeFields that = (ClassWithSuperTypeFields)o; |
| 636 | + return Objects.equals(value, that.value) && Objects.equals(list, that.list) |
| 637 | + && Objects.equals(serializable, that.serializable); |
| 638 | + } |
| 639 | + |
| 640 | + @Override |
| 641 | + public int hashCode () { |
| 642 | + return Objects.hash(value, list, serializable); |
| 643 | + } |
| 644 | + } |
496 | 645 | } |
0 commit comments