1 16 17 package org.springframework.context; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.aop.framework.ProxyFactory; 22 import org.springframework.beans.BeansException; 23 import org.springframework.beans.ITestBean; 24 import org.springframework.beans.MutablePropertyValues; 25 import org.springframework.beans.TestBean; 26 import org.springframework.context.event.EventPublicationInterceptor; 27 import org.springframework.context.support.StaticApplicationContext; 28 29 33 public class EventPublicationInterceptorTests extends TestCase { 34 35 public void testWithIncorrectRequiredProperties() throws Exception { 36 EventPublicationInterceptor interceptor = new EventPublicationInterceptor(); 37 ApplicationContext ctx = new StaticApplicationContext(); 38 interceptor.setApplicationEventPublisher(ctx); 39 40 try { 41 interceptor.afterPropertiesSet(); 42 fail("Should have thrown IllegalArgumentException"); 43 } 44 catch (IllegalArgumentException ex) { 45 } 47 48 try { 49 interceptor.setApplicationEventClass(getClass()); 50 interceptor.afterPropertiesSet(); 51 fail("Should have thrown IllegalArgumentException"); 52 } 53 catch (IllegalArgumentException ex) { 54 } 56 } 57 58 public void testExpectedBehavior() throws Exception { 59 TestBean target = new TestBean(); 60 final TestListener listener = new TestListener(); 61 62 class TestContext extends StaticApplicationContext { 63 protected void onRefresh() throws BeansException { 64 addListener(listener); 65 } 66 } 67 68 MutablePropertyValues pvs = new MutablePropertyValues(); 69 pvs.addPropertyValue("applicationEventClass", TestEvent.class.getName()); 70 72 StaticApplicationContext ctx = new TestContext(); 73 ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs); 74 ctx.refresh(); 75 76 EventPublicationInterceptor interceptor = 77 (EventPublicationInterceptor) ctx.getBean("publisher"); 78 ProxyFactory factory = new ProxyFactory(target); 79 factory.addAdvice(0, interceptor); 80 81 ITestBean testBean = (ITestBean) factory.getProxy(); 82 83 testBean.getAge(); 85 assertTrue("Interceptor published 1 event", listener.getEventCount() == 2); 87 } 88 89 90 public static class TestEvent extends ApplicationEvent { 91 92 public TestEvent(Object source) { 93 super(source); 94 } 95 } 96 97 } 98 | Popular Tags |