/*
* Copyright 2009 Red Hat, Inc.
* Red Hat 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.hornetq.tests.integration.management;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import junit.framework.Assert;
import org.hornetq.api.core.Pair;
import org.hornetq.api.core.SimpleString;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.core.client.HornetQClient;
import org.hornetq.api.core.management.BridgeControl;
import org.hornetq.api.core.management.NotificationType;
import org.hornetq.api.core.management.ObjectNameBuilder;
import org.hornetq.core.config.BridgeConfiguration;
import org.hornetq.core.config.Configuration;
import org.hornetq.core.config.CoreQueueConfiguration;
import org.hornetq.core.config.impl.ConfigurationImpl;
import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory;
import org.hornetq.core.remoting.impl.invm.TransportConstants;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.core.server.management.Notification;
import org.hornetq.tests.integration.SimpleNotificationService;
import org.hornetq.tests.util.RandomUtil;
/**
* A BridgeControlTest
*
* @author <a href="jmesnil@redhat.com">Jeff Mesnil</a>
*
* Created 11 dec. 2008 17:38:58
*
*/
public class BridgeControlTest extends ManagementTestBase
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
private HornetQServer server_0;
private BridgeConfiguration bridgeConfig;
private HornetQServer server_1;
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
public void testAttributes() throws Exception
{
checkResource(ObjectNameBuilder.DEFAULT.getBridgeObjectName(bridgeConfig.getName()));
BridgeControl bridgeControl = createBridgeControl(bridgeConfig.getName(), mbeanServer);
Assert.assertEquals(bridgeConfig.getName(), bridgeControl.getName());
Assert.assertEquals(bridgeConfig.getDiscoveryGroupName(), bridgeControl.getDiscoveryGroupName());
Assert.assertEquals(bridgeConfig.getQueueName(), bridgeControl.getQueueName());
Assert.assertEquals(bridgeConfig.getForwardingAddress(), bridgeControl.getForwardingAddress());
Assert.assertEquals(bridgeConfig.getFilterString(), bridgeControl.getFilterString());
Assert.assertEquals(bridgeConfig.getRetryInterval(), bridgeControl.getRetryInterval());
Assert.assertEquals(bridgeConfig.getRetryIntervalMultiplier(), bridgeControl.getRetryIntervalMultiplier());
Assert.assertEquals(bridgeConfig.getReconnectAttempts(), bridgeControl.getReconnectAttempts());
Assert.assertEquals(bridgeConfig.isUseDuplicateDetection(), bridgeControl.isUseDuplicateDetection());
String[] connectorPairData = bridgeControl.getStaticConnectors();
Assert.assertEquals(bridgeConfig.getStaticConnectors().get(0), connectorPairData[0]);
Assert.assertTrue(bridgeControl.isStarted());
}
public void testStartStop() throws Exception
{
checkResource(ObjectNameBuilder.DEFAULT.getBridgeObjectName(bridgeConfig.getName()));
BridgeControl bridgeControl = createBridgeControl(bridgeConfig.getName(), mbeanServer);
// started by the server
Assert.assertTrue(bridgeControl.isStarted());
bridgeControl.stop();
Assert.assertFalse(bridgeControl.isStarted());
bridgeControl.start();
Assert.assertTrue(bridgeControl.isStarted());
}
public void testNotifications() throws Exception
{
SimpleNotificationService.Listener notifListener = new SimpleNotificationService.Listener();
BridgeControl bridgeControl = createBridgeControl(bridgeConfig.getName(), mbeanServer);
server_0.getManagementService().addNotificationListener(notifListener);
Assert.assertEquals(0, notifListener.getNotifications().size());
bridgeControl.stop();
Assert.assertEquals(1, notifListener.getNotifications().size());
Notification notif = notifListener.getNotifications().get(0);
Assert.assertEquals(NotificationType.BRIDGE_STOPPED, notif.getType());
Assert.assertEquals(bridgeControl.getName(), notif.getProperties()
.getSimpleStringProperty(new SimpleString("name"))
.toString());
bridgeControl.start();
Assert.assertEquals(2, notifListener.getNotifications().size());
notif = notifListener.getNotifications().get(1);
Assert.assertEquals(NotificationType.BRIDGE_STARTED, notif.getType());
Assert.assertEquals(bridgeControl.getName(), notif.getProperties()
.getSimpleStringProperty(new SimpleString("name"))
.toString());
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
@Override
protected void setUp() throws Exception
{
super.setUp();
Map<String, Object> acceptorParams = new HashMap<String, Object>();
acceptorParams.put(TransportConstants.SERVER_ID_PROP_NAME, 1);
TransportConfiguration acceptorConfig = new TransportConfiguration(InVMAcceptorFactory.class.getName(),
acceptorParams,
RandomUtil.randomString());
TransportConfiguration connectorConfig = new TransportConfiguration(InVMConnectorFactory.class.getName(),
acceptorParams,
RandomUtil.randomString());
CoreQueueConfiguration sourceQueueConfig = new CoreQueueConfiguration(RandomUtil.randomString(),
RandomUtil.randomString(),
null,
false);
CoreQueueConfiguration targetQueueConfig = new CoreQueueConfiguration(RandomUtil.randomString(),
RandomUtil.randomString(),
null,
false);
List<String> connectors = new ArrayList<String>();
connectors.add(connectorConfig.getName());
bridgeConfig = new BridgeConfiguration(RandomUtil.randomString(),
sourceQueueConfig.getName(),
targetQueueConfig.getAddress(),
null,
null,
RandomUtil.randomPositiveLong(),
RandomUtil.randomDouble(),
RandomUtil.randomPositiveInt(),
RandomUtil.randomBoolean(),
RandomUtil.randomPositiveInt(),
HornetQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD,
connectors,
false,
ConfigurationImpl.DEFAULT_CLUSTER_USER,
ConfigurationImpl.DEFAULT_CLUSTER_PASSWORD);
Configuration conf_1 = createBasicConfig();
conf_1.setSecurityEnabled(false);
conf_1.setJMXManagementEnabled(true);
conf_1.setClustered(true);
conf_1.getAcceptorConfigurations().add(acceptorConfig);
conf_1.getQueueConfigurations().add(targetQueueConfig);
Configuration conf_0 = createBasicConfig();
conf_0.setSecurityEnabled(false);
conf_0.setJMXManagementEnabled(true);
conf_0.setClustered(true);
conf_0.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
conf_0.getConnectorConfigurations().put(connectorConfig.getName(), connectorConfig);
conf_0.getQueueConfigurations().add(sourceQueueConfig);
conf_0.getBridgeConfigurations().add(bridgeConfig);
server_1 = HornetQServers.newHornetQServer(conf_1, MBeanServerFactory.createMBeanServer(), false);
server_1.start();
server_0 = HornetQServers.newHornetQServer(conf_0, mbeanServer, false);
server_0.start();
}
@Override
protected void tearDown() throws Exception
{
server_0.stop();
server_1.stop();
server_0 = null;
bridgeConfig = null;
server_1 = null;
super.tearDown();
}
protected BridgeControl createBridgeControl(final String name, final MBeanServer mbeanServer) throws Exception
{
return ManagementControlHelper.createBridgeControl(name, mbeanServer);
}
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}