diff options
Diffstat (limited to 'java/common/src/test')
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java (renamed from java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java) | 106 | ||||
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/test/utils/QpidTestCase.java | 10 | ||||
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java | 90 | ||||
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/transport/ConnectionTest.java | 7 | ||||
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/transport/TestNetworkConnection.java | 12 | ||||
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/transport/network/TransportTest.java | 5 | ||||
-rw-r--r-- | java/common/src/test/java/org/apache/qpid/transport/network/io/IdleTimeoutTickerTest.java | 257 |
7 files changed, 405 insertions, 82 deletions
diff --git a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java index 16f35613d8..1ecf450551 100644 --- a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java +++ b/java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java @@ -22,8 +22,6 @@ package org.apache.qpid.framing; import junit.framework.Assert; import junit.framework.TestCase; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.apache.qpid.AMQPInvalidClassException; @@ -33,10 +31,8 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -public class PropertyFieldTableTest extends TestCase +public class FieldTableTest extends TestCase { - private static final Logger _logger = LoggerFactory.getLogger(PropertyFieldTableTest.class); - /** * Test that setting a similar named value replaces any previous value set on that name */ @@ -696,66 +692,8 @@ public class PropertyFieldTableTest extends TestCase result.setObject("object-short", Short.MAX_VALUE); size += 1 + EncodingUtils.encodedShortStringLength("object-short") + EncodingUtils.encodedShortLength(); Assert.assertEquals(size, result.getEncodedSize()); - } - // public void testEncodingSize1() - // { - // PropertyFieldTable table = new PropertyFieldTable(); - // int length = 0; - // result.put("one", 1L); - // length = EncodingUtils.encodedShortStringLength("one"); - // length += 1 + EncodingUtils.encodedLongLength(); - // assertEquals(length, result.getEncodedSize()); - // - // result.put("two", 2L); - // length += EncodingUtils.encodedShortStringLength("two"); - // length += 1 + EncodingUtils.encodedLongLength(); - // assertEquals(length, result.getEncodedSize()); - // - // result.put("three", 3L); - // length += EncodingUtils.encodedShortStringLength("three"); - // length += 1 + EncodingUtils.encodedLongLength(); - // assertEquals(length, result.getEncodedSize()); - // - // result.put("four", 4L); - // length += EncodingUtils.encodedShortStringLength("four"); - // length += 1 + EncodingUtils.encodedLongLength(); - // assertEquals(length, result.getEncodedSize()); - // - // result.put("five", 5L); - // length += EncodingUtils.encodedShortStringLength("five"); - // length += 1 + EncodingUtils.encodedLongLength(); - // assertEquals(length, result.getEncodedSize()); - // - // //fixme should perhaps be expanded to incorporate all types. - // - // final ByteBuffer buffer = ByteBuffer.allocate((int) result.getEncodedSize()); // FIXME XXX: Is cast a problem? - // - // result.writeToBuffer(buffer); - // - // buffer.flip(); - // - // long length = buffer.getUnsignedInt(); - // - // try - // { - // PropertyFieldTable table2 = new PropertyFieldTable(buffer, length); - // - // Assert.assertEquals((Long) 1L, table2.getLong("one")); - // Assert.assertEquals((Long) 2L, table2.getLong("two")); - // Assert.assertEquals((Long) 3L, table2.getLong("three")); - // Assert.assertEquals((Long) 4L, table2.getLong("four")); - // Assert.assertEquals((Long) 5L, table2.getLong("five")); - // } - // catch (AMQFrameDecodingException e) - // { - // e.printStackTrace(); - // fail("PFT should be instantiated from bytes." + e.getCause()); - // } - // - // } - /** * Additional test for setObject */ @@ -883,7 +821,6 @@ public class PropertyFieldTableTest extends TestCase { fail("property name are allowed to start with # and $s"); } - } /** @@ -919,7 +856,6 @@ public class PropertyFieldTableTest extends TestCase Assert.assertEquals("1", table.getObject("n1")); Assert.assertEquals("2", table.getObject("n2")); Assert.assertEquals("3", table.getObject("n3")); - } public void testAddAll() @@ -952,29 +888,51 @@ public class PropertyFieldTableTest extends TestCase assertEquals("Unexpected number of entries in table1 after addAll", 2, table1.size()); } - private void assertBytesEqual(byte[] expected, byte[] actual) + /** + * Tests that when copying properties into a new FielTable using the addAll() method, the + * properties are successfully added to the destination table when the source FieldTable + * was created from encoded input bytes, + */ + public void testAddingAllFromFieldTableCreatedUsingEncodedBytes() throws Exception { - Assert.assertEquals(expected.length, actual.length); + AMQShortString myBooleanTestProperty = new AMQShortString("myBooleanTestProperty"); - for (int index = 0; index < expected.length; index++) - { - Assert.assertEquals(expected[index], actual[index]); - } + //Create a new FieldTable and use it to encode data into a byte array. + FieldTable encodeTable = new FieldTable(); + encodeTable.put(myBooleanTestProperty, true); + byte[] data = encodeTable.getDataAsBytes(); + int length = data.length; + + //Verify we got the expected mount of encoded data (1B type hdr + 21B for name + 1B type hdr + 1B for boolean) + assertEquals("unexpected data length", 24, length); + + //Create a second FieldTable from the encoded bytes + FieldTable tableFromBytes = new FieldTable(new DataInputStream(new ByteArrayInputStream(data)), length); + + //Create a final FieldTable and addAll() from the table created with encoded bytes + FieldTable destinationTable = new FieldTable(); + assertTrue("unexpected size", destinationTable.isEmpty()); + destinationTable.addAll(tableFromBytes); + + //Verify that the destination table now contains the expected entry + assertEquals("unexpected size", 1, destinationTable.size()); + assertTrue("expected property not present", destinationTable.containsKey(myBooleanTestProperty)); + assertTrue("unexpected property value", destinationTable.getBoolean(myBooleanTestProperty)); } - private void assertBytesNotEqual(byte[] expected, byte[] actual) + private void assertBytesEqual(byte[] expected, byte[] actual) { Assert.assertEquals(expected.length, actual.length); for (int index = 0; index < expected.length; index++) { - Assert.assertFalse(expected[index] == actual[index]); + Assert.assertEquals(expected[index], actual[index]); } } public static junit.framework.Test suite() { - return new junit.framework.TestSuite(PropertyFieldTableTest.class); + return new junit.framework.TestSuite(FieldTableTest.class); } } 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 ec06400b7d..08f7387b75 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 @@ -41,6 +41,7 @@ public class QpidTestCase extends TestCase public static final String QPID_HOME = System.getProperty("QPID_HOME"); public static final String TEST_RESOURCES_DIR = QPID_HOME + "/../test-profiles/test_resources/"; public static final String TMP_FOLDER = System.getProperty("java.io.tmpdir"); + public static final String LOG4J_CONFIG_FILE_PATH = System.getProperty("log4j.configuration.file"); private static final Logger _logger = Logger.getLogger(QpidTestCase.class); @@ -115,12 +116,7 @@ public class QpidTestCase extends TestCase public QpidTestCase() { - this("QpidTestCase"); - } - - public QpidTestCase(String name) - { - super(name); + super(); } public void run(TestResult testResult) @@ -204,6 +200,8 @@ public class QpidTestCase extends TestCase { System.setProperty(property, value); } + + _logger.info("Set system property \"" + property + "\" to: \"" + value + "\""); } /** diff --git a/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java b/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java index 056d11faaa..14dec8efad 100644 --- a/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java +++ b/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java @@ -21,6 +21,12 @@ package org.apache.qpid.test.utils; import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import java.io.FileOutputStream; + +import junit.framework.TestCase; import org.apache.qpid.util.FileUtils; @@ -30,6 +36,7 @@ import org.apache.qpid.util.FileUtils; public class TestFileUtils { private static final String SYSTEM_TMP_DIR = System.getProperty("java.io.tmpdir"); + private static final String SUFFIX = "tmp"; /** * Create and return a temporary directory that will be deleted on exit. @@ -60,4 +67,87 @@ public class TestFileUtils return testDir; } + + public static File createTempFile(TestCase testcase) + { + return createTempFile(testcase, SUFFIX); + } + + public static File createTempFile(TestCase testcase, String suffix) + { + String prefix = testcase.getClass().getSimpleName() + "-" + testcase.getName(); + + File tmpFile; + try + { + tmpFile = File.createTempFile(prefix, suffix); + tmpFile.deleteOnExit(); + } + catch (IOException e) + { + throw new RuntimeException("Cannot create temporary file with prefix " + prefix + " and suffix " + SUFFIX, e); + } + + return tmpFile; + } + + /** + * Creates a temporary file from the resource name given, using the resource name as the file suffix. + * + * This is required because the tests use the jar files as their class path. + */ + public static File createTempFileFromResource(TestCase testCase, String resourceName) + { + File dst = createTempFile(testCase, resourceName); + InputStream in = testCase.getClass().getResourceAsStream(resourceName); + try + { + FileUtils.copy(in, dst); + } + catch (Exception e) + { + throw new RuntimeException("Cannot copy resource " + resourceName + + " to temp file " + dst.getAbsolutePath(), e); + } + dst.deleteOnExit(); + return dst; + } + + /** + * Creates a temporary file for given test with given suffix in file name. + * The given content is stored in the file using UTF-8 encoding. + */ + public static File createTempFile(TestCase testcase, String suffix, String content) + { + File file = createTempFile(testcase, suffix); + if (content != null) + { + FileOutputStream fos = null; + try + { + fos = new FileOutputStream(file); + fos.write(content.getBytes("UTF-8")); + fos.flush(); + } + catch (Exception e) + { + throw new RuntimeException("Cannot add the content into temp file " + file.getAbsolutePath(), e); + } + finally + { + if (fos != null) + { + try + { + fos.close(); + } + catch (IOException e) + { + throw new RuntimeException("Cannot close output stream into temp file " + file.getAbsolutePath(), e); + } + } + } + } + return file; + } } diff --git a/java/common/src/test/java/org/apache/qpid/transport/ConnectionTest.java b/java/common/src/test/java/org/apache/qpid/transport/ConnectionTest.java index f3715f351e..12bbd20228 100644 --- a/java/common/src/test/java/org/apache/qpid/transport/ConnectionTest.java +++ b/java/common/src/test/java/org/apache/qpid/transport/ConnectionTest.java @@ -155,6 +155,7 @@ public class ConnectionTest extends QpidTestCase implements SessionListener { final Connection conn = new Connection(); conn.setConnectionDelegate(new ClientDelegate(new ConnectionSettings())); + conn.addConnectionListener(new ConnectionListener() { public void opened(Connection conn) {} @@ -225,6 +226,12 @@ public class ConnectionTest extends QpidTestCase implements SessionListener ssn.setSessionListener(ConnectionTest.this); return ssn; } + + @Override + public void connectionStartOk(Connection conn, ConnectionStartOk ok) + { + tuneAuthorizedConnection(conn); + } }; try diff --git a/java/common/src/test/java/org/apache/qpid/transport/TestNetworkConnection.java b/java/common/src/test/java/org/apache/qpid/transport/TestNetworkConnection.java index 893f66c5ff..a19c2e7e43 100644 --- a/java/common/src/test/java/org/apache/qpid/transport/TestNetworkConnection.java +++ b/java/common/src/test/java/org/apache/qpid/transport/TestNetworkConnection.java @@ -83,6 +83,18 @@ public class TestNetworkConnection implements NetworkConnection return null; } + @Override + public int getMaxReadIdle() + { + return 0; + } + + @Override + public int getMaxWriteIdle() + { + return 0; + } + public void setMaxWriteIdle(int idleTime) { 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 index c882d3437e..bf9a5843d6 100644 --- 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 @@ -128,7 +128,8 @@ public class TransportTest extends QpidTestCase } public NetworkConnection connect(ConnectionSettings settings, - Receiver<ByteBuffer> delegate, SSLContext sslContext) + Receiver<ByteBuffer> delegate, + TransportActivity transportActivity) { throw new UnsupportedOperationException(); } @@ -148,7 +149,7 @@ public class TransportTest extends QpidTestCase } public void accept(NetworkTransportConfiguration config, - ProtocolEngineFactory factory, SSLContext sslContext) + ProtocolEngineFactory factory, SSLContext sslContext) { throw new UnsupportedOperationException(); } diff --git a/java/common/src/test/java/org/apache/qpid/transport/network/io/IdleTimeoutTickerTest.java b/java/common/src/test/java/org/apache/qpid/transport/network/io/IdleTimeoutTickerTest.java new file mode 100644 index 0000000000..5cdd7a8597 --- /dev/null +++ b/java/common/src/test/java/org/apache/qpid/transport/network/io/IdleTimeoutTickerTest.java @@ -0,0 +1,257 @@ +/* + * + * 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.io; + +import junit.framework.TestCase; + +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.security.Principal; + +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.transport.Sender; +import org.apache.qpid.transport.network.NetworkConnection; +import org.apache.qpid.transport.network.TransportActivity; + +public class IdleTimeoutTickerTest extends TestCase implements TransportActivity, NetworkConnection +{ + private IdleTimeoutTicker _ticker; + private static final int DEFAULT_TIMEOUT = 567890; + private long _lastReadTime; + private long _lastWriteTime; + private long _currentTime; + private int _maxWriteIdle; + private int _maxReadIdle; + private boolean _readerIdle; + private boolean _writerIdle; + + @Override + public void setUp() throws Exception + { + super.setUp(); + _ticker = new IdleTimeoutTicker(this, DEFAULT_TIMEOUT); + _ticker.setConnection(this); + _readerIdle = false; + _writerIdle = false; + _lastReadTime = 0l; + _lastWriteTime = 0l; + _maxReadIdle = 0; + _maxWriteIdle = 0; + } + + public void testNoIdle() throws Exception + { + _maxReadIdle = 4; + _maxWriteIdle = 2; + _lastReadTime = 0; + _lastWriteTime = 1500; + _currentTime = 3000; + // Current time = 3s, + // last read = 0s, max read idle = 4s, should check in 1s + // last write = 1.5s, max write idle = 2s, should check in 0.5s + long nextTime = _ticker.tick(_currentTime); + assertEquals("Incorrect next tick calculation", 500l, nextTime); + assertFalse("Incorrectly caused reader idle", _readerIdle); + assertFalse("Incorrectly caused writer idle", _writerIdle); + + + // Current time = 3.4s, + // last read = 0s, max read idle = 4s, should check in 0.6s + // last write = 3.1s, max write idle = 2s, should check in 1.7s + _lastWriteTime = 3100; + _currentTime = 3400; + nextTime = _ticker.tick(_currentTime); + assertEquals("Incorrect next tick calculation", 600l, nextTime); + assertFalse("Incorrectly caused reader idle", _readerIdle); + assertFalse("Incorrectly caused writer idle", _writerIdle); + + _maxReadIdle = 0; + nextTime = _ticker.tick(_currentTime); + assertEquals("Incorrect next tick calculation", 1700l, nextTime); + assertFalse("Incorrectly caused reader idle", _readerIdle); + assertFalse("Incorrectly caused writer idle", _writerIdle); + + _maxWriteIdle = 0; + nextTime = _ticker.tick(_currentTime); + assertEquals("Incorrect next tick calculation", DEFAULT_TIMEOUT, nextTime); + assertFalse("Incorrectly caused reader idle", _readerIdle); + assertFalse("Incorrectly caused writer idle", _writerIdle); + + } + + public void testReaderIdle() throws Exception + { + _maxReadIdle = 4; + _maxWriteIdle = 0; + _lastReadTime = 0; + _lastWriteTime = 2500; + _currentTime = 4000; + // Current time = 4s, + // last read = 0s, max read idle = 4s, reader idle + long nextTime = _ticker.tick(_currentTime); + + assertTrue(_readerIdle); + assertFalse(_writerIdle); + + _readerIdle = false; + + // last write = 2.5s, max write idle = 2s, should check in 0.5s + _maxWriteIdle = 2; + nextTime = _ticker.tick(_currentTime); + assertTrue(_readerIdle); + assertFalse(_writerIdle); + + _readerIdle = false; + // last write = 1.5s, max write idle = 2s, should check in 0.5s + + _lastWriteTime = 1500; + nextTime = _ticker.tick(_currentTime); + + assertTrue(_readerIdle); + assertTrue(_writerIdle); + + } + + public void testWriterIdle() throws Exception + { + _maxReadIdle = 0; + _maxWriteIdle = 2; + _lastReadTime = 0; + _lastWriteTime = 1500; + _currentTime = 4000; + // Current time = 4s, + // last write = 1.5s, max write idle = 2s, writer idle + long nextTime = _ticker.tick(_currentTime); + + assertTrue(_writerIdle); + assertFalse(_readerIdle); + assertEquals(2000l,nextTime); + + _writerIdle = false; + _lastWriteTime = 1500; + _maxReadIdle = 5; + + nextTime = _ticker.tick(_currentTime); + + assertTrue(_writerIdle); + assertFalse(_readerIdle); + assertEquals(1000l,nextTime); + + } + + //------------------------------------------------------------------------- + // Implement TransportActivity methods + //------------------------------------------------------------------------- + + @Override + public long getLastReadTime() + { + return _lastReadTime; + } + + @Override + public long getLastWriteTime() + { + return _lastWriteTime; + } + + @Override + public void writerIdle() + { + _writerIdle = true; + _lastWriteTime = _currentTime; + } + + @Override + public void readerIdle() + { + _readerIdle = true; + } + + //------------------------------------------------------------------------- + // Implement NetworkConnection methods + // Only actually use those relating to idle timeouts + //------------------------------------------------------------------------- + + @Override + public Sender<ByteBuffer> getSender() + { + return null; + } + + @Override + public void start() + { + } + + @Override + public void close() + { + } + + @Override + public SocketAddress getRemoteAddress() + { + return null; + } + + @Override + public SocketAddress getLocalAddress() + { + return null; + } + + @Override + public void setMaxWriteIdle(int sec) + { + _maxWriteIdle = sec; + } + + @Override + public void setMaxReadIdle(int sec) + { + _maxReadIdle = sec; + } + + @Override + public void setPeerPrincipal(Principal principal) + { + } + + @Override + public Principal getPeerPrincipal() + { + return null; + } + + @Override + public int getMaxReadIdle() + { + return _maxReadIdle; + } + + @Override + public int getMaxWriteIdle() + { + return _maxWriteIdle; + } +} |