01: /**
02: *
03: */package org.objectweb.celtix.bus.transports.jms;
04:
05: import junit.extensions.TestSetup;
06: import junit.framework.TestSuite;
07:
08: import org.activemq.broker.impl.BrokerContainerImpl;
09: import org.activemq.store.vm.VMPersistenceAdapter;
10:
11: class JMSBrokerSetup extends TestSetup {
12: JMSEmbeddedBroker jmsBrokerThread;
13: String jmsBrokerUrl = "tcp://localhost:61616";
14: String activeMQStorageDir;
15:
16: public JMSBrokerSetup(TestSuite suite, String url) {
17: super (suite);
18: jmsBrokerUrl = url;
19: }
20:
21: public JMSBrokerSetup(TestSuite suite) {
22: super (suite);
23: }
24:
25: public void setUp() throws Exception {
26:
27: jmsBrokerThread = new JMSEmbeddedBroker(jmsBrokerUrl);
28: jmsBrokerThread.startBroker();
29: }
30:
31: public void tearDown() throws Exception {
32: synchronized (this ) {
33: jmsBrokerThread.shutdownBroker = true;
34: }
35: if (jmsBrokerThread != null) {
36: jmsBrokerThread.join();
37: }
38:
39: jmsBrokerThread = null;
40: }
41:
42: class JMSEmbeddedBroker extends Thread {
43: boolean shutdownBroker;
44: final String brokerUrl;
45: Exception exception;
46:
47: public JMSEmbeddedBroker(String url) {
48: brokerUrl = url;
49: }
50:
51: public void startBroker() throws Exception {
52: synchronized (this ) {
53: super .start();
54: try {
55: wait();
56: if (exception != null) {
57: throw exception;
58: }
59: } catch (InterruptedException ex) {
60: ex.printStackTrace();
61: }
62: }
63: }
64:
65: public void run() {
66: try {
67: ContainerWapper container;
68: synchronized (this ) {
69: container = new ContainerWapper();
70: container.addConnector(brokerUrl);
71: container
72: .setPersistenceAdapter(new VMPersistenceAdapter());
73: container.start();
74: Thread.sleep(200);
75: notifyAll();
76: }
77: synchronized (this ) {
78: while (!shutdownBroker) {
79: wait(1000);
80: }
81: }
82: container.shutdown();
83: container = null;
84: } catch (Exception e) {
85: exception = e;
86: e.printStackTrace();
87: }
88: }
89:
90: }
91:
92: class ContainerWapper extends BrokerContainerImpl {
93:
94: public void shutdown() {
95: super.containerShutdown();
96: }
97: }
98: }
|