Skip to content

Commit

Permalink
fix: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
junyuan committed Feb 27, 2023
1 parent ccb18d2 commit 6b0138d
Showing 1 changed file with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.caucho.hessian.io.Hessian2Output;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
Expand All @@ -39,35 +40,35 @@
*/
public class SofaResponseHessianSerializerTest {

@Before
@After
public void tearDown() {
GenericThrowExceptionUtil.setGenericThrowException(false);
public void clean() {
GenericThrowExceptionUtil.clearCacheDeserializerMap();
}

// @Test
// public void testCustomThrowableDeserializer() throws Exception {
// GenericMultipleClassLoaderSofaSerializerFactory factory = new GenericMultipleClassLoaderSofaSerializerFactory();
// SofaResponseHessianSerializer serializer = new SofaResponseHessianSerializer(null, factory);
//
// ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
// Hessian2Output hessian2Output = new Hessian2Output(bsOut);
// hessian2Output.setSerializerFactory(factory);
//
// SofaResponse sofaResponse = new SofaResponse();
// MockError mockError = new MockError("MockError");
// sofaResponse.setAppResponse(mockError);
// hessian2Output.writeObject(sofaResponse);
// hessian2Output.flush();
//
// ByteArrayWrapperByteBuf bsIn = new ByteArrayWrapperByteBuf(bsOut.toByteArray());
// Map<String, String> ctx = new HashMap<>();
// ctx.put(RemotingConstants.HEAD_GENERIC_TYPE, "2");
// SofaResponse sofaResponse2 = new SofaResponse();
// serializer.decodeObjectByTemplate(bsIn, ctx, sofaResponse2);
// Assert.assertTrue(sofaResponse2.getAppResponse() instanceof GenericObject);
// Assert.assertEquals("MockError", ((GenericObject) sofaResponse2.getAppResponse()).getField("detailMessage"));
// }
@Test
public void testCustomThrowableDeserializer() throws Exception {
GenericMultipleClassLoaderSofaSerializerFactory factory = new GenericMultipleClassLoaderSofaSerializerFactory();
SofaResponseHessianSerializer serializer = new SofaResponseHessianSerializer(null, factory);

ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
Hessian2Output hessian2Output = new Hessian2Output(bsOut);
hessian2Output.setSerializerFactory(factory);

SofaResponse sofaResponse = new SofaResponse();
MockError mockError = new MockError("MockError");
sofaResponse.setAppResponse(mockError);
hessian2Output.writeObject(sofaResponse);
hessian2Output.flush();

ByteArrayWrapperByteBuf bsIn = new ByteArrayWrapperByteBuf(bsOut.toByteArray());
Map<String, String> ctx = new HashMap<>();
ctx.put(RemotingConstants.HEAD_GENERIC_TYPE, "2");
SofaResponse sofaResponse2 = new SofaResponse();
serializer.decodeObjectByTemplate(bsIn, ctx, sofaResponse2);
Assert.assertTrue(sofaResponse2.getAppResponse() instanceof GenericObject);
Assert.assertEquals("MockError", ((GenericObject) sofaResponse2.getAppResponse()).getField("detailMessage"));
}

@Test
public void testCustomThrowableDeserializerEnabled() throws Exception {
Expand All @@ -93,8 +94,6 @@ public void testCustomThrowableDeserializerEnabled() throws Exception {
serializer.decodeObjectByTemplate(bsIn, ctx, sofaResponse2);
Assert.assertTrue(sofaResponse2.getAppResponse() instanceof MockError);
Assert.assertEquals("MockError", ((MockError) sofaResponse2.getAppResponse()).getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
GenericThrowExceptionUtil.setGenericThrowException(false);
GenericThrowExceptionUtil.clearCacheDeserializerMap();
Expand Down Expand Up @@ -124,8 +123,6 @@ public void testCustomThrowableDeserializerEnabledForIncompatible() throws Excep
Assert.assertTrue(((RuntimeException) sofaResponse2.getAppResponse()).getMessage().startsWith(
"occur business exception, but type=com.alipay.sofa.rpc.codec.sofahessian.mock.DeprecatedMockError class is not "
+ "found, error: "));
} catch (Throwable e) {
e.printStackTrace();
} finally {
GenericThrowExceptionUtil.setGenericThrowException(false);
GenericThrowExceptionUtil.clearCacheDeserializerMap();
Expand Down

0 comments on commit 6b0138d

Please # to comment.