KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ClientTestSupport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq;
19
20 import java.util.concurrent.TimeUnit JavaDoc;
21 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
22
23 import org.apache.activemq.ActiveMQConnectionFactory;
24 import org.apache.activemq.broker.Broker;
25 import org.apache.activemq.broker.BrokerFactory;
26 import org.apache.activemq.broker.BrokerService;
27 import org.apache.activemq.broker.StubConnection;
28 import org.apache.activemq.broker.TransportConnector;
29 import org.apache.activemq.command.ActiveMQDestination;
30 import org.apache.activemq.command.ConnectionId;
31 import org.apache.activemq.command.ConnectionInfo;
32 import org.apache.activemq.command.ConsumerInfo;
33 import org.apache.activemq.command.Message;
34 import org.apache.activemq.command.MessageAck;
35 import org.apache.activemq.command.MessageDispatch;
36 import org.apache.activemq.command.RemoveInfo;
37 import org.apache.activemq.command.SessionInfo;
38 import org.apache.activemq.transport.TransportFactory;
39
40 import javax.jms.JMSException JavaDoc;
41
42 import java.io.File JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.net.URI JavaDoc;
45 import java.net.URISyntaxException JavaDoc;
46
47 import junit.framework.TestCase;
48
49 public class ClientTestSupport extends TestCase {
50
51     private ActiveMQConnectionFactory connFactory;
52     protected BrokerService broker;
53     private String JavaDoc brokerURL = "vm://localhost?broker.persistent=false";
54
55     protected long idGenerator=0;
56
57     public void setUp() throws Exception JavaDoc {
58         final AtomicBoolean JavaDoc connected = new AtomicBoolean JavaDoc(false);
59         TransportConnector connector;
60
61         // Start up a broker with a tcp connector.
62
try {
63             broker = BrokerFactory.createBroker(new URI JavaDoc(this.brokerURL));
64             String JavaDoc brokerId = broker.getBrokerName();
65             connector = new TransportConnector(broker.getBroker(), TransportFactory.bind(brokerId,new URI JavaDoc(this.brokerURL))) {
66                 // Hook into the connector so we can assert that the server accepted a connection.
67
protected org.apache.activemq.broker.Connection createConnection(org.apache.activemq.transport.Transport transport) throws IOException JavaDoc {
68                     connected.set(true);
69                     return super.createConnection(transport);
70                 }
71             };
72             connector.start();
73             broker.start();
74
75         } catch (IOException JavaDoc e) {
76             throw new JMSException JavaDoc("Error creating broker " + e);
77         } catch (URISyntaxException JavaDoc e) {
78             throw new JMSException JavaDoc("Error creating broker " + e);
79         }
80
81         URI JavaDoc connectURI;
82         connectURI = connector.getServer().getConnectURI();
83
84         // This should create the connection.
85
connFactory = new ActiveMQConnectionFactory(connectURI);
86     }
87
88     
89     protected void tearDown() throws Exception JavaDoc {
90         super.tearDown();
91         if (broker != null) {
92             broker.stop();
93         }
94     }
95
96
97     public ActiveMQConnectionFactory getConnectionFactory() throws JMSException JavaDoc {
98         if(this.connFactory == null){
99             throw new JMSException JavaDoc("ActiveMQConnectionFactory is null ");
100         }
101         return this.connFactory;
102     }
103
104     //Helper Classes
105
protected ConnectionInfo createConnectionInfo() throws Exception JavaDoc {
106         ConnectionInfo info = new ConnectionInfo();
107         info.setConnectionId(new ConnectionId("connection:"+(++idGenerator)));
108         info.setClientId( info.getConnectionId().getValue() );
109         return info;
110     }
111
112     protected SessionInfo createSessionInfo(ConnectionInfo connectionInfo) throws Exception JavaDoc {
113         SessionInfo info = new SessionInfo(connectionInfo, ++idGenerator);
114         return info;
115     }
116
117     protected ConsumerInfo createConsumerInfo(SessionInfo sessionInfo, ActiveMQDestination destination) throws Exception JavaDoc {
118         ConsumerInfo info = new ConsumerInfo(sessionInfo, ++idGenerator);
119         info.setBrowser(false);
120         info.setDestination(destination);
121         info.setPrefetchSize(1000);
122         info.setDispatchAsync(false);
123         return info;
124     }
125
126     protected RemoveInfo closeConsumerInfo(ConsumerInfo consumerInfo) {
127         return consumerInfo.createRemoveCommand();
128     }
129
130     protected MessageAck createAck(ConsumerInfo consumerInfo, Message msg, int count, byte ackType) {
131         MessageAck ack = new MessageAck();
132         ack.setAckType(ackType);
133         ack.setConsumerId(consumerInfo.getConsumerId());
134         ack.setDestination( msg.getDestination() );
135         ack.setLastMessageId( msg.getMessageId() );
136         ack.setMessageCount(count);
137         return ack;
138     }
139
140     protected Message receiveMessage(StubConnection connection, int MAX_WAIT) throws InterruptedException JavaDoc {
141         while( true ) {
142             Object JavaDoc o = connection.getDispatchQueue().poll(MAX_WAIT, TimeUnit.MILLISECONDS);
143
144             if( o == null )
145                 return null;
146
147             if( o instanceof MessageDispatch ) {
148                 MessageDispatch dispatch = (MessageDispatch)o;
149                 return dispatch.getMessage();
150             }
151         }
152     }
153
154     protected Broker getBroker() throws Exception JavaDoc{
155        return this.broker != null?this.broker.getBroker():null;
156     }
157
158     public static void removeMessageStore() {
159         if( System.getProperty("activemq.store.dir")!=null ) {
160             recursiveDelete(new File JavaDoc(System.getProperty("activemq.store.dir")));
161         }
162         if( System.getProperty("derby.system.home")!=null ) {
163             recursiveDelete(new File JavaDoc(System.getProperty("derby.system.home")));
164         }
165     }
166
167     public static void recursiveDelete(File JavaDoc f) {
168         if( f.isDirectory() ) {
169             File JavaDoc[] files = f.listFiles();
170             for (int i = 0; i < files.length; i++) {
171                 recursiveDelete(files[i]);
172             }
173         }
174         f.delete();
175     }
176
177 }
178
Popular Tags