001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.spring;
006:
007: import org.springframework.context.ApplicationEvent;
008: import org.springframework.context.support.ClassPathXmlApplicationContext;
009:
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.DSOSpringConfigHelper;
013: import com.tc.object.config.StandardDSOSpringConfigHelper;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tctest.TransparentTestBase;
017: import com.tctest.runner.AbstractTransparentApp;
018: import com.tctest.spring.bean.SimpleListener;
019: import com.tctest.spring.bean.SingletonEvent;
020:
021: import java.util.Iterator;
022:
023: /**
024: * Test case for <code>ApplicationEventPublisher</code>.
025: *
026: * @see org.springframework.context.ApplicationEventPublisher#publishEvent(ApplicationEvent event)
027: */
028: public class AppContextEvents_Test extends TransparentTestBase {
029: private static final int LOOP_ITERATIONS = 1;
030: private static final int EXECUTION_COUNT = 1;
031: private static final int NODE_COUNT = 2;
032:
033: public AppContextEvents_Test() {
034: //
035: }
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: getTransparentAppConfig().setClientCount(NODE_COUNT)
040: .setApplicationInstancePerClientCount(EXECUTION_COUNT)
041: .setIntensity(LOOP_ITERATIONS);
042: initializeTestRunner();
043: }
044:
045: protected Class getApplicationClass() {
046: return AppContextEventsApp.class;
047: }
048:
049: public static class AppContextEventsApp extends
050: AbstractTransparentApp {
051:
052: public AppContextEventsApp(String appId, ApplicationConfig cfg,
053: ListenerProvider listenerProvider) {
054: super (appId, cfg, listenerProvider);
055: }
056:
057: public void run() {
058: try {
059: ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext(
060: "com/tctest/spring/beanfactory.xml");
061: ClassPathXmlApplicationContext ctx2 = new ClassPathXmlApplicationContext(
062: new String[] { "com/tctest/spring/beanfactory3.xml" },
063: ctx1);
064:
065: SimpleListener simpleListener = (SimpleListener) ctx1
066: .getBean("simpleListener");
067:
068: moveToStageAndWait(1);
069:
070: ctx1.publishEvent(new SingletonEvent("ctx1",
071: "Test event1 " + getApplicationId()));
072: moveToStageAndWait(2);
073:
074: waitEvents(simpleListener, NODE_COUNT, 20000L);
075: assertEquals(NODE_COUNT, simpleListener.size());
076:
077: moveToStageAndWait(3);
078:
079: ctx2.publishEvent(new SingletonEvent("ctx2",
080: "Test event2 " + getApplicationId()));
081: moveToStageAndWait(4);
082:
083: waitEvents(simpleListener, NODE_COUNT * 2, 20000L);
084:
085: int ctx2Count = 0;
086: for (Iterator it = simpleListener.takeEvents()
087: .iterator(); it.hasNext();) {
088: SingletonEvent e = (SingletonEvent) it.next();
089: if (e.getSource().equals("ctx2"))
090: ctx2Count++;
091: }
092:
093: assertEquals(NODE_COUNT, ctx2Count);
094:
095: } catch (Throwable e) {
096: moveToStage(1);
097: moveToStage(2);
098: moveToStage(3);
099: notifyError(e);
100:
101: }
102: }
103:
104: private void waitEvents(SimpleListener listener, int count,
105: long timeout) {
106: long time = System.currentTimeMillis();
107: while (listener.size() < count
108: && (System.currentTimeMillis() - time) < timeout) {
109: try {
110: Thread.sleep(50L);
111: } catch (Exception e) {
112: // ignore
113: }
114: }
115: }
116:
117: public static void visitL1DSOConfig(ConfigVisitor visitor,
118: DSOClientConfigHelper config) {
119: config.addIncludePattern(
120: "com.tctest.spring.bean.SingletonEvent", true,
121: true, false);
122:
123: DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
124: springConfig
125: .addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing
126: // framework
127: springConfig.addConfigPattern("*/beanfactory.xml");
128: springConfig.addConfigPattern("*/beanfactory3.xml");
129: springConfig
130: .addDistributedEvent("com.tctest.spring.bean.SingletonEvent");
131: config.addDSOSpringConfig(springConfig);
132: }
133:
134: }
135:
136: }
|