use of org.apache.openejb.arquillian.common.mockito.MockitoEnricher in project tomee by apache.
the class OpenEJBInjectionEnricher method enrich.
@Override
public void enrich(final Object testInstance) {
if (!SystemInstance.isInitialized()) {
return;
}
new MockitoEnricher().enrich(testInstance);
final AppContext context = appContext.get();
if (context != null) {
OpenEJBEnricher.enrich(testInstance, context);
} else {
// try to enrich with all for deployment at startup feature - only once context can be used in a class
final List<AppContext> appContexts = SystemInstance.get().getComponent(ContainerSystem.class).getAppContexts();
final Class<?> clazz = testInstance.getClass();
for (final AppContext appContext : appContexts) {
if (appContext.getWebBeansContext() == null) {
continue;
}
try {
final BeanManager bm = appContext.getWebBeansContext().getBeanManagerImpl();
final AnnotatedType<?> at = bm.createAnnotatedType(clazz);
bm.createInjectionTarget(at);
final CreationalContext<Object> cc = bm.createCreationalContext(null);
OWBInjector.inject(bm, testInstance, cc);
cc.release();
} catch (final Exception e) {
// no-op
}
}
}
}
use of org.apache.openejb.arquillian.common.mockito.MockitoEnricher in project tomee by apache.
the class OpenEJBEnricher method enrich.
public static void enrich(final Object testInstance, final AppContext appCtx) {
// don't rely on arquillian since this enrichment should absolutely be done before the following ones
new MockitoEnricher().enrich(testInstance);
AppContext ctx = appCtx;
if (ctx == null) {
ctx = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE);
if (ctx == null) {
return;
}
}
final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
final WebBeansContext appWBC = ctx.getWebBeansContext();
final BeanManagerImpl bm = appWBC == null ? null : appWBC.getBeanManagerImpl();
boolean ok = false;
for (final WebContext web : ctx.getWebContexts()) {
final WebBeansContext webBeansContext = web.getWebBeansContext();
if (webBeansContext == null) {
continue;
}
final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
if (webBeansContext != appWBC && webAppBm.isInUse()) {
try {
doInject(testInstance, context, webAppBm);
ok = true;
break;
} catch (final Exception e) {
// no-op, try next
}
}
}
if (bm != null && bm.isInUse() && !ok) {
try {
doInject(testInstance, context, bm);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
if (RuntimeException.class.isInstance(e)) {
throw RuntimeException.class.cast(e);
}
throw new OpenEJBRuntimeException(e);
}
}
if (context != null) {
final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
final ThreadContext oldContext = ThreadContext.enter(callContext);
try {
final InjectionProcessor processor = new InjectionProcessor<>(testInstance, context.getInjections(), context.getJndiContext());
processor.createInstance();
} catch (final OpenEJBException e) {
// ignored
} finally {
ThreadContext.exit(oldContext);
}
}
}
Aggregations