// The classLoader will go away only when these following variables are released
ClassLoader loader = newClassLoader();
Class<?> beanClass = loader.loadClass(className);
Object bean = beanClass.newInstance();
WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
// -----------------------------------------------------------------------------
WeakReference<ClassLoader> someRef = new WeakReference<ClassLoader>(loader);
// Sanity checks only
assertNotNull("ClassLoader is null", loader);
assertNotNull("BeanClass is null", beanClass);
assertNotSame("ClassLoaders should be different..", getClass().getClassLoader(), beanClass.getClassLoader());
assertSame("BeanClass ClassLoader incorrect", beanClass.getClassLoader(), loader);
// if you comment the following line, the testcase will work, and the ClassLoader will be released.
// That proves that nothing is wrong with the test, and WrapDynaClass is holding a reference
assertEquals("initialValue", wrapDynaBean.get("name"));
// this should make the reference go away.
loader = null;
beanClass = null;
bean = null;