/** * create a bean of class clazz in context scope * @param clazz the clazz of the Bean to be created * @param scope either ApplicationScoped or Dependent * @return the created bean */ public Object create(Class<T> clazz, Class<? extends Annotation> scope) { Bean<?> bean = bm.resolve(bm.getBeans(clazz)); if (bean != null) { Object result = create((Contextual<T>) bean, scope); if (result == null) { throw new RuntimeException("Could not create Bean to be initialized of Class: " + clazz); } return result; } else { throw new RuntimeException("Could not resolve Bean to be initialized of Class: " + clazz); } }
/** * close according to AutoCloseable * * @throws Exception check Exception should not occur. */ @Override public void close() throws Exception { closeIt(); } }
/** * create a bean in context * @param b the Bean as described and found by the weld init. * @param scope either ApplicationScoped or Dependent * @return the created bean */ public Object create(Contextual<T> b, Class<? extends Annotation> scope) { try { final CreationalContext<T> cb = bm.createCreationalContext(b); // assumes the bean will exist only once Context context = bm.getContext(scope); final Object o = context.get(b, cb); creationalContexts.add(cb); return o; } catch (Throwable thw) { getLogger().error("Exception during create of Bean {}", b); getLogger().error("Exception: ", thw); throw new RuntimeException(thw); } }
public void initContexts() throws Exception { System.setProperty("java.naming.factory.initial", "com.oneandone.iocunit.naming.CdiTesterContextFactory"); initialContext = new InitialContext(); final BeanManager beanManager = get(BeanManager.class); initialContext.bind("java:comp/BeanManager", beanManager); this.creationalContexts = new CreationalContexts(beanManager); postStartupActions(); }
public void shutdownWeldIfRunning(boolean ignoreException) throws NamingException { if(weldStarter != null) { logger.trace("----> shutting down Weld"); if(ignoreException) { try { if (creationalContexts != null) { creationalContexts.close(); creationalContexts = null; } if (initialContext != null) { initialContext.close(); initialContext = null; } weldStarter.tearDown(); } catch (Throwable thw) { logger.debug("Ignored {}", thw); } } else { weldStarter.tearDown(); } weldStarter = null; } }
/** * create a bean in context * @param b the Bean as described and found by the weld init. * @param scope either ApplicationScoped or Dependent * @return the created bean */ public Object create(Contextual<T> b, Class<? extends Annotation> scope) { try { final CreationalContext<T> cb = bm.createCreationalContext(b); // assumes the bean will exist only once Context context = bm.getContext(scope); final Object o = context.get(b, cb); creationalContexts.add(cb); return o; } catch (Throwable thw) { getLogger().error("Exception during create of Bean {}", b); getLogger().error("Exception: ", thw); throw new RuntimeException(thw); } }
@Override public void evaluate() throws Throwable { if (startupException != null) { if (frameworkMethodP.getAnnotation(Test.class).expected() == startupException.getClass()) { return; } throw startupException; } System.setProperty("java.naming.factory.initial", "com.oneandone.iocunit.naming.CdiTesterContextFactory"); InitialContext initialContext = new InitialContext(); final BeanManager beanManager = analyzeAndStarter.get(BeanManager.class); initialContext.bind("java:comp/BeanManager", beanManager); try (CreationalContexts creationalContexts = new CreationalContexts(beanManager)) { defaultStatement.evaluate(); } finally { initialContext.close(); analyzeAndStarter.tearDown(); } } };
/** * create a bean of class clazz in context scope * @param clazz the clazz of the Bean to be created * @param scope either ApplicationScoped or Dependent * @return the created bean */ public Object create(Class<T> clazz, Class<? extends Annotation> scope) { Bean<?> bean = bm.resolve(bm.getBeans(clazz)); if (bean != null) { Object result = create((Contextual<T>) bean, scope); if (result == null) { throw new RuntimeException("Could not create Bean to be initialized of Class: " + clazz); } return result; } else { throw new RuntimeException("Could not resolve Bean to be initialized of Class: " + clazz); } }
/** * close according to AutoCloseable * * @throws Exception check Exception should not occur. */ @Override public void close() throws Exception { closeIt(); } }
private Object createTestInstance(Class<?> clazz) throws Exception { startupException = null; analyzeAndStarter.shutdownWeldIfRunning(false); if(!analyzeAndStarter.isRunning()) { logger.trace("----> starting up Weld."); startupException = analyzeAndStarter.checkVersion(); System.setProperty("java.naming.factory.initial", "com.oneandone.iocunit.naming.CdiTesterContextFactory"); try { if (startupException == null) analyzeAndStarter.analyzeAndStart(clazz, null); } catch (ClassFormatError e) { startupException = parseClassFormatError(e); } catch (Throwable e) { if(startupException == null) { startupException = e; } } // store info about explicit param injection, either from global settings or from annotation on the test class if(startupException != null) { return clazz.newInstance(); // prepare default, to allow beforeEach to handle exception. } analyzeAndStarter.initContexts(); Object test = analyzeAndStarter.getCreationalContexts().create(clazz, ApplicationScoped.class); logger.trace("---->Found testinstance {}\n", test); return test; } return null; }
/** * Run the action, throwing a {@code Throwable} if anything goes wrong. */ @Override public void evaluate() throws Throwable { if (analyzeAndStarter.weldSetup == null) initWeld(); if (startupException != null) { if (method != null && method.getAnnotation(Test.class).expected() == startupException.getClass()) { return; } throw startupException; } try { analyzeAndStarter.initContexts(); Object test = analyzeAndStarter.getCreationalContexts().create(clazz, ApplicationScoped.class); initWeldFields(test, test.getClass()); next.evaluate(); } finally { analyzeAndStarter.tearDown(); } }