qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arnaudsi...@apache.org
Subject svn commit: r580051 - in /incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid: test/unit/tests.properties test/unit/transacted/TransactedTest.java testutil/QpidTestCase.java
Date Thu, 27 Sep 2007 15:01:11 GMT
Author: arnaudsimon
Date: Thu Sep 27 08:01:11 2007
New Revision: 580051

URL: http://svn.apache.org/viewvc?rev=580051&view=rev
Log:
Added generic TestCase for connecting to remote broker 

Added:
    incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties
    incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java
  (with props)
Modified:
    incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java

Added: incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties?rev=580051&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties
(added)
+++ incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/tests.properties
Thu Sep 27 08:01:11 2007
@@ -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

Modified: incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java?rev=580051&r1=580050&r2=580051&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java
(original)
+++ incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/transacted/TransactedTest.java
Thu Sep 27 08:01:11 2007
@@ -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.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 @@
     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 @@
             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 @@
         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 @@
         testCon.close();
         _logger.info("Close prep connection");
         prepCon.close();
-        _logger.info("Kill broker");
-        TransportConnection.killAllVMBrokers();
         super.tearDown();
     }
 

Added: incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java?rev=580051&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java
(added)
+++ incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java
Thu Sep 27 08:01:11 2007
@@ -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;
+    }
+}

Propchange: incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/testutil/QpidTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message