Search in sources :

Example 1 with MockitoEnricher

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
            }
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) AppContext(org.apache.openejb.AppContext) BeanManager(javax.enterprise.inject.spi.BeanManager) MockitoEnricher(org.apache.openejb.arquillian.common.mockito.MockitoEnricher)

Example 2 with MockitoEnricher

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);
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenEJBException(org.apache.openejb.OpenEJBException) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) InjectionProcessor(org.apache.openejb.InjectionProcessor) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) MockitoEnricher(org.apache.openejb.arquillian.common.mockito.MockitoEnricher)

Aggregations

AppContext (org.apache.openejb.AppContext)2 MockitoEnricher (org.apache.openejb.arquillian.common.mockito.MockitoEnricher)2 ContainerSystem (org.apache.openejb.spi.ContainerSystem)2 BeanManager (javax.enterprise.inject.spi.BeanManager)1 BeanContext (org.apache.openejb.BeanContext)1 InjectionProcessor (org.apache.openejb.InjectionProcessor)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 WebContext (org.apache.openejb.core.WebContext)1 WebBeansContext (org.apache.webbeans.config.WebBeansContext)1 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)1