KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > TransportBrokerTestSupport


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;
19
20 import java.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.activemq.broker.BrokerService;
26 import org.apache.activemq.broker.BrokerTest;
27 import org.apache.activemq.broker.StubConnection;
28 import org.apache.activemq.broker.TransportConnector;
29 import org.apache.activemq.transport.Transport;
30 import org.apache.activemq.transport.TransportFactory;
31
32 abstract public class TransportBrokerTestSupport extends BrokerTest {
33
34     private TransportConnector connector;
35     ArrayList JavaDoc connections = new ArrayList JavaDoc();
36
37     protected void setUp() throws Exception JavaDoc {
38         super.setUp();
39     }
40
41     protected BrokerService createBroker() throws Exception JavaDoc {
42         BrokerService service = super.createBroker();
43         connector = service.addConnector(getBindLocation());
44         return service;
45     }
46     
47     protected abstract String JavaDoc getBindLocation();
48
49     protected void tearDown() throws Exception JavaDoc {
50         for (Iterator JavaDoc iter = connections.iterator(); iter.hasNext();) {
51             StubConnection connection = (StubConnection) iter.next();
52             connection.stop();
53             iter.remove();
54         }
55         connector.stop();
56         super.tearDown();
57     }
58
59     protected URI JavaDoc getBindURI() throws URISyntaxException JavaDoc {
60         return new URI JavaDoc(getBindLocation());
61     }
62
63     protected StubConnection createConnection() throws Exception JavaDoc {
64         URI JavaDoc bindURI = getBindURI();
65         
66         // Note: on platforms like OS X we cannot bind to the actual hostname, so we
67
// instead use the original host name (typically localhost) to bind to
68

69         URI JavaDoc actualURI = connector.getServer().getConnectURI();
70         URI JavaDoc connectURI = new URI JavaDoc(actualURI.getScheme(), actualURI.getUserInfo(), bindURI.getHost(), actualURI.getPort(), actualURI.getPath(), actualURI
71                 .getQuery(), actualURI.getFragment());
72
73         Transport transport = TransportFactory.connect(connectURI);
74         StubConnection connection = new StubConnection(transport);
75         connections.add(connection);
76         return connection;
77     }
78
79 }
80
Popular Tags