|
| 1 | +package org.springframework.integration.aws.config.xml; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 5 | +import org.mockito.InjectMocks; |
| 6 | +import org.mockito.Mock; |
| 7 | +import org.mockito.Spy; |
| 8 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 9 | +import org.springframework.beans.PropertyValue; |
| 10 | +import org.springframework.beans.factory.config.ConstructorArgumentValues; |
| 11 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 12 | +import org.springframework.beans.factory.config.TypedStringValue; |
| 13 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 14 | +import org.springframework.beans.factory.xml.ParserContext; |
| 15 | +import org.springframework.beans.factory.xml.XmlReaderContext; |
| 16 | +import org.w3c.dom.Element; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.BDDMockito.*; |
| 20 | + |
| 21 | +@ExtendWith(MockitoExtension.class) |
| 22 | +class XmlBeanDefinitionBuilderTest { |
| 23 | + |
| 24 | + @Mock |
| 25 | + private Element element; |
| 26 | + |
| 27 | + @Mock |
| 28 | + private ParserContext parserContext; |
| 29 | + |
| 30 | + @Spy |
| 31 | + private final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(); |
| 32 | + |
| 33 | + @InjectMocks |
| 34 | + private XmlBeanDefinitionBuilder builder; |
| 35 | + |
| 36 | + @Test |
| 37 | + void testProperties() { |
| 38 | + given(element.getAttribute("value-property")) |
| 39 | + .willReturn("value"); |
| 40 | + given(element.getAttribute("ref-property")) |
| 41 | + .willReturn("bean"); |
| 42 | + given(element.getAttribute("defined-property")) |
| 43 | + .willReturn("test"); |
| 44 | + |
| 45 | + var def = builder |
| 46 | + .addConstructorArgValue("value-property") |
| 47 | + .addConstructorArgReference("ref-property") |
| 48 | + .setPropertyValue("value-property") |
| 49 | + .setPropertyValue("property", "value-property") |
| 50 | + .setPropertyReference("ref-property") |
| 51 | + .setPropertyReference("ref", "ref-property") |
| 52 | + .setPropertyValueIfAttributeDefined("defined-property") |
| 53 | + .setPropertyValueIfAttributeDefined("undefined-property") |
| 54 | + .setPropertyValueIfAttributeDefined("expected", "defined-property") |
| 55 | + .setPropertyValueIfAttributeDefined("unexpected", "undefined-property") |
| 56 | + .build(); |
| 57 | + |
| 58 | + assertThat(def.getConstructorArgumentValues()) |
| 59 | + .returns(2, ConstructorArgumentValues::getArgumentCount) |
| 60 | + .returns(new TypedStringValue("value"), ctor -> ctor.getIndexedArgumentValue(0, null).getValue()) |
| 61 | + .returns(new RuntimeBeanReference("bean"), ctor -> ctor.getIndexedArgumentValue(1, null).getValue()) |
| 62 | + ; |
| 63 | + |
| 64 | + assertThat(def.getPropertyValues()) |
| 65 | + .containsOnly( |
| 66 | + new PropertyValue("valueProperty", new TypedStringValue("value")), |
| 67 | + new PropertyValue("property", new TypedStringValue("value")), |
| 68 | + new PropertyValue("refProperty", new RuntimeBeanReference("bean")), |
| 69 | + new PropertyValue("ref", new RuntimeBeanReference("bean")), |
| 70 | + new PropertyValue("definedProperty", new TypedStringValue("test")), |
| 71 | + new PropertyValue("expected", new TypedStringValue("test")) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testExclusiveProperties() { |
| 77 | + given(element.getAttribute(any())) |
| 78 | + .willReturn(null); |
| 79 | + given(element.getAttribute("a-property")) |
| 80 | + .willReturn("a"); |
| 81 | + given(element.getAttribute("b-property")) |
| 82 | + .willReturn("b"); |
| 83 | + given(element.getAttribute("value")) |
| 84 | + .willReturn("c"); |
| 85 | + given(element.getAttribute("test-expression")) |
| 86 | + .willReturn("d"); |
| 87 | + |
| 88 | + var def = builder |
| 89 | + .setPropertyValueIfExclusiveAttributeDefined("a-property", "x-property") |
| 90 | + .setPropertyValueIfExclusiveAttributeDefined("y-property", "b-property") |
| 91 | + .setPropertyValueIfAttributeDefined("m-property", "n-property") |
| 92 | + .setPropertyValueIfExclusiveAttributeDefined("a-property", "x-property", "aExpectedProperty", "aUnexpectedProperty") |
| 93 | + .setPropertyValueIfExclusiveAttributeDefined("y-property", "b-property", "bUnexpectedProperty", "bExpectedProperty") |
| 94 | + .setPropertyValueIfExclusiveAttributeDefined("m-property", "n-property", "mUnexpectedProperty", "nUnexpectedProperty") |
| 95 | + .setExpressionValueIfAttributeDefined("nonexistent") |
| 96 | + .setExpressionValueIfAttributeDefined("value") |
| 97 | + .setExpressionValueIfAttributeDefined("test") |
| 98 | + .build(); |
| 99 | + |
| 100 | + assertThat(def.getPropertyValues()) |
| 101 | + .containsOnly( |
| 102 | + new PropertyValue("aProperty", new TypedStringValue("a")), |
| 103 | + new PropertyValue("bProperty", new TypedStringValue("b")), |
| 104 | + new PropertyValue("aExpectedProperty", new TypedStringValue("a")), |
| 105 | + new PropertyValue("bExpectedProperty", new TypedStringValue("b")), |
| 106 | + new PropertyValue("value", new TypedStringValue("c")), |
| 107 | + new PropertyValue("testExpressionString", new TypedStringValue("d")) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testExclusivePropertiesError(@Mock XmlReaderContext readerContext) { |
| 113 | + given(parserContext.getReaderContext()) |
| 114 | + .willReturn(readerContext); |
| 115 | + |
| 116 | + given(element.getAttribute("a-property")) |
| 117 | + .willReturn("a"); |
| 118 | + given(element.getAttribute("b-property")) |
| 119 | + .willReturn("b"); |
| 120 | + given(element.getAttribute("c-property")) |
| 121 | + .willReturn("c"); |
| 122 | + given(element.getAttribute("d-property")) |
| 123 | + .willReturn("d"); |
| 124 | + given(element.getAttribute("test")) |
| 125 | + .willReturn("e"); |
| 126 | + given(element.getAttribute("test-expression")) |
| 127 | + .willReturn("f"); |
| 128 | + |
| 129 | + var def = builder |
| 130 | + .setPropertyValueIfExclusiveAttributeDefined("a-property", "b-property") |
| 131 | + .setPropertyValueIfExclusiveAttributeDefined("c-property", "d-property", "c", "d") |
| 132 | + .setExpressionValueIfAttributeDefined("test") |
| 133 | + .build(); |
| 134 | + |
| 135 | + assertThat(def.getPropertyValues()) |
| 136 | + .isEmpty(); |
| 137 | + then(readerContext).should() |
| 138 | + .error("a-property and b-property attributes are mutually exclusive", element); |
| 139 | + then(readerContext).should() |
| 140 | + .error("c-property and d-property attributes are mutually exclusive", element); |
| 141 | + then(readerContext).should() |
| 142 | + .error("test and test-expression attributes are mutually exclusive", element); |
| 143 | + } |
| 144 | +} |
0 commit comments