Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/main/java/com/rabbitmq/jms/admin/RMQConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,22 @@ private static String uriVirtualHostEscape(String vHost) {
*/
@Override
public Reference getReference() throws NamingException {
Reference ref = new Reference(RMQConnectionFactory.class.getName());
Reference ref = new Reference(RMQConnectionFactory.class.getName(), RMQObjectFactory.class.getName(), null);
addStringRefProperty(ref, "uri", this.getUri());
addStringRefProperty(ref, "host", this.getHost());
addStringRefProperty(ref, "password", this.getPassword());
addIntegerRefProperty(ref, "port", this.getPort());
addIntegerRefProperty(ref, "queueBrowserReadMax", this.getQueueBrowserReadMax());
addIntegerRefProperty(ref, "onMessageTimeoutMs", this.getOnMessageTimeoutMs());
addIntegerRefProperty(ref, "channelsQos", this.getChannelsQos());
addBooleanProperty(ref, "ssl", this.ssl);
addLongRefProperty(ref, "terminationTimeout", this.getTerminationTimeout());
addStringRefProperty(ref, "username", this.getUsername());
addStringRefProperty(ref, "virtualHost", this.getVirtualHost());
addBooleanProperty(ref, "cleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose",
this.isCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose());
addBooleanProperty(ref, "declareReplyToDestination",
this.declareReplyToDestination);
return ref;
}

Expand Down Expand Up @@ -591,6 +603,35 @@ private static void addIntegerRefProperty(Reference ref,
ref.add(ra);
}

/**
* Adds an long valued property to a Reference (as a RefAddr).
* @param ref - the reference to contain the value
* @param propertyName - the name of the property
* @param value - the value to store with the property
*/
private static void addLongRefProperty(Reference ref,
String propertyName,
Long value) {
if (value == null || propertyName == null) return;
RefAddr ra = new StringRefAddr(propertyName, String.valueOf(value));
ref.add(ra);
}

/**
* Adds a boolean valued property to a Reference (as a StringRefAddr) if the value is <code>true</code>
* (default <code>false</code> on read assumed).
* @param ref - the reference to contain the value
* @param propertyName - the name of the property
* @param value - the value to store with the property
*/
private static final void addBooleanProperty(Reference ref,
String propertyName,
boolean value) {
if (propertyName==null) return;
RefAddr ra = new StringRefAddr(propertyName, String.valueOf(value));
ref.add(ra);
}

/**
* {@inheritDoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/rabbitmq/jms/admin/RMQDestination.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public String getQueueName() throws JMSException {

@Override
public Reference getReference() throws NamingException {
Reference ref = new Reference(this.getClass().getCanonicalName());
Reference ref = new Reference(this.getClass().getCanonicalName(), RMQObjectFactory.class.getName(), null);
addStringProperty(ref, "destinationName", this.destinationName);
addBooleanProperty(ref, "amqp", this.amqp);
addBooleanProperty(ref, "isQueue", this.isQueue);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/rabbitmq/jms/admin/RMQObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,25 @@ public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable<?,
* javax.jms.ConnectionFactory
* javax.jms.QueueConnectionFactory
* javax.jms.TopicConnectionFactory
* com.rabbitmq.jms.admin.RMQConnectionFactory
* javax.jms.Topic
* javax.jms.Queue
* com.rabbitmq.jms.admin.RMQDestination
*
*/
boolean topic = false;
boolean connectionFactory = false;
if ( javax.jms.QueueConnectionFactory.class.getName().equals(className)
|| javax.jms.TopicConnectionFactory.class.getName().equals(className)
|| javax.jms.ConnectionFactory.class.getName().equals(className)
|| RMQConnectionFactory.class.getName().equals(className)
) {
connectionFactory = true;
} else if (javax.jms.Topic.class.getName().equals(className)) {
topic = true;
} else if (javax.jms.Queue.class.getName().equals(className)) {
} else if (RMQDestination.class.getName().equals(className)) {
topic = !getBooleanProperty(ref, environment, "isQueue", true, false);
} else {
throw new NamingException(String.format("Unknown class [%s]", className));
}
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/com/rabbitmq/jms/admin/RMQConnectionFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.naming.StringRefAddr;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
Expand All @@ -34,8 +35,18 @@ public class RMQConnectionFactoryTest {
static {
RMQConnectionFactory defaultFact = new RMQConnectionFactory();
defaultProps.setProperty("uri", defaultFact.getUri());
defaultProps.setProperty("host", defaultFact.getHost());
defaultProps.setProperty("password", defaultFact.getPassword());
defaultProps.setProperty("port", "5672");
defaultProps.setProperty("queueBrowserReadMax", "0");
defaultProps.setProperty("onMessageTimeoutMs", "2000");
defaultProps.setProperty("channelsQos", "-1");
defaultProps.setProperty("ssl", "false");
defaultProps.setProperty("terminationTimeout", "15000");
defaultProps.setProperty("username", "guest");
defaultProps.setProperty("virtualHost", "/");
defaultProps.setProperty("cleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose", "false");
defaultProps.setProperty("declareReplyToDestination", "true");
}

private static Properties getProps(Reference ref) {
Expand Down Expand Up @@ -87,7 +98,7 @@ private static void removeRefProperty(Reference ref,
public void testDefaultConnectionFactoryReference() throws Exception {
RMQConnectionFactory connFactory = new RMQConnectionFactory();
Reference ref = connFactory.getReference();

assertThat(getProps(ref)).hasSameSizeAs(defaultProps);
assertEquals(defaultProps, getProps(ref), "Not the default properties");
}

Expand Down Expand Up @@ -121,10 +132,14 @@ public void testConnectionFactoryRegeneration() throws Exception {
connFactory.setPassword("my-password");
connFactory.setPort(42);
connFactory.setQueueBrowserReadMax(52);
connFactory.setOnMessageTimeoutMs(66);
connFactory.setChannelsQos(250);
connFactory.useSslProtocol();
connFactory.setTerminationTimeout(1234567890123456789L);
connFactory.setUsername("fred");
connFactory.setVirtualHost("bill");
connFactory.setCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose(true);
connFactory.setDeclareReplyToDestination(false);

Reference ref = connFactory.getReference();

Expand All @@ -136,12 +151,19 @@ public void testConnectionFactoryRegeneration() throws Exception {
assertEquals("my-password", newFactory.getPassword(), "Not the correct password");
assertEquals(42, newFactory.getPort(), "Not the correct port");
assertEquals(52, newFactory.getQueueBrowserReadMax(), "Not the correct queueBrowserReadMax");
assertEquals(66, newFactory.getOnMessageTimeoutMs());
assertEquals(250, newFactory.getChannelsQos());
assertEquals(true, newFactory.isSsl(), "Not the correct ssl");

assertEquals(15000L, newFactory.getTerminationTimeout(), "Not the correct terminationTimeout");
assertEquals(1234567890123456789L, newFactory.getTerminationTimeout(), "Not the correct terminationTimeout");

assertEquals("fred", newFactory.getUsername(), "Not the correct username");
assertEquals("bill", newFactory.getVirtualHost(), "Not the correct virtualHost");
assertTrue(newFactory.isCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose());

Field declareReplyToDestinationField = RMQConnectionFactory.class.getDeclaredField("declareReplyToDestination");
declareReplyToDestinationField.setAccessible(true);
assertFalse((Boolean) declareReplyToDestinationField.get(newFactory));
}

@Test
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/com/rabbitmq/jms/admin/RMQDestinationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. */
package com.rabbitmq.jms.admin;

import org.junit.jupiter.api.Test;

import javax.naming.Reference;

import static org.assertj.core.api.Assertions.assertThat;

public class RMQDestinationTest {

RMQObjectFactory rmqObjectFactory = new RMQObjectFactory();

@Test
void queueRegeneration() throws Exception {
RMQDestination queue = new RMQDestination("queue", true, false);
Reference reference = queue.getReference();
RMQDestination newQueue = (RMQDestination) rmqObjectFactory.getObjectInstance(reference, null, null, null);
assertThat(newQueue.isQueue()).isTrue();
assertThat(newQueue.getDestinationName()).isEqualTo("queue");
assertThat(newQueue.isAmqp()).isFalse();
}

@Test
void topicRegeneration() throws Exception {
RMQDestination topic = new RMQDestination("topic", false, false);
Reference reference = topic.getReference();
RMQDestination newTopic = (RMQDestination) rmqObjectFactory.getObjectInstance(reference, null, null, null);
assertThat(newTopic.isQueue()).isFalse();
assertThat(newTopic.getDestinationName()).isEqualTo("topic");
assertThat(newTopic.isAmqp()).isFalse();
}

@Test
void amqpDestinationRegeneration() throws Exception {
RMQDestination destination = new RMQDestination(
"destination", "exchange", "routing-key", "queue"
);
Reference reference = destination.getReference();
RMQDestination newReference = (RMQDestination) rmqObjectFactory.getObjectInstance(reference, null, null, null);
assertThat(newReference.isQueue()).isTrue();
assertThat(newReference.getDestinationName()).isEqualTo("destination");
assertThat(newReference.isAmqp()).isTrue();
assertThat(newReference.getAmqpExchangeName()).isEqualTo("exchange");
assertThat(newReference.getAmqpRoutingKey()).isEqualTo("routing-key");
assertThat(newReference.getAmqpQueueName()).isEqualTo("queue");
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved. */
package com.rabbitmq.jms.admin;

import org.junit.jupiter.api.Test;
Expand All @@ -21,7 +22,7 @@ public class RMQObjectFactoryTest {
private RMQObjectFactory rmqObjectFactory = new RMQObjectFactory();

@Test
public void getObjectInstanceShouldCreateARMQConnectionFactoryViaReference() throws Exception {
public void getObjectInstanceShouldCreateAMQPConnectionFactoryViaReference() throws Exception {

Reference ref = new Reference(ConnectionFactory.class.getName());

Expand All @@ -42,7 +43,7 @@ public void getObjectInstanceShouldCreateARMQConnectionFactoryViaReference() thr


@Test
public void getObjectInstanceShouldCreateARMQConnectionFactoryViaEnvironment() throws Exception {
public void getObjectInstanceShouldCreateAMQPConnectionFactoryViaEnvironment() throws Exception {

Hashtable<?, ?> environment = new Hashtable<Object, Object>() {{
put("className", "javax.jms.ConnectionFactory");
Expand All @@ -69,7 +70,7 @@ public void getObjectInstanceShouldCreateARMQConnectionFactoryViaEnvironment() t
}

@Test
public void getObjectInstanceShouldCreateARMQDestinationQUEUEViaEnvironment() throws Exception {
public void getObjectInstanceShouldCreateAMQPDestinationQUEUEViaEnvironment() throws Exception {

Hashtable<?, ?> environment = new Hashtable<Object, Object>() {{
put("className", "javax.jms.Queue");
Expand Down