1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.usecases;
19  
20  import junit.framework.TestCase;
21  import org.codehaus.activemq.ActiveMQConnectionFactory;
22  import org.codehaus.activemq.broker.impl.BrokerConnectorImpl;
23  import org.codehaus.activemq.broker.impl.BrokerContainerImpl;
24  import org.codehaus.activemq.message.DefaultWireFormat;
25  
26  import javax.jms.JMSException;
27  
28  /***
29   * @author Oliver Belikan
30   * @version $Revision: 1.2 $
31   */
32  public class StartAndStopBrokerTest extends TestCase {
33      public void testStartupShutdown() throws Exception {
34          // This systemproperty is used if we dont want to
35          // have persistence messages as a default
36          System.setProperty("activemq.persistenceAdapter",
37                  "org.codehaus.activemq.store.vm.VMPersistenceAdapter");
38  
39          // configuration of container and all protocolls
40          BrokerContainerImpl container = createBroker();
41  
42          // start a client
43          ActiveMQConnectionFactory factory = new
44                  ActiveMQConnectionFactory("tcp://localhost:9100");
45          factory.start();
46          factory.createConnection();
47  
48          // stop activemq broker
49          container.stop();
50  
51          // start activemq broker again
52          container = createBroker();
53  
54          // start a client again
55          factory = new
56                  ActiveMQConnectionFactory("tcp://localhost:9100");
57          factory.start();
58          factory.createConnection();
59  
60          // stop activemq broker
61          container.stop();
62  
63      }
64  
65      protected BrokerContainerImpl createBroker() throws JMSException {
66          BrokerContainerImpl container = new BrokerContainerImpl("DefaultBroker");
67  
68          // Start internal vm protocoll
69          BrokerConnectorImpl vmConnector = new BrokerConnectorImpl(container, "vm://localhost", new DefaultWireFormat());
70          BrokerConnectorImpl tcpConnector = new BrokerConnectorImpl(container, "tcp://localhost:9100", new DefaultWireFormat());
71  
72          // start activemq broker
73          container.start();
74          return container;
75      }
76  
77  }