Add some tests and cleanup of the testing client
This commit is contained in:
Timothy Bish 2015-03-17 15:28:48 -04:00
parent 934ad44add
commit 1a0f73ed19
9 changed files with 258 additions and 31 deletions

View File

@ -41,7 +41,7 @@ public abstract class AmqpAbstractResource<E extends Endpoint> implements AmqpRe
protected AsyncResult openRequest; protected AsyncResult openRequest;
protected AsyncResult closeRequest; protected AsyncResult closeRequest;
private AmqpStateInspector amqpStateInspector = new AmqpStateInspector(); private AmqpValidator amqpStateInspector;
private E endpoint; private E endpoint;
@ -184,13 +184,13 @@ public abstract class AmqpAbstractResource<E extends Endpoint> implements AmqpRe
this.endpoint = endpoint; this.endpoint = endpoint;
} }
public AmqpStateInspector getStateInspector() { public AmqpValidator getStateInspector() {
return amqpStateInspector; return amqpStateInspector;
} }
public void setStateInspector(AmqpStateInspector stateInspector) { public void setStateInspector(AmqpValidator stateInspector) {
if (stateInspector == null) { if (stateInspector == null) {
stateInspector = new AmqpStateInspector(); stateInspector = new AmqpValidator();
} }
this.amqpStateInspector = stateInspector; this.amqpStateInspector = stateInspector;

View File

@ -38,7 +38,7 @@ public class AmqpClient {
private final String password; private final String password;
private final URI remoteURI; private final URI remoteURI;
private AmqpStateInspector stateInspector = new AmqpStateInspector(); private AmqpValidator stateInspector = new AmqpValidator();
private List<Symbol> offeredCapabilities = Collections.emptyList(); private List<Symbol> offeredCapabilities = Collections.emptyList();
private Map<Symbol, Object> offeredProperties = Collections.emptyMap(); private Map<Symbol, Object> offeredProperties = Collections.emptyMap();
@ -176,7 +176,7 @@ public class AmqpClient {
/** /**
* @return the currently set state inspector used to check state after various events. * @return the currently set state inspector used to check state after various events.
*/ */
public AmqpStateInspector getStateInspector() { public AmqpValidator getStateInspector() {
return stateInspector; return stateInspector;
} }
@ -187,9 +187,9 @@ public class AmqpClient {
* @param stateInspector * @param stateInspector
* the new state inspector to use. * the new state inspector to use.
*/ */
public void setStateInspector(AmqpStateInspector stateInspector) { public void setStateInspector(AmqpValidator stateInspector) {
if (stateInspector == null) { if (stateInspector == null) {
stateInspector = new AmqpStateInspector(); stateInspector = new AmqpValidator();
} }
this.stateInspector = stateInspector; this.stateInspector = stateInspector;

View File

@ -222,10 +222,9 @@ public class AmqpConnection extends AmqpAbstractResource<Connection> implements
@Override @Override
public void run() { public void run() {
checkClosed(); checkClosed();
session.setEndpoint(getEndpoint().session()); session.setEndpoint(getEndpoint().session());
session.setStateInspector(getStateInspector());
session.open(request); session.open(request);
pumpToProtonTransport(); pumpToProtonTransport();
} }
}); });

View File

@ -69,6 +69,7 @@ public class AmqpSession extends AmqpAbstractResource<Session> {
@Override @Override
public void run() { public void run() {
checkClosed(); checkClosed();
sender.setStateInspector(getStateInspector());
sender.open(request); sender.open(request);
pumpToProtonTransport(); pumpToProtonTransport();
} }
@ -90,16 +91,56 @@ public class AmqpSession extends AmqpAbstractResource<Session> {
* @throws Exception if an error occurs while creating the receiver. * @throws Exception if an error occurs while creating the receiver.
*/ */
public AmqpReceiver createReceiver(String address) throws Exception { public AmqpReceiver createReceiver(String address) throws Exception {
return createReceiver(address, null, false);
}
/**
* Create a receiver instance using the given address
*
* @param address
* the address to which the receiver will subscribe for its messages.
* @param selector
* the JMS selector to use for the subscription
*
* @return a newly created receiver that is ready for use.
*
* @throws Exception if an error occurs while creating the receiver.
*/
public AmqpReceiver createReceiver(String address, String selector) throws Exception {
return createReceiver(address, selector, false);
}
/**
* Create a receiver instance using the given address
*
* @param address
* the address to which the receiver will subscribe for its messages.
* @param selector
* the JMS selector to use for the subscription
* @param noLocal
* should the subscription have messages from its connection filtered.
*
* @return a newly created receiver that is ready for use.
*
* @throws Exception if an error occurs while creating the receiver.
*/
public AmqpReceiver createReceiver(String address, String selector, boolean noLocal) throws Exception {
checkClosed(); checkClosed();
final AmqpReceiver receiver = new AmqpReceiver(AmqpSession.this, address, getNextReceiverId());
final ClientFuture request = new ClientFuture(); final ClientFuture request = new ClientFuture();
final AmqpReceiver receiver = new AmqpReceiver(AmqpSession.this, address, getNextReceiverId());
receiver.setNoLocal(noLocal);
if (selector != null && !selector.isEmpty()) {
receiver.setSelector(selector);
}
connection.getScheduler().execute(new Runnable() { connection.getScheduler().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
checkClosed(); checkClosed();
receiver.setStateInspector(getStateInspector());
receiver.open(request); receiver.open(request);
pumpToProtonTransport(); pumpToProtonTransport();
} }
@ -123,17 +164,64 @@ public class AmqpSession extends AmqpAbstractResource<Session> {
* @throws Exception if an error occurs while creating the receiver. * @throws Exception if an error occurs while creating the receiver.
*/ */
public AmqpReceiver createDurableReceiver(String address, String subscriptionName) throws Exception { public AmqpReceiver createDurableReceiver(String address, String subscriptionName) throws Exception {
return createDurableReceiver(address, subscriptionName, null, false);
}
/**
* Create a receiver instance using the given address that creates a durable subscription.
*
* @param address
* the address to which the receiver will subscribe for its messages.
* @param subscriptionName
* the name of the subscription that is being created.
* @param selector
* the JMS selector to use for the subscription
*
* @return a newly created receiver that is ready for use.
*
* @throws Exception if an error occurs while creating the receiver.
*/
public AmqpReceiver createDurableReceiver(String address, String subscriptionName, String selector) throws Exception {
return createDurableReceiver(address, subscriptionName, selector, false);
}
/**
* Create a receiver instance using the given address that creates a durable subscription.
*
* @param address
* the address to which the receiver will subscribe for its messages.
* @param subscriptionName
* the name of the subscription that is being created.
* @param selector
* the JMS selector to use for the subscription
* @param noLocal
* should the subscription have messages from its connection filtered.
*
* @return a newly created receiver that is ready for use.
*
* @throws Exception if an error occurs while creating the receiver.
*/
public AmqpReceiver createDurableReceiver(String address, String subscriptionName, String selector, boolean noLocal) throws Exception {
checkClosed(); checkClosed();
if (subscriptionName == null || subscriptionName.isEmpty()) {
throw new IllegalArgumentException("subscription name must not be null or empty.");
}
final ClientFuture request = new ClientFuture();
final AmqpReceiver receiver = new AmqpReceiver(AmqpSession.this, address, getNextReceiverId()); final AmqpReceiver receiver = new AmqpReceiver(AmqpSession.this, address, getNextReceiverId());
receiver.setSubscriptionName(subscriptionName); receiver.setSubscriptionName(subscriptionName);
final ClientFuture request = new ClientFuture(); receiver.setNoLocal(noLocal);
if (selector != null && !selector.isEmpty()) {
receiver.setSelector(selector);
}
connection.getScheduler().execute(new Runnable() { connection.getScheduler().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
checkClosed(); checkClosed();
receiver.setStateInspector(getStateInspector());
receiver.open(request); receiver.open(request);
pumpToProtonTransport(); pumpToProtonTransport();
} }

View File

@ -0,0 +1,44 @@
/**
* 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.activemq.transport.amqp.client;
import org.apache.qpid.proton.amqp.DescribedType;
import org.apache.qpid.proton.amqp.UnsignedLong;
/**
* A Described Type wrapper for an unsupported filter that the broker should ignore.
*/
public class AmqpUnknownFilterType implements DescribedType {
public static final AmqpUnknownFilterType UNKOWN_FILTER = new AmqpUnknownFilterType();
private final String payload;
public AmqpUnknownFilterType() {
this.payload = "UnknownFilter{}";
}
@Override
public Object getDescriptor() {
return UnsignedLong.valueOf(0x0000468C00000099L);
}
@Override
public Object getDescribed() {
return this.payload;
}
}

View File

@ -17,14 +17,15 @@
package org.apache.activemq.transport.amqp.client; package org.apache.activemq.transport.amqp.client;
import org.apache.qpid.proton.engine.Connection; import org.apache.qpid.proton.engine.Connection;
import org.apache.qpid.proton.engine.Link; import org.apache.qpid.proton.engine.Receiver;
import org.apache.qpid.proton.engine.Sender;
import org.apache.qpid.proton.engine.Session; import org.apache.qpid.proton.engine.Session;
/** /**
* Abstract base for a validation hook that is used in tests to check * Abstract base for a validation hook that is used in tests to check
* the state of a remote resource after a variety of lifecycle events. * the state of a remote resource after a variety of lifecycle events.
*/ */
public class AmqpStateInspector { public class AmqpValidator {
private boolean valid = true; private boolean valid = true;
private String errorMessage; private String errorMessage;
@ -37,7 +38,11 @@ public class AmqpStateInspector {
} }
public void inspectOpenedResource(Link link) { public void inspectOpenedResource(Sender sender) {
}
public void inspectOpenedResource(Receiver receiver) {
} }
@ -49,11 +54,19 @@ public class AmqpStateInspector {
} }
public void inspectClosedResource(Link link) { public void inspectClosedResource(Sender sender) {
} }
public void inspectDetachedResource(Link link) { public void inspectClosedResource(Receiver receiver) {
}
public void inspectDetachedResource(Sender sender) {
}
public void inspectDetachedResource(Receiver receiver) {
} }
@ -80,7 +93,7 @@ public class AmqpStateInspector {
} }
} }
public void assertIfStateChecksFailed() { public void assertValid() {
if (!isValid()) { if (!isValid()) {
throw new AssertionError(errorMessage); throw new AssertionError(errorMessage);
} }

View File

@ -124,14 +124,14 @@ public class UnmodifiableLink implements Link {
@Override @Override
public Source getSource() { public Source getSource() {
// TODO Auto-generated method stub // TODO Figure out a simple way to wrap the odd Source types in Proton-J
return null; return link.getSource();
} }
@Override @Override
public Target getTarget() { public Target getTarget() {
// TODO Auto-generated method stub // TODO Figure out a simple way to wrap the odd Source types in Proton-J
return null; return link.getTarget();
} }
@Override @Override
@ -146,14 +146,14 @@ public class UnmodifiableLink implements Link {
@Override @Override
public Source getRemoteSource() { public Source getRemoteSource() {
// TODO Auto-generated method stub // TODO Figure out a simple way to wrap the odd Source types in Proton-J
return null; return link.getSource();
} }
@Override @Override
public Target getRemoteTarget() { public Target getRemoteTarget() {
// TODO Auto-generated method stub // TODO Figure out a simple way to wrap the odd Target types in Proton-J
return null; return link.getTarget();
} }
@Override @Override

View File

@ -26,7 +26,7 @@ import java.util.Map;
import org.apache.activemq.transport.amqp.client.AmqpClient; import org.apache.activemq.transport.amqp.client.AmqpClient;
import org.apache.activemq.transport.amqp.client.AmqpClientTestSupport; import org.apache.activemq.transport.amqp.client.AmqpClientTestSupport;
import org.apache.activemq.transport.amqp.client.AmqpConnection; import org.apache.activemq.transport.amqp.client.AmqpConnection;
import org.apache.activemq.transport.amqp.client.AmqpStateInspector; import org.apache.activemq.transport.amqp.client.AmqpValidator;
import org.apache.qpid.proton.amqp.Symbol; import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.transport.AmqpError; import org.apache.qpid.proton.amqp.transport.AmqpError;
import org.apache.qpid.proton.amqp.transport.ErrorCondition; import org.apache.qpid.proton.amqp.transport.ErrorCondition;
@ -62,7 +62,7 @@ public class AmqpConnectionsTest extends AmqpClientTestSupport {
AmqpClient client = createAmqpClient(); AmqpClient client = createAmqpClient();
assertNotNull(client); assertNotNull(client);
client.setStateInspector(new AmqpStateInspector() { client.setStateInspector(new AmqpValidator() {
@Override @Override
public void inspectOpenedResource(Connection connection) { public void inspectOpenedResource(Connection connection) {
@ -88,6 +88,7 @@ public class AmqpConnectionsTest extends AmqpClientTestSupport {
assertEquals(1, getProxyToBroker().getCurrentConnectionsCount()); assertEquals(1, getProxyToBroker().getCurrentConnectionsCount());
connection.getStateInspector().assertValid();
connection.close(); connection.close();
assertEquals(0, getProxyToBroker().getCurrentConnectionsCount()); assertEquals(0, getProxyToBroker().getCurrentConnectionsCount());
@ -131,7 +132,7 @@ public class AmqpConnectionsTest extends AmqpClientTestSupport {
connection1.connect(); connection1.connect();
assertEquals(1, getProxyToBroker().getCurrentConnectionsCount()); assertEquals(1, getProxyToBroker().getCurrentConnectionsCount());
connection2.setStateInspector(new AmqpStateInspector() { connection2.setStateInspector(new AmqpValidator() {
@Override @Override
public void inspectOpenedResource(Connection connection) { public void inspectOpenedResource(Connection connection) {
@ -160,7 +161,7 @@ public class AmqpConnectionsTest extends AmqpClientTestSupport {
LOG.info("Second connection with same container Id failed as expected."); LOG.info("Second connection with same container Id failed as expected.");
} }
connection2.getStateInspector().assertIfStateChecksFailed(); connection2.getStateInspector().assertValid();
assertEquals(1, getProxyToBroker().getCurrentConnectionsCount()); assertEquals(1, getProxyToBroker().getCurrentConnectionsCount());

View File

@ -16,10 +16,14 @@
*/ */
package org.apache.activemq.transport.amqp.interop; package org.apache.activemq.transport.amqp.interop;
import static org.apache.activemq.transport.amqp.AmqpSupport.JMS_SELECTOR_FILTER_IDS;
import static org.apache.activemq.transport.amqp.AmqpSupport.NO_LOCAL_FILTER_IDS;
import static org.apache.activemq.transport.amqp.AmqpSupport.findFilter;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.activemq.broker.jmx.QueueViewMBean; import org.apache.activemq.broker.jmx.QueueViewMBean;
@ -29,7 +33,11 @@ import org.apache.activemq.transport.amqp.client.AmqpConnection;
import org.apache.activemq.transport.amqp.client.AmqpMessage; import org.apache.activemq.transport.amqp.client.AmqpMessage;
import org.apache.activemq.transport.amqp.client.AmqpReceiver; import org.apache.activemq.transport.amqp.client.AmqpReceiver;
import org.apache.activemq.transport.amqp.client.AmqpSession; import org.apache.activemq.transport.amqp.client.AmqpSession;
import org.apache.activemq.transport.amqp.client.AmqpValidator;
import org.apache.activemq.util.Wait; import org.apache.activemq.util.Wait;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.messaging.Source;
import org.apache.qpid.proton.engine.Receiver;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -62,6 +70,80 @@ public class AmqpReceiverTest extends AmqpClientTestSupport {
connection.close(); connection.close();
} }
@Test(timeout = 60000)
public void testCreateQueueReceiverWithJMSSelector() throws Exception {
AmqpClient client = createAmqpClient();
client.setStateInspector(new AmqpValidator() {
@SuppressWarnings("unchecked")
@Override
public void inspectOpenedResource(Receiver receiver) {
LOG.info("Receiver opened: {}", receiver);
if (receiver.getSource() == null) {
markAsInvalid("Link opened with null source.");
}
Source source = (Source) receiver.getSource();
Map<Symbol, Object> filters = source.getFilter();
if (findFilter(filters, JMS_SELECTOR_FILTER_IDS) == null) {
markAsInvalid("Broker did not return the JMS Filter on Attach");
}
}
});
AmqpConnection connection = client.connect();
AmqpSession session = connection.createSession();
assertEquals(0, brokerService.getAdminView().getQueues().length);
session.createReceiver("queue://" + getTestName(), "JMSPriority > 8");
assertEquals(1, brokerService.getAdminView().getQueueSubscribers().length);
connection.getStateInspector().assertValid();
connection.close();
}
@Test(timeout = 60000)
public void testCreateQueueReceiverWithNoLocalSet() throws Exception {
AmqpClient client = createAmqpClient();
client.setStateInspector(new AmqpValidator() {
@SuppressWarnings("unchecked")
@Override
public void inspectOpenedResource(Receiver receiver) {
LOG.info("Receiver opened: {}", receiver);
if (receiver.getSource() == null) {
markAsInvalid("Link opened with null source.");
}
Source source = (Source) receiver.getSource();
Map<Symbol, Object> filters = source.getFilter();
if (findFilter(filters, NO_LOCAL_FILTER_IDS) == null) {
markAsInvalid("Broker did not return the NoLocal Filter on Attach");
}
}
});
AmqpConnection connection = client.connect();
AmqpSession session = connection.createSession();
assertEquals(0, brokerService.getAdminView().getQueues().length);
session.createReceiver("queue://" + getTestName(), null, true);
assertEquals(1, brokerService.getAdminView().getQueueSubscribers().length);
connection.getStateInspector().assertValid();
connection.close();
}
@Test(timeout = 60000) @Test(timeout = 60000)
public void testCreateTopicReceiver() throws Exception { public void testCreateTopicReceiver() throws Exception {
AmqpClient client = createAmqpClient(); AmqpClient client = createAmqpClient();