com.ttdev.wicketpagetest.MockableCDIBeanInjector Maven / Gradle / Ivy
package com.ttdev.wicketpagetest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import org.apache.wicket.Component;
import org.apache.wicket.application.ComponentInstantiationListenerCollection;
import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.cdi.CdiConfiguration;
import org.apache.wicket.cdi.CdiContainer;
import org.apache.wicket.injection.IFieldValueFactory;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.util.lang.Args;
import org.jboss.weld.environment.servlet.Listener;
public class MockableCDIBeanInjector extends MockableBeanInjector {
private static MockableCDIBeanInjector instance;
public static void injectBeans(Object object) {
instance.inject(object);
}
public MockableCDIBeanInjector(
final IComponentInstantiationListener listener) {
super(Inject.class, new Injector() {
@Override
public void inject(Object component) {
if (component instanceof Component) {
listener.onInstantiation((Component) component);
} else {
CdiContainer.get().getNonContextualManager()
.inject(component);
}
}
});
}
public static void installInjector(final WebApplication webapp) {
BeanManager manager = (BeanManager) webapp.getServletContext()
.getAttribute(Listener.BEAN_MANAGER_ATTRIBUTE_NAME);
new CdiConfiguration(manager).configure(webapp);
ComponentInstantiationListenerCollection listeners = webapp
.getComponentInstantiationListeners();
Iterator iterator = listeners
.iterator();
IComponentInstantiationListener instantiationListener = null;
while (iterator.hasNext()) {
instantiationListener = iterator.next();
if (instantiationListener.getClass().getSimpleName()
.equals("ComponentInjector")) {
break;
}
}
Args.notNull(instantiationListener, "instantiationListener");
listeners.remove(instantiationListener);
MockableBeanInjector.installInjector(webapp,
instance = new MockableCDIBeanInjector(instantiationListener));
}
@Override
public void inject(Object object) {
injectOriginals(object);
injectMocks(object);
}
@Override
protected void inject(Object object, IFieldValueFactory factory) {
final Class> clazz = object.getClass();
Field[] fields = findFields(clazz, factory);
for (final Field field : fields) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
try {
Object value = factory.getFieldValue(field, object);
if (value != null) {
field.set(object, value);
}
} catch (IllegalArgumentException e) {
throw new RuntimeException("error while injecting object ["
+ object.toString() + "] of type ["
+ object.getClass().getName() + "]", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("error while injecting object ["
+ object.toString() + "] of type ["
+ object.getClass().getName() + "]", e);
}
}
}
private Field[] findFields(Class> clazz, final IFieldValueFactory factory) {
List matched = new ArrayList();
while (clazz != null) {
Field[] fields = clazz.getDeclaredFields();
for (final Field field : fields) {
if (factory.supportsField(field)) {
matched.add(field);
}
}
clazz = clazz.getSuperclass();
}
return matched.toArray(new Field[matched.size()]);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy