@Test public void parseInternal_customSqsClient_shouldProduceContainerWithCustomSqsClientUsed() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-amazon-sqs.xml", getClass())); //Assert BeanDefinition sqsAsync = registry.getBeanDefinition("myClient"); assertNotNull(sqsAsync); BeanDefinition abstractContainerDefinition = registry.getBeanDefinition(SimpleMessageListenerContainer.class.getName() + "#0"); assertNotNull(abstractContainerDefinition); assertEquals(3, abstractContainerDefinition.getPropertyValues().size()); assertEquals("myClient", ((RuntimeBeanReference) abstractContainerDefinition.getPropertyValues().getPropertyValue("amazonSqs").getValue()).getBeanName()); }
@Test public void parseInternal_withSendToMessageTemplateAttribute_mustBeSetOnTheBeanDefinition() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-with-send-to-message-template.xml", getClass())); //Assert BeanDefinition queueMessageHandler = registry.getBeanDefinition(QueueMessageHandler.class.getName() + "#0"); assertNotNull(queueMessageHandler); assertEquals(1, queueMessageHandler.getPropertyValues().size()); ManagedList<?> returnValueHandlers = (ManagedList<?>) queueMessageHandler.getPropertyValues().getPropertyValue("customReturnValueHandlers").getValue(); assertEquals(1, returnValueHandlers.size()); RootBeanDefinition sendToReturnValueHandler = (RootBeanDefinition) returnValueHandlers.get(0); assertEquals("messageTemplate", ((RuntimeBeanReference) sendToReturnValueHandler.getConstructorArgumentValues().getArgumentValue(0, RuntimeBeanReference.class).getValue()).getBeanName()); }
@Test public void parseInternal_withCustomProperties_customPropertiesConfiguredOnContainer() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-properties.xml", getClass())); //Assert BeanDefinition abstractContainerDefinition = registry.getBeanDefinition(SimpleMessageListenerContainer.class.getName() + "#0"); assertNotNull(abstractContainerDefinition); assertEquals("false", abstractContainerDefinition.getPropertyValues().getPropertyValue("autoStartup").getValue()); assertEquals("9", abstractContainerDefinition.getPropertyValues().getPropertyValue("maxNumberOfMessages").getValue()); assertEquals("6", abstractContainerDefinition.getPropertyValues().getPropertyValue("visibilityTimeout").getValue()); assertEquals("3", abstractContainerDefinition.getPropertyValues().getPropertyValue("waitTimeOut").getValue()); }
@Test public void generateBeanNameWithNamedComponent() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); assertEquals("walden", beanName); }
@Test public void generateBeanNameWithDefaultNamedComponent() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); assertEquals("thoreau", beanName); }
@Test public void generateBeanNameWithNamedComponentWhereTheNameIsBlank() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithBlankName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Test public void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnonymousComponent.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Test public void generateBeanNameFromMetaComponentWithStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("henry", beanName); }
@Test public void generateBeanNameFromMetaComponentWithNonStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName); }
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithoutName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithoutName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithBlankName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithBlankName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition( ComposedControllerAnnotationWithStringValue.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("restController", beanName); }
@Test(expected = BeanDefinitionStoreException.class) public void withOpenInputStream() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream( "test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); }
@Test public void withOpenInputStreamAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test public void withImport() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new ClassPathResource("import.xml", getClass()); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test public void withWildcardImport() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new ClassPathResource("importPattern.xml", getClass()); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test public void withInputSourceAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test public void withFreshInputStream() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new ClassPathResource("test.xml", getClass()); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test public void testGenerateBeanNameWithNamedComponent() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); assertEquals("walden", beanName); }
@Test public void testGenerateBeanNameWithDefaultNamedComponent() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); assertEquals("thoreau", beanName); }
@Test public void testGenerateBeanNameWithNamedComponentWhereTheNameIsBlank() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithBlankName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Test public void testGenerateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnonymousComponent.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Test public void testGenerateBeanNameFromMetaComponentWithStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("henry", beanName); }
@Test public void testGenerateBeanNameFromMetaComponentWithNonStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName); }
public void testSetParserClassToNull() { try { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); new XmlBeanDefinitionReader(registry).setDocumentReaderClass(null); fail("Should have thrown IllegalArgumentException (null parserClass)"); } catch (IllegalArgumentException expected) { } }
public void testSetParserClassToUnsupportedParserType() throws Exception { try { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); new XmlBeanDefinitionReader(registry).setDocumentReaderClass(String.class); fail("Should have thrown IllegalArgumentException (unsupported parserClass)"); } catch (IllegalArgumentException expected) { } }
public void testWithOpenInputStream() { try { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); fail("Should have thrown BeanDefinitionStoreException (can't determine validation mode)"); } catch (BeanDefinitionStoreException expected) { } }
public void testWithOpenInputStreamAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }
public void testWithInputSource() { try { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); fail("Should have thrown BeanDefinitionStoreException (can't determine validation mode)"); } catch (BeanDefinitionStoreException expected) { } }
public void testWithInputSourceAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test public void parseInternal_withCustomAmazonSnsClient_shouldPassItAsConstructorArg() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-amazon-sns.xml", getClass())); //Assert BeanDefinition notificationMessagingTemplateBeanDefinition = registry.getBeanDefinition("notificationMessagingTemplate"); assertEquals("mySnsClient", ((RuntimeBeanReference) notificationMessagingTemplateBeanDefinition.getConstructorArgumentValues() .getArgumentValue(0, RuntimeBeanReference.class).getValue()).getBeanName()); }
@Test public void parseInternal_withDefaultDestination_mustBeSetOnTemplate() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-with-default-destination.xml", getClass())); //Assert BeanDefinition notificationMessagingTemplateBeanDefinition = registry.getBeanDefinition("notificationMessagingTemplate"); assertEquals("myDefaultDestination", notificationMessagingTemplateBeanDefinition.getPropertyValues() .getPropertyValue("defaultDestinationName").getValue()); }
@Test public void parseInternal_withCustomAmazonSqsClient_shouldPassItAsConstructorArg() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-amazon-sqs.xml", getClass())); //Assert BeanDefinition queueMessagingTemplateBeanDefinition = registry.getBeanDefinition("queueMessagingTemplate"); assertEquals("myClient", ((RuntimeBeanReference) queueMessagingTemplateBeanDefinition.getConstructorArgumentValues() .getArgumentValue(0, RuntimeBeanReference.class).getValue()).getBeanName()); }
@Test public void parseInternal_withDefaultDestination_mustBeSetOnTemplate() throws Exception { //Arrange SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-with-default-destination.xml", getClass())); //Assert BeanDefinition queueMessagingTemplateBeanDefinition = registry.getBeanDefinition("queueMessagingTemplate"); assertEquals("myDefaultDestination", queueMessagingTemplateBeanDefinition.getPropertyValues() .getPropertyValue("defaultDestinationName").getValue()); }
private BeanDefinitionRegistry getBeanFactory(String location) { XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(new SimpleBeanDefinitionRegistry()); beanDefinitionReader.setResourceLoader(context); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(context)); beanDefinitionReader.loadBeanDefinitions(location); return beanDefinitionReader.getBeanFactory(); }
@Test public void setParserClassSunnyDay() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); new XmlBeanDefinitionReader(registry).setDocumentReaderClass(DefaultBeanDefinitionDocumentReader.class); }
@Test(expected = IllegalArgumentException.class) public void setParserClassToNull() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); new XmlBeanDefinitionReader(registry).setDocumentReaderClass(null); }
@Test(expected = IllegalArgumentException.class) public void setParserClassToUnsupportedParserType() throws Exception { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); new XmlBeanDefinitionReader(registry).setDocumentReaderClass(String.class); }
@Test(expected = BeanDefinitionStoreException.class) public void withInputSource() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); }
public void testSetParserClassSunnyDay() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); new XmlBeanDefinitionReader(registry).setDocumentReaderClass(DefaultBeanDefinitionDocumentReader.class); }