KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > vm > VMTransportEmbeddedBrokerTest


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.transport.vm;
19
20 import java.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
22
23 import javax.jms.DeliveryMode JavaDoc;
24
25 import org.apache.activemq.broker.BrokerRegistry;
26 import org.apache.activemq.broker.BrokerTestSupport;
27 import org.apache.activemq.broker.StubConnection;
28 import org.apache.activemq.command.ActiveMQQueue;
29 import org.apache.activemq.command.ConnectionInfo;
30 import org.apache.activemq.command.ConsumerInfo;
31 import org.apache.activemq.command.Message;
32 import org.apache.activemq.command.ProducerInfo;
33 import org.apache.activemq.command.SessionInfo;
34 import org.apache.activemq.transport.Transport;
35 import org.apache.activemq.transport.TransportFactory;
36 import org.apache.activemq.util.IOExceptionSupport;
37
38 /**
39  * Used to see if the VM transport starts an embedded broker on demand.
40  *
41  * @version $Revision$
42  */

43 public class VMTransportEmbeddedBrokerTest extends BrokerTestSupport {
44
45     public static void main(String JavaDoc[] args) {
46         junit.textui.TestRunner.run(VMTransportEmbeddedBrokerTest.class);
47     }
48
49     public void testConsumerPrefetchAtOne() throws Exception JavaDoc {
50         
51         // Make sure the broker is created due to the connection being started.
52
assertNull(BrokerRegistry.getInstance().lookup("localhost"));
53         StubConnection connection = createConnection();
54         assertNotNull(BrokerRegistry.getInstance().lookup("localhost"));
55
56         // Start a producer and consumer
57
ConnectionInfo connectionInfo = createConnectionInfo();
58         SessionInfo sessionInfo = createSessionInfo(connectionInfo);
59         ProducerInfo producerInfo = createProducerInfo(sessionInfo);
60         connection.send(connectionInfo);
61         connection.send(sessionInfo);
62         connection.send(producerInfo);
63         
64         ActiveMQQueue destination = new ActiveMQQueue("TEST");
65
66         ConsumerInfo consumerInfo = createConsumerInfo(sessionInfo, destination);
67         consumerInfo.setPrefetchSize(1);
68         connection.send(consumerInfo);
69         
70         // Send 2 messages to the broker.
71
connection.send(createMessage(producerInfo, destination, DeliveryMode.NON_PERSISTENT));
72         connection.send(createMessage(producerInfo, destination, DeliveryMode.NON_PERSISTENT));
73         
74         // Make sure only 1 message was delivered.
75
Message m = receiveMessage(connection);
76         assertNotNull(m);
77         assertNoMessagesLeft(connection);
78         
79         // Make sure the broker is shutdown when the connection is stopped.
80
assertNotNull(BrokerRegistry.getInstance().lookup("localhost"));
81         connection.stop();
82         assertNull(BrokerRegistry.getInstance().lookup("localhost"));
83     }
84
85     protected void setUp() throws Exception JavaDoc {
86         // Don't call super since it manually starts up a broker.
87
}
88     protected void tearDown() throws Exception JavaDoc {
89         // Don't call super since it manually tears down a broker.
90
}
91     protected StubConnection createConnection() throws Exception JavaDoc {
92         try {
93             Transport transport = TransportFactory.connect(new URI JavaDoc("vm://localhost?broker.persistent=false"));
94             StubConnection connection = new StubConnection(transport);
95             return connection;
96         } catch (URISyntaxException JavaDoc e) {
97             throw IOExceptionSupport.create(e);
98         }
99     }
100
101 }
102
Popular Tags