-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exception when write replace return itself
lo1nt
committed
Mar 20, 2024
1 parent
da95611
commit 5e1e17b
Showing
2 changed files
with
136 additions
and
5 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
129 changes: 129 additions & 0 deletions
129
src/test/java/com/caucho/hessian/io/WriteReplaceTest.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,129 @@ | ||
/* | ||
* Ant Group | ||
* Copyright (c) 2004-2024 All Rights Reserved. | ||
*/ | ||
package com.caucho.hessian.io; | ||
|
||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* | ||
* @author junyuan | ||
* @version WriteReplaceTest.java, v 0.1 2024-03-20 10:34 junyuan Exp $ | ||
*/ | ||
public class WriteReplaceTest { | ||
private static SerializerFactory factory; | ||
private static ByteArrayOutputStream os; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
factory = new SerializerFactory(); | ||
os = new ByteArrayOutputStream(); | ||
} | ||
|
||
@Test | ||
public void TestWriteReplace() throws IOException { | ||
TestObject origin = new TestObject(); | ||
origin.setName("testWR"); | ||
|
||
os.reset(); | ||
Hessian2Output output = new Hessian2Output(os); | ||
|
||
output.setSerializerFactory(factory); | ||
try { | ||
output.writeObject(origin); | ||
} catch (Exception e) { | ||
Assert.fail("should be no exception"); | ||
} | ||
output.flush(); | ||
|
||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); | ||
Hessian2Input input = new Hessian2Input(is); | ||
input.setSerializerFactory(factory); | ||
TestObject actual = (TestObject)input.readObject(); | ||
Assert.assertEquals(actual.name, origin.name); | ||
} | ||
|
||
@Test | ||
public void TestWrappedWriteReplace() throws IOException { | ||
WrappedTestObject origin = new WrappedTestObject(); | ||
TestObject testObject = new TestObject(); | ||
testObject.setName("testWR"); | ||
origin.setTestObject(testObject); | ||
|
||
os.reset(); | ||
Hessian2Output output = new Hessian2Output(os); | ||
|
||
output.setSerializerFactory(factory); | ||
try { | ||
output.writeObject(origin); | ||
} catch (Exception e) { | ||
Assert.fail("should be no exception"); | ||
} | ||
output.flush(); | ||
|
||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); | ||
Hessian2Input input = new Hessian2Input(is); | ||
input.setSerializerFactory(factory); | ||
WrappedTestObject actual = (WrappedTestObject)input.readObject(); | ||
Assert.assertEquals(actual.testObject.name, origin.testObject.name); | ||
} | ||
|
||
private static class WrappedTestObject implements Serializable { | ||
private TestObject testObject; | ||
|
||
/** | ||
* Getter method for property <tt>testObject</tt>. | ||
* | ||
* @return property value of testObject | ||
*/ | ||
public TestObject getTestObject() { | ||
return testObject; | ||
} | ||
|
||
/** | ||
* Setter method for property <tt>testObject</tt>. | ||
* | ||
* @param testObject value to be assigned to property testObject | ||
*/ | ||
public void setTestObject(TestObject testObject) { | ||
this.testObject = testObject; | ||
} | ||
} | ||
|
||
|
||
private static class TestObject implements Serializable { | ||
private static final long serialVersionUID = -452701306050912437L; | ||
|
||
String name; | ||
|
||
Object writeReplace() { | ||
return this; | ||
} | ||
|
||
/** | ||
* Getter method for property <tt>name</tt>. | ||
* | ||
* @return property value of name | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Setter method for property <tt>name</tt>. | ||
* | ||
* @param name value to be assigned to property name | ||
*/ | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
} |