diff options
| author | Arnaud Simon <arnaudsimon@apache.org> | 2007-09-27 15:01:11 +0000 |
|---|---|---|
| committer | Arnaud Simon <arnaudsimon@apache.org> | 2007-09-27 15:01:11 +0000 |
| commit | 7190479334fb9bf3942eae4d2d92685d72e82d55 (patch) | |
| tree | 69b471da0c9f9d2ba4ca7d338dedbcf5c0da7135 /java/client/src/test | |
| parent | ec8cb6b78e3a750ab1117387634a4313a64898de (diff) | |
| download | qpid-python-7190479334fb9bf3942eae4d2d92685d72e82d55.tar.gz | |
Added generic TestCase for connecting to remote broker
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@580051 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/client/src/test')
3 files changed, 249 insertions, 11 deletions
diff --git a/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties b/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties new file mode 100644 index 0000000000..250a9e5296 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties @@ -0,0 +1,39 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory + +# use the following property to configure the default connector +#java.naming.provider.url - ignored. + +# register some connection factories +# connectionfactory.[jndiname] = [ConnectionURL] +connectionfactory.local = qpid:password=guest;username=guest;client_id=clientid;virtualhost=test@tcp:localhost:5672 + +# register some queues in JNDI using the form +# queue.[jndiName] = [physicalName] +queue.MyQueue = example.MyQueue + +# register some topics in JNDI using the form +# topic.[jndiName] = [physicalName] +topic.ibmStocks = stocks.nyse.ibm + +# Register an AMQP destination in JNDI +# NOTE: Qpid currently only supports direct,topics and headers +# destination.[jniName] = [BindingURL] +destination.direct = direct://amq.direct//directQueue diff --git a/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java b/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java index 678474a18b..9e9815bcc0 100644 --- a/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java +++ b/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java @@ -20,14 +20,13 @@ */ package org.apache.qpid.test.unit.transacted; -import junit.framework.TestCase; import org.apache.qpid.client.AMQConnection; import org.apache.qpid.client.AMQQueue; import org.apache.qpid.client.AMQSession; -import org.apache.qpid.client.transport.TransportConnection; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.jms.Session; +import org.apache.qpid.testutil.QpidTestCase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,10 +37,9 @@ import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.TextMessage; -public class TransactedTest extends TestCase +public class TransactedTest extends QpidTestCase { private AMQQueue queue1; - private AMQQueue queue2; private AMQConnection con; private Session session; @@ -61,10 +59,8 @@ public class TransactedTest extends TestCase protected void setUp() throws Exception { super.setUp(); - TransportConnection.createVMBroker(1); _logger.info("Create Connection"); - con = new AMQConnection("vm://:1", "guest", "guest", "TransactedTest", "test"); - + con = (AMQConnection) getConnection("guest", "guest"); _logger.info("Create Session"); session = con.createSession(true, Session.SESSION_TRANSACTED); _logger.info("Create Q1"); @@ -72,7 +68,7 @@ public class TransactedTest extends TestCase new AMQQueue(session.getDefaultQueueExchangeName(), new AMQShortString("Q1"), new AMQShortString("Q1"), false, true); _logger.info("Create Q2"); - queue2 = new AMQQueue(session.getDefaultQueueExchangeName(), new AMQShortString("Q2"), false); + AMQQueue queue2 = new AMQQueue(session.getDefaultQueueExchangeName(), new AMQShortString("Q2"), false); _logger.info("Create Consumer of Q1"); consumer1 = session.createConsumer(queue1); @@ -101,7 +97,7 @@ public class TransactedTest extends TestCase prepCon.start(); _logger.info("Create test connection"); - testCon = new AMQConnection("vm://:1", "guest", "guest", "TestConnection", "test"); + testCon = (AMQConnection) getConnection("guest", "guest"); _logger.info("Create test session"); testSession = testCon.createSession(false, AMQSession.NO_ACKNOWLEDGE); _logger.info("Create test consumer of q2"); @@ -116,8 +112,6 @@ public class TransactedTest extends TestCase testCon.close(); _logger.info("Close prep connection"); prepCon.close(); - _logger.info("Kill broker"); - TransportConnection.killAllVMBrokers(); super.tearDown(); } diff --git a/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java b/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java new file mode 100644 index 0000000000..598211c393 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java @@ -0,0 +1,205 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.testutil; + +import junit.framework.TestCase; + +import javax.jms.Connection; +import javax.naming.InitialContext; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import org.apache.qpid.client.transport.TransportConnection; +import org.apache.qpid.client.AMQConnection; +import org.apache.qpid.client.AMQConnectionFactory; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * + */ +public class QpidTestCase extends TestCase +{ + /* this clas logger */ + private static final Logger _logger = LoggerFactory.getLogger(QpidTestCase.class); + + /* Test properties */ + private static final String SHEL = "broker_shel"; + private static final String BROKER_PATH = "broker_path"; + private static final String BROKER_PARAM = "broker_param"; + private static final String BROKER_VM = "vm"; + /** + * The process where the remote broker is running. + */ + private Process _brokerProcess; + + /* The test property values */ + // The default broker is an in-VM one + private String _shel = BROKER_VM; + private String _brokerPath = ""; + private String _brokerParams = ""; + + /* The broker communication objects */ + private InitialContext _initialContext; + private AMQConnectionFactory _connectionFactory; + + //--------- JUnit support + + protected void setUp() throws Exception + { + super.setUp(); + // get the propeties if they are set + if (System.getProperties().containsKey(SHEL)) + { + _shel = System.getProperties().getProperty(SHEL); + } + if (System.getProperties().containsKey(BROKER_PATH)) + { + _brokerPath = System.getProperties().getProperty(BROKER_PATH); + } + if (System.getProperties().containsKey(BROKER_PARAM)) + { + _brokerParams = System.getProperties().getProperty(BROKER_PARAM); + } + if (!_shel.equals(BROKER_VM)) + { + // start a new broker + startBroker(); + } + else + { + // create an in_VM broker + TransportConnection.createVMBroker(1); + } + System.out.println("========================================="); + System.out.println("= " + _shel + " " + _brokerPath + " " + _brokerParams); + } + + /** + * This method _is invoked after each test case. + * + * @throws Exception + */ + protected void tearDown() throws Exception + { + super.tearDown(); + _logger.info("Kill broker"); + if (_brokerProcess != null) + { + // destroy the currently running broker + _brokerProcess.destroy(); + } + else + { + TransportConnection.killAllVMBrokers(); + } + } + + //--------- Util method + + /** + * This method starts a remote server by spawning an external process. + * + * @throws Exception If the broker cannot be started + */ + public void startBroker() throws Exception + { + _logger.info("Starting broker"); + Runtime rt = Runtime.getRuntime(); + _brokerProcess = rt.exec(_shel + " " + _brokerPath + " " + _brokerParams); + BufferedReader reader = new BufferedReader(new InputStreamReader(_brokerProcess.getInputStream())); + if (reader.ready()) + { + //bad, we had an error starting the broker + throw new Exception("Problem when starting the broker: " + reader.readLine()); + } + } + + /** + * Stop the currently running broker. + */ + public void stopBroker() + { + _logger.info("Stopping broker"); + // stooping the broker + if (_brokerProcess != null) + { + _brokerProcess.destroy(); + } + } + + /** + * we assume that the environment is correctly set + * i.e. -Djava.naming.provider.url="..//example010.properties" + * TODO should be a way of setting that through maven + * + * @return an initial context + * @throws Exception if there is an error getting the context + */ + public InitialContext getInitialContext() throws Exception + { + _logger.info("get InitialContext"); + if (_initialContext == null) + { + _initialContext = new InitialContext(); + } + return _initialContext; + } + + /** + * Get a connection factory for the currently used broker + * + * @return A conection factory + * @throws Exception if there is an error getting the tactory + */ + public AMQConnectionFactory getConnectionFactory() throws Exception + { + _logger.info("get ConnectionFactory"); + if (_connectionFactory == null) + { + _connectionFactory = (AMQConnectionFactory) getInitialContext().lookup("local"); + } + return _connectionFactory; + } + + /** + * Get a connection (remote or in-VM) + * + * @param username The user name + * @param password The user password + * @return a newly created connection + * @throws Exception if there is an error getting the connection + */ + public Connection getConnection(String username, String password) throws Exception + { + _logger.info("get Connection"); + Connection con; + if (_shel.equals(BROKER_VM)) + { + con = new AMQConnection("vm://:1", username, password, "Test", "test"); + } + else + { + con = getConnectionFactory().createConnection(username, password); + } + return con; + } +} |
