/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.cache.mgmt;

import java.util.Set;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Test for http://jira.jboss.com/jira/browse/JBCACHE-1048
 * 
 * @author Brian Stansberry
 */
public class ChannelRegistrationTest extends TestCase
{
   private static final String CLUSTER_NAME = "JBCACHE-1048";    

   private MBeanServer m_server;

   TreeCache cache = null;
   boolean optimistic = false;
   /**
    * 
    */
   public ChannelRegistrationTest()
   {
      super();
   }

   /**
    * @param arg0
    */
   public ChannelRegistrationTest(String arg0)
   {
      super(arg0);
   }

   public static Test suite()
   {
      return new TestSuite(ChannelRegistrationTest.class);
   }
   protected void setUp() throws Exception
   {
      super.setUp();
      m_server = MBeanServerFactory.createMBeanServer();
      cache = createCache(CLUSTER_NAME);
   }

   protected void tearDown() throws Exception
   {
      m_server = null;
      
      super.tearDown();
      if(cache != null)
      {
         cache.destroyService();
         cache = null;
      }
   }

   private TreeCache createCache(String clusterName) throws Exception
   {
      TreeCache cache = new TreeCache();
      PropertyConfigurator config=new PropertyConfigurator();
      config.configure(cache, "META-INF/replSync-service.xml");
      cache.setCacheMode(TreeCache.REPL_SYNC);
      cache.setClusterName(clusterName);
      
      return cache;
   }
   
   public void testChannelRegistration() throws Exception
   {
      m_server.registerMBean(cache, new ObjectName("jboss.cache:test=ChannelRegistrationTest"));
      
      cache.startService();     
      
      Set channelMBean = getChannelMBeans();
      assertNotNull("Channel mbean registered", channelMBean);
      assertEquals("Only one channel mbean", 1, channelMBean.size()); 
      
      Set protocolMBeans = getProtocolMBeans();
      assertNotNull("Protocol mbeans registered", channelMBean);
      assertTrue("Multiple protocol mbeans", protocolMBeans.size() > 2);
      
      cache.stopService();
      
      channelMBean = getChannelMBeans();
      if (channelMBean != null)
         assertTrue("Channel mbean undeployed", channelMBean.size() == 0); 
      
      protocolMBeans = getProtocolMBeans();
      if (protocolMBeans != null)
         assertTrue("Protocol mbeans undeployed", protocolMBeans.size() == 0); 
      
      
   }
   
   protected Set getChannelMBeans() throws Exception
   {
      ObjectName on = new ObjectName("jboss.jgroups:type=channel,cluster=JBCACHE-1048");
      return m_server.queryNames(on, null);
   }
   
   protected Set getProtocolMBeans() throws Exception
   {
      ObjectName on = new ObjectName("jboss.jgroups:*,type=protocol,cluster=JBCACHE-1048");
      return m_server.queryNames(on, null);
   }

}
