KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > EventPublicationInterceptorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
30  * @author Dmitriy Kopylenko
31  * @author Juergen Hoeller
32  */

33 public class EventPublicationInterceptorTests extends TestCase {
34
35     public void testWithIncorrectRequiredProperties() throws Exception JavaDoc {
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 JavaDoc ex) {
45             // expected
46
}
47
48         try {
49             interceptor.setApplicationEventClass(getClass());
50             interceptor.afterPropertiesSet();
51             fail("Should have thrown IllegalArgumentException");
52         }
53         catch (IllegalArgumentException JavaDoc ex) {
54             // expected
55
}
56     }
57
58     public void testExpectedBehavior() throws Exception JavaDoc {
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         // should automatically receive applicationEventPublisher reference
71

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         // invoke any method on the advised proxy to see if the interceptor has been invoked
84
testBean.getAge();
85         // two events: ContextRefreshedEvent and TestEvent
86
assertTrue("Interceptor published 1 event", listener.getEventCount() == 2);
87     }
88
89
90     public static class TestEvent extends ApplicationEvent {
91
92         public TestEvent(Object JavaDoc source) {
93             super(source);
94         }
95     }
96
97 }
98
Popular Tags