diff options
| author | Robert Gemmell <robbie@apache.org> | 2011-07-14 08:15:31 +0000 |
|---|---|---|
| committer | Robert Gemmell <robbie@apache.org> | 2011-07-14 08:15:31 +0000 |
| commit | b4f74c44b73e0e3ca21a0c39487135bdab12e4af (patch) | |
| tree | 075c9102f312e016022715449bb03f56c8ca9b40 /java/common/src/test | |
| parent | 05ebb6d1625b94e240648c2d251f3c524c7ab7d7 (diff) | |
| download | qpid-python-b4f74c44b73e0e3ca21a0c39487135bdab12e4af.tar.gz | |
QPID-3345: restore/add ability to use sys props to select the NetworkTransport used to make/accept connections
Applied patch by Keith Wall <keith.wall@gmail.com>
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1146594 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src/test')
| -rw-r--r-- | java/common/src/test/java/org/apache/qpid/test/utils/QpidTestCase.java | 49 | ||||
| -rw-r--r-- | java/common/src/test/java/org/apache/qpid/transport/network/TransportTest.java | 157 |
2 files changed, 206 insertions, 0 deletions
diff --git a/java/common/src/test/java/org/apache/qpid/test/utils/QpidTestCase.java b/java/common/src/test/java/org/apache/qpid/test/utils/QpidTestCase.java index 808374b06e..89542e8125 100644 --- a/java/common/src/test/java/org/apache/qpid/test/utils/QpidTestCase.java +++ b/java/common/src/test/java/org/apache/qpid/test/utils/QpidTestCase.java @@ -25,7 +25,9 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import junit.framework.TestCase; import junit.framework.TestResult; @@ -37,6 +39,8 @@ public class QpidTestCase extends TestCase { protected static final Logger _logger = Logger.getLogger(QpidTestCase.class); + private final Map<String, String> _propertiesSetForTest = new HashMap<String, String>(); + /** * Some tests are excluded when the property test.excludes is set to true. * An exclusion list is either a file (prop test.excludesfile) which contains one test name @@ -133,4 +137,49 @@ public class QpidTestCase extends TestCase { return AvailablePortFinder.getNextAvailable(10000); } + + /** + * Set a System property for duration of this test only. The tearDown will + * guarantee to reset the property to its previous value after the test + * completes. + * + * @param property The property to set + * @param value the value to set it to. + */ + protected void setTestSystemProperty(String property, String value) + { + if (!_propertiesSetForTest.containsKey(property)) + { + // Record the current value so we can revert it later. + _propertiesSetForTest.put(property, System.getProperty(property)); + } + + System.setProperty(property, value); + } + + /** + * Restore the System property values that were set by this test run. + */ + protected void revertTestSystemProperties() + { + for (String key : _propertiesSetForTest.keySet()) + { + String value = _propertiesSetForTest.get(key); + if (value != null) + { + System.setProperty(key, value); + } + else + { + System.clearProperty(key); + } + } + + _propertiesSetForTest.clear(); + } + + protected void tearDown() throws java.lang.Exception + { + revertTestSystemProperties(); + } } diff --git a/java/common/src/test/java/org/apache/qpid/transport/network/TransportTest.java b/java/common/src/test/java/org/apache/qpid/transport/network/TransportTest.java new file mode 100644 index 0000000000..4e504c69eb --- /dev/null +++ b/java/common/src/test/java/org/apache/qpid/transport/network/TransportTest.java @@ -0,0 +1,157 @@ +/* + * + * 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.transport.network; + + +import java.nio.ByteBuffer; + +import org.apache.qpid.framing.ProtocolVersion; +import org.apache.qpid.protocol.ProtocolEngineFactory; +import org.apache.qpid.ssl.SSLContextFactory; +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.transport.ConnectionSettings; +import org.apache.qpid.transport.NetworkTransportConfiguration; +import org.apache.qpid.transport.Receiver; +import org.apache.qpid.transport.TransportException; +import org.apache.qpid.transport.network.io.IoNetworkTransport; +import org.apache.qpid.transport.network.mina.MinaNetworkTransport; + +public class TransportTest extends QpidTestCase +{ + + + + public void testDefaultGetOutgoingTransportForv0_8() throws Exception + { + final OutgoingNetworkTransport networkTransport = Transport.getOutgoingTransportInstance(ProtocolVersion.v8_0); + assertNotNull(networkTransport); + assertTrue(networkTransport instanceof MinaNetworkTransport); + } + + public void testGloballyOverriddenOutgoingTransportForv0_8() throws Exception + { + setTestSystemProperty(Transport.QPID_TRANSPORT_PROPNAME, TestOutgoingNetworkTransport.class.getName()); + + final OutgoingNetworkTransport networkTransport = Transport.getOutgoingTransportInstance(ProtocolVersion.v8_0); + assertNotNull(networkTransport); + assertTrue(networkTransport instanceof TestOutgoingNetworkTransport); + } + + public void testProtocolSpecificOverriddenOutgoingTransportForv0_8() throws Exception + { + setTestSystemProperty(Transport.QPID_TRANSPORT_V0_8_PROPNAME, TestOutgoingNetworkTransport.class.getName()); + + final OutgoingNetworkTransport networkTransport = Transport.getOutgoingTransportInstance(ProtocolVersion.v8_0); + assertNotNull(networkTransport); + assertTrue(networkTransport instanceof TestOutgoingNetworkTransport); + } + + public void testDefaultGetOutgoingTransportForv0_10() throws Exception + { + final OutgoingNetworkTransport networkTransport = Transport.getOutgoingTransportInstance(ProtocolVersion.v0_10); + assertNotNull(networkTransport); + assertTrue(networkTransport instanceof IoNetworkTransport); + } + + public void testDefaultGetIncomingTransport() throws Exception + { + final IncomingNetworkTransport networkTransport = Transport.getIncomingTransportInstance(); + assertNotNull(networkTransport); + assertTrue(networkTransport instanceof MinaNetworkTransport); + } + + public void testOverriddenGetIncomingTransport() throws Exception + { + setTestSystemProperty(Transport.QPID_BROKER_TRANSPORT_PROPNAME, TestIncomingNetworkTransport.class.getName()); + + final IncomingNetworkTransport networkTransport = Transport.getIncomingTransportInstance(); + assertNotNull(networkTransport); + assertTrue(networkTransport instanceof TestIncomingNetworkTransport); + } + + public void testInvalidOutgoingTransportClassName() throws Exception + { + setTestSystemProperty(Transport.QPID_TRANSPORT_PROPNAME, "invalid"); + + try + { + Transport.getOutgoingTransportInstance(ProtocolVersion.v0_10); + fail("Should have failed to load the invalid class"); + } + catch(TransportException te) + { + //expected, ignore + } + } + + public void testInvalidOutgoingTransportProtocolVersion() throws Exception + { + try + { + Transport.getOutgoingTransportInstance(new ProtocolVersion((byte)0, (byte)0)); + fail("Should have failed to load the transport for invalid protocol version"); + } + catch(IllegalArgumentException iae) + { + //expected, ignore + } + } + + public static class TestOutgoingNetworkTransport implements OutgoingNetworkTransport + { + + public void close() + { + throw new UnsupportedOperationException(); + } + + public NetworkConnection getConnection() + { + throw new UnsupportedOperationException(); + } + + public NetworkConnection connect(ConnectionSettings settings, + Receiver<ByteBuffer> delegate, SSLContextFactory sslFactory) + { + throw new UnsupportedOperationException(); + } + } + + public static class TestIncomingNetworkTransport implements IncomingNetworkTransport + { + + public void close() + { + throw new UnsupportedOperationException(); + } + + public NetworkConnection getConnection() + { + throw new UnsupportedOperationException(); + } + + public void accept(NetworkTransportConfiguration config, + ProtocolEngineFactory factory, SSLContextFactory sslFactory) + { + throw new UnsupportedOperationException(); + } + } +} |
