String address = RandomUtil.randomString();
String name = RandomUtil.randomString();
String routingName = RandomUtil.randomString();
String forwardingAddress = RandomUtil.randomString();
HornetQServerControl serverControl = createManagementControl();
checkNoResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name));
assertEquals(0, serverControl.getDivertNames().length);
serverControl.createDivert(name.toString(), routingName, address, forwardingAddress, true, null, null);
checkResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name));
DivertControl divertControl = ManagementControlHelper.createDivertControl(name.toString(), mbeanServer);
assertEquals(name.toString(), divertControl.getUniqueName());
assertEquals(address, divertControl.getAddress());
assertEquals(forwardingAddress, divertControl.getForwardingAddress());
assertEquals(routingName, divertControl.getRoutingName());
assertTrue(divertControl.isExclusive());
assertNull(divertControl.getFilter());
assertNull(divertControl.getTransformerClassName());
String[] divertNames = serverControl.getDivertNames();
assertEquals(1, divertNames.length);
assertEquals(name, divertNames[0]);
// check that a message sent to the address is diverted exclusively
ClientSessionFactory csf = new ClientSessionFactoryImpl(new TransportConfiguration(InVMConnectorFactory.class.getName()));
ClientSession session = csf.createSession();
String divertQueue = RandomUtil.randomString();
String queue = RandomUtil.randomString();
session.createQueue(forwardingAddress, divertQueue);
session.createQueue(address, queue);
ClientProducer producer = session.createProducer(address);
ClientMessage message = session.createMessage(false);
String text = RandomUtil.randomString();
message.putStringProperty("prop", text);
producer.send(message);
ClientConsumer consumer = session.createConsumer(queue);
ClientConsumer divertedConsumer = session.createConsumer(divertQueue);
session.start();
assertNull(consumer.receiveImmediate());
message = divertedConsumer.receive(5000);
assertNotNull(message);
assertEquals(text, message.getStringProperty("prop"));
serverControl.destroyDivert(name.toString());
checkNoResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name));
assertEquals(0, serverControl.getDivertNames().length);
// check that a message is no longer diverted
message = session.createMessage(false);
String text2 = RandomUtil.randomString();
message.putStringProperty("prop", text2);