protected void setUp() throws Exception { BundleContext bundleContext = new MockBundleContext() { // service reference already registered public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException { return new ServiceReference[0]; } }; appContext = new GenericApplicationContext(); appContext.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); appContext.setClassLoader(getClass().getClassLoader()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext); // reader.setEventListener(this.listener); reader.loadBeanDefinitions(new ClassPathResource("osgiReferenceNestedBeans.xml", getClass())); appContext.refresh(); }
protected void setUp() throws Exception { BundleContext bundleContext = new MockBundleContext() { // service reference already registered public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException { return new ServiceReference[] { new MockServiceReference(new String[] { Serializable.class.getName() }) }; } }; appContext = new GenericApplicationContext(); appContext.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); appContext.setClassLoader(getClass().getClassLoader()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext); reader.loadBeanDefinitions(new ClassPathResource("osgiDefaults.xml", getClass())); appContext.refresh(); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); applicationContext = new GenericApplicationContext(); applicationContext.setClassLoader(getClass().getClassLoader()); applicationContext.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); applicationContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); } }); reader = new XmlBeanDefinitionReader(applicationContext); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(getConfig(), getClass())); applicationContext.refresh(); blueprintContainer = new SpringBlueprintContainer(applicationContext); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); } }); reader = new XmlBeanDefinitionReader(context); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); blueprintContainer = new SpringBlueprintContainer(context); }
protected void setUp() throws Exception { BundleContext bundleContext = new MockBundleContext() { // service reference already registered public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException { return new ServiceReference[] { new MockServiceReference(new String[] { Cloneable.class.getName() }) }; } }; context = new GenericApplicationContext(); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); context.setClassLoader(getClass().getClassLoader()); reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); SpringBlueprintConverterService converterService = new SpringBlueprintConverterService(null, context.getBeanFactory()); converterService.add(new GenericConverter()); context.getBeanFactory().setConversionService(converterService); reader = new XmlBeanDefinitionReader(context); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); blueprintContainer = new SpringBlueprintContainer(context); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); beanFactory.registerSingleton("blueprintContainer", new SpringBlueprintContainer(context)); } }); reader = new XmlBeanDefinitionReader(context); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); blueprintContainer = new SpringBlueprintContainer(context); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().setConversionService( new SpringBlueprintConverterService(null, context.getBeanFactory())); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); reader = new XmlBeanDefinitionReader(context); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); blueprintContainer = new SpringBlueprintContainer(context); }
@Test public void testCamelProducer() throws Exception { // Starting Spring context. try (GenericApplicationContext context = new AnnotationConfigApplicationContext(ExampleConfiguration.class)) { context.start(); // Sending Camel message. CamelContext camel = context.getBean(CamelContext.class); ProducerTemplate producerTemplate = camel.createProducerTemplate(); producerTemplate.sendBody("direct:start", "Send me to the Sponge"); // Waiting for the engine to process an event. Engine engine = context.getBean(Engine.class); await().atMost(60, TimeUnit.SECONDS) .until(() -> engine.getOperations().getVariable(AtomicBoolean.class, "sentCamelMessage").get()); assertFalse(engine.isError()); context.stop(); } }
public KinesisTestBinder(AmazonKinesisAsync amazonKinesis, KinesisBinderConfigurationProperties kinesisBinderConfigurationProperties) { this.amazonKinesis = amazonKinesis; KinesisStreamProvisioner provisioningProvider = new KinesisStreamProvisioner(amazonKinesis, kinesisBinderConfigurationProperties); KinesisMessageChannelBinder binder = new KinesisMessageChannelBinder(amazonKinesis, kinesisBinderConfigurationProperties, provisioningProvider); GenericApplicationContext context = new GenericApplicationContext(); context.refresh(); binder.setApplicationContext(context); setBinder(binder); }
public static ApplicationContext buildAppContext(ApplicationContext parent, Stream<TestBean> beans, Collection<BeanFactoryPostProcessor> postProcessors) { final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); final GenericApplicationContext applicationContext = new GenericApplicationContext(beanFactory, parent); postProcessors.forEach(applicationContext::addBeanFactoryPostProcessor); beans.forEach(entry -> { final String factoryBean = entry.getName() + "_factory"; beanFactory.registerSingleton(factoryBean, (Supplier<Object>) entry::getBean); beanFactory.registerBeanDefinition(entry.getName(), BeanDefinitionBuilder .rootBeanDefinition(entry.getBean() != null ? entry.getBean().getClass() : Object.class) .setFactoryMethodOnBean("get", factoryBean) .getBeanDefinition()); }); applicationContext.refresh(); return applicationContext; }
private void registerCommonFunctionsToContext(GenericApplicationContext appcontext) { DefaultListableBeanFactory beanFactory = appcontext.getDefaultListableBeanFactory(); beanFactory.registerSingleton("camera", getCamera()); beanFactory.registerSingleton("settingsManager", settingsManager); beanFactory.registerSingleton("renderManager", renderManager); beanFactory.registerSingleton("application", this); beanFactory.registerSingleton("rootNode", rootNode); beanFactory.registerSingleton("assetManager", getAssetManager()); beanFactory.registerSingleton("inputManager", getInputManager()); beanFactory.registerSingleton("flyByCamera", getFlyByCamera()); beanFactory.registerSingleton("audioRenderer", getAudioRenderer()); beanFactory.registerSingleton("guiViewPort", getGuiViewPort()); beanFactory.registerSingleton("listener", listener); beanFactory.registerSingleton("planeRegistry", new PlaneRegistryLoader(settingsManager.assetDirectory()).planeRegistry()); OptionsManager optionsManager = new OptionsManager(APP_ROOT); beanFactory.registerSingleton("options",optionsManager.loadOptionsFromFileSystem()); beanFactory.registerSingleton("optionsManager",optionsManager); }
@Before public void setup() throws Exception { @SuppressWarnings("resource") GenericApplicationContext cxt = new GenericApplicationContext(); cxt.refresh(); this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory()); Method method = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class, String.class, String.class); this.paramRequired = new SynthesizingMethodParameter(method, 0); this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1); this.paramSystemProperty = new SynthesizingMethodParameter(method, 2); this.paramNotAnnotated = new SynthesizingMethodParameter(method, 3); this.paramNativeHeader = new SynthesizingMethodParameter(method, 4); this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class); }
@Test public void testAutowiredFieldResolvesWithMultipleQualifierValues() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class); qualifier.setAttribute("number", 456); person1.addQualifier(qualifier); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class); qualifier2.setAttribute("number", 123); person2.addQualifier(qualifier2); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldWithMultipleAttributesTestBean bean = (QualifiedFieldWithMultipleAttributesTestBean) context.getBean("autowired"); assertEquals(MARK, bean.getPerson().getName()); }
@Test public void asyncMethodsWithQualifierThroughInterface() throws Exception { originalThreadName = Thread.currentThread().getName(); GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(SimpleAsyncMethodWithQualifierBean.class)); context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.registerBeanDefinition("e0", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.refresh(); SimpleInterface asyncTest = context.getBean("asyncTest", SimpleInterface.class); asyncTest.doNothing(5); asyncTest.doSomething(10); Future<String> future = asyncTest.returnSomething(20); assertEquals("20", future.get()); Future<String> future2 = asyncTest.returnSomething2(30); assertEquals("30", future2.get()); }
@Test public void testAutowiredConstructorArgumentWithMultipleNonQualifiedCandidates() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e instanceof UnsatisfiedDependencyException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testRegistrationTwoEndpoints() throws Exception { this.context = new GenericApplicationContext(); this.context.registerBeanDefinition("endpointMbeanExporter", new RootBeanDefinition(EndpointMBeanExporter.class)); this.context.registerBeanDefinition("endpoint1", new RootBeanDefinition(TestEndpoint.class)); this.context.registerBeanDefinition("endpoint2", new RootBeanDefinition(TestEndpoint.class)); this.context.refresh(); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertThat(mbeanExporter.getServer() .getMBeanInfo(getObjectName("endpoint1", this.context))).isNotNull(); assertThat(mbeanExporter.getServer() .getMBeanInfo(getObjectName("endpoint2", this.context))).isNotNull(); }
@Test public void testPrivatePersistenceContextField() throws Exception { mockEmf = mock(EntityManagerFactory.class, withSettings().serializable()); GenericApplicationContext gac = new GenericApplicationContext(); gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf); gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class)); gac.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class)); gac.registerBeanDefinition(FactoryBeanWithPersistenceContextField.class.getName(), new RootBeanDefinition(FactoryBeanWithPersistenceContextField.class)); gac.refresh(); DefaultPrivatePersistenceContextField bean = (DefaultPrivatePersistenceContextField) gac.getBean( DefaultPrivatePersistenceContextField.class.getName()); FactoryBeanWithPersistenceContextField bean2 = (FactoryBeanWithPersistenceContextField) gac.getBean( "&" + FactoryBeanWithPersistenceContextField.class.getName()); assertNotNull(bean.em); assertNotNull(bean2.em); assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean.em)); assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean2.em)); }
@Test public void asyncPrototypeClassListener() throws Exception { originalThreadName = Thread.currentThread().getName(); listenerCalled = 0; listenerConstructed = 0; GenericApplicationContext context = new GenericApplicationContext(); RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class); listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); context.registerBeanDefinition("asyncTest", listenerDef); context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); context.close(); Thread.sleep(1000); assertEquals(2, listenerCalled); assertEquals(2, listenerConstructed); }
@Test public void testAutowiredFieldResolvesMetaQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(MetaQualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); MetaQualifiedFieldTestBean bean = (MetaQualifiedFieldTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
@Test public void testAutowiredMethodParameterResolvesQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedMethodParameterTestBean bean = (QualifiedMethodParameterTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
@Test public void testAutowiredMethodParameterWithMultipleNonQualifiedCandidates() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testChangeScriptWithRefreshableBeanFunctionality() throws Exception { BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true); BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean(); GenericApplicationContext ctx = new GenericApplicationContext(); ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition); ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition); ctx.refresh(); Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME); assertEquals(MESSAGE_TEXT, messenger.getMessage()); // cool; now let's change the script and check the refresh behaviour... pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE); StaticScriptSource source = getScriptSource(ctx); source.setScript(CHANGED_SCRIPT); Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME); // the updated script surrounds the message in quotes before returning... assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, refreshedMessenger.getMessage()); }
/** * Modeled after the Spring implementation in {@link AbstractGenericContextLoader}, * this method creates and refreshes the application context while providing for * processing of additional Camel specific post-refresh actions. We do not provide the * pre-post hooks for customization seen in {@link AbstractGenericContextLoader} because * they probably are unnecessary for 90+% of users. * <p/> * For some functionality, we cannot use {@link org.springframework.test.context.TestExecutionListener} because we need * to both produce the desired outcome during application context loading, and also cleanup * after ourselves even if the test class never executes. Thus the listeners, which * only run if the application context is successfully initialized are insufficient to * provide the behavior described above. */ @Override public ApplicationContext loadContext(String... locations) throws Exception { Class<?> testClass = getTestClass(); if (LOG.isDebugEnabled()) { LOG.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "]."); } try { GenericApplicationContext context = createContext(testClass, null); loadBeanDefinitions(context, locations); return loadContext(context, testClass); } finally { cleanup(testClass); } }
@Test public void testAutowiredFieldResolvesWithMultipleQualifierValuesAndExplicitDefaultValue() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class); qualifier.setAttribute("number", 456); person1.addQualifier(qualifier); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class); qualifier2.setAttribute("number", 123); qualifier2.setAttribute("value", "default"); person2.addQualifier(qualifier2); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldWithMultipleAttributesTestBean bean = (QualifiedFieldWithMultipleAttributesTestBean) context.getBean("autowired"); assertEquals(MARK, bean.getPerson().getName()); }
/** * Handles disabling of JMX on Camel contexts based on {@link DisableJmx}. * * @param context the initialized Spring context * @param testClass the test class being executed */ protected void handleDisableJmx(GenericApplicationContext context, Class<?> testClass) { CamelSpringTestHelper.setOriginalJmxDisabledValue(System.getProperty(JmxSystemPropertyKeys.DISABLED)); if (testClass.isAnnotationPresent(DisableJmx.class)) { if (testClass.getAnnotation(DisableJmx.class).value()) { LOG.info("Disabling Camel JMX globally as DisableJmx annotation was found and disableJmx is set to true."); System.setProperty(JmxSystemPropertyKeys.DISABLED, "true"); } else { LOG.info("Enabling Camel JMX as DisableJmx annotation was found and disableJmx is set to false."); System.clearProperty(JmxSystemPropertyKeys.DISABLED); } } else { LOG.info("Disabling Camel JMX globally for tests by default. Use the DisableJMX annotation to override the default setting."); System.setProperty(JmxSystemPropertyKeys.DISABLED, "true"); } }
/** * Apply any relevant post processing the {@link ApplicationContext}. Subclasses can * apply additional processing as required. * @param context the application context */ protected void postProcessApplicationContext(ConfigurableApplicationContext context) { if (this.beanNameGenerator != null) { context.getBeanFactory().registerSingleton( AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, this.beanNameGenerator); } if (this.resourceLoader != null) { if (context instanceof GenericApplicationContext) { ((GenericApplicationContext) context) .setResourceLoader(this.resourceLoader); } if (context instanceof DefaultResourceLoader) { ((DefaultResourceLoader) context) .setClassLoader(this.resourceLoader.getClassLoader()); } } }
@Test public void testAutowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); // qualifier added, and value matches the default person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "default")); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldWithDefaultValueTestBean bean = (QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
@Test public void testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh(); RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse")); ctx.registerBeanDefinition("test", rbd); ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) ctx.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ctx.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000); }
public static Test suite() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader(ac); bdr.setScopeMetadataResolver(new Jsr330ScopeMetadataResolver()); bdr.registerBean(Convertible.class); bdr.registerBean(DriversSeat.class, Drivers.class); bdr.registerBean(Seat.class, Primary.class); bdr.registerBean(V8Engine.class); bdr.registerBean(SpareTire.class, "spare"); bdr.registerBean(Cupholder.class); bdr.registerBean(Tire.class, Primary.class); bdr.registerBean(FuelTank.class); ac.refresh(); Car car = ac.getBean(Car.class); return Tck.testsFor(car, false, true); }
@Test public void testAutowiredFieldWithMultipleNonQualifiedCandidates() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testViewScopedClass() { GenericApplicationContext acx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(acx); acx.registerBeanDefinition("viewScopedClass", new AnnotatedGenericBeanDefinition( new StandardAnnotationMetadata(ViewScopedClass.class))); acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition( ScopedBeansConfiguration.class)); acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment())); acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment())); acx.refresh(); assertThat(acx.getBeanDefinition("viewScopedClass").getScope()) .isEqualTo(ViewScope.SCOPE_VIEW); assertThat(acx.getBeanDefinition("viewScopedBean").getScope()) .isEqualTo(ViewScope.SCOPE_VIEW); }
@Test public void testPublicSpecificExtendedPersistenceContextSetter() throws Exception { EntityManagerFactory mockEmf2 = mock(EntityManagerFactory.class); EntityManager mockEm2 = mock(EntityManager.class); given(mockEmf2.createEntityManager()).willReturn(mockEm2); GenericApplicationContext gac = new GenericApplicationContext(); gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf); gac.getDefaultListableBeanFactory().registerSingleton("unit2", mockEmf2); gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class)); gac.registerBeanDefinition(SpecificPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(SpecificPublicPersistenceContextSetter.class)); gac.refresh(); SpecificPublicPersistenceContextSetter bean = (SpecificPublicPersistenceContextSetter) gac.getBean( SpecificPublicPersistenceContextSetter.class.getName()); assertNotNull(bean.getEntityManager()); bean.getEntityManager().flush(); verify(mockEm2).getTransaction(); verify(mockEm2).flush(); }
@Test public void testAutowiredMethodParameterWithStaticallyQualifiedCandidateAmongOthers() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedMethodParameterTestBean bean = (QualifiedMethodParameterTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
@Test public void closingChildContextDoesNotCleanUpLoggingSystem() { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName()); this.initializer.onApplicationEvent( new ApplicationStartedEvent(this.springApplication, new String[0])); TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils .getField(this.initializer, "loggingSystem"); assertThat(loggingSystem.cleanedUp).isFalse(); GenericApplicationContext childContext = new GenericApplicationContext(); childContext.setParent(this.context); this.initializer.onApplicationEvent(new ContextClosedEvent(childContext)); assertThat(loggingSystem.cleanedUp).isFalse(); this.initializer.onApplicationEvent(new ContextClosedEvent(this.context)); assertThat(loggingSystem.cleanedUp).isTrue(); childContext.close(); }
@Test public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() { GenericApplicationContext context = new GenericApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); scanner.scan(BASE_PACKAGE); scanner.scan("org.springframework.context.annotation5"); assertTrue(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("stubFooDao")); assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean("otherFooDao")); context.refresh(); assertFalse(context.getBeanFactory().containsSingleton("otherFooDao")); assertFalse(context.getBeanFactory().containsSingleton("fooServiceImpl")); FooServiceImpl service = context.getBean("fooServiceImpl", FooServiceImpl.class); assertTrue(context.getBeanFactory().containsSingleton("otherFooDao")); assertEquals("other", service.foo(1)); }
@Test public void testAutowiredConstructorArgumentWithSingleQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); person.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedConstructorArgumentTestBean bean = (QualifiedConstructorArgumentTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }