[AMQ-8981] Fix CustomBridgeFactoryTest

(cherry picked from commit 82a2374764)
This commit is contained in:
Matt Pavlovich 2022-08-22 09:35:43 -05:00 committed by Jean-Baptiste Onofré
parent 0dde1a15df
commit b70b2960a4
5 changed files with 93 additions and 79 deletions

View File

@ -53,10 +53,17 @@ public class BaseNetworkTest {
}
protected void doTearDown() throws Exception {
localConnection.close();
remoteConnection.close();
localBroker.stop();
remoteBroker.stop();
if(localConnection != null)
localConnection.close();
if(remoteConnection != null)
remoteConnection.close();
if(localBroker != null)
localBroker.stop();
if(remoteBroker != null)
remoteBroker.stop();
}
protected void doSetUp(boolean deleteAllMessages) throws Exception {

View File

@ -0,0 +1,49 @@
package org.apache.activemq.network;
import java.util.Arrays;
import java.util.List;
import org.apache.activemq.command.Message;
public class CompositeNetworkBridgeListener implements NetworkBridgeListener {
private final List<NetworkBridgeListener> listeners;
public CompositeNetworkBridgeListener(NetworkBridgeListener... wrapped) {
this.listeners = Arrays.asList(wrapped);
}
@Override
public void bridgeFailed() {
for (NetworkBridgeListener listener : listeners) {
listener.bridgeFailed();
}
}
@Override
public void onStart(NetworkBridge bridge) {
for (NetworkBridgeListener listener : listeners) {
listener.onStart(bridge);
}
}
@Override
public void onStop(NetworkBridge bridge) {
for (NetworkBridgeListener listener : listeners) {
listener.onStop(bridge);
}
}
@Override
public void onOutboundMessage(NetworkBridge bridge, Message message) {
for (NetworkBridgeListener listener : listeners) {
listener.onOutboundMessage(bridge, message);
}
}
@Override
public void onInboundMessage(NetworkBridge bridge, Message message) {
for (NetworkBridgeListener listener : listeners) {
listener.onInboundMessage(bridge, message);
}
}
}

View File

@ -48,7 +48,7 @@ public class CustomBridgeFactoryTest extends BaseNetworkTest {
/**
* Verification of outgoing communication - from local broker (with customized bridge configured) to remote one.
*/
@Test
@Test(timeout = 10000)
public void verifyOutgoingCommunication() throws JMSException {
CustomNetworkBridgeFactory bridgeFactory = getCustomNetworkBridgeFactory();
NetworkBridgeListener listener = bridgeFactory.getListener();
@ -66,7 +66,7 @@ public class CustomBridgeFactoryTest extends BaseNetworkTest {
/**
* Additional test which makes sure that custom bridge receives notification about broker shutdown.
*/
@Test
@Test(timeout = 10000)
public void verifyBrokerShutdown() {
shutdownTest(() -> {
try {
@ -81,7 +81,7 @@ public class CustomBridgeFactoryTest extends BaseNetworkTest {
/**
* Verification of network connector shutdown.
*/
@Test
@Test(timeout = 10000)
public void verifyConnectorShutdown() {
shutdownTest(() -> {
try {
@ -143,75 +143,4 @@ public class CustomBridgeFactoryTest extends BaseNetworkTest {
protected String getLocalBrokerURI() {
return "org/apache/activemq/network/localBroker-custom-factory.xml";
}
// test classes
static class CustomNetworkBridgeFactory implements BridgeFactory {
private final NetworkBridgeListener listener;
CustomNetworkBridgeFactory() {
this(Mockito.mock(NetworkBridgeListener.class));
}
CustomNetworkBridgeFactory(NetworkBridgeListener listener) {
this.listener = listener;
}
public NetworkBridgeListener getListener() {
return listener;
}
@Override
public DemandForwardingBridge createNetworkBridge(NetworkBridgeConfiguration configuration, Transport localTransport, Transport remoteTransport, NetworkBridgeListener listener) {
DemandForwardingBridge bridge = new DemandForwardingBridge(configuration, localTransport, remoteTransport);
bridge.setNetworkBridgeListener(new CompositeNetworkBridgeListener(this.listener, listener));
return bridge;
}
}
static class CompositeNetworkBridgeListener implements NetworkBridgeListener {
private final List<NetworkBridgeListener> listeners;
public CompositeNetworkBridgeListener(NetworkBridgeListener ... wrapped) {
this.listeners = Arrays.asList(wrapped);
}
@Override
public void bridgeFailed() {
for (NetworkBridgeListener listener : listeners) {
listener.bridgeFailed();
}
}
@Override
public void onStart(NetworkBridge bridge) {
for (NetworkBridgeListener listener : listeners) {
listener.onStart(bridge);
}
}
@Override
public void onStop(NetworkBridge bridge) {
for (NetworkBridgeListener listener : listeners) {
listener.onStop(bridge);
}
}
@Override
public void onOutboundMessage(NetworkBridge bridge, Message message) {
for (NetworkBridgeListener listener : listeners) {
listener.onOutboundMessage(bridge, message);
}
}
@Override
public void onInboundMessage(NetworkBridge bridge, Message message) {
for (NetworkBridgeListener listener : listeners) {
listener.onInboundMessage(bridge, message);
}
}
}
}

View File

@ -0,0 +1,29 @@
package org.apache.activemq.network;
import org.apache.activemq.transport.Transport;
import org.mockito.Mockito;
public class CustomNetworkBridgeFactory implements BridgeFactory {
private final NetworkBridgeListener listener;
public CustomNetworkBridgeFactory() {
this(Mockito.mock(NetworkBridgeListener.class));
}
public CustomNetworkBridgeFactory(NetworkBridgeListener listener) {
this.listener = listener;
}
public NetworkBridgeListener getListener() {
return listener;
}
@Override
public DemandForwardingBridge createNetworkBridge(NetworkBridgeConfiguration configuration, Transport localTransport, Transport remoteTransport, NetworkBridgeListener listener) {
DemandForwardingBridge bridge = new DemandForwardingBridge(configuration, localTransport, remoteTransport);
bridge.setNetworkBridgeListener(new CompositeNetworkBridgeListener(this.listener, listener));
return bridge;
}
}

View File

@ -27,7 +27,7 @@
<networkConnectors>
<networkConnector uri="static:(tcp://localhost:61617)" name="networkConnector">
<bridgeFactory>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.network.CustomBridgeFactoryTest.CustomNetworkBridgeFactory" />
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.network.CustomNetworkBridgeFactory" />
</bridgeFactory>
</networkConnector>
</networkConnectors>