Java Code Examples for org.apache.deltaspike.core.api.provider.BeanProvider#injectFields()
The following examples show how to use
org.apache.deltaspike.core.api.provider.BeanProvider#injectFields() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CdiTestRunner.java From deltaspike with Apache License 2.0 | 6 votes |
@Override protected Object createTest() throws Exception { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Class<?> type = getTestClass().getJavaClass(); Set<Bean<?>> beans = beanManager.getBeans(type); Object result; if (!USE_TEST_CLASS_AS_CDI_BEAN || beans == null || beans.isEmpty()) { result = super.createTest(); BeanProvider.injectFields(result); //fallback to simple injection } else { Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans); CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean); result = beanManager.getReference(bean, type, creationalContext); } return result; }
Example 2
Source File: MockedRequestScopedBeanWithInjectionTest.java From deltaspike with Apache License 2.0 | 6 votes |
@Test public void mixedMocksWithInjection1() { SessionScopedBean mockedSessionScopedBean = mock(SessionScopedBean.class); when(mockedSessionScopedBean.getCount()).thenReturn(7); mockManager.addMock(mockedSessionScopedBean); RequestScopedBean mockedRequestScopedBean = new MockedRequestScopedBeanWithInjection(); BeanProvider.injectFields(mockedRequestScopedBean); mockManager.addMock(mockedRequestScopedBean); Assert.assertEquals(7, requestScopedBean.getCount()); requestScopedBean.increaseCount(); //not delegated Assert.assertEquals(7, requestScopedBean.getCount()); sessionScopedBean.increaseCount(); Assert.assertEquals(7, sessionScopedBean.getCount()); //still mocked Assert.assertEquals(7, requestScopedBean.getCount()); //still mocked }
Example 3
Source File: MockedRequestScopedBeanWithInjectionTest.java From deltaspike with Apache License 2.0 | 6 votes |
@Test public void mixedMocksWithInjection2() { SessionScopedBean mockedSessionScopedBean = mock(SessionScopedBean.class); when(mockedSessionScopedBean.getCount()).thenReturn(14); mockManager.addMock(mockedSessionScopedBean); RequestScopedBean mockedRequestScopedBean = new MockedRequestScopedBeanWithInjection(); BeanProvider.injectFields(mockedRequestScopedBean); mockManager.addMock(mockedRequestScopedBean); Assert.assertEquals(14, requestScopedBean.getCount()); requestScopedBean.increaseCount(); //not delegated Assert.assertEquals(14, requestScopedBean.getCount()); sessionScopedBean.increaseCount(); Assert.assertEquals(14, sessionScopedBean.getCount()); //still mocked Assert.assertEquals(14, requestScopedBean.getCount()); //still mocked }
Example 4
Source File: CdiTestRunner.java From deltaspike with Apache License 2.0 | 5 votes |
@Override public void evaluate() throws Throwable { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Class<?> type = this.method.getMethod().getDeclaringClass(); Set<Bean<?>> beans = beanManager.getBeans(type); if (!USE_TEST_CLASS_AS_CDI_BEAN || beans == null || beans.isEmpty()) { if (!ALLOW_INJECTION_POINT_MANIPULATION) { BeanProvider.injectFields(this.originalTarget); //fallback to simple injection } invokeMethod(this.originalTarget); } else { Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans); CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean); Object target = beanManager.getReference(bean, type, creationalContext); try { invokeMethod(target); } finally { if (bean.getScope().equals(Dependent.class)) { bean.destroy(target, creationalContext); } } } }
Example 5
Source File: MockedRequestScopedBeanWithInjectionTest.java From deltaspike with Apache License 2.0 | 5 votes |
@Test public void manualMockWithInjection() { RequestScopedBean mockedRequestScopedBean = new MockedRequestScopedBeanWithInjection(); BeanProvider.injectFields(mockedRequestScopedBean); mockManager.addMock(mockedRequestScopedBean); Assert.assertEquals(0, requestScopedBean.getCount()); requestScopedBean.increaseCount(); //not delegated Assert.assertEquals(0, requestScopedBean.getCount()); sessionScopedBean.increaseCount(); Assert.assertEquals(1, sessionScopedBean.getCount()); Assert.assertEquals(1, requestScopedBean.getCount()); }
Example 6
Source File: AbstractContextualReferenceWrapper.java From deltaspike with Apache License 2.0 | 5 votes |
@Override public void restoreState(FacesContext context, Object state) { Object[] wrappedState = (Object[]) state; if (this.wrapped == null) //fallback for full state-saving { Class wrappedClass = ClassUtils.tryToLoadClassForName((String)wrappedState[0]); T resolvedInstance = resolveInstanceForClass(context, wrappedClass); //TODO re-visit logic for multiple (custom) wrappers if (resolvedInstance instanceof AbstractContextualReferenceWrapper) { resolvedInstance = ((AbstractContextualReferenceWrapper<T>)resolvedInstance).getWrapped(); } this.wrapped = resolvedInstance; } if (this.wrapped == null) { this.wrapped = (T)ClassUtils.tryToInstantiateClassForName((String)wrappedState[0]); BeanProvider.injectFields(this.wrapped); } if (this.wrapped instanceof StateHolder) { ((StateHolder) this.wrapped).restoreState(context, wrappedState[1]); } }
Example 7
Source File: AbstractQuartzScheduler.java From deltaspike with Apache License 2.0 | 5 votes |
@Override public void jobToBeExecuted(JobExecutionContext jobExecutionContext) { Class<?> jobClass = ProxyUtils.getUnproxiedClass(jobExecutionContext.getJobInstance().getClass()); Scheduled scheduled = jobClass.getAnnotation(Scheduled.class); //can happen with manually registered job-instances (via #unwrap) if (scheduled == null && !jobClass.equals(DynamicExpressionObserverJob.class)) { scheduled = DEFAULT_SCHEDULED_LITERAL; } if (scheduled == null) { return; } JobListenerContext jobListenerContext = new JobListenerContext(); currentJobListenerContext.set(jobListenerContext); jobListenerContext.startContexts(scheduled); boolean jobInstanceIsBean; try { jobInstanceIsBean = Boolean.TRUE.equals(jobExecutionContext.getScheduler().getContext().get(jobClass.getName())); } catch (SchedulerException e) { jobInstanceIsBean = false; } if (!jobInstanceIsBean) { BeanProvider.injectFields(jobExecutionContext.getJobInstance()); } }
Example 8
Source File: DynamicExpressionObserverJob.java From deltaspike with Apache License 2.0 | 4 votes |
@Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap jobDataMap = context.getMergedJobDataMap(); String configExpression = jobDataMap.getString(CONFIG_EXPRESSION_KEY); String triggerId = jobDataMap.getString(TRIGGER_ID_KEY); String activeCronExpression = jobDataMap.getString(ACTIVE_CRON_EXPRESSION_KEY); String configKey = configExpression.substring(1, configExpression.length() - 1); String configuredValue = ConfigResolver.getPropertyAwarePropertyValue(configKey, activeCronExpression); if (!activeCronExpression.equals(configuredValue)) { //both #put calls are needed currently context.getJobDetail().getJobDataMap().put(ACTIVE_CRON_EXPRESSION_KEY, configuredValue); context.getTrigger().getJobDataMap().put(ACTIVE_CRON_EXPRESSION_KEY, configuredValue); BeanProvider.injectFields(this); JobKey observerJobKey = context.getJobDetail().getKey(); String observedJobName = observerJobKey.getName() .substring(0, observerJobKey.getName().length() - OBSERVER_POSTFIX.length()); JobKey observedJobKey = new JobKey(observedJobName, observerJobKey.getGroup()); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(triggerId) .forJob(observedJobName, observedJobKey.getGroup()) .withSchedule(CronScheduleBuilder.cronSchedule(configuredValue)) .build(); //use rescheduleJob instead of delete + add //(unwrap is ok here, because this class will only get active in case of a quartz-scheduler) org.quartz.Scheduler quartzScheduler = scheduler.unwrap(org.quartz.Scheduler.class); try { quartzScheduler.rescheduleJob(trigger.getKey(), trigger); } catch (SchedulerException e) { LOG.warning("failed to updated cron-expression for " + observedJobKey); } } }
Example 9
Source File: BeanProviderTest.java From deltaspike with Apache License 2.0 | 4 votes |
@Test public void injectBeansInNonManagedInstance() throws Exception { ManualBean manualBean = new ManualBean(); Assert.assertNull(manualBean.getTestBean()); BeanProvider.injectFields(manualBean); Assert.assertNotNull(manualBean.getTestBean()); Assert.assertEquals(4711, manualBean.getTestBean().getI()); int newValue = 14; manualBean.getTestBean().setI(newValue); Assert.assertEquals(newValue, manualBean.getTestBean().getI()); TestBean testBean = BeanProvider.getContextualReference(TestBean.class); Assert.assertEquals(newValue, testBean.getI()); testBean.setI(4711); // reset the value if this test is executed first }