Java Code Examples for org.springframework.util.SerializationTestUtils#serializeAndDeserialize()

The following examples show how to use org.springframework.util.SerializationTestUtils#serializeAndDeserialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: AspectProxyFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example 3
Source File: ComponentScanParserScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
}
 
Example 4
Source File: AspectProxyFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example 5
Source File: ComponentScanParserScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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();
}
 
Example 6
Source File: InjectAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 7
Source File: MessageHeadersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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"));
}
 
Example 8
Source File: MessageHeadersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"));
}
 
Example 9
Source File: TransactionAttributeSourceAdvisorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void serializability() throws Exception {
	TransactionInterceptor ti = new TransactionInterceptor();
	ti.setTransactionAttributes(new Properties());
	TransactionAttributeSourceAdvisor tas = new TransactionAttributeSourceAdvisor(ti);
	SerializationTestUtils.serializeAndDeserialize(tas);
}
 
Example 10
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 11
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 12
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 13
Source File: NameMatchMethodPointcutTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 14
Source File: InjectAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 15
Source File: AnnotationDrivenTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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");
}
 
Example 16
Source File: NameMatchMethodPointcutTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 17
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 18
Source File: AspectProxyFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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));
}
 
Example 19
Source File: MessageHeaderAccessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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));
}
 
Example 20
Source File: AnnotationDrivenTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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");
}