/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt 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 test.implementation.objectname;

import junit.framework.TestCase;

import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;
import java.util.Hashtable;

/**
 * Tests wildcards in the hashtable property constructor <p>
 *
 * @author  <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
 */
public class ObjectNameTestCase
  extends TestCase
{
   // Attributes ----------------------------------------------------------------

   // Constructor ---------------------------------------------------------------

   /**
    * Construct the test
    */
   public ObjectNameTestCase(String s)
   {
      super(s);
   }

   // Tests --------------------------------------------------------------------

   /**
    * Test wild card in property this should work
    */
   public void testWildCardOneProperty()
      throws Exception
   {
      Hashtable ht = new Hashtable();
      ht.put("key", "value");
      ht.put("*", "*");
      ObjectName name = null;
      try
      {
         name = new ObjectName("domain", ht);
      }
      catch (MalformedObjectNameException e)
      {
         fail("Should accept *,* in properties");
      }
      if (name.isPattern() == false)
         fail("Name should be a pattern with properties: \n" + ht);
      if (name.isPropertyPattern() == false)
         fail("Name should be a property pattern with properties: \n" + ht);
      assertEquals("key=value,*", name.getCanonicalKeyPropertyListString());
   }

   /**
    * Test wild card in property this should work
    */
   public void testWildCardNoProperty()
      throws Exception
   {
      Hashtable ht = new Hashtable();
      ht.put("*", "*");
      ObjectName name = null;
      try
      {
         name = new ObjectName("domain", ht);
      }
      catch (MalformedObjectNameException e)
      {
         fail("Should accept *,* in properties");
      }
      if (name.isPattern() == false)
         fail("Name should be a pattern with properties: \n" + ht);
      if (name.isPropertyPattern() == false)
         fail("Name should be a property pattern with properties: \n" + ht);
      assertEquals("*", name.getCanonicalKeyPropertyListString());
   }

   /**
    * Test wild card in key only
    */
   public void testWildCardKeyOnly()
      throws Exception
   {
      Hashtable ht = new Hashtable();
      ht.put("*", "value");
      boolean caught = false;
      try
      {
         new ObjectName("domain", ht);
      }
      catch (MalformedObjectNameException e)
      {
         caught = true;
      }
      assertTrue(caught);
   }

   /**
    * Test wild card in value only
    */
   public void testWildCardValueOnly()
      throws Exception
   {
      Hashtable ht = new Hashtable();
      ht.put("key", "*");
      boolean caught = false;
      try
      {
         new ObjectName("domain", ht);
      }
      catch (MalformedObjectNameException e)
      {
         caught = true;
      }
      assertTrue(caught);
   }
}
