org.springframework.tests.context.SimpleMapScope Java Examples

The following examples show how to use org.springframework.tests.context.SimpleMapScope. 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: ComponentScanAnnotationIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void withScopedProxy() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxy.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
	// test serializability
	assertThat(bean.foo(1), equalTo("bar"));
	FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
	assertThat(deserialized, notNullValue());
	assertThat(deserialized.foo(1), equalTo("bar"));
}
 
Example #2
Source File: ComponentScanParserScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testTargetClassScopedProxy() throws Exception {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyTargetClassTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());

	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	// test serializability
	assertEquals("bar", bean.foo(1));
	ScopedProxyTestBean deserialized = (ScopedProxyTestBean) SerializationTestUtils.serializeAndDeserialize(bean);
	assertNotNull(deserialized);
	assertEquals("bar", deserialized.foo(1));
	context.close();
}
 
Example #3
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 #4
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 #5
Source File: ComponentScanParserScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testTargetClassScopedProxy() throws Exception {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyTargetClassTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());

	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	// test serializability
	assertEquals("bar", bean.foo(1));
	ScopedProxyTestBean deserialized = (ScopedProxyTestBean) SerializationTestUtils.serializeAndDeserialize(bean);
	assertNotNull(deserialized);
	assertEquals("bar", deserialized.foo(1));
	context.close();
}
 
Example #6
Source File: ComponentScanParserScopedProxyTests.java    From spring4-understanding with Apache License 2.0 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 #7
Source File: ComponentScanAnnotationIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void withScopedProxy() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxy.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
	// test serializability
	assertThat(bean.foo(1), equalTo("bar"));
	FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
	assertThat(deserialized, notNullValue());
	assertThat(deserialized.foo(1), equalTo("bar"));
}
 
Example #8
Source File: ComponentScanAnnotationIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withScopedProxy() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxy.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
	// test serializability
	assertThat(bean.foo(1), equalTo("bar"));
	FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
	assertThat(deserialized, notNullValue());
	assertThat(deserialized.foo(1), equalTo("bar"));
}
 
Example #9
Source File: ScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCglibScopedProxy() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
	bf.setSerializationId("Y");
	SimpleMapScope scope = new SimpleMapScope();
	bf.registerScope("request", scope);

	TestBean tb = (TestBean) bf.getBean("testBean");
	assertTrue(AopUtils.isCglibProxy(tb.getFriends()));
	assertTrue(tb.getFriends() instanceof ScopedObject);
	ScopedObject scoped = (ScopedObject) tb.getFriends();
	assertEquals(ArrayList.class, scoped.getTargetObject().getClass());
	tb.getFriends().add("myFriend");

	assertTrue(scope.getMap().containsKey("scopedTarget.scopedList"));
	assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass());

	ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
	assertNotNull(deserialized);
	assertTrue(AopUtils.isCglibProxy(deserialized));
	assertTrue(deserialized.contains("myFriend"));
	assertTrue(deserialized instanceof ScopedObject);
	ScopedObject scopedDeserialized = (ScopedObject) deserialized;
	assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass());

	bf.setSerializationId(null);
}
 
Example #10
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
	DummyInvocationHandler ih = new DummyInvocationHandler();
	Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {EntityManager.class}, ih);
	given(mockEmf.createEntityManager()).willReturn((EntityManager) mockEm);

	GenericApplicationContext gac = new GenericApplicationContext();
	SimpleMapScope myScope = new SimpleMapScope();
	gac.getDefaultListableBeanFactory().registerScope("myScope", myScope);
	gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
	gac.registerBeanDefinition("annotationProcessor",
			new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class);
	bd.setScope("myScope");
	gac.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), bd);
	gac.refresh();

	DefaultPublicPersistenceContextSetter bean = (DefaultPublicPersistenceContextSetter) gac.getBean(
			DefaultPublicPersistenceContextSetter.class.getName());
	assertNotNull(bean.em);
	assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean.em));

	SimpleMapScope serialized = (SimpleMapScope) SerializationTestUtils.serializeAndDeserialize(myScope);
	serialized.close();
	assertTrue(DummyInvocationHandler.closed);
	DummyInvocationHandler.closed = false;
}
 
Example #11
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
	DummyInvocationHandler ih = new DummyInvocationHandler();
	Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] {EntityManager.class}, ih);
	given(mockEmf.createEntityManager()).willReturn((EntityManager) mockEm);

	GenericApplicationContext gac = new GenericApplicationContext();
	SimpleMapScope myScope = new SimpleMapScope();
	gac.getDefaultListableBeanFactory().registerScope("myScope", myScope);
	gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
	gac.registerBeanDefinition("annotationProcessor",
			new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class);
	bd.setScope("myScope");
	gac.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), bd);
	gac.refresh();

	DefaultPublicPersistenceContextSetter bean = (DefaultPublicPersistenceContextSetter) gac.getBean(
			DefaultPublicPersistenceContextSetter.class.getName());
	assertNotNull(bean.em);
	assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean.em));

	SimpleMapScope serialized = (SimpleMapScope) SerializationTestUtils.serializeAndDeserialize(myScope);
	serialized.close();
	assertTrue(DummyInvocationHandler.closed);
	DummyInvocationHandler.closed = false;
}
 
Example #12
Source File: ComponentScanAnnotationIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}
 
Example #13
Source File: ComponentScanAnnotationIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}
 
Example #14
Source File: ComponentScanParserScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultScopedProxy() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyDefaultTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));
	context.close();
}
 
Example #15
Source File: ComponentScanParserScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoScopedProxy() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyNoTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));
	context.close();
}
 
Example #16
Source File: ComponentScanParserScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testTargetClassScopedProxy() throws Exception {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyTargetClassTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	// test serializability
	assertEquals("bar", bean.foo(1));
	ScopedProxyTestBean deserialized = (ScopedProxyTestBean) SerializationTestUtils.serializeAndDeserialize(bean);
	assertNotNull(deserialized);
	assertEquals("bar", deserialized.foo(1));
	context.close();
}
 
Example #17
Source File: ScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testScopedOverride() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
	SimpleMapScope scope = new SimpleMapScope();
	ctx.getBeanFactory().registerScope("request", scope);
	ctx.refresh();

	ITestBean bean = (ITestBean) ctx.getBean("testBean");
	assertEquals("male", bean.getName());
	assertEquals(99, bean.getAge());

	assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
	assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
 
Example #18
Source File: ScopedProxyTests.java    From spring4-understanding with Apache License 2.0 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 #19
Source File: ScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCglibScopedProxy() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
	bf.setSerializationId("Y");
	SimpleMapScope scope = new SimpleMapScope();
	bf.registerScope("request", scope);

	TestBean tb = (TestBean) bf.getBean("testBean");
	assertTrue(AopUtils.isCglibProxy(tb.getFriends()));
	assertTrue(tb.getFriends() instanceof ScopedObject);
	ScopedObject scoped = (ScopedObject) tb.getFriends();
	assertEquals(ArrayList.class, scoped.getTargetObject().getClass());
	tb.getFriends().add("myFriend");

	assertTrue(scope.getMap().containsKey("scopedTarget.scopedList"));
	assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass());

	ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
	assertNotNull(deserialized);
	assertTrue(AopUtils.isCglibProxy(deserialized));
	assertTrue(deserialized.contains("myFriend"));
	assertTrue(deserialized instanceof ScopedObject);
	ScopedObject scopedDeserialized = (ScopedObject) deserialized;
	assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass());

	bf.setSerializationId(null);
}
 
Example #20
Source File: ScopedProxyTests.java    From java-technology-stack 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 #21
Source File: ScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testScopedOverride() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
	SimpleMapScope scope = new SimpleMapScope();
	ctx.getBeanFactory().registerScope("request", scope);
	ctx.refresh();

	ITestBean bean = (ITestBean) ctx.getBean("testBean");
	assertEquals("male", bean.getName());
	assertEquals(99, bean.getAge());

	assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
	assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
 
Example #22
Source File: ComponentScanParserScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNoScopedProxy() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyNoTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());

	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));
	context.close();
}
 
Example #23
Source File: ComponentScanParserScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDefaultScopedProxy() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyDefaultTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());

	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));
	context.close();
}
 
Example #24
Source File: ComponentScanAnnotationIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}
 
Example #25
Source File: ComponentScanAnnotationIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
	ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ctx.refresh();
	// should cast to the interface
	FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
	// should be dynamic proxy
	assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}
 
Example #26
Source File: PersistenceInjectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
	DummyInvocationHandler ih = new DummyInvocationHandler();
	Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] {EntityManager.class}, ih);
	given(mockEmf.createEntityManager()).willReturn((EntityManager) mockEm);

	GenericApplicationContext gac = new GenericApplicationContext();
	SimpleMapScope myScope = new SimpleMapScope();
	gac.getDefaultListableBeanFactory().registerScope("myScope", myScope);
	gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
	gac.registerBeanDefinition("annotationProcessor",
			new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class);
	bd.setScope("myScope");
	gac.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), bd);
	gac.refresh();

	DefaultPublicPersistenceContextSetter bean = (DefaultPublicPersistenceContextSetter) gac.getBean(
			DefaultPublicPersistenceContextSetter.class.getName());
	assertNotNull(bean.em);
	assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean.em));

	SimpleMapScope serialized = (SimpleMapScope) SerializationTestUtils.serializeAndDeserialize(myScope);
	serialized.close();
	assertTrue(DummyInvocationHandler.closed);
	DummyInvocationHandler.closed = false;
}
 
Example #27
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCglibScopedProxy() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
	bf.setSerializationId("Y");
	SimpleMapScope scope = new SimpleMapScope();
	bf.registerScope("request", scope);

	TestBean tb = (TestBean) bf.getBean("testBean");
	assertTrue(AopUtils.isCglibProxy(tb.getFriends()));
	assertTrue(tb.getFriends() instanceof ScopedObject);
	ScopedObject scoped = (ScopedObject) tb.getFriends();
	assertEquals(ArrayList.class, scoped.getTargetObject().getClass());
	tb.getFriends().add("myFriend");

	assertTrue(scope.getMap().containsKey("scopedTarget.scopedList"));
	assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass());

	ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
	assertNotNull(deserialized);
	assertTrue(AopUtils.isCglibProxy(deserialized));
	assertTrue(deserialized.contains("myFriend"));
	assertTrue(deserialized instanceof ScopedObject);
	ScopedObject scopedDeserialized = (ScopedObject) deserialized;
	assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass());

	bf.setSerializationId(null);
}
 
Example #28
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 #29
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testScopedOverride() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
	SimpleMapScope scope = new SimpleMapScope();
	ctx.getBeanFactory().registerScope("request", scope);
	ctx.refresh();

	ITestBean bean = (ITestBean) ctx.getBean("testBean");
	assertEquals("male", bean.getName());
	assertEquals(99, bean.getAge());

	assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
	assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
 
Example #30
Source File: ComponentScanParserScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNoScopedProxy() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyNoTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());

	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));
	context.close();
}