KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > AppContextEvents_Test


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring;
5
6 import org.springframework.context.ApplicationEvent;
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.DSOSpringConfigHelper;
12 import com.tc.object.config.StandardDSOSpringConfigHelper;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tctest.TransparentTestBase;
16 import com.tctest.runner.AbstractTransparentApp;
17 import com.tctest.spring.bean.SimpleListener;
18 import com.tctest.spring.bean.SingletonEvent;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * Test case for <code>ApplicationEventPublisher</code>.
25  *
26  * @see org.springframework.context.ApplicationEventPublisher#publishEvent(ApplicationEvent event)
27  */

28 public class AppContextEvents_Test extends TransparentTestBase {
29   private static final int LOOP_ITERATIONS = 1;
30   private static final int EXECUTION_COUNT = 1;
31   private static final int NODE_COUNT = 2;
32
33   public AppContextEvents_Test() {
34     disableAllUntil("2008-01-01"); //covered by system test
35
}
36
37   protected void setUp() throws Exception JavaDoc {
38     super.setUp();
39     getTransparentAppConfig().setClientCount(NODE_COUNT).setApplicationInstancePerClientCount(EXECUTION_COUNT)
40         .setIntensity(LOOP_ITERATIONS);
41     initializeTestRunner();
42   }
43
44   protected Class JavaDoc getApplicationClass() {
45     return AppContextEventsApp.class;
46   }
47
48   
49   public static class AppContextEventsApp extends AbstractTransparentApp {
50
51     public AppContextEventsApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
52       super(appId, cfg, listenerProvider);
53     }
54
55     public void run() {
56         try {
57           ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory.xml");
58           ClassPathXmlApplicationContext ctx2 =
59             new ClassPathXmlApplicationContext(new String JavaDoc[] {"com/tctest/spring/beanfactory3.xml"}, ctx1);
60           
61           SimpleListener simpleListener = (SimpleListener) ctx1.getBean("simpleListener");
62           List JavaDoc events = simpleListener.getEvents();
63           
64           moveToStageAndWait(1);
65
66           ctx1.publishEvent(new SingletonEvent("ctx1", "Test event1 "+getApplicationId()));
67           moveToStageAndWait(2);
68
69           waitEvents(events, NODE_COUNT, 5000L);
70           assertEquals("Haven't received distributed events: "+events, NODE_COUNT, events.size());
71
72           moveToStageAndWait(3);
73           
74           ctx2.publishEvent(new SingletonEvent("ctx2", "Test event2 "+getApplicationId()));
75           moveToStageAndWait(4);
76
77           waitEvents(events, NODE_COUNT * 2, 5000L);
78           
79           int ctx2Count = 0;
80           for (Iterator JavaDoc it = events.iterator(); it.hasNext();) {
81             SingletonEvent e = (SingletonEvent) it.next();
82             if(e.getSource().equals("ctx2")) ctx2Count++;
83           }
84           
85           assertEquals("Haven't received distributed events: "+events, NODE_COUNT, ctx2Count);
86           
87         } catch (Throwable JavaDoc e) {
88           moveToStage(1);
89           moveToStage(2);
90           moveToStage(3);
91           notifyError(e);
92            
93         }
94     }
95
96     private void waitEvents(List JavaDoc events, int count, long timeout) {
97       long time = System.currentTimeMillis();
98       while(events.size()<count && (System.currentTimeMillis()-time)<timeout) {
99         try {
100           Thread.sleep(50L);
101         } catch (Exception JavaDoc e) {
102           // ignore
103
}
104       }
105     }
106
107     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
108       config.addIncludePattern("com.tctest.spring.bean.SingletonEvent", true, true, false);
109       
110       DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
111       springConfig.addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing framework
112
springConfig.addConfigPattern("*/beanfactory.xml");
113       springConfig.addConfigPattern("*/beanfactory3.xml");
114       springConfig.addDistributedEvent("com.tctest.spring.bean.SingletonEvent");
115       config.addDSOSpringConfig(springConfig);
116     }
117     
118   }
119   
120 }
121
Popular Tags