/*
 * 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.compliance.loading;

import java.net.URL;
import java.util.Set;

import javax.management.Attribute;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.ServiceNotFoundException;
import javax.management.loading.DefaultLoaderRepository;
import javax.management.loading.MLet;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

/**
 * MLet tests.
 *
 * @author <a href="mailto:jplindfo@helsinki.fi">Juha Lindfors</a>
 */
public class MLetTEST extends TestCase
{
   public MLetTEST(String s)
   {
      super(s);
   }

   public void testCreateAndRegister() throws Exception
   {
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
   
      try
      {
         server.registerMBean(mlet, name);
      }
      finally
      {
         server.unregisterMBean(name);
      }
   }
   
   public void testMLetLoadClassFromURLInConstructor() throws Exception
   {
      // NOTE: 
      // the urls used here are relative to the location of the build.xml
      
      final String MBEANS_URL = "file:./output/etc/test/compliance/loading/MyMBeans.jar";
      
      // make sure the class is not available
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial");
         
         fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }

      MBeanServer server = MBeanServerFactory.createMBeanServer();
      ObjectName name = new ObjectName("test:name=mlet");
      MLet mlet = new MLet(new URL[] { new URL(MBEANS_URL)} );
      
      // make sure the class is not available
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial");
         
         fail("class test.compliance.loading.support.Trivial found in CL repository after MLet construction.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }

      try
      {      
         server.registerMBean(mlet, name);
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial");
      }
      finally
      {
         server.unregisterMBean(name); 
      }
      // make sure the class is not available
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial");
         
         fail("class test.compliance.loading.support.Trivial was still found in CL repository.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }
   }
   
   public void testBasicMLetFileLoad() throws Exception
   {
      // NOTE: 
      // the urls used here are relative to the location of the build.xml
      
      final String MLET_URL = "file:./output/etc/test/compliance/loading/BasicConfig.mlet";
      
      // make sure the classes are loaded from mlet, not system cl
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial");
         
         fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }
      // make sure the classes are loaded from mlet, not system cl
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial2");
         
         fail("class test.compliance.loading.support.Trivial2 was already found in CL repository.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }
     
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      server.registerMBean(mlet, name);
      
      Set mbeans = (Set)server.invoke(name, "getMBeansFromURL", 
                                      new Object[] { MLET_URL },
                                      new String[] { String.class.getName() }
      );

      try 
      {         
         assertTrue(server.isRegistered(new ObjectName("test:name=Trivial")));
         assertTrue(server.isRegistered(new ObjectName("test:name=Trivial2")));
      }
      catch (AssertionFailedError e)
      {
         URL[] urls = mlet.getURLs();
         URL url = null;
         
         if (urls != null && urls.length > 0)
            url = urls[0];
         
         fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" +
              MLET_URL + "' resulting into MLET codebase URL '" + url + "' and therefore fails " +
              "to load the required classes from the Java archive (MyMBeans.jar)");
      }

      assertTrue(server.getMBeanInfo(new ObjectName("test:name=Trivial")) != null);
      assertTrue(server.getMBeanInfo(new ObjectName("test:name=Trivial2")) != null);
      
      assertTrue(server.getAttribute(new ObjectName("test:name=Trivial2"), "Something").equals("foo"));

      server.invoke(new ObjectName("test:name=Trivial"), "doOperation",
      new Object[] { "Test" },
      new String[] { String.class.getName() }
      );
      
      server.invoke(new ObjectName("test:name=Trivial2"), "doOperation",
      new Object[] { "Test" },
      new String[] { String.class.getName() }
      );
   }

   /**
    * Make sure the versioning MLet installer won't replace MBeans that were
    * not registered with version information.
    */
   public void testConflictingMLetFileLoad() throws Exception
   {
      // NOTE: 
      // the urls used here are relative to the location of the build.xml
      
      final String MLET_URL1 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet";
      final String MLET_URL2 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet";
      
      // make sure the classes are loaded from mlet, not system cl
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial3");
         
         fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }
      // make sure the classes are loaded from mlet, not system cl
      try
      {
         DefaultLoaderRepository.loadClass("test.compliance.loading.support.Trivial4");
         
         fail("class test.compliance.loading.support.Trivial2 was already found in CL repository.");
      }
      catch (ClassNotFoundException e)
      {
         // expected
      }
     
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      server.registerMBean(mlet, name);
      
      Set mbeans = (Set)server.invoke(name, "getMBeansFromURL", 
                                      new Object[] { MLET_URL1 },
                                      new String[] { String.class.getName() }
      );

      ObjectName oname = new ObjectName("test:name=Trivial2"); 
      server.setAttribute(oname, new Attribute("Something", "Something"));
      
      mlet = new MLet();
      name = new ObjectName("test:name=mlet2");
      server.registerMBean(mlet, name);
      
      mbeans = (Set)server.invoke(name, "getMBeansFromURL", 
                               new Object[] { MLET_URL2 },
                               new String[] { String.class.getName() }
      );
      
      oname = new ObjectName("test:name=Trivial2");
      String value = (String)server.getAttribute(oname, "Something");

      assertTrue(value.equals("Something")); 
   }

      
   public void testMalformedURLLoad() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      try
      {
         server.registerMBean(mlet, name);
         
         server.invoke(name, "getMBeansFromURL",
         new Object[] { "output/etc/test/compliance/loading/BasicConfig.mlet" },
         new String[] { String.class.getName() }
         );
         
         // should not reach here
         fail("FAILS IN RI: Malformed URL in getMBeansURL() should result in ServiceNotFoundException thrown.");
      }
      catch (AssertionFailedError e)
      {
         // defensive: in case assertXXX() or fail() are later added
         throw e;
      }
      catch (MBeanException e)
      {
         assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }
   
   public void testMissingMLetTagInLoad() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      try
      {
         server.registerMBean(mlet, name);
         
         server.invoke(name, "getMBeansFromURL",
         new Object[] { "file:./output/etc/test/compliance/loading/MissingMLET.mlet" },
         new String[] { String.class.getName() }
         );
         
         // should not reach here
         fail("MLet text file missing the MLET tag should result in ServiceNotFoundException thrown.");
      }
      catch (AssertionFailedError e)
      {
         // defensive: in case assertXXX() or fail() are added later
         throw e;
      }
      catch (MBeanException e)
      {
         assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }

   public void testMissingMandatoryArchiveTagInLoad() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      try
      {
         server.registerMBean(mlet, name);
         
         server.invoke(name, "getMBeansFromURL",
         new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryArchive.mlet" },
         new String[] { String.class.getName() }
         );
         
         // should not reach here
         fail("MLet text file missing mandatory ARCHIVE attribute should result in ServiceNotFoundException thrown.");
      }
      catch (AssertionFailedError e)
      {
         // defensive: in case assertXXX() or fail() are added later
         throw e;
      }
      catch (MBeanException e)
      {
         assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }
   
   public void testMissingMandatoryCodeTagInLoad() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      try
      {
         server.registerMBean(mlet, name);
         
         server.invoke(name, "getMBeansFromURL",
         new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryCode.mlet" },
         new String[] { String.class.getName() }
         );
         
         // should not reach here
         fail("MLet text file missing mandatory CODE attribute should result in ServiceNotFoundException thrown.");
      }
      catch (AssertionFailedError e)
      {
         // defensive: in case assertXXX() or fail() are added later
         throw e;
      }
      catch (MBeanException e)
      {
         assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }         
   
   public void testArchiveListInMLet() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      final String MLET_URL = "file:./output/etc/test/compliance/loading/ArchiveList.mlet";
   
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");

      try
      {
         server.registerMBean(mlet, name);
      
         server.invoke(name, "getMBeansFromURL",
         new Object[] { MLET_URL },
         new String[] { String.class.getName() }
         );
      
         Class c = null;
      
         try
         {
            c  = mlet.loadClass("test.compliance.loading.support.AClass");
         }
         catch (ClassNotFoundException e)
         {
            URL[] urls = mlet.getURLs();
            fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" +
                 MLET_URL + "' resulting into MLET codebase URL '" + urls[0] + "' and therefore fails " +
                 "to load the required classes from the Java archive.");            
         }
      
         Object o = c.newInstance();
      
         server.setAttribute(new ObjectName("test:name=AnotherTrivial"), new Attribute("Something", o));
         o = server.getAttribute(new ObjectName("test:name=AnotherTrivial"), "Something");
      
         assertTrue(o.getClass().isAssignableFrom(c));
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }
   
   public void testUnexpectedEndOfFile() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      try
      {
         server.registerMBean(mlet, name);
         
         server.invoke(name, "getMBeansFromURL",
         new Object[] { "file:./output/etc/test/compliance/loading/UnexpectedEnd.mlet" },
         new String[] { String.class.getName() }
         );
         
         // should not reach here
         fail("Unexpected end of file from mlet text file did not cause an exception.");
      }
      catch (AssertionFailedError e)
      {
         throw e;
      }
      catch (MBeanException e)
      {
         assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }
   
   public void testMissingEndMLetTag() throws Exception
   {
      // NOTE:
      // the urls used here are relative to the location of the build.xml
      
      MBeanServer server = MBeanServerFactory.createMBeanServer();
      MLet mlet = new MLet();
      ObjectName name = new ObjectName("test:name=mlet");
      try
      {
         server.registerMBean(mlet, name);
         
         server.invoke(name, "getMBeansFromURL",
         new Object[] { "file:./output/etc/test/compliance/loading/MissingEndTag.mlet" },
         new String[] { String.class.getName() }
         );
      
         assertTrue(!server.isRegistered(new ObjectName("test:name=Trivial")));
      }
      catch (AssertionFailedError e)
      {
         throw e;
      }
      catch (MBeanException e)
      {
         assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
      }
      finally
      {
         try
         {
            server.unregisterMBean(name);
         }
         catch (Exception ignored) {}
      }
   }
   
}
