forked from EsotericSoftware/kryo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EsotericSoftware#424 from xfrendx/master
Fix for CompatibleFieldSerializer inheritance issue
- Loading branch information
Showing
7 changed files
with
253 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,14 @@ | |
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | ||
|
||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | ||
|
||
package com.esotericsoftware.kryo; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer; | ||
import com.esotericsoftware.kryo.serializers.FieldSerializer; | ||
|
||
/** @author Nathan Sweet <[email protected]> */ | ||
public class CompatibleFieldSerializerTest extends KryoTestCase { | ||
|
@@ -71,6 +72,21 @@ public void testRemovedField () throws FileNotFoundException { | |
assertEquals(object1, object2); | ||
} | ||
|
||
public void testExtendedClass() throws FileNotFoundException { | ||
ExtendedTestClass extendedObject = new ExtendedTestClass(); | ||
|
||
// this test would fail with DEFAULT field name strategy | ||
kryo.getFieldSerializerConfig().setCachedFieldNameStrategy(FieldSerializer.CachedFieldNameStrategy.EXTENDED); | ||
|
||
CompatibleFieldSerializer serializer = new CompatibleFieldSerializer(kryo, ExtendedTestClass.class); | ||
kryo.register(ExtendedTestClass.class, serializer); | ||
roundTrip(286, 286, extendedObject); | ||
|
||
ExtendedTestClass object2 = (ExtendedTestClass) kryo.readClassAndObject(input); | ||
assertEquals(extendedObject, object2); | ||
} | ||
|
||
|
||
static public class TestClass { | ||
public String text = "something"; | ||
public int moo = 120; | ||
|
@@ -97,7 +113,36 @@ public boolean equals (Object obj) { | |
} | ||
} | ||
|
||
static public class ExtendedTestClass extends TestClass { | ||
// keep the same names of attributes like TestClass | ||
public String text = "extendedSomething"; | ||
public int moo = 127; | ||
public long moo2 = 5555; | ||
public TestClass child; | ||
public int zzz = 222; | ||
public AnotherClass other; | ||
|
||
public boolean equals (Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null) return false; | ||
if (getClass() != obj.getClass()) return false; | ||
ExtendedTestClass other = (ExtendedTestClass) obj; | ||
|
||
if (!super.equals(obj)) return false; | ||
if (child == null) { | ||
if (other.child != null) return false; | ||
} else if (!child.equals(other.child)) return false; | ||
if (moo != other.moo) return false; | ||
if (moo2 != other.moo2) return false; | ||
if (text == null) { | ||
if (other.text != null) return false; | ||
} else if (!text.equals(other.text)) return false; | ||
if (zzz != other.zzz) return false; | ||
return true; | ||
} | ||
} | ||
|
||
static public class AnotherClass { | ||
String value; | ||
} | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
test/com/esotericsoftware/kryo/FieldSerializerInheritanceTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* Copyright (c) 2008, Nathan Sweet | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following | ||
* conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided with the distribution. | ||
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | ||
|
||
package com.esotericsoftware.kryo; | ||
|
||
import com.esotericsoftware.kryo.serializers.FieldSerializer; | ||
import org.junit.Assert; | ||
|
||
/** | ||
* Created by phamrak on 8.6.2016. | ||
*/ | ||
public class FieldSerializerInheritanceTest extends KryoTestCase { | ||
public void testDefaultStrategyForDefaultClass() { | ||
TestDefault testDefault = new TestDefault(); | ||
testDefault.a = "someDefaultValue"; | ||
kryo.setDefaultSerializer(FieldSerializer.class); | ||
kryo.register(TestDefault.class); | ||
|
||
roundTrip(17, 17, testDefault); | ||
|
||
FieldSerializer serializer = (FieldSerializer) kryo.getSerializer(TestDefault.class); | ||
assertNotNull(serializer.getField("a")); | ||
serializer.removeField("a"); | ||
assertFieldRemoved(serializer, "a"); | ||
} | ||
|
||
public void testDefaultStrategyForExtendedClass() { | ||
TestExtended testExtended = new TestExtended(); | ||
((TestDefault) testExtended).a = "someDefaultValue"; | ||
testExtended.a = "someExtendedValue"; | ||
kryo.setDefaultSerializer(FieldSerializer.class); | ||
kryo.register(TestExtended.class); | ||
|
||
roundTrip(34, 34, testExtended); | ||
|
||
FieldSerializer serializer = (FieldSerializer) kryo.getSerializer(TestExtended.class); | ||
|
||
// the "a" field needs to be removed 2x, once for TestDefault.a and once for TestExtended.a. You | ||
// can't remove the second one without removing the first one (in DEFAULT field name strategy) | ||
assertNotNull(serializer.getField("a")); | ||
serializer.removeField("a"); | ||
assertNotNull(serializer.getField("a")); | ||
serializer.removeField("a"); | ||
assertFieldRemoved(serializer, "a"); | ||
} | ||
|
||
public void testExtendedStrategyForExtendedClass() { | ||
TestExtended testExtended = new TestExtended(); | ||
((TestDefault) testExtended).a = "someDefaultValue"; | ||
testExtended.a = "someExtendedValue"; | ||
kryo.getFieldSerializerConfig().setCachedFieldNameStrategy(FieldSerializer.CachedFieldNameStrategy.EXTENDED); | ||
kryo.setDefaultSerializer(FieldSerializer.class); | ||
kryo.register(TestExtended.class); | ||
|
||
roundTrip(34, 34, testExtended); | ||
|
||
FieldSerializer serializer = (FieldSerializer) kryo.getSerializer(TestExtended.class); | ||
|
||
// Simple class name is part of field name in EXTENDED field name strategy. | ||
assertNotNull(serializer.getField("TestDefault.a")); | ||
serializer.removeField("TestDefault.a"); | ||
assertFieldRemoved(serializer, "TestDefault.a"); | ||
assertNotNull(serializer.getField("TestExtended.a")); | ||
serializer.removeField("TestExtended.a"); | ||
assertFieldRemoved(serializer, "TestExtended.a"); | ||
} | ||
|
||
protected void assertFieldRemoved(FieldSerializer serializer, String fieldName) { | ||
try { | ||
assertNull(serializer.getField(fieldName)); | ||
Assert.fail("Expected IllegalArgumentException to be thrown for serializer.getField(" + fieldName + ")"); | ||
} catch (IllegalArgumentException iae) { | ||
assertTrue(true); | ||
} | ||
} | ||
|
||
static public class TestDefault { | ||
private String a; | ||
|
||
public String getA() { | ||
return a; | ||
} | ||
|
||
public void setA(String a) { | ||
this.a = a; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
TestDefault that = (TestDefault) o; | ||
|
||
return a != null ? a.equals(that.a) : that.a == null; | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return a != null ? a.hashCode() : 0; | ||
} | ||
} | ||
|
||
static public class TestExtended extends TestDefault { | ||
private String a; | ||
|
||
public String getA() { | ||
return a; | ||
} | ||
|
||
public void setA(String a) { | ||
this.a = a; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
if (!super.equals(o)) return false; | ||
|
||
TestExtended that = (TestExtended) o; | ||
return a != null ? a.equals(that.a) : that.a == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return a != null ? a.hashCode() : 0; | ||
} | ||
} | ||
} |
This file contains 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