From 9c73ef7a5ac10acd6a50d5d52bd721fc2faa5919 Mon Sep 17 00:00:00 2001 From: Kim van der Riet Date: Thu, 28 Feb 2013 16:14:30 +0000 Subject: Update from trunk r1375509 through r1450773 git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/asyncstore@1451244 13f79535-47bb-0310-9956-ffa450edef68 --- java/common/src/main/java/common.bnd | 2 +- .../qpid/configuration/ClientProperties.java | 15 + .../org/apache/qpid/framing/AMQShortString.java | 19 +- .../java/org/apache/qpid/framing/FieldTable.java | 1 + .../qpid/properties/ConnectionStartProperties.java | 27 +- .../org/apache/qpid/protocol/ProtocolEngine.java | 5 +- .../java/org/apache/qpid/transport/Connection.java | 88 +- .../transport/NetworkTransportConfiguration.java | 8 - .../org/apache/qpid/transport/ServerDelegate.java | 5 +- .../java/org/apache/qpid/transport/Session.java | 22 +- .../network/IncomingNetworkTransport.java | 4 +- .../qpid/transport/network/NetworkConnection.java | 4 + .../network/OutgoingNetworkTransport.java | 5 +- .../org/apache/qpid/transport/network/Ticker.java | 29 + .../qpid/transport/network/TransportActivity.java | 33 + .../transport/network/io/IdleTimeoutTicker.java | 87 ++ .../transport/network/io/IoNetworkConnection.java | 31 +- .../transport/network/io/IoNetworkTransport.java | 51 +- .../qpid/transport/network/io/IoReceiver.java | 100 ++- .../main/java/org/apache/qpid/url/BindingURL.java | 3 + .../main/java/org/apache/qpid/util/FileUtils.java | 13 + .../main/java/org/apache/qpid/util/NetMatcher.java | 300 ------- .../org/apache/qpid/framing/FieldTableTest.java | 938 ++++++++++++++++++++ .../qpid/framing/PropertyFieldTableTest.java | 980 --------------------- .../org/apache/qpid/test/utils/QpidTestCase.java | 10 +- .../org/apache/qpid/test/utils/TestFileUtils.java | 90 ++ .../org/apache/qpid/transport/ConnectionTest.java | 7 + .../qpid/transport/TestNetworkConnection.java | 12 + .../qpid/transport/network/TransportTest.java | 5 +- .../network/io/IdleTimeoutTickerTest.java | 257 ++++++ 30 files changed, 1769 insertions(+), 1382 deletions(-) create mode 100644 java/common/src/main/java/org/apache/qpid/transport/network/Ticker.java create mode 100644 java/common/src/main/java/org/apache/qpid/transport/network/TransportActivity.java create mode 100644 java/common/src/main/java/org/apache/qpid/transport/network/io/IdleTimeoutTicker.java delete mode 100644 java/common/src/main/java/org/apache/qpid/util/NetMatcher.java create mode 100644 java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java delete mode 100644 java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java create mode 100644 java/common/src/test/java/org/apache/qpid/transport/network/io/IdleTimeoutTickerTest.java (limited to 'java/common/src') diff --git a/java/common/src/main/java/common.bnd b/java/common/src/main/java/common.bnd index b34f8deacc..84350fdc75 100755 --- a/java/common/src/main/java/common.bnd +++ b/java/common/src/main/java/common.bnd @@ -17,7 +17,7 @@ # under the License. # -ver: 0.19.0 +ver: 0.21.0 Bundle-SymbolicName: qpid-common Bundle-Version: ${ver} diff --git a/java/common/src/main/java/org/apache/qpid/configuration/ClientProperties.java b/java/common/src/main/java/org/apache/qpid/configuration/ClientProperties.java index 5268ce9bc2..7aa280ce02 100644 --- a/java/common/src/main/java/org/apache/qpid/configuration/ClientProperties.java +++ b/java/common/src/main/java/org/apache/qpid/configuration/ClientProperties.java @@ -87,6 +87,8 @@ public class ClientProperties public static final String USE_LEGACY_MAP_MESSAGE_FORMAT = "qpid.use_legacy_map_message"; + public static final String USE_LEGACY_STREAM_MESSAGE_FORMAT = "qpid.use_legacy_stream_message"; + public static final String AMQP_VERSION = "qpid.amqp.version"; public static final String QPID_VERIFY_CLIENT_ID = "qpid.verify_client_id"; @@ -190,6 +192,19 @@ public class ClientProperties */ public static final long DEFAULT_FLOW_CONTROL_WAIT_NOTIFY_PERIOD = 5000L; + /** + * System property to control whether the client will declare queues during + * consumer creation when using BindingURLs. + */ + public static final String QPID_DECLARE_QUEUES_PROP_NAME = "qpid.declare_queues"; + + /** + * System property to control whether the client will declare exchanges during + * producer/consumer creation when using BindingURLs. + */ + public static final String QPID_DECLARE_EXCHANGES_PROP_NAME = "qpid.declare_exchanges"; + public static final String VERIFY_QUEUE_ON_SEND = "qpid.verify_queue_on_send"; + private ClientProperties() { diff --git a/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java b/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java index fdc71e31f9..1381390640 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java +++ b/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java @@ -110,7 +110,7 @@ public final class AMQShortString implements CharSequence, Comparable() { - + @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > LOCAL_INTERN_CACHE_SIZE; @@ -845,22 +845,15 @@ public final class AMQShortString implements CharSequence, Comparable +public interface ProtocolEngine extends Receiver, TransportActivity { // Returns the remote address of the NetworkDriver SocketAddress getRemoteAddress(); @@ -56,6 +57,6 @@ public interface ProtocolEngine extends Receiver void readerIdle(); - public void setNetworkConnection(NetworkConnection network, Sender sender); + public void setNetworkConnection(NetworkConnection network, Sender sender); } \ No newline at end of file diff --git a/java/common/src/main/java/org/apache/qpid/transport/Connection.java b/java/common/src/main/java/org/apache/qpid/transport/Connection.java index e87851cf7d..cdca726148 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/Connection.java +++ b/java/common/src/main/java/org/apache/qpid/transport/Connection.java @@ -21,12 +21,7 @@ package org.apache.qpid.transport; import org.apache.qpid.framing.ProtocolVersion; -import org.apache.qpid.transport.network.Assembler; -import org.apache.qpid.transport.network.Disassembler; -import org.apache.qpid.transport.network.InputHandler; -import org.apache.qpid.transport.network.NetworkConnection; -import org.apache.qpid.transport.network.OutgoingNetworkTransport; -import org.apache.qpid.transport.network.Transport; +import org.apache.qpid.transport.network.*; import org.apache.qpid.transport.network.security.SecurityLayer; import org.apache.qpid.transport.network.security.SecurityLayerFactory; import org.apache.qpid.transport.util.Logger; @@ -73,6 +68,8 @@ public class Connection extends ConnectionInvoker //Usable channels are numbered 0 to - 1 public static final int MAX_CHANNEL_MAX = 0xFFFF; public static final int MIN_USABLE_CHANNEL_NUM = 0; + private long _lastSendTime; + private long _lastReadTime; public enum State { NEW, CLOSED, OPENING, OPEN, CLOSING, CLOSE_RCVD, RESUMING } @@ -89,15 +86,15 @@ public class Connection extends ConnectionInvoker public static interface SessionFactory { - Session newSession(Connection conn, Binary name, long expiry); + Session newSession(Connection conn, Binary name, long expiry, boolean isNoReplay); } private static final class DefaultSessionFactory implements SessionFactory { - public Session newSession(final Connection conn, final Binary name, final long expiry) + public Session newSession(final Connection conn, final Binary name, final long expiry, final boolean isNoReplay) { - return new Session(conn, name, expiry); + return new Session(conn, name, expiry, isNoReplay); } } @@ -232,9 +229,10 @@ public class Connection extends ConnectionInvoker addConnectionListener((ConnectionListener)secureReceiver); } - NetworkConnection network = transport.connect(settings, secureReceiver, null); - _remoteAddress = network.getRemoteAddress(); - _localAddress = network.getLocalAddress(); + NetworkConnection network = transport.connect(settings, secureReceiver, new ConnectionActivity()); + + setRemoteAddress(network.getRemoteAddress()); + setLocalAddress(network.getLocalAddress()); final Sender secureSender = securityLayer.sender(network.getSender()); if(secureSender instanceof ConnectionListener) @@ -298,7 +296,12 @@ public class Connection extends ConnectionInvoker public Session createSession(long expiry) { - return createSession(UUID.randomUUID().toString(), expiry); + return createSession(expiry, false); + } + + public Session createSession(long expiry, boolean isNoReplay) + { + return createSession(UUID.randomUUID().toString(), expiry, isNoReplay); } public Session createSession(String name) @@ -311,12 +314,22 @@ public class Connection extends ConnectionInvoker return createSession(Strings.toUTF8(name), expiry); } + public Session createSession(String name, long expiry,boolean isNoReplay) + { + return createSession(new Binary(Strings.toUTF8(name)), expiry, isNoReplay); + } + public Session createSession(byte[] name, long expiry) { return createSession(new Binary(name), expiry); } public Session createSession(Binary name, long expiry) + { + return createSession(name, expiry, false); + } + + public Session createSession(Binary name, long expiry, boolean isNoReplay) { synchronized (lock) { @@ -331,7 +344,7 @@ public class Connection extends ConnectionInvoker throw new ConnectionException("Timed out waiting for connection to be ready. Current state is :" + state); } - Session ssn = _sessionFactory.newSession(this, name, expiry); + Session ssn = _sessionFactory.newSession(this, name, expiry, isNoReplay); registerSession(ssn); map(ssn); ssn.attach(); @@ -369,6 +382,7 @@ public class Connection extends ConnectionInvoker public void received(ProtocolEvent event) { + _lastReadTime = System.currentTimeMillis(); if(log.isDebugEnabled()) { log.debug("RECV: [%s] %s", this, event); @@ -378,6 +392,7 @@ public class Connection extends ConnectionInvoker public void send(ProtocolEvent event) { + _lastSendTime = System.currentTimeMillis(); if(log.isDebugEnabled()) { log.debug("SEND: [%s] %s", this, event); @@ -728,6 +743,17 @@ public class Connection extends ConnectionInvoker return _localAddress; } + protected void setRemoteAddress(SocketAddress remoteAddress) + { + _remoteAddress = remoteAddress; + } + + protected void setLocalAddress(SocketAddress localAddress) + { + _localAddress = localAddress; + } + + private void invokeSessionDetached(int channel, SessionDetachCode sessionDetachCode) { SessionDetached sessionDetached = new SessionDetached(); @@ -735,4 +761,38 @@ public class Connection extends ConnectionInvoker sessionDetached.setCode(sessionDetachCode); invoke(sessionDetached); } + + + protected void doHeartBeat() + { + connectionHeartbeat(); + } + + private class ConnectionActivity implements TransportActivity + { + @Override + public long getLastReadTime() + { + return _lastReadTime; + } + + @Override + public long getLastWriteTime() + { + return _lastSendTime; + } + + @Override + public void writerIdle() + { + connectionHeartbeat(); + } + + @Override + public void readerIdle() + { + // TODO + + } + } } diff --git a/java/common/src/main/java/org/apache/qpid/transport/NetworkTransportConfiguration.java b/java/common/src/main/java/org/apache/qpid/transport/NetworkTransportConfiguration.java index 20d6f98fa6..12f8d801dc 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/NetworkTransportConfiguration.java +++ b/java/common/src/main/java/org/apache/qpid/transport/NetworkTransportConfiguration.java @@ -38,14 +38,6 @@ public interface NetworkTransportConfiguration // The amount of memory in bytes to allocate to the outgoing buffer Integer getSendBufferSize(); - Integer getPort(); - - String getHost(); - - String getTransport(); - - Integer getConnectorProcessors(); - InetSocketAddress getAddress(); boolean needClientAuth(); diff --git a/java/common/src/main/java/org/apache/qpid/transport/ServerDelegate.java b/java/common/src/main/java/org/apache/qpid/transport/ServerDelegate.java index e9a7d51456..1e0d5b9698 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/ServerDelegate.java +++ b/java/common/src/main/java/org/apache/qpid/transport/ServerDelegate.java @@ -71,7 +71,8 @@ public class ServerDelegate extends ConnectionDelegate if (mechanism == null || mechanism.length() == 0) { - tuneAuthorizedConnection(conn); + conn.connectionClose(ConnectionCloseCode.CONNECTION_FORCED, + "No Sasl mechanism was specified"); return; } @@ -82,7 +83,7 @@ public class ServerDelegate extends ConnectionDelegate if (ss == null) { conn.connectionClose(ConnectionCloseCode.CONNECTION_FORCED, - "null SASL mechanism: " + mechanism); + "No SaslServer could be created for mechanism: " + mechanism); return; } conn.setSaslServer(ss); diff --git a/java/common/src/main/java/org/apache/qpid/transport/Session.java b/java/common/src/main/java/org/apache/qpid/transport/Session.java index 95c3e4669f..8b29d6e424 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/Session.java +++ b/java/common/src/main/java/org/apache/qpid/transport/Session.java @@ -25,7 +25,6 @@ import org.apache.qpid.configuration.ClientProperties; import org.apache.qpid.transport.network.Frame; import org.apache.qpid.transport.util.Logger; import org.apache.qpid.transport.util.Waiter; - import static org.apache.qpid.transport.Option.COMPLETED; import static org.apache.qpid.transport.Option.SYNC; import static org.apache.qpid.transport.Option.TIMELY_REPLY; @@ -132,19 +131,31 @@ public class Session extends SessionInvoker private final Object stateLock = new Object(); private final AtomicBoolean _failoverRequired = new AtomicBoolean(false); + private boolean _isNoReplay = false; protected Session(Connection connection, Binary name, long expiry) { this(connection, new SessionDelegate(), name, expiry); } + protected Session(Connection connection, Binary name, long expiry, boolean noReplay) + { + this(connection, new SessionDelegate(), name, expiry, noReplay); + } + protected Session(Connection connection, SessionDelegate delegate, Binary name, long expiry) + { + this(connection, delegate, name, expiry,false); + } + + protected Session(Connection connection, SessionDelegate delegate, Binary name, long expiry, boolean noReplay) { this.connection = connection; this.delegate = delegate; this.name = name; this.expiry = expiry; this.closing = false; + this._isNoReplay = noReplay; initReceiver(); } @@ -282,6 +293,7 @@ public class Session extends SessionInvoker void resume() { _failoverRequired.set(false); + synchronized (commandsLock) { attach(); @@ -414,7 +426,7 @@ public class Session extends SessionInvoker if(log.isDebugEnabled()) { - log.debug("ID: [%s] %s", this.channel, id); + log.debug("identify: ch=%s, commandId=%s", this.channel, id); } if ((id & 0xff) == 0) @@ -443,7 +455,7 @@ public class Session extends SessionInvoker { if(log.isDebugEnabled()) { - log.debug("%s processed([%d,%d]) %s %s", this, lower, upper, syncPoint, maxProcessed); + log.debug("%s ch=%s processed([%d,%d]) %s %s", this, channel, lower, upper, syncPoint, maxProcessed); } boolean flush; @@ -451,7 +463,7 @@ public class Session extends SessionInvoker { if(log.isDebugEnabled()) { - log.debug("%s", processed); + log.debug("%s processed: %s", this, processed); } if (ge(upper, commandsIn)) @@ -740,7 +752,7 @@ public class Session extends SessionInvoker sessionCommandPoint(0, 0); } - boolean replayTransfer = !closing && !transacted && + boolean replayTransfer = !_isNoReplay && !closing && !transacted && m instanceof MessageTransfer && ! m.isUnreliable(); diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/IncomingNetworkTransport.java b/java/common/src/main/java/org/apache/qpid/transport/network/IncomingNetworkTransport.java index 4d4274278f..8437ef1a94 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/network/IncomingNetworkTransport.java +++ b/java/common/src/main/java/org/apache/qpid/transport/network/IncomingNetworkTransport.java @@ -27,5 +27,7 @@ import javax.net.ssl.SSLContext; public interface IncomingNetworkTransport extends NetworkTransport { - public void accept(NetworkTransportConfiguration config, ProtocolEngineFactory factory, SSLContext sslContext); + public void accept(NetworkTransportConfiguration config, + ProtocolEngineFactory factory, + SSLContext sslContext); } \ No newline at end of file diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/NetworkConnection.java b/java/common/src/main/java/org/apache/qpid/transport/network/NetworkConnection.java index 12c42d6643..050d194c47 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/network/NetworkConnection.java +++ b/java/common/src/main/java/org/apache/qpid/transport/network/NetworkConnection.java @@ -50,4 +50,8 @@ public interface NetworkConnection void setPeerPrincipal(Principal principal); Principal getPeerPrincipal(); + + int getMaxReadIdle(); + + int getMaxWriteIdle(); } diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/OutgoingNetworkTransport.java b/java/common/src/main/java/org/apache/qpid/transport/network/OutgoingNetworkTransport.java index 854d76430b..45231aa05d 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/network/OutgoingNetworkTransport.java +++ b/java/common/src/main/java/org/apache/qpid/transport/network/OutgoingNetworkTransport.java @@ -23,12 +23,13 @@ package org.apache.qpid.transport.network; import org.apache.qpid.transport.ConnectionSettings; import org.apache.qpid.transport.Receiver; -import javax.net.ssl.SSLContext; import java.nio.ByteBuffer; public interface OutgoingNetworkTransport extends NetworkTransport { public NetworkConnection getConnection(); - public NetworkConnection connect(ConnectionSettings settings, Receiver delegate, SSLContext sslContext); + public NetworkConnection connect(ConnectionSettings settings, + Receiver delegate, + TransportActivity transportActivity); } \ No newline at end of file diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/Ticker.java b/java/common/src/main/java/org/apache/qpid/transport/network/Ticker.java new file mode 100644 index 0000000000..210b014a57 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/transport/network/Ticker.java @@ -0,0 +1,29 @@ +/* + * + * 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; + +public interface Ticker +{ + int getTimeToNextTick(long currentTime); + + int tick(long currentTime); +} diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/TransportActivity.java b/java/common/src/main/java/org/apache/qpid/transport/network/TransportActivity.java new file mode 100644 index 0000000000..2ee336d9b2 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/transport/network/TransportActivity.java @@ -0,0 +1,33 @@ +/* + * + * 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; + +public interface TransportActivity +{ + long getLastReadTime(); + + long getLastWriteTime(); + + void writerIdle(); + + void readerIdle(); +} diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/io/IdleTimeoutTicker.java b/java/common/src/main/java/org/apache/qpid/transport/network/io/IdleTimeoutTicker.java new file mode 100644 index 0000000000..54a2a360bb --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/transport/network/io/IdleTimeoutTicker.java @@ -0,0 +1,87 @@ +/* + * + * 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 org.apache.qpid.transport.network.NetworkConnection; +import org.apache.qpid.transport.network.Ticker; +import org.apache.qpid.transport.network.TransportActivity; + +class IdleTimeoutTicker implements Ticker +{ + private final TransportActivity _transport; + private final int _defaultTimeout; + private NetworkConnection _connection; + + public IdleTimeoutTicker(TransportActivity transport, int defaultTimeout) + { + _transport = transport; + _defaultTimeout = defaultTimeout; + } + + @Override + public int getTimeToNextTick(long currentTime) + { + long nextTime = -1; + final long maxReadIdle = 1000l * _connection.getMaxReadIdle(); + + if(maxReadIdle > 0) + { + nextTime = _transport.getLastReadTime() + maxReadIdle; + } + + long maxWriteIdle = 1000l * _connection.getMaxWriteIdle(); + + if(maxWriteIdle > 0) + { + long writeTime = _transport.getLastWriteTime() + maxWriteIdle; + if(nextTime == -1l || writeTime < nextTime) + { + nextTime = writeTime; + } + } + return nextTime == -1 ? _defaultTimeout : (int) (nextTime - currentTime); + } + + @Override + public int tick(long currentTime) + { + // writer Idle + long maxWriteIdle = 1000l * _connection.getMaxWriteIdle(); + if(maxWriteIdle > 0 && maxWriteIdle+ _transport.getLastWriteTime() <= currentTime) + { + _transport.writerIdle(); + } + // reader Idle + final long maxReadIdle = 1000l * _connection.getMaxReadIdle(); + if(maxReadIdle > 0 && maxReadIdle+ _transport.getLastReadTime() <= currentTime) + { + + _transport.readerIdle(); + } + return getTimeToNextTick(currentTime); + } + + public void setConnection(NetworkConnection connection) + { + _connection = connection; + } +} diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkConnection.java b/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkConnection.java index 2658296c5f..f5c09ac2cc 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkConnection.java +++ b/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkConnection.java @@ -26,7 +26,9 @@ import java.nio.ByteBuffer; import java.security.Principal; import org.apache.qpid.transport.Receiver; import org.apache.qpid.transport.Sender; +import org.apache.qpid.transport.network.Ticker; import org.apache.qpid.transport.network.NetworkConnection; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,14 +40,23 @@ public class IoNetworkConnection implements NetworkConnection private final IoSender _ioSender; private final IoReceiver _ioReceiver; private Principal _principal; + private int _maxReadIdle; + private int _maxWriteIdle; public IoNetworkConnection(Socket socket, Receiver delegate, - int sendBufferSize, int receiveBufferSize, long timeout) + int sendBufferSize, int receiveBufferSize, long timeout) + { + this(socket,delegate,sendBufferSize,receiveBufferSize,timeout,null); + } + + public IoNetworkConnection(Socket socket, Receiver delegate, + int sendBufferSize, int receiveBufferSize, long timeout, Ticker ticker) { _socket = socket; _timeout = timeout; _ioReceiver = new IoReceiver(_socket, delegate, receiveBufferSize,_timeout); + _ioReceiver.setTicker(ticker); _ioSender = new IoSender(_socket, 2 * sendBufferSize, _timeout); @@ -88,14 +99,12 @@ public class IoNetworkConnection implements NetworkConnection public void setMaxWriteIdle(int sec) { - // TODO implement support for setting heartbeating config in this way - // Currently a socket timeout is used in IoSender + _maxWriteIdle = sec; } public void setMaxReadIdle(int sec) { - // TODO implement support for setting heartbeating config in this way - // Currently a socket timeout is used in IoSender + _maxReadIdle = sec; } @Override @@ -109,4 +118,16 @@ public class IoNetworkConnection implements NetworkConnection { return _principal; } + + @Override + public int getMaxReadIdle() + { + return _maxReadIdle; + } + + @Override + public int getMaxWriteIdle() + { + return _maxWriteIdle; + } } diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkTransport.java b/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkTransport.java index 9b6f0a0b1b..c8027e143e 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkTransport.java +++ b/java/common/src/main/java/org/apache/qpid/transport/network/io/IoNetworkTransport.java @@ -41,9 +41,8 @@ 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.IncomingNetworkTransport; -import org.apache.qpid.transport.network.NetworkConnection; -import org.apache.qpid.transport.network.OutgoingNetworkTransport; +import org.apache.qpid.transport.network.*; + import org.slf4j.LoggerFactory; public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNetworkTransport @@ -56,7 +55,9 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet private IoNetworkConnection _connection; private AcceptingThread _acceptor; - public NetworkConnection connect(ConnectionSettings settings, Receiver delegate, SSLContext sslContext) + public NetworkConnection connect(ConnectionSettings settings, + Receiver delegate, + TransportActivity transportActivity) { int sendBufferSize = settings.getWriteBufferSize(); int receiveBufferSize = settings.getReadBufferSize(); @@ -91,7 +92,9 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet try { - _connection = new IoNetworkConnection(_socket, delegate, sendBufferSize, receiveBufferSize, TIMEOUT); + IdleTimeoutTicker ticker = new IdleTimeoutTicker(transportActivity, TIMEOUT); + _connection = new IoNetworkConnection(_socket, delegate, sendBufferSize, receiveBufferSize, TIMEOUT, ticker); + ticker.setConnection(_connection); _connection.start(); } catch(Exception e) @@ -128,9 +131,10 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet return _connection; } - public void accept(NetworkTransportConfiguration config, ProtocolEngineFactory factory, SSLContext sslContext) + public void accept(NetworkTransportConfiguration config, + ProtocolEngineFactory factory, + SSLContext sslContext) { - try { _acceptor = new AcceptingThread(config, factory, sslContext); @@ -141,8 +145,6 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet { throw new TransportException("Unable to start server socket", e); } - - } private class AcceptingThread extends Thread @@ -152,15 +154,16 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet private ProtocolEngineFactory _factory; private SSLContext _sslContext; private ServerSocket _serverSocket; + private int _timeout; private AcceptingThread(NetworkTransportConfiguration config, ProtocolEngineFactory factory, - SSLContext sslContext) - throws IOException + SSLContext sslContext) throws IOException { _config = config; _factory = factory; _sslContext = sslContext; + _timeout = TIMEOUT; InetSocketAddress address = config.getAddress(); @@ -172,15 +175,19 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet { SSLServerSocketFactory socketFactory = _sslContext.getServerSocketFactory(); _serverSocket = socketFactory.createServerSocket(); - ((SSLServerSocket)_serverSocket).setNeedClientAuth(config.needClientAuth()); - ((SSLServerSocket)_serverSocket).setWantClientAuth(config.wantClientAuth()); + if(config.needClientAuth()) + { + ((SSLServerSocket)_serverSocket).setNeedClientAuth(true); + } + else if(config.wantClientAuth()) + { + ((SSLServerSocket)_serverSocket).setWantClientAuth(true); + } } _serverSocket.setReuseAddress(true); _serverSocket.bind(address); - - } @@ -217,6 +224,7 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet { socket = _serverSocket.accept(); socket.setTcpNoDelay(_config.getTcpNoDelay()); + socket.setSoTimeout(_timeout); final Integer sendBufferSize = _config.getSendBufferSize(); final Integer receiveBufferSize = _config.getReceiveBufferSize(); @@ -224,10 +232,12 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet socket.setSendBufferSize(sendBufferSize); socket.setReceiveBufferSize(receiveBufferSize); - ProtocolEngine engine = _factory.newProtocolEngine(); - NetworkConnection connection = new IoNetworkConnection(socket, engine, sendBufferSize, receiveBufferSize, TIMEOUT); + final IdleTimeoutTicker ticker = new IdleTimeoutTicker(engine, TIMEOUT); + NetworkConnection connection = new IoNetworkConnection(socket, engine, sendBufferSize, receiveBufferSize, _timeout, + ticker); + ticker.setConnection(connection); if(_sslContext != null) { @@ -248,14 +258,14 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet } catch(RuntimeException e) { - LOGGER.error("Error in Acceptor thread on port " + _config.getPort(), e); + LOGGER.error("Error in Acceptor thread on address " + _config.getAddress(), e); closeSocketIfNecessary(socket); } catch(IOException e) { if(!_closed) { - LOGGER.error("Error in Acceptor thread on port " + _config.getPort(), e); + LOGGER.error("Error in Acceptor thread on address " + _config.getAddress(), e); closeSocketIfNecessary(socket); try { @@ -275,7 +285,7 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet { if(LOGGER.isDebugEnabled()) { - LOGGER.debug("Acceptor exiting, no new connections will be accepted on port " + _config.getPort()); + LOGGER.debug("Acceptor exiting, no new connections will be accepted on address " + _config.getAddress()); } } } @@ -294,6 +304,7 @@ public class IoNetworkTransport implements OutgoingNetworkTransport, IncomingNet } } } + } } diff --git a/java/common/src/main/java/org/apache/qpid/transport/network/io/IoReceiver.java b/java/common/src/main/java/org/apache/qpid/transport/network/io/IoReceiver.java index 7e63071c16..06a43e21c6 100644 --- a/java/common/src/main/java/org/apache/qpid/transport/network/io/IoReceiver.java +++ b/java/common/src/main/java/org/apache/qpid/transport/network/io/IoReceiver.java @@ -24,6 +24,7 @@ import org.apache.qpid.common.Closeable; import org.apache.qpid.thread.Threading; import org.apache.qpid.transport.Receiver; import org.apache.qpid.transport.TransportException; +import org.apache.qpid.transport.network.Ticker; import org.apache.qpid.transport.util.Logger; import javax.net.ssl.SSLSocket; @@ -31,6 +32,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.Socket; import java.net.SocketException; +import java.net.SocketTimeoutException; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicBoolean; @@ -51,6 +53,8 @@ final class IoReceiver implements Runnable, Closeable private final AtomicBoolean closed = new AtomicBoolean(false); private final Thread receiverThread; private static final boolean shutdownBroken; + + private Ticker _ticker; static { String osName = System.getProperty("os.name"); @@ -136,7 +140,7 @@ final class IoReceiver implements Runnable, Closeable { final int threshold = bufferSize / 2; - // I set the read buffer size simillar to SO_RCVBUF + // I set the read buffer size similar to SO_RCVBUF // Haven't tested with a lower value to see if it's better or worse byte[] buffer = new byte[bufferSize]; try @@ -144,27 +148,71 @@ final class IoReceiver implements Runnable, Closeable InputStream in = socket.getInputStream(); int read = 0; int offset = 0; - while ((read = in.read(buffer, offset, bufferSize-offset)) != -1) + long currentTime; + while(read != -1) { - if (read > 0) + try + { + while ((read = in.read(buffer, offset, bufferSize-offset)) != -1) + { + if (read > 0) + { + ByteBuffer b = ByteBuffer.wrap(buffer,offset,read); + receiver.received(b); + offset+=read; + if (offset > threshold) + { + offset = 0; + buffer = new byte[bufferSize]; + } + } + currentTime = System.currentTimeMillis(); + + if(_ticker != null) + { + int tick = _ticker.getTimeToNextTick(currentTime); + if(tick <= 0) + { + tick = _ticker.tick(currentTime); + } + try + { + if(!socket.isClosed()) + { + socket.setSoTimeout(tick <= 0 ? 1 : tick); + } + } + catch(SocketException e) + { + // ignore - closed socket + } + } + } + } + catch (SocketTimeoutException e) { - ByteBuffer b = ByteBuffer.wrap(buffer,offset,read); - receiver.received(b); - offset+=read; - if (offset > threshold) + currentTime = System.currentTimeMillis(); + if(_ticker != null) { - offset = 0; - buffer = new byte[bufferSize]; + final int tick = _ticker.tick(currentTime); + if(!socket.isClosed()) + { + try + { + socket.setSoTimeout(tick <= 0 ? 1 : tick ); + } + catch(SocketException ex) + { + // ignore - closed socket + } + } } } } } catch (Throwable t) { - if (!(shutdownBroken && - t instanceof SocketException && - t.getMessage().equalsIgnoreCase("socket closed") && - closed.get())) + if (shouldReport(t)) { receiver.exception(t); } @@ -183,4 +231,30 @@ final class IoReceiver implements Runnable, Closeable } } + private boolean shouldReport(Throwable t) + { + boolean brokenClose = closed.get() && + shutdownBroken && + t instanceof SocketException && + "socket closed".equalsIgnoreCase(t.getMessage()); + + boolean sslSocketClosed = closed.get() && + socket instanceof SSLSocket && + t instanceof SocketException && + "Socket is closed".equalsIgnoreCase(t.getMessage()); + + return !brokenClose && !sslSocketClosed; + } + + public Ticker getTicker() + { + return _ticker; + } + + public void setTicker(Ticker ticker) + { + _ticker = ticker; + } + + } diff --git a/java/common/src/main/java/org/apache/qpid/url/BindingURL.java b/java/common/src/main/java/org/apache/qpid/url/BindingURL.java index 0e6c865a16..61585443b1 100644 --- a/java/common/src/main/java/org/apache/qpid/url/BindingURL.java +++ b/java/common/src/main/java/org/apache/qpid/url/BindingURL.java @@ -36,6 +36,9 @@ public interface BindingURL public static final String OPTION_SUBSCRIPTION = "subscription"; public static final String OPTION_ROUTING_KEY = "routingkey"; public static final String OPTION_BINDING_KEY = "bindingkey"; + public static final String OPTION_EXCHANGE_AUTODELETE = "exchangeautodelete"; + public static final String OPTION_EXCHANGE_DURABLE = "exchangedurable"; + public static final String OPTION_EXCHANGE_INTERNAL = "exchangeinternal"; /** * This option is only applicable for 0-8/0-9/0-9-1 protocols connection diff --git a/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/java/common/src/main/java/org/apache/qpid/util/FileUtils.java index 2d3e321812..7362099070 100644 --- a/java/common/src/main/java/org/apache/qpid/util/FileUtils.java +++ b/java/common/src/main/java/org/apache/qpid/util/FileUtils.java @@ -220,6 +220,19 @@ public class FileUtils public static void copyCheckedEx(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); + copy(in, dst); + } + + /** + * Copies the specified InputStream to the specified destination file. If the destination file does not exist, + * it is created. + * + * @param in The InputStream + * @param dst The destination file name. + * @throws IOException + */ + public static void copy(InputStream in, File dst) throws IOException + { try { if (!dst.exists()) diff --git a/java/common/src/main/java/org/apache/qpid/util/NetMatcher.java b/java/common/src/main/java/org/apache/qpid/util/NetMatcher.java deleted file mode 100644 index 971dd3fe2a..0000000000 --- a/java/common/src/main/java/org/apache/qpid/util/NetMatcher.java +++ /dev/null @@ -1,300 +0,0 @@ -/*********************************************************************** - * Copyright (c) 2000-2006 The Apache Software Foundation. * - * All rights reserved. * - * ------------------------------------------------------------------- * - * Licensed 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.util; - -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - -public class NetMatcher -{ - private ArrayList networks; - - public void initInetNetworks(final Collection nets) - { - networks = new ArrayList(); - for (Iterator iter = nets.iterator(); iter.hasNext(); ) - { - try - { - InetNetwork net = InetNetwork.getFromString((String) iter.next()); - if (!networks.contains(net)) - { - networks.add(net); - } - } - catch (java.net.UnknownHostException uhe) - { - log("Cannot resolve address: " + uhe.getMessage()); - } - } - networks.trimToSize(); - } - - public void initInetNetworks(final String[] nets) - { - networks = new ArrayList(); - for (int i = 0; i < nets.length; i++) - { - try - { - InetNetwork net = InetNetwork.getFromString(nets[i]); - if (!networks.contains(net)) - { - networks.add(net); - } - } - catch (java.net.UnknownHostException uhe) - { - log("Cannot resolve address: " + uhe.getMessage()); - } - } - networks.trimToSize(); - } - - public boolean matchInetNetwork(final String hostIP) - { - InetAddress ip = null; - - try - { - ip = InetAddress.getByName(hostIP); - } - catch (java.net.UnknownHostException uhe) - { - log("Cannot resolve address for " + hostIP + ": " + uhe.getMessage()); - } - - boolean sameNet = false; - - if (ip != null) - { - for (Iterator iter = networks.iterator(); (!sameNet) && iter.hasNext(); ) - { - InetNetwork network = (InetNetwork) iter.next(); - sameNet = network.contains(ip); - } - } - return sameNet; - } - - public boolean matchInetNetwork(final InetAddress ip) - { - boolean sameNet = false; - - for (Iterator iter = networks.iterator(); (!sameNet) && iter.hasNext(); ) - { - InetNetwork network = (InetNetwork) iter.next(); - sameNet = network.contains(ip); - } - return sameNet; - } - - public NetMatcher() - { - } - - public NetMatcher(final String[] nets) - { - initInetNetworks(nets); - } - - public NetMatcher(final Collection nets) - { - initInetNetworks(nets); - } - - public String toString() { - return networks.toString(); - } - - protected void log(String s) { } -} - -class InetNetwork -{ - /* - * Implements network masking, and is compatible with RFC 1518 and - * RFC 1519, which describe CIDR: Classless Inter-Domain Routing. - */ - - private InetAddress network; - private InetAddress netmask; - - public InetNetwork(InetAddress ip, InetAddress netmask) - { - network = maskIP(ip, netmask); - this.netmask = netmask; - } - - public boolean contains(final String name) throws java.net.UnknownHostException - { - return network.equals(maskIP(InetAddress.getByName(name), netmask)); - } - - public boolean contains(final InetAddress ip) - { - return network.equals(maskIP(ip, netmask)); - } - - public String toString() - { - return network.getHostAddress() + "/" + netmask.getHostAddress(); - } - - public int hashCode() - { - return maskIP(network, netmask).hashCode(); - } - - public boolean equals(Object obj) - { - return (obj != null) && (obj instanceof InetNetwork) && - ((((InetNetwork)obj).network.equals(network)) && (((InetNetwork)obj).netmask.equals(netmask))); - } - - public static InetNetwork getFromString(String netspec) throws java.net.UnknownHostException - { - if (netspec.endsWith("*")) - { - netspec = normalizeFromAsterisk(netspec); - } - else - { - int iSlash = netspec.indexOf('/'); - if (iSlash == -1) - { - netspec += "/255.255.255.255"; - } - else if (netspec.indexOf('.', iSlash) == -1) - { - netspec = normalizeFromCIDR(netspec); - } - } - - return new InetNetwork(InetAddress.getByName(netspec.substring(0, netspec.indexOf('/'))), - InetAddress.getByName(netspec.substring(netspec.indexOf('/') + 1))); - } - - public static InetAddress maskIP(final byte[] ip, final byte[] mask) - { - try - { - return getByAddress(new byte[] - { - (byte) (mask[0] & ip[0]), - (byte) (mask[1] & ip[1]), - (byte) (mask[2] & ip[2]), - (byte) (mask[3] & ip[3]) - }); - } - catch(Exception _) {} - { - return null; - } - } - - public static InetAddress maskIP(final InetAddress ip, final InetAddress mask) - { - return maskIP(ip.getAddress(), mask.getAddress()); - } - - /* - * This converts from an uncommon "wildcard" CIDR format - * to "address + mask" format: - * - * * => 000.000.000.0/000.000.000.0 - * xxx.* => xxx.000.000.0/255.000.000.0 - * xxx.xxx.* => xxx.xxx.000.0/255.255.000.0 - * xxx.xxx.xxx.* => xxx.xxx.xxx.0/255.255.255.0 - */ - static private String normalizeFromAsterisk(final String netspec) - { - String[] masks = { "0.0.0.0/0.0.0.0", "0.0.0/255.0.0.0", "0.0/255.255.0.0", "0/255.255.255.0" }; - char[] srcb = netspec.toCharArray(); - int octets = 0; - for (int i = 1; i < netspec.length(); i++) - { - if (srcb[i] == '.') - { - octets++; - } - } - return (octets == 0) ? masks[0] : netspec.substring(0, netspec.length() -1 ).concat(masks[octets]); - } - - /* - * RFC 1518, 1519 - Classless Inter-Domain Routing (CIDR) - * This converts from "prefix + prefix-length" format to - * "address + mask" format, e.g. from xxx.xxx.xxx.xxx/yy - * to xxx.xxx.xxx.xxx/yyy.yyy.yyy.yyy. - */ - static private String normalizeFromCIDR(final String netspec) - { - final int bits = 32 - Integer.parseInt(netspec.substring(netspec.indexOf('/')+1)); - final int mask = (bits == 32) ? 0 : 0xFFFFFFFF - ((1 << bits)-1); - - return netspec.substring(0, netspec.indexOf('/') + 1) + - Integer.toString(mask >> 24 & 0xFF, 10) + "." + - Integer.toString(mask >> 16 & 0xFF, 10) + "." + - Integer.toString(mask >> 8 & 0xFF, 10) + "." + - Integer.toString(mask >> 0 & 0xFF, 10); - } - - private static java.lang.reflect.Method getByAddress = null; - - static { - try { - Class inetAddressClass = Class.forName("java.net.InetAddress"); - Class[] parameterTypes = { byte[].class }; - getByAddress = inetAddressClass.getMethod("getByAddress", parameterTypes); - } catch (Exception e) { - getByAddress = null; - } - } - - private static InetAddress getByAddress(byte[] ip) throws java.net.UnknownHostException - { - InetAddress addr = null; - if (getByAddress != null) - { - try - { - addr = (InetAddress) getByAddress.invoke(null, new Object[] { ip }); - } - catch (IllegalAccessException e) - { - } - catch (java.lang.reflect.InvocationTargetException e) - { - } - } - - if (addr == null) { - addr = InetAddress.getByName - ( - Integer.toString(ip[0] & 0xFF, 10) + "." + - Integer.toString(ip[1] & 0xFF, 10) + "." + - Integer.toString(ip[2] & 0xFF, 10) + "." + - Integer.toString(ip[3] & 0xFF, 10) - ); - } - return addr; - } -} diff --git a/java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java new file mode 100644 index 0000000000..1ecf450551 --- /dev/null +++ b/java/common/src/test/java/org/apache/qpid/framing/FieldTableTest.java @@ -0,0 +1,938 @@ +/* + * + * 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.framing; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.qpid.AMQPInvalidClassException; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class FieldTableTest extends TestCase +{ + /** + * Test that setting a similar named value replaces any previous value set on that name + */ + public void testReplacement() + { + FieldTable table1 = new FieldTable(); + // Set a boolean value + table1.setBoolean("value", true); + // Check length of table is correct ( + + ) + int size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedBooleanLength(); + Assert.assertEquals(size, table1.getEncodedSize()); + + // reset value to an integer + table1.setInteger("value", Integer.MAX_VALUE); + + // Check the length has changed accordingly ( + + ) + size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedIntegerLength(); + Assert.assertEquals(size, table1.getEncodedSize()); + + // Check boolean value is null + Assert.assertEquals(null, table1.getBoolean("value")); + // ... and integer value is good + Assert.assertEquals((Integer) Integer.MAX_VALUE, table1.getInteger("value")); + } + + /** + * Set a boolean and check that we can only get it back as a boolean and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testBoolean() + { + FieldTable table1 = new FieldTable(); + table1.setBoolean("value", true); + Assert.assertTrue(table1.propertyExists("value")); + + // Test Getting right value back + Assert.assertEquals((Boolean) true, table1.getBoolean("value")); + + // Check we don't get anything back for other gets + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // except value as a string + Assert.assertEquals("true", table1.getString("value")); + + table1.remove("value"); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getBoolean("Rubbish")); + } + + /** + * Set a byte and check that we can only get it back as a byte and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testByte() + { + FieldTable table1 = new FieldTable(); + table1.setByte("value", Byte.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + // Tests lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(Byte.MAX_VALUE, (byte) table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("" + Byte.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getByte("Rubbish")); + } + + /** + * Set a short and check that we can only get it back as a short and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testShort() + { + FieldTable table1 = new FieldTable(); + table1.setShort("value", Short.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + // Tests lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(Short.MAX_VALUE, (short) table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("" + Short.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getShort("Rubbish")); + } + + /** + * Set a char and check that we can only get it back as a char + * Check that attempting to lookup a non existent value returns null + */ + public void testChar() + { + FieldTable table1 = new FieldTable(); + table1.setChar("value", 'c'); + Assert.assertTrue(table1.propertyExists("value")); + + // Tests lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals('c', (char) table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("c", table1.getString("value")); + + table1.remove("value"); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getCharacter("Rubbish")); + } + + /** + * Set a double and check that we can only get it back as a double + * Check that attempting to lookup a non existent value returns null + */ + public void testDouble() + { + FieldTable table1 = new FieldTable(); + table1.setDouble("value", Double.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + // Tests lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(Double.MAX_VALUE, (double) table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("" + Double.MAX_VALUE, table1.getString("value")); + table1.remove("value"); + // but after a removeKey it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getDouble("Rubbish")); + } + + /** + * Set a float and check that we can only get it back as a float + * Check that attempting to lookup a non existent value returns null + */ + public void testFloat() + { + FieldTable table1 = new FieldTable(); + table1.setFloat("value", Float.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + // Tests lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(Float.MAX_VALUE, (float) table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("" + Float.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + // but after a removeKey it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getFloat("Rubbish")); + } + + /** + * Set an int and check that we can only get it back as an int + * Check that attempting to lookup a non existent value returns null + */ + public void testInt() + { + FieldTable table1 = new FieldTable(); + table1.setInteger("value", Integer.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + // Tets lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(Integer.MAX_VALUE, (int) table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("" + Integer.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + // but after a removeKey it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getInteger("Rubbish")); + } + + /** + * Set a long and check that we can only get it back as a long + * Check that attempting to lookup a non existent value returns null + */ + public void testLong() + { + FieldTable table1 = new FieldTable(); + table1.setLong("value", Long.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + // Tets lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(Long.MAX_VALUE, (long) table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + // ... and a the string value of it. + Assert.assertEquals("" + Long.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + // but after a removeKey it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getLong("Rubbish")); + } + + /** + * Set a double and check that we can only get it back as a double + * Check that attempting to lookup a non existent value returns null + */ + public void testBytes() + { + byte[] bytes = { 99, 98, 97, 96, 95 }; + + FieldTable table1 = new FieldTable(); + table1.setBytes("value", bytes); + Assert.assertTrue(table1.propertyExists("value")); + + // Tets lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + assertBytesEqual(bytes, table1.getBytes("value")); + + // ... and a the string value of it is null + Assert.assertEquals(null, table1.getString("value")); + + table1.remove("value"); + // but after a removeKey it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero length for encoding + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getBytes("Rubbish")); + } + + /** + * Calls all methods that can be used to check the table is empty + * - getEncodedSize + * - isEmpty + * - length + * + * @param table to check is empty + */ + private void checkEmpty(FieldTable table) + { + Assert.assertEquals(0, table.getEncodedSize()); + Assert.assertTrue(table.isEmpty()); + Assert.assertEquals(0, table.size()); + + Assert.assertEquals(0, table.keySet().size()); + } + + /** + * Set a String and check that we can only get it back as a String + * Check that attempting to lookup a non existent value returns null + */ + public void testString() + { + FieldTable table1 = new FieldTable(); + table1.setString("value", "Hello"); + Assert.assertTrue(table1.propertyExists("value")); + + // Test lookups we shouldn't get anything back for other gets + // we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + Assert.assertEquals("Hello", table1.getString("value")); + + // Try setting a null value and read it back + table1.setString("value", null); + + Assert.assertEquals(null, table1.getString("value")); + + // but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + // but after a removeKey it doesn't + Assert.assertFalse(table1.containsKey("value")); + + checkEmpty(table1); + + // Looking up an invalid value returns null + Assert.assertEquals(null, table1.getString("Rubbish")); + + // Additional Test that haven't been covered for string + table1.setObject("value", "Hello"); + // Check that it was set correctly + Assert.assertEquals("Hello", table1.getString("value")); + } + + /** Check that a nested field table parameter correctly encodes and decodes to a byte buffer. */ + public void testNestedFieldTable() throws IOException + { + byte[] testBytes = new byte[] { 0, 1, 2, 3, 4, 5 }; + + FieldTable outerTable = new FieldTable(); + FieldTable innerTable = new FieldTable(); + + // Put some stuff in the inner table. + innerTable.setBoolean("bool", true); + innerTable.setByte("byte", Byte.MAX_VALUE); + innerTable.setBytes("bytes", testBytes); + innerTable.setChar("char", 'c'); + innerTable.setDouble("double", Double.MAX_VALUE); + innerTable.setFloat("float", Float.MAX_VALUE); + innerTable.setInteger("int", Integer.MAX_VALUE); + innerTable.setLong("long", Long.MAX_VALUE); + innerTable.setShort("short", Short.MAX_VALUE); + innerTable.setString("string", "hello"); + innerTable.setString("null-string", null); + + // Put the inner table in the outer one. + outerTable.setFieldTable("innerTable", innerTable); + + // Write the outer table into the buffer. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + outerTable.writeToBuffer(new DataOutputStream(baos)); + + byte[] data = baos.toByteArray(); + + // Extract the table back from the buffer again. + try + { + FieldTable extractedOuterTable = EncodingUtils.readFieldTable(new DataInputStream(new ByteArrayInputStream(data))); + + FieldTable extractedTable = extractedOuterTable.getFieldTable("innerTable"); + + Assert.assertEquals((Boolean) true, extractedTable.getBoolean("bool")); + Assert.assertEquals((Byte) Byte.MAX_VALUE, extractedTable.getByte("byte")); + assertBytesEqual(testBytes, extractedTable.getBytes("bytes")); + Assert.assertEquals((Character) 'c', extractedTable.getCharacter("char")); + Assert.assertEquals(Double.MAX_VALUE, extractedTable.getDouble("double")); + Assert.assertEquals(Float.MAX_VALUE, extractedTable.getFloat("float")); + Assert.assertEquals((Integer) Integer.MAX_VALUE, extractedTable.getInteger("int")); + Assert.assertEquals((Long) Long.MAX_VALUE, extractedTable.getLong("long")); + Assert.assertEquals((Short) Short.MAX_VALUE, extractedTable.getShort("short")); + Assert.assertEquals("hello", extractedTable.getString("string")); + Assert.assertEquals(null, extractedTable.getString("null-string")); + } + catch (AMQFrameDecodingException e) + { + fail("Failed to decode field table with nested inner table."); + } + } + + public void testValues() + { + FieldTable table = new FieldTable(); + table.setBoolean("bool", true); + table.setByte("byte", Byte.MAX_VALUE); + byte[] bytes = { 99, 98, 97, 96, 95 }; + table.setBytes("bytes", bytes); + table.setChar("char", 'c'); + table.setDouble("double", Double.MAX_VALUE); + table.setFloat("float", Float.MAX_VALUE); + table.setInteger("int", Integer.MAX_VALUE); + table.setLong("long", Long.MAX_VALUE); + table.setShort("short", Short.MAX_VALUE); + table.setString("string", "Hello"); + table.setString("null-string", null); + + table.setObject("object-bool", true); + table.setObject("object-byte", Byte.MAX_VALUE); + table.setObject("object-bytes", bytes); + table.setObject("object-char", 'c'); + table.setObject("object-double", Double.MAX_VALUE); + table.setObject("object-float", Float.MAX_VALUE); + table.setObject("object-int", Integer.MAX_VALUE); + table.setObject("object-long", Long.MAX_VALUE); + table.setObject("object-short", Short.MAX_VALUE); + table.setObject("object-string", "Hello"); + + try + { + table.setObject("Null-object", null); + fail("null values are not allowed"); + } + catch (AMQPInvalidClassException aice) + { + assertEquals("Null values are not allowed to be set", + AMQPInvalidClassException.INVALID_OBJECT_MSG + "null", aice.getMessage()); + } + + try + { + table.setObject("Unsupported-object", new Exception()); + fail("Non primitive values are not allowed"); + } + catch (AMQPInvalidClassException aice) + { + assertEquals("Non primitive values are not allowed to be set", + AMQPInvalidClassException.INVALID_OBJECT_MSG + Exception.class, aice.getMessage()); + } + + Assert.assertEquals((Boolean) true, table.getBoolean("bool")); + Assert.assertEquals((Byte) Byte.MAX_VALUE, table.getByte("byte")); + assertBytesEqual(bytes, table.getBytes("bytes")); + Assert.assertEquals((Character) 'c', table.getCharacter("char")); + Assert.assertEquals(Double.MAX_VALUE, table.getDouble("double")); + Assert.assertEquals(Float.MAX_VALUE, table.getFloat("float")); + Assert.assertEquals((Integer) Integer.MAX_VALUE, table.getInteger("int")); + Assert.assertEquals((Long) Long.MAX_VALUE, table.getLong("long")); + Assert.assertEquals((Short) Short.MAX_VALUE, table.getShort("short")); + Assert.assertEquals("Hello", table.getString("string")); + Assert.assertEquals(null, table.getString("null-string")); + + Assert.assertEquals(true, table.getObject("object-bool")); + Assert.assertEquals(Byte.MAX_VALUE, table.getObject("object-byte")); + assertBytesEqual(bytes, (byte[]) table.getObject("object-bytes")); + Assert.assertEquals('c', table.getObject("object-char")); + Assert.assertEquals(Double.MAX_VALUE, table.getObject("object-double")); + Assert.assertEquals(Float.MAX_VALUE, table.getObject("object-float")); + Assert.assertEquals(Integer.MAX_VALUE, table.getObject("object-int")); + Assert.assertEquals(Long.MAX_VALUE, table.getObject("object-long")); + Assert.assertEquals(Short.MAX_VALUE, table.getObject("object-short")); + Assert.assertEquals("Hello", table.getObject("object-string")); + } + + public void testWriteBuffer() throws IOException + { + byte[] bytes = { 99, 98, 97, 96, 95 }; + + FieldTable table = new FieldTable(); + table.setBoolean("bool", true); + table.setByte("byte", Byte.MAX_VALUE); + + table.setBytes("bytes", bytes); + table.setChar("char", 'c'); + table.setInteger("int", Integer.MAX_VALUE); + table.setLong("long", Long.MAX_VALUE); + table.setDouble("double", Double.MAX_VALUE); + table.setFloat("float", Float.MAX_VALUE); + table.setShort("short", Short.MAX_VALUE); + table.setString("string", "hello"); + table.setString("null-string", null); + + + ByteArrayOutputStream baos = new ByteArrayOutputStream((int) table.getEncodedSize() + 4); + table.writeToBuffer(new DataOutputStream(baos)); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + DataInputStream dis = new DataInputStream(bais); + + + long length = dis.readInt() & 0xFFFFFFFFL; + + FieldTable table2 = new FieldTable(dis, length); + + Assert.assertEquals((Boolean) true, table2.getBoolean("bool")); + Assert.assertEquals((Byte) Byte.MAX_VALUE, table2.getByte("byte")); + assertBytesEqual(bytes, table2.getBytes("bytes")); + Assert.assertEquals((Character) 'c', table2.getCharacter("char")); + Assert.assertEquals(Double.MAX_VALUE, table2.getDouble("double")); + Assert.assertEquals(Float.MAX_VALUE, table2.getFloat("float")); + Assert.assertEquals((Integer) Integer.MAX_VALUE, table2.getInteger("int")); + Assert.assertEquals((Long) Long.MAX_VALUE, table2.getLong("long")); + Assert.assertEquals((Short) Short.MAX_VALUE, table2.getShort("short")); + Assert.assertEquals("hello", table2.getString("string")); + Assert.assertEquals(null, table2.getString("null-string")); + } + + public void testEncodingSize() + { + FieldTable result = new FieldTable(); + int size = 0; + + result.setBoolean("boolean", true); + size += 1 + EncodingUtils.encodedShortStringLength("boolean") + EncodingUtils.encodedBooleanLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setByte("byte", (byte) Byte.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("byte") + EncodingUtils.encodedByteLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + byte[] _bytes = { 99, 98, 97, 96, 95 }; + + result.setBytes("bytes", _bytes); + size += 1 + EncodingUtils.encodedShortStringLength("bytes") + 4 + _bytes.length; + Assert.assertEquals(size, result.getEncodedSize()); + + result.setChar("char", (char) 'c'); + size += 1 + EncodingUtils.encodedShortStringLength("char") + EncodingUtils.encodedCharLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setDouble("double", (double) Double.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("double") + EncodingUtils.encodedDoubleLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setFloat("float", (float) Float.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("float") + EncodingUtils.encodedFloatLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setInteger("int", (int) Integer.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("int") + EncodingUtils.encodedIntegerLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setLong("long", (long) Long.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("long") + EncodingUtils.encodedLongLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setShort("short", (short) Short.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("short") + EncodingUtils.encodedShortLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setString("result", "Hello"); + size += 1 + EncodingUtils.encodedShortStringLength("result") + EncodingUtils.encodedLongStringLength("Hello"); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-bool", true); + size += 1 + EncodingUtils.encodedShortStringLength("object-bool") + EncodingUtils.encodedBooleanLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-byte", Byte.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("object-byte") + EncodingUtils.encodedByteLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-bytes", _bytes); + size += 1 + EncodingUtils.encodedShortStringLength("object-bytes") + 4 + _bytes.length; + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-char", 'c'); + size += 1 + EncodingUtils.encodedShortStringLength("object-char") + EncodingUtils.encodedCharLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-double", Double.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("object-double") + EncodingUtils.encodedDoubleLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-float", Float.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("object-float") + EncodingUtils.encodedFloatLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-int", Integer.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("object-int") + EncodingUtils.encodedIntegerLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-long", Long.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("object-long") + EncodingUtils.encodedLongLength(); + Assert.assertEquals(size, result.getEncodedSize()); + + result.setObject("object-short", Short.MAX_VALUE); + size += 1 + EncodingUtils.encodedShortStringLength("object-short") + EncodingUtils.encodedShortLength(); + Assert.assertEquals(size, result.getEncodedSize()); + } + + /** + * Additional test for setObject + */ + public void testSetObject() + { + FieldTable table = new FieldTable(); + + // Try setting a non primative object + + try + { + table.setObject("value", this); + fail("Only primative values allowed in setObject"); + } + catch (AMQPInvalidClassException iae) + { + // normal path + } + // so length should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + + /** + * Additional test checkPropertyName doesn't accept Null + */ + public void testCheckPropertyNameasNull() + { + FieldTable table = new FieldTable(); + + try + { + table.setObject((String) null, "String"); + fail("Null property name is not allowed"); + } + catch (IllegalArgumentException iae) + { + // normal path + } + // so length should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + + /** + * Additional test checkPropertyName doesn't accept an empty String + */ + public void testCheckPropertyNameasEmptyString() + { + FieldTable table = new FieldTable(); + + try + { + table.setObject("", "String"); + fail("empty property name is not allowed"); + } + catch (IllegalArgumentException iae) + { + // normal path + } + // so length should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + + /** + * Additional test checkPropertyName doesn't accept an empty String + */ + public void testCheckPropertyNamehasMaxLength() + { + FieldTable table = new FieldTable(true); + + StringBuffer longPropertyName = new StringBuffer(129); + + for (int i = 0; i < 129; i++) + { + longPropertyName.append("x"); + } + + try + { + table.setObject(longPropertyName.toString(), "String"); + fail("property name must be < 128 characters"); + } + catch (IllegalArgumentException iae) + { + // normal path + } + // so length should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + + /** + * Additional test checkPropertyName starts with a letter + */ + public void testCheckPropertyNameStartCharacterIsLetter() + { + FieldTable table = new FieldTable(true); + + // Try a name that starts with a number + try + { + table.setObject("1", "String"); + fail("property name must start with a letter"); + } + catch (IllegalArgumentException iae) + { + // normal path + } + // so length should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + + /** + * Additional test checkPropertyName starts with a hash or a dollar + */ + public void testCheckPropertyNameStartCharacterIsHashorDollar() + { + FieldTable table = new FieldTable(true); + + // Try a name that starts with a number + try + { + table.setObject("#", "String"); + table.setObject("$", "String"); + } + catch (IllegalArgumentException iae) + { + fail("property name are allowed to start with # and $s"); + } + } + + /** + * Additional test to test the contents of the table + */ + public void testContents() + { + FieldTable table = new FieldTable(); + + table.setObject("StringProperty", "String"); + + Assert.assertEquals("String", table.getString("StringProperty")); + + // Test Clear + + table.clear(); + + checkEmpty(table); + } + + /** + * Test the contents of the sets + */ + public void testSets() + { + + FieldTable table = new FieldTable(); + + table.setObject("n1", "1"); + table.setObject("n2", "2"); + table.setObject("n3", "3"); + + Assert.assertEquals("1", table.getObject("n1")); + Assert.assertEquals("2", table.getObject("n2")); + Assert.assertEquals("3", table.getObject("n3")); + } + + public void testAddAll() + { + final FieldTable table1 = new FieldTable(); + table1.setInteger("int1", 1); + table1.setInteger("int2", 2); + assertEquals("Unexpected number of entries in table1", 2, table1.size()); + + final FieldTable table2 = new FieldTable(); + table2.setInteger("int3", 3); + table2.setInteger("int4", 4); + assertEquals("Unexpected number of entries in table2", 2, table2.size()); + + table1.addAll(table2); + assertEquals("Unexpected number of entries in table1 after addAll", 4, table1.size()); + assertEquals(Integer.valueOf(3), table1.getInteger("int3")); + } + + public void testAddAllWithEmptyFieldTable() + { + final FieldTable table1 = new FieldTable(); + table1.setInteger("int1", 1); + table1.setInteger("int2", 2); + assertEquals("Unexpected number of entries in table1", 2, table1.size()); + + final FieldTable emptyFieldTable = new FieldTable(); + + table1.addAll(emptyFieldTable); + assertEquals("Unexpected number of entries in table1 after addAll", 2, table1.size()); + } + + /** + * 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 + { + AMQShortString myBooleanTestProperty = new AMQShortString("myBooleanTestProperty"); + + //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 assertBytesEqual(byte[] expected, byte[] actual) + { + Assert.assertEquals(expected.length, actual.length); + + for (int index = 0; index < expected.length; index++) + { + Assert.assertEquals(expected[index], actual[index]); + } + } + + public static junit.framework.Test suite() + { + return new junit.framework.TestSuite(FieldTableTest.class); + } + +} diff --git a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java deleted file mode 100644 index 16f35613d8..0000000000 --- a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java +++ /dev/null @@ -1,980 +0,0 @@ -/* - * - * 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.framing; - -import junit.framework.Assert; -import junit.framework.TestCase; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.apache.qpid.AMQPInvalidClassException; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public class PropertyFieldTableTest 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 - */ - public void testReplacement() - { - FieldTable table1 = new FieldTable(); - // Set a boolean value - table1.setBoolean("value", true); - // Check length of table is correct ( + + ) - int size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedBooleanLength(); - Assert.assertEquals(size, table1.getEncodedSize()); - - // reset value to an integer - table1.setInteger("value", Integer.MAX_VALUE); - - // Check the length has changed accordingly ( + + ) - size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedIntegerLength(); - Assert.assertEquals(size, table1.getEncodedSize()); - - // Check boolean value is null - Assert.assertEquals(null, table1.getBoolean("value")); - // ... and integer value is good - Assert.assertEquals((Integer) Integer.MAX_VALUE, table1.getInteger("value")); - } - - /** - * Set a boolean and check that we can only get it back as a boolean and a string - * Check that attempting to lookup a non existent value returns null - */ - public void testBoolean() - { - FieldTable table1 = new FieldTable(); - table1.setBoolean("value", true); - Assert.assertTrue(table1.propertyExists("value")); - - // Test Getting right value back - Assert.assertEquals((Boolean) true, table1.getBoolean("value")); - - // Check we don't get anything back for other gets - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // except value as a string - Assert.assertEquals("true", table1.getString("value")); - - table1.remove("value"); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getBoolean("Rubbish")); - } - - /** - * Set a byte and check that we can only get it back as a byte and a string - * Check that attempting to lookup a non existent value returns null - */ - public void testByte() - { - FieldTable table1 = new FieldTable(); - table1.setByte("value", Byte.MAX_VALUE); - Assert.assertTrue(table1.propertyExists("value")); - - // Tests lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(Byte.MAX_VALUE, (byte) table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("" + Byte.MAX_VALUE, table1.getString("value")); - - table1.remove("value"); - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getByte("Rubbish")); - } - - /** - * Set a short and check that we can only get it back as a short and a string - * Check that attempting to lookup a non existent value returns null - */ - public void testShort() - { - FieldTable table1 = new FieldTable(); - table1.setShort("value", Short.MAX_VALUE); - Assert.assertTrue(table1.propertyExists("value")); - - // Tests lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(Short.MAX_VALUE, (short) table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("" + Short.MAX_VALUE, table1.getString("value")); - - table1.remove("value"); - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getShort("Rubbish")); - } - - /** - * Set a char and check that we can only get it back as a char - * Check that attempting to lookup a non existent value returns null - */ - public void testChar() - { - FieldTable table1 = new FieldTable(); - table1.setChar("value", 'c'); - Assert.assertTrue(table1.propertyExists("value")); - - // Tests lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals('c', (char) table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("c", table1.getString("value")); - - table1.remove("value"); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getCharacter("Rubbish")); - } - - /** - * Set a double and check that we can only get it back as a double - * Check that attempting to lookup a non existent value returns null - */ - public void testDouble() - { - FieldTable table1 = new FieldTable(); - table1.setDouble("value", Double.MAX_VALUE); - Assert.assertTrue(table1.propertyExists("value")); - - // Tests lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(Double.MAX_VALUE, (double) table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("" + Double.MAX_VALUE, table1.getString("value")); - table1.remove("value"); - // but after a removeKey it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getDouble("Rubbish")); - } - - /** - * Set a float and check that we can only get it back as a float - * Check that attempting to lookup a non existent value returns null - */ - public void testFloat() - { - FieldTable table1 = new FieldTable(); - table1.setFloat("value", Float.MAX_VALUE); - Assert.assertTrue(table1.propertyExists("value")); - - // Tests lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(Float.MAX_VALUE, (float) table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("" + Float.MAX_VALUE, table1.getString("value")); - - table1.remove("value"); - // but after a removeKey it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getFloat("Rubbish")); - } - - /** - * Set an int and check that we can only get it back as an int - * Check that attempting to lookup a non existent value returns null - */ - public void testInt() - { - FieldTable table1 = new FieldTable(); - table1.setInteger("value", Integer.MAX_VALUE); - Assert.assertTrue(table1.propertyExists("value")); - - // Tets lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(Integer.MAX_VALUE, (int) table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("" + Integer.MAX_VALUE, table1.getString("value")); - - table1.remove("value"); - // but after a removeKey it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getInteger("Rubbish")); - } - - /** - * Set a long and check that we can only get it back as a long - * Check that attempting to lookup a non existent value returns null - */ - public void testLong() - { - FieldTable table1 = new FieldTable(); - table1.setLong("value", Long.MAX_VALUE); - Assert.assertTrue(table1.propertyExists("value")); - - // Tets lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(Long.MAX_VALUE, (long) table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - - // ... and a the string value of it. - Assert.assertEquals("" + Long.MAX_VALUE, table1.getString("value")); - - table1.remove("value"); - // but after a removeKey it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getLong("Rubbish")); - } - - /** - * Set a double and check that we can only get it back as a double - * Check that attempting to lookup a non existent value returns null - */ - public void testBytes() - { - byte[] bytes = { 99, 98, 97, 96, 95 }; - - FieldTable table1 = new FieldTable(); - table1.setBytes("value", bytes); - Assert.assertTrue(table1.propertyExists("value")); - - // Tets lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - assertBytesEqual(bytes, table1.getBytes("value")); - - // ... and a the string value of it is null - Assert.assertEquals(null, table1.getString("value")); - - table1.remove("value"); - // but after a removeKey it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero length for encoding - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getBytes("Rubbish")); - } - - /** - * Calls all methods that can be used to check the table is empty - * - getEncodedSize - * - isEmpty - * - length - * - * @param table to check is empty - */ - private void checkEmpty(FieldTable table) - { - Assert.assertEquals(0, table.getEncodedSize()); - Assert.assertTrue(table.isEmpty()); - Assert.assertEquals(0, table.size()); - - Assert.assertEquals(0, table.keySet().size()); - } - - /** - * Set a String and check that we can only get it back as a String - * Check that attempting to lookup a non existent value returns null - */ - public void testString() - { - FieldTable table1 = new FieldTable(); - table1.setString("value", "Hello"); - Assert.assertTrue(table1.propertyExists("value")); - - // Test lookups we shouldn't get anything back for other gets - // we should get right value back for this type .... - Assert.assertEquals(null, table1.getBoolean("value")); - Assert.assertEquals(null, table1.getByte("value")); - Assert.assertEquals(null, table1.getShort("value")); - Assert.assertEquals(null, table1.getCharacter("value")); - Assert.assertEquals(null, table1.getDouble("value")); - Assert.assertEquals(null, table1.getFloat("value")); - Assert.assertEquals(null, table1.getInteger("value")); - Assert.assertEquals(null, table1.getLong("value")); - Assert.assertEquals(null, table1.getBytes("value")); - Assert.assertEquals("Hello", table1.getString("value")); - - // Try setting a null value and read it back - table1.setString("value", null); - - Assert.assertEquals(null, table1.getString("value")); - - // but still contains the value - Assert.assertTrue(table1.containsKey("value")); - - table1.remove("value"); - // but after a removeKey it doesn't - Assert.assertFalse(table1.containsKey("value")); - - checkEmpty(table1); - - // Looking up an invalid value returns null - Assert.assertEquals(null, table1.getString("Rubbish")); - - // Additional Test that haven't been covered for string - table1.setObject("value", "Hello"); - // Check that it was set correctly - Assert.assertEquals("Hello", table1.getString("value")); - } - - /** Check that a nested field table parameter correctly encodes and decodes to a byte buffer. */ - public void testNestedFieldTable() throws IOException - { - byte[] testBytes = new byte[] { 0, 1, 2, 3, 4, 5 }; - - FieldTable outerTable = new FieldTable(); - FieldTable innerTable = new FieldTable(); - - // Put some stuff in the inner table. - innerTable.setBoolean("bool", true); - innerTable.setByte("byte", Byte.MAX_VALUE); - innerTable.setBytes("bytes", testBytes); - innerTable.setChar("char", 'c'); - innerTable.setDouble("double", Double.MAX_VALUE); - innerTable.setFloat("float", Float.MAX_VALUE); - innerTable.setInteger("int", Integer.MAX_VALUE); - innerTable.setLong("long", Long.MAX_VALUE); - innerTable.setShort("short", Short.MAX_VALUE); - innerTable.setString("string", "hello"); - innerTable.setString("null-string", null); - - // Put the inner table in the outer one. - outerTable.setFieldTable("innerTable", innerTable); - - // Write the outer table into the buffer. - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - outerTable.writeToBuffer(new DataOutputStream(baos)); - - byte[] data = baos.toByteArray(); - - // Extract the table back from the buffer again. - try - { - FieldTable extractedOuterTable = EncodingUtils.readFieldTable(new DataInputStream(new ByteArrayInputStream(data))); - - FieldTable extractedTable = extractedOuterTable.getFieldTable("innerTable"); - - Assert.assertEquals((Boolean) true, extractedTable.getBoolean("bool")); - Assert.assertEquals((Byte) Byte.MAX_VALUE, extractedTable.getByte("byte")); - assertBytesEqual(testBytes, extractedTable.getBytes("bytes")); - Assert.assertEquals((Character) 'c', extractedTable.getCharacter("char")); - Assert.assertEquals(Double.MAX_VALUE, extractedTable.getDouble("double")); - Assert.assertEquals(Float.MAX_VALUE, extractedTable.getFloat("float")); - Assert.assertEquals((Integer) Integer.MAX_VALUE, extractedTable.getInteger("int")); - Assert.assertEquals((Long) Long.MAX_VALUE, extractedTable.getLong("long")); - Assert.assertEquals((Short) Short.MAX_VALUE, extractedTable.getShort("short")); - Assert.assertEquals("hello", extractedTable.getString("string")); - Assert.assertEquals(null, extractedTable.getString("null-string")); - } - catch (AMQFrameDecodingException e) - { - fail("Failed to decode field table with nested inner table."); - } - } - - public void testValues() - { - FieldTable table = new FieldTable(); - table.setBoolean("bool", true); - table.setByte("byte", Byte.MAX_VALUE); - byte[] bytes = { 99, 98, 97, 96, 95 }; - table.setBytes("bytes", bytes); - table.setChar("char", 'c'); - table.setDouble("double", Double.MAX_VALUE); - table.setFloat("float", Float.MAX_VALUE); - table.setInteger("int", Integer.MAX_VALUE); - table.setLong("long", Long.MAX_VALUE); - table.setShort("short", Short.MAX_VALUE); - table.setString("string", "Hello"); - table.setString("null-string", null); - - table.setObject("object-bool", true); - table.setObject("object-byte", Byte.MAX_VALUE); - table.setObject("object-bytes", bytes); - table.setObject("object-char", 'c'); - table.setObject("object-double", Double.MAX_VALUE); - table.setObject("object-float", Float.MAX_VALUE); - table.setObject("object-int", Integer.MAX_VALUE); - table.setObject("object-long", Long.MAX_VALUE); - table.setObject("object-short", Short.MAX_VALUE); - table.setObject("object-string", "Hello"); - - try - { - table.setObject("Null-object", null); - fail("null values are not allowed"); - } - catch (AMQPInvalidClassException aice) - { - assertEquals("Null values are not allowed to be set", - AMQPInvalidClassException.INVALID_OBJECT_MSG + "null", aice.getMessage()); - } - - try - { - table.setObject("Unsupported-object", new Exception()); - fail("Non primitive values are not allowed"); - } - catch (AMQPInvalidClassException aice) - { - assertEquals("Non primitive values are not allowed to be set", - AMQPInvalidClassException.INVALID_OBJECT_MSG + Exception.class, aice.getMessage()); - } - - Assert.assertEquals((Boolean) true, table.getBoolean("bool")); - Assert.assertEquals((Byte) Byte.MAX_VALUE, table.getByte("byte")); - assertBytesEqual(bytes, table.getBytes("bytes")); - Assert.assertEquals((Character) 'c', table.getCharacter("char")); - Assert.assertEquals(Double.MAX_VALUE, table.getDouble("double")); - Assert.assertEquals(Float.MAX_VALUE, table.getFloat("float")); - Assert.assertEquals((Integer) Integer.MAX_VALUE, table.getInteger("int")); - Assert.assertEquals((Long) Long.MAX_VALUE, table.getLong("long")); - Assert.assertEquals((Short) Short.MAX_VALUE, table.getShort("short")); - Assert.assertEquals("Hello", table.getString("string")); - Assert.assertEquals(null, table.getString("null-string")); - - Assert.assertEquals(true, table.getObject("object-bool")); - Assert.assertEquals(Byte.MAX_VALUE, table.getObject("object-byte")); - assertBytesEqual(bytes, (byte[]) table.getObject("object-bytes")); - Assert.assertEquals('c', table.getObject("object-char")); - Assert.assertEquals(Double.MAX_VALUE, table.getObject("object-double")); - Assert.assertEquals(Float.MAX_VALUE, table.getObject("object-float")); - Assert.assertEquals(Integer.MAX_VALUE, table.getObject("object-int")); - Assert.assertEquals(Long.MAX_VALUE, table.getObject("object-long")); - Assert.assertEquals(Short.MAX_VALUE, table.getObject("object-short")); - Assert.assertEquals("Hello", table.getObject("object-string")); - } - - public void testWriteBuffer() throws IOException - { - byte[] bytes = { 99, 98, 97, 96, 95 }; - - FieldTable table = new FieldTable(); - table.setBoolean("bool", true); - table.setByte("byte", Byte.MAX_VALUE); - - table.setBytes("bytes", bytes); - table.setChar("char", 'c'); - table.setInteger("int", Integer.MAX_VALUE); - table.setLong("long", Long.MAX_VALUE); - table.setDouble("double", Double.MAX_VALUE); - table.setFloat("float", Float.MAX_VALUE); - table.setShort("short", Short.MAX_VALUE); - table.setString("string", "hello"); - table.setString("null-string", null); - - - ByteArrayOutputStream baos = new ByteArrayOutputStream((int) table.getEncodedSize() + 4); - table.writeToBuffer(new DataOutputStream(baos)); - - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - DataInputStream dis = new DataInputStream(bais); - - - long length = dis.readInt() & 0xFFFFFFFFL; - - FieldTable table2 = new FieldTable(dis, length); - - Assert.assertEquals((Boolean) true, table2.getBoolean("bool")); - Assert.assertEquals((Byte) Byte.MAX_VALUE, table2.getByte("byte")); - assertBytesEqual(bytes, table2.getBytes("bytes")); - Assert.assertEquals((Character) 'c', table2.getCharacter("char")); - Assert.assertEquals(Double.MAX_VALUE, table2.getDouble("double")); - Assert.assertEquals(Float.MAX_VALUE, table2.getFloat("float")); - Assert.assertEquals((Integer) Integer.MAX_VALUE, table2.getInteger("int")); - Assert.assertEquals((Long) Long.MAX_VALUE, table2.getLong("long")); - Assert.assertEquals((Short) Short.MAX_VALUE, table2.getShort("short")); - Assert.assertEquals("hello", table2.getString("string")); - Assert.assertEquals(null, table2.getString("null-string")); - } - - public void testEncodingSize() - { - FieldTable result = new FieldTable(); - int size = 0; - - result.setBoolean("boolean", true); - size += 1 + EncodingUtils.encodedShortStringLength("boolean") + EncodingUtils.encodedBooleanLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setByte("byte", (byte) Byte.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("byte") + EncodingUtils.encodedByteLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - byte[] _bytes = { 99, 98, 97, 96, 95 }; - - result.setBytes("bytes", _bytes); - size += 1 + EncodingUtils.encodedShortStringLength("bytes") + 4 + _bytes.length; - Assert.assertEquals(size, result.getEncodedSize()); - - result.setChar("char", (char) 'c'); - size += 1 + EncodingUtils.encodedShortStringLength("char") + EncodingUtils.encodedCharLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setDouble("double", (double) Double.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("double") + EncodingUtils.encodedDoubleLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setFloat("float", (float) Float.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("float") + EncodingUtils.encodedFloatLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setInteger("int", (int) Integer.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("int") + EncodingUtils.encodedIntegerLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setLong("long", (long) Long.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("long") + EncodingUtils.encodedLongLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setShort("short", (short) Short.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("short") + EncodingUtils.encodedShortLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setString("result", "Hello"); - size += 1 + EncodingUtils.encodedShortStringLength("result") + EncodingUtils.encodedLongStringLength("Hello"); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-bool", true); - size += 1 + EncodingUtils.encodedShortStringLength("object-bool") + EncodingUtils.encodedBooleanLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-byte", Byte.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("object-byte") + EncodingUtils.encodedByteLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-bytes", _bytes); - size += 1 + EncodingUtils.encodedShortStringLength("object-bytes") + 4 + _bytes.length; - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-char", 'c'); - size += 1 + EncodingUtils.encodedShortStringLength("object-char") + EncodingUtils.encodedCharLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-double", Double.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("object-double") + EncodingUtils.encodedDoubleLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-float", Float.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("object-float") + EncodingUtils.encodedFloatLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-int", Integer.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("object-int") + EncodingUtils.encodedIntegerLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - result.setObject("object-long", Long.MAX_VALUE); - size += 1 + EncodingUtils.encodedShortStringLength("object-long") + EncodingUtils.encodedLongLength(); - Assert.assertEquals(size, result.getEncodedSize()); - - 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 - */ - public void testSetObject() - { - FieldTable table = new FieldTable(); - - // Try setting a non primative object - - try - { - table.setObject("value", this); - fail("Only primative values allowed in setObject"); - } - catch (AMQPInvalidClassException iae) - { - // normal path - } - // so length should be zero - Assert.assertEquals(0, table.getEncodedSize()); - } - - /** - * Additional test checkPropertyName doesn't accept Null - */ - public void testCheckPropertyNameasNull() - { - FieldTable table = new FieldTable(); - - try - { - table.setObject((String) null, "String"); - fail("Null property name is not allowed"); - } - catch (IllegalArgumentException iae) - { - // normal path - } - // so length should be zero - Assert.assertEquals(0, table.getEncodedSize()); - } - - /** - * Additional test checkPropertyName doesn't accept an empty String - */ - public void testCheckPropertyNameasEmptyString() - { - FieldTable table = new FieldTable(); - - try - { - table.setObject("", "String"); - fail("empty property name is not allowed"); - } - catch (IllegalArgumentException iae) - { - // normal path - } - // so length should be zero - Assert.assertEquals(0, table.getEncodedSize()); - } - - /** - * Additional test checkPropertyName doesn't accept an empty String - */ - public void testCheckPropertyNamehasMaxLength() - { - FieldTable table = new FieldTable(true); - - StringBuffer longPropertyName = new StringBuffer(129); - - for (int i = 0; i < 129; i++) - { - longPropertyName.append("x"); - } - - try - { - table.setObject(longPropertyName.toString(), "String"); - fail("property name must be < 128 characters"); - } - catch (IllegalArgumentException iae) - { - // normal path - } - // so length should be zero - Assert.assertEquals(0, table.getEncodedSize()); - } - - /** - * Additional test checkPropertyName starts with a letter - */ - public void testCheckPropertyNameStartCharacterIsLetter() - { - FieldTable table = new FieldTable(true); - - // Try a name that starts with a number - try - { - table.setObject("1", "String"); - fail("property name must start with a letter"); - } - catch (IllegalArgumentException iae) - { - // normal path - } - // so length should be zero - Assert.assertEquals(0, table.getEncodedSize()); - } - - /** - * Additional test checkPropertyName starts with a hash or a dollar - */ - public void testCheckPropertyNameStartCharacterIsHashorDollar() - { - FieldTable table = new FieldTable(true); - - // Try a name that starts with a number - try - { - table.setObject("#", "String"); - table.setObject("$", "String"); - } - catch (IllegalArgumentException iae) - { - fail("property name are allowed to start with # and $s"); - } - - } - - /** - * Additional test to test the contents of the table - */ - public void testContents() - { - FieldTable table = new FieldTable(); - - table.setObject("StringProperty", "String"); - - Assert.assertEquals("String", table.getString("StringProperty")); - - // Test Clear - - table.clear(); - - checkEmpty(table); - } - - /** - * Test the contents of the sets - */ - public void testSets() - { - - FieldTable table = new FieldTable(); - - table.setObject("n1", "1"); - table.setObject("n2", "2"); - table.setObject("n3", "3"); - - Assert.assertEquals("1", table.getObject("n1")); - Assert.assertEquals("2", table.getObject("n2")); - Assert.assertEquals("3", table.getObject("n3")); - - } - - public void testAddAll() - { - final FieldTable table1 = new FieldTable(); - table1.setInteger("int1", 1); - table1.setInteger("int2", 2); - assertEquals("Unexpected number of entries in table1", 2, table1.size()); - - final FieldTable table2 = new FieldTable(); - table2.setInteger("int3", 3); - table2.setInteger("int4", 4); - assertEquals("Unexpected number of entries in table2", 2, table2.size()); - - table1.addAll(table2); - assertEquals("Unexpected number of entries in table1 after addAll", 4, table1.size()); - assertEquals(Integer.valueOf(3), table1.getInteger("int3")); - } - - public void testAddAllWithEmptyFieldTable() - { - final FieldTable table1 = new FieldTable(); - table1.setInteger("int1", 1); - table1.setInteger("int2", 2); - assertEquals("Unexpected number of entries in table1", 2, table1.size()); - - final FieldTable emptyFieldTable = new FieldTable(); - - table1.addAll(emptyFieldTable); - assertEquals("Unexpected number of entries in table1 after addAll", 2, table1.size()); - } - - private void assertBytesEqual(byte[] expected, byte[] actual) - { - Assert.assertEquals(expected.length, actual.length); - - for (int index = 0; index < expected.length; index++) - { - Assert.assertEquals(expected[index], actual[index]); - } - } - - private void assertBytesNotEqual(byte[] expected, byte[] actual) - { - Assert.assertEquals(expected.length, actual.length); - - for (int index = 0; index < expected.length; index++) - { - Assert.assertFalse(expected[index] == actual[index]); - } - } - - public static junit.framework.Test suite() - { - return new junit.framework.TestSuite(PropertyFieldTableTest.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 delegate, SSLContext sslContext) + Receiver 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 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; + } +} -- cgit v1.2.1