/*
 * 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 org.jboss.test.classloader.test;

import java.io.File;
import java.net.URL;

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

import org.jboss.mx.loading.UnifiedLoaderRepository3;
import org.jboss.test.util.ClassMover;
import org.apache.log4j.Logger;

/** Basic tests of the org.jboss.mx.loading.* classes
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.5 $
 */
public class BasicLoaderUnitTestCase extends TestCase
{
   static Logger log = Logger.getLogger(BasicLoaderUnitTestCase.class);

   public BasicLoaderUnitTestCase(String name)
   {
      super(name);
   }

   /** Test the UnifiedLoaderRepository for multi-threaded class loading
    */
   public void testNoClassDefFoundError() throws Exception
   {
      UnifiedLoaderRepository3 ulr = new UnifiedLoaderRepository3();
      String libDir = System.getProperty("jbosstest.deploy.dir");
      File cwd = new File(libDir);
      URL cp = new URL(cwd.toURL(), "../classes");
      log.info("Using cp: " + cp);
      ClassLoader loader = ulr.newClassLoader(cp, true);

      File bakFile = null;
      try
      {
         bakFile = ClassMover.move("org.jboss.test.classloader.test.ex.BaseException");
         loader.loadClass("org.jboss.test.classloader.test.ex.DerivedException");
         fail("Should not have loaded DerivedException");
      }
      catch (ClassNotFoundException e)
      {
         String msg = e.getMessage();
         log.info("CNFE msg: " + msg, e);
         int index = msg.indexOf("BaseException");
         assertTrue("Saw BaseException in CNFE: (" + index + "), msg=" + msg, index > 0);
      }
      finally
      {
         if (bakFile != null)
            ClassMover.restore(bakFile);
      }

   }

   public void testNoClassDefFoundError2() throws Exception
   {
      UnifiedLoaderRepository3 ulr = new UnifiedLoaderRepository3();
      String libDir = System.getProperty("jbosstest.deploy.dir");
      File cwd = new File(libDir);
      URL cp = new URL(cwd.toURL(), "../classes");
      log.info("Using cp: " + cp);
      ClassLoader loader = ulr.newClassLoader(cp, true);

      File bakFile = null;
      try
      {
         bakFile = ClassMover.move("org.jboss.test.classloader.test.ex.BaseException");
         Class c = loader.loadClass("org.jboss.test.classloader.test.ex.ExThrower");
         c.getMethods();
         fail("Should not have gotten ExThrower methods");
      }
      catch (NoClassDefFoundError e)
      {
         String msg = e.getMessage();
         log.info("CNFE msg: " + msg, e);
         int index = msg.indexOf("BaseException");
         assertTrue("Saw BaseException in CNFE: (" + index + "), msg=" + msg, index > 0);
      }
      finally
      {
         if (bakFile != null)
            ClassMover.restore(bakFile);
      }

   }

   public void testDeadlockScenario1() throws Exception
   {
      String deployDir = System.getProperty("jbosstest.deploy.dir");
      File libDir = new File(deployDir);
      log.info("Using cp: " + libDir);
      DeadlockTests32 test = new DeadlockTests32(libDir);
      test.testDeadLock();
   }

   public void testDeadlockScenario2() throws Exception
   {
      String deployDir = System.getProperty("jbosstest.deploy.dir");
      File libDir = new File(deployDir);
      log.info("Using cp: " + libDir);
      DeadlockTests32 test = new DeadlockTests32(libDir);
      test.testDeadLockAndCircularity();
   }

   public static Test suite()
   {
      TestSuite suite = new TestSuite();
      suite.addTest(new BasicLoaderUnitTestCase("testNoClassDefFoundError"));
      suite.addTest(new BasicLoaderUnitTestCase("testNoClassDefFoundError2"));
      suite.addTest(new BasicLoaderUnitTestCase("testDeadlockScenario1"));
      suite.addTest(new BasicLoaderUnitTestCase("testDeadlockScenario2"));
      return suite;
   }
}
