001
014
015 package com.liferay.portal.test.runners;
016
017 import com.liferay.portal.aspectj.WeavingClassLoader;
018 import com.liferay.portal.kernel.process.ClassPathUtil;
019 import com.liferay.portal.kernel.test.NewClassLoaderJUnitTestRunner;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.test.AdviseWith;
023
024 import java.io.File;
025
026 import java.lang.reflect.Method;
027
028 import java.net.MalformedURLException;
029 import java.net.URL;
030
031 import org.aspectj.lang.annotation.Aspect;
032
033 import org.junit.runners.model.FrameworkMethod;
034 import org.junit.runners.model.InitializationError;
035
036
039 public class AspectJMockingNewClassLoaderJUnitTestRunner
040 extends NewClassLoaderJUnitTestRunner {
041
042 public AspectJMockingNewClassLoaderJUnitTestRunner(Class<?> clazz)
043 throws InitializationError {
044
045 super(clazz);
046 }
047
048 @Override
049 protected ClassLoader createClassLoader(FrameworkMethod frameworkMethod) {
050 AdviseWith adviseWith = frameworkMethod.getAnnotation(AdviseWith.class);
051
052 if (adviseWith == null) {
053 return super.createClassLoader(frameworkMethod);
054 }
055
056 Class<?>[] adviceClasses = adviseWith.adviceClasses();
057
058 if (ArrayUtil.isEmpty(adviceClasses)) {
059 return super.createClassLoader(frameworkMethod);
060 }
061
062 for (Class<?> adviceClass : adviceClasses) {
063 Aspect aspect = adviceClass.getAnnotation(Aspect.class);
064
065 if (aspect == null) {
066 throw new IllegalArgumentException(
067 "Class " + adviceClass.getName() + " is not an aspect");
068 }
069 }
070
071 String jvmClassPath = ClassPathUtil.getJVMClassPath(true);
072
073 URL[] urls = null;
074
075 try {
076 urls = ClassPathUtil.getClassPathURLs(jvmClassPath);
077 }
078 catch (MalformedURLException murle) {
079 throw new RuntimeException(murle);
080 }
081
082 String dumpDirName = System.getProperty("junit.aspectj.dump");
083
084 Method method = frameworkMethod.getMethod();
085
086 Class<?> clazz = method.getDeclaringClass();
087
088 String className = clazz.getName();
089
090 File dumpDir = new File(
091 dumpDirName,
092 className.concat(StringPool.PERIOD).concat(method.getName()));
093
094 return new WeavingClassLoader(urls, adviceClasses, dumpDir);
095 }
096
097 }