Package org.jboss.test.aop.classpool.test

Source Code of org.jboss.test.aop.classpool.test.ClassPoolTest

/*
* 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.test.aop.classpool.test;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import javassist.ClassPool;

import org.jboss.aop.classpool.AOPClassPoolRepository;
import org.jboss.aop.classpool.BaseClassPoolDomain;
import org.jboss.aop.classpool.ClassPoolDomain;
import org.jboss.aop.classpool.ClassPoolToClassPoolDomainAdapter;
import org.jboss.aop.classpool.DelegatingClassPool;
import org.jboss.aop.classpool.NonDelegatingClassPool;
import org.jboss.test.AbstractTestCaseWithSetup;
import org.jboss.test.AbstractTestDelegate;

/**
*
* @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
* @version $Revision: 1.1 $
*/
public class ClassPoolTest extends AbstractTestCaseWithSetup
{  
   public final static URL JAR_A = getURLRelativeToProjectRoot("target/jboss-aop-asintegration-core-test-classpool-a.jar");
   public final static URL JAR_B = getURLRelativeToProjectRoot("target/jboss-aop-asintegration-core-test-classpool-b.jar");
   public final static String CLASS_A = "org.jboss.test.aop.classpool.support.a.A";
   public final static String CLASS_B = "org.jboss.test.aop.classpool.support.b.B";
  
   //Hard references to classloaders used for pools to avoid them being garbage collected before the test finished
   private final static List<ClassLoader> loaders = new ArrayList<ClassLoader>();
  
   public ClassPoolTest(String name)
   {
      super(name);
   }

   protected static URL getURLRelativeToProjectRoot(String relativePath)
   {
      try
      {
         URL url = ClassPoolTest.class.getProtectionDomain().getCodeSource().getLocation();
         String location = url.toString();
         int index = location.lastIndexOf("/asintegration-core/") + "/asintegration-core/".length();
         location = location.substring(0, index);
        
         location = location + relativePath;
         return new URL(location);
      }
      catch (MalformedURLException e)
      {
         // AutoGenerated
         throw new RuntimeException(e);
      }
   }
  
   public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
   {
      AbstractTestDelegate delegate = new ClassPoolTestDelegate(clazz);
      String property = System.getProperty("jboss.aop.secure", "true");
      boolean enableSecurity = Boolean.valueOf(property).booleanValue();
      delegate.enableSecurity = enableSecurity;
      return delegate;
   }

   protected static ClassPoolDomain createClassPoolDomain(String name, ClassPoolDomain parent, boolean parentFirst)
   {
      ClassPoolDomain domain = new BaseClassPoolDomain(name, parent, parentFirst);
      return domain;
   }
  
   protected static DelegatingClassPool createDelegatingClassPool(ClassPoolDomain domain, URL...urls) throws ClassNotFoundException
   {
      return new DelegatingClassPool(domain, createClassLoader(urls), ClassPool.getDefault(), AOPClassPoolRepository.getInstance());
   }

   protected static ClassPoolDomain createNonDelegatingClassPoolDomain(URL...urls) throws ClassNotFoundException
   {
      NonDelegatingClassPool pool = new NonDelegatingClassPool(createClassLoader(urls), ClassPool.getDefault(), AOPClassPoolRepository.getInstance(), true);
      return new ClassPoolToClassPoolDomainAdapter(pool);
   }
  
   protected static ClassLoader createClassLoader(URL...urls) throws ClassNotFoundException
   {
      ClassLoader loader = new URLClassLoader(urls);
      for (URL url : urls)
      {
         if (url == JAR_A)
         {
            loader.loadClass(CLASS_A);
         }
         else if (url == JAR_B)
         {
            loader.loadClass(CLASS_B);
         }
           
      }
      //Once all the classes in the loader have been loaded, it seems to clear the URLs. Work around this
      //by recreating the loader since we need the urls in the URLClassLoaderIsLocalResourcePlugin
      loader = new URLClassLoader(urls);
      //Add hard reference to loader
      loaders.add(loader);
      return loader;
   }
  
   @Override
   protected void setUp() throws Exception
   {
      super.setUp();
//      enableTrace("org.jboss.aop.classpool");
   }

   protected void assertCannotLoadClass(ClassLoader loader, String classname)
   {
      try
      {
         loader.loadClass(classname);
         fail("Should not have been able to load " + classname);
      }
      catch(ClassNotFoundException expected)
      {
      }
   }
  
   /**
    * The test classes should not be on the launcher classpath
    */
   public void testClassesNotOnClasspath()
   {
      assertCannotLoadClass(this.getClass().getClassLoader(), CLASS_A);
      assertCannotLoadClass(this.getClass().getClassLoader(), CLASS_B);
   }}
TOP

Related Classes of org.jboss.test.aop.classpool.test.ClassPoolTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.