下面列出了org.springframework.util.SerializationTestUtils#serializeAndDeserialize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
@SuppressWarnings("unchecked")
public void testWithInstance() throws Exception {
MultiplyReturnValue aspect = new MultiplyReturnValue();
int multiple = 3;
aspect.setMultiple(multiple);
TestBean target = new TestBean();
target.setAge(24);
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(target);
proxyFactory.addAspect(aspect);
ITestBean proxy = proxyFactory.getProxy();
assertEquals(target.getAge() * multiple, proxy.getAge());
ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
}
@Test
@SuppressWarnings("unchecked")
public void testWithInstance() throws Exception {
MultiplyReturnValue aspect = new MultiplyReturnValue();
int multiple = 3;
aspect.setMultiple(multiple);
TestBean target = new TestBean();
target.setAge(24);
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(target);
proxyFactory.addAspect(aspect);
ITestBean proxy = proxyFactory.getProxy();
assertEquals(target.getAge() * multiple, proxy.getAge());
ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
}
@Test
public void testInterfacesScopedProxy() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/context/annotation/scopedProxyInterfacesTests.xml");
context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
// should cast to the interface
FooService bean = (FooService) context.getBean("scopedProxyTestBean");
// should be dynamic proxy
assertTrue(AopUtils.isJdkDynamicProxy(bean));
// test serializability
assertEquals("bar", bean.foo(1));
FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
assertNotNull(deserialized);
assertEquals("bar", deserialized.foo(1));
context.close();
}
@Test
public void testInterfacesScopedProxy() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/context/annotation/scopedProxyInterfacesTests.xml");
context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
// should cast to the interface
FooService bean = (FooService) context.getBean("scopedProxyTestBean");
// should be dynamic proxy
assertTrue(AopUtils.isJdkDynamicProxy(bean));
// test serializability
assertEquals("bar", bean.foo(1));
FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
assertNotNull(deserialized);
assertEquals("bar", deserialized.foo(1));
context.close();
}
@Test
public void serializable() throws Exception {
TestBean1 tb = new TestBean1();
CallCountingTransactionManager ptm = new CallCountingTransactionManager();
AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(ITestBean1.class);
proxyFactory.addAdvice(ti);
proxyFactory.setTarget(tb);
ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
proxy.getAge();
assertEquals(1, ptm.commits);
ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy);
serializedProxy.getAge();
Advised advised = (Advised) serializedProxy;
TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
CallCountingTransactionManager serializedPtm =
(CallCountingTransactionManager) serializedTi.getTransactionManager();
assertEquals(2, serializedPtm.commits);
}
@Test
public void serializeWithAllSerializableHeaders() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("name", "joe");
map.put("age", 42);
MessageHeaders input = new MessageHeaders(map);
MessageHeaders output = (MessageHeaders) SerializationTestUtils.serializeAndDeserialize(input);
assertEquals("joe", output.get("name"));
assertEquals(42, output.get("age"));
assertEquals("joe", input.get("name"));
assertEquals(42, input.get("age"));
}
@Test
@SuppressWarnings("resource")
public void serializableWithPreviousUsage() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenProxyTargetClassTests.xml", getClass());
TransactionalService service = context.getBean("service", TransactionalService.class);
service.setSomething("someName");
service = (TransactionalService) SerializationTestUtils.serializeAndDeserialize(service);
service.setSomething("someName");
}
@Test
public void serializeMutableHeaders() throws Exception {
Map<String, Object> headers = new HashMap<>();
headers.put("foo", "bar");
Message<String> message = new GenericMessage<>("test", headers);
MessageHeaderAccessor mutableAccessor = MessageHeaderAccessor.getMutableAccessor(message);
mutableAccessor.setContentType(MimeTypeUtils.TEXT_PLAIN);
message = new GenericMessage<>(message.getPayload(), mutableAccessor.getMessageHeaders());
Message<?> output = (Message<?>) SerializationTestUtils.serializeAndDeserialize(message);
assertEquals("test", output.getPayload());
assertEquals("bar", output.getHeaders().get("foo"));
assertNotNull(output.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
@Test
@SuppressWarnings("unchecked")
public void testSerializable() throws Exception {
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
proxyFactory.addAspect(LoggingAspectOnVarargs.class);
ITestBean proxy = proxyFactory.getProxy();
assertTrue(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C));
ITestBean tb = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
assertTrue(tb.doWithVarargs(MyEnum.A, MyOtherEnum.C));
}
@Test
public void testObjectFactoryWithBeanMethod() throws Exception {
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMethodInjectionBean.class));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
bf.setSerializationId("test");
ObjectFactoryMethodInjectionBean bean = (ObjectFactoryMethodInjectionBean) bf.getBean("annotatedBean");
assertSame(bf.getBean("testBean"), bean.getTestBean());
bean = (ObjectFactoryMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean);
assertSame(bf.getBean("testBean"), bean.getTestBean());
}
@Test
public void testSerializable() throws Throwable {
testSets();
// Count is now 2
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
p2.getName();
assertEquals(2, nop2.getCount());
p2.echo(null);
assertEquals(3, nop2.getCount());
}
@Test
@SuppressWarnings("resource")
public void serializableWithoutPreviousUsage() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenProxyTargetClassTests.xml", getClass());
TransactionalService service = context.getBean("service", TransactionalService.class);
service = (TransactionalService) SerializationTestUtils.serializeAndDeserialize(service);
service.setSomething("someName");
}
@Test
public void testObjectFactoryWithTypedListField() throws Exception {
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListFieldInjectionBean.class));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
bf.setSerializationId("test");
ObjectFactoryListFieldInjectionBean bean = (ObjectFactoryListFieldInjectionBean) bf.getBean("annotatedBean");
assertSame(bf.getBean("testBean"), bean.getTestBean());
bean = (ObjectFactoryListFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean);
assertSame(bf.getBean("testBean"), bean.getTestBean());
}
@Test
public void testSerializable() throws Throwable {
testSets();
// Count is now 2
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
p2.getName();
assertEquals(2, nop2.getCount());
p2.echo(null);
assertEquals(3, nop2.getCount());
}
@Test
public void testSerializationWithManualConfiguration() throws Exception {
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
bpp.setInitAnnotationType(PostConstruct.class);
bpp.setDestroyAnnotationType(PreDestroy.class);
InitDestroyAnnotationBeanPostProcessor bpp2 = (InitDestroyAnnotationBeanPostProcessor)
SerializationTestUtils.serializeAndDeserialize(bpp);
AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean();
bpp2.postProcessBeforeDestruction(bean, "annotatedBean");
assertTrue(bean.destroyCalled);
}
@Test
public void testObjectFactoryWithTypedListField() throws Exception {
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListFieldInjectionBean.class));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
bf.setSerializationId("test");
ObjectFactoryListFieldInjectionBean bean = (ObjectFactoryListFieldInjectionBean) bf.getBean("annotatedBean");
assertSame(bf.getBean("testBean"), bean.getTestBean());
bean = (ObjectFactoryListFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean);
assertSame(bf.getBean("testBean"), bean.getTestBean());
}
@Test
public void testJdkScopedProxy() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT);
bf.setSerializationId("X");
SimpleMapScope scope = new SimpleMapScope();
bf.registerScope("request", scope);
ITestBean bean = (ITestBean) bf.getBean("testBean");
assertNotNull(bean);
assertTrue(AopUtils.isJdkDynamicProxy(bean));
assertTrue(bean instanceof ScopedObject);
ScopedObject scoped = (ScopedObject) bean;
assertEquals(TestBean.class, scoped.getTargetObject().getClass());
bean.setAge(101);
assertTrue(scope.getMap().containsKey("testBeanTarget"));
assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass());
ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean);
assertNotNull(deserialized);
assertTrue(AopUtils.isJdkDynamicProxy(deserialized));
assertEquals(101, bean.getAge());
assertTrue(deserialized instanceof ScopedObject);
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass());
bf.setSerializationId(null);
}
@Test
public void serializability() throws Exception {
TransactionInterceptor ti = new TransactionInterceptor();
ti.setTransactionAttributes(new Properties());
TransactionAttributeSourceAdvisor tas = new TransactionAttributeSourceAdvisor(ti);
SerializationTestUtils.serializeAndDeserialize(tas);
}
@Test
public void serializeWithAllSerializableHeaders() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("name", "joe");
map.put("age", 42);
MessageHeaders input = new MessageHeaders(map);
MessageHeaders output = (MessageHeaders) SerializationTestUtils.serializeAndDeserialize(input);
assertEquals("joe", output.get("name"));
assertEquals(42, output.get("age"));
assertEquals("joe", input.get("name"));
assertEquals(42, input.get("age"));
}
@Test
public void testObjectFactoryWithBeanField() throws Exception {
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
bf.setSerializationId("test");
ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean");
assertSame(bf.getBean("testBean"), bean.getTestBean());
bean = (ObjectFactoryFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean);
assertSame(bf.getBean("testBean"), bean.getTestBean());
}