/** * Test that lookup beans have their default instance created after registration. */ @Test public void test_register_singleton() { Singleton singleton = config.get(Singleton.class); assertNotNull(singleton); }
public void afterDeploymentValidation(@Observes AfterDeploymentValidation event) { ConfigContext ctx = CDI.current().select(ConfigContext.class).get(); for (Class<?> cls : schemas){ ctx.register(cls); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void runtimeAllAndAssert(Class clazz, Object... objects) throws AssertionFailedError { List<Object> reslut = config.list(clazz); assertReflectionEquals(objects, reslut, LENIENT_ORDER); } }
@Before public void setupDefaultConfigData() { sp1 = new SingletonParent(); s1 = new Singleton(); c1 = getChild("c1"); c2 = getChild("c2"); p1 = getParent("p1"); p1.add(c2, c1); p1.set(c1); p1.put(c1); p1.put(c2); p2 = getParent("p2"); p2.add(c1, c2); p2.set(c2); p2.put(c1); p2.put(c2); g1 = getGrandfather("g1"); g1.add(p1, p2); g2 = getGrandfather("g2"); g2.add(p1, p2); g2.put(p1); config.registerObserver(observer); config.register(Person.class, Grandfather.class, Parent.class, Child.class, Singleton.class, SingletonParent.class, JSR303Validation.class); if (defaultBeans == null) { // toBeans steals quite a bit of performance when having larger hierarchies. defaultBeans = ImmutableList.copyOf(toBeans(c1, c2, p1, p2, g1, g2)); } }
/** * Test that lookup beans have their default instance created after registration. */ @Test public void test_register_singleton() { Singleton singleton = config.get(Singleton.class); assertNotNull(singleton); }
@Before public void setup() { config.register(Grandfather.class, Parent.class, Child.class);
@SuppressWarnings({ "unchecked", "rawtypes" }) private void runtimeAllAndAssert(Class clazz, Object... objects) throws AssertionFailedError { List<Object> reslut = config.list(clazz); assertReflectionEquals(objects, reslut, LENIENT_ORDER); }
private void runtimeGetAndAssert(Object object, Bean bean) throws AssertionFailedError { Object o = config.get(bean.getId().getInstanceId(), object.getClass()).get(); assertReflectionEquals(object, o, LENIENT_ORDER); }
config.register(Grandfather.class, Parent.class, Child.class, Singleton.class, SingletonParent.class, JSR303Validation.class); if (defaultBeans == null) {
private void runtimeGetAndAssert(Object object, Bean bean) throws AssertionFailedError { Object o = config.get(bean.getId().getInstanceId(), object.getClass()); assertReflectionEquals(object, o, LENIENT_ORDER); }
@Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { Bean<T> bean = (Bean<T>) contextual; cctx = creationalContext; if (ctx == null) { ctx = CDI.current().select(ConfigContext.class).get(); } final Object object; if (cache.containsKey(bean.getBeanClass())) { object = cache.get(bean.getBeanClass()); } else { object = ctx.get(bean.getBeanClass()); cache.put(bean.getBeanClass(), object); } return (T) object; }
@Override public <T> T get(Contextual<T> contextual) { if (cctx == null) { return null; } if (ctx == null) { ctx = CDI.current().select(ConfigContext.class).get(); } Bean<T> bean = (Bean<T>) contextual; final Object object; if (cache.containsKey(bean.getBeanClass())) { object = cache.get(bean.getBeanClass()); } else { object = ctx.get(bean.getBeanClass()); cache.put(bean.getBeanClass(), object); } return (T) object; }
@Test public void test_context_injection() { CdiSingletonConfig config = new CdiSingletonConfig("newvalue"); Bean bean = toBean(config); admin.create(bean); admin.set(bean); BeanId id = BeanId.createSingleton(CdiSingletonConfig.class.getSimpleName()); bean = admin.get(id).get(); assertThat(bean.getValues("value").get(0), is("newvalue")); config = this.config.get(CdiSingletonConfig.class); assertThat(config.getValue(), is("newvalue")); } }
@Produces public ServerSpecificConfigProxy<T> produceServerConfigProxy(InjectionPoint ip){ Class<?> declaringClass = ip.getMember().getDeclaringClass(); Class<?> configCls = getParameterizedType(declaringClass, ip.getAnnotated().getBaseType()); try { Optional<?> optionalServer = config.get(serverName.getName(), configCls); if (optionalServer.isPresent()) { return new ServerSpecificConfigProxy(optionalServer.get()); } else { log.debug("Config instance {} of type {} not found", serverName.getName(), configCls); Object config = newInstance(configCls, serverName.getName()); return new ServerSpecificConfigProxy(config); } } catch (AbortRuntimeException e) { if (e.getEvent().getCode() != Events.CFG304){ throw e; } log.debug("Config instance {} of type {} not found", serverName.getName(), configCls); Object config = newInstance(configCls, serverName.getName()); return new ServerSpecificConfigProxy(config); } }
/** * Test that lookup references are automatically assigned. */ @Test public void test_singleton_references() { // provision a bean without the lookup reference. Bean singletonParent = toBean(sp1); admin.create(singletonParent); // assert that the lookup reference is set for config SingletonParent parent = config.get(SingletonParent.class); assertNotNull(parent.getSingleton()); // assert that the lookup is available from admin but // references, however, is not set because it does not exist Bean result = admin.get(singletonParent.getId()).get(); assertNotNull(result); }
/** * Test that lookup references are automatically assigned without needing to provision them * from admin context. */ @Test public void test_singleton_references() { // provision a bean without the lookup reference. Bean singletonParent = toBean(sp1); admin.create(singletonParent); // asert that the lookup reference is set for config SingletonParent parent = config.get(singletonParent.getId().getInstanceId(), SingletonParent.class).get(); assertNotNull(parent.getSingleton()); // assert that the lookup reference is set for admin Bean result = admin.get(singletonParent.getId()).get(); BeanId singletonId = result.getFirstReference("lookup"); assertThat(singletonId, is(s1.getBeanId())); assertThat(singletonId.getBean(), is(toBean(s1))); }