|
16 | 16 |
|
17 | 17 | package org.springframework.beans.factory.xml;
|
18 | 18 |
|
19 |
| -import java.util.Arrays; |
20 |
| - |
21 | 19 | import org.junit.jupiter.api.Test;
|
22 | 20 | import org.xml.sax.InputSource;
|
23 | 21 |
|
|
29 | 27 | import org.springframework.core.io.ClassPathResource;
|
30 | 28 | import org.springframework.core.io.InputStreamResource;
|
31 | 29 | import org.springframework.core.io.Resource;
|
32 |
| -import org.springframework.util.ObjectUtils; |
33 | 30 |
|
34 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
35 | 32 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 33 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
36 | 34 |
|
37 | 35 | /**
|
38 | 36 | * @author Rick Evans
|
39 | 37 | * @author Juergen Hoeller
|
40 | 38 | * @author Sam Brannen
|
41 | 39 | */
|
42 |
| -public class XmlBeanDefinitionReaderTests { |
| 40 | +class XmlBeanDefinitionReaderTests { |
| 41 | + |
| 42 | + private final SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 43 | + |
| 44 | + private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); |
| 45 | + |
43 | 46 |
|
44 | 47 | @Test
|
45 |
| - public void setParserClassSunnyDay() { |
46 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
47 |
| - new XmlBeanDefinitionReader(registry).setDocumentReaderClass(DefaultBeanDefinitionDocumentReader.class); |
| 48 | + void setReaderClass() { |
| 49 | + assertThatNoException().isThrownBy(() -> reader.setDocumentReaderClass(DefaultBeanDefinitionDocumentReader.class)); |
48 | 50 | }
|
49 | 51 |
|
50 | 52 | @Test
|
51 |
| - public void withOpenInputStream() { |
52 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 53 | + void withInputStreamResourceWithoutExplicitValidationMode() { |
53 | 54 | Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml"));
|
54 |
| - assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> |
55 |
| - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource)); |
| 55 | + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> reader.loadBeanDefinitions(resource)); |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | @Test
|
59 |
| - public void withOpenInputStreamAndExplicitValidationMode() { |
60 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 59 | + void withInputStreamResourceAndExplicitValidationMode() { |
61 | 60 | Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml"));
|
62 |
| - XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); |
63 | 61 | reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD);
|
64 | 62 | reader.loadBeanDefinitions(resource);
|
65 |
| - testBeanDefinitions(registry); |
| 63 | + assertBeanDefinitions(registry); |
66 | 64 | }
|
67 | 65 |
|
68 | 66 | @Test
|
69 |
| - public void withImport() { |
70 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 67 | + void withImport() { |
71 | 68 | Resource resource = new ClassPathResource("import.xml", getClass());
|
72 |
| - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); |
73 |
| - testBeanDefinitions(registry); |
| 69 | + reader.loadBeanDefinitions(resource); |
| 70 | + assertBeanDefinitions(registry); |
74 | 71 | }
|
75 | 72 |
|
76 | 73 | @Test
|
77 |
| - public void withWildcardImport() { |
78 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 74 | + void withWildcardImport() { |
79 | 75 | Resource resource = new ClassPathResource("importPattern.xml", getClass());
|
80 |
| - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); |
81 |
| - testBeanDefinitions(registry); |
| 76 | + reader.loadBeanDefinitions(resource); |
| 77 | + assertBeanDefinitions(registry); |
82 | 78 | }
|
83 | 79 |
|
84 | 80 | @Test
|
85 |
| - public void withInputSource() { |
86 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 81 | + void withInputSourceWithoutExplicitValidationMode() { |
87 | 82 | InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml"));
|
88 |
| - assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> |
89 |
| - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource)); |
| 83 | + assertThatExceptionOfType(BeanDefinitionStoreException.class) |
| 84 | + .isThrownBy(() -> reader.loadBeanDefinitions(resource)) |
| 85 | + .withMessageStartingWith("Unable to determine validation mode for [resource loaded through SAX InputSource]:"); |
90 | 86 | }
|
91 | 87 |
|
92 | 88 | @Test
|
93 |
| - public void withInputSourceAndExplicitValidationMode() { |
94 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 89 | + void withInputSourceAndExplicitValidationMode() { |
95 | 90 | InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml"));
|
96 |
| - XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); |
97 | 91 | reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD);
|
98 | 92 | reader.loadBeanDefinitions(resource);
|
99 |
| - testBeanDefinitions(registry); |
| 93 | + assertBeanDefinitions(registry); |
100 | 94 | }
|
101 | 95 |
|
102 | 96 | @Test
|
103 |
| - public void withFreshInputStream() { |
104 |
| - SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); |
| 97 | + void withClassPathResource() { |
105 | 98 | Resource resource = new ClassPathResource("test.xml", getClass());
|
106 |
| - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); |
107 |
| - testBeanDefinitions(registry); |
| 99 | + reader.loadBeanDefinitions(resource); |
| 100 | + assertBeanDefinitions(registry); |
108 | 101 | }
|
109 | 102 |
|
110 |
| - private void testBeanDefinitions(BeanDefinitionRegistry registry) { |
| 103 | + private void assertBeanDefinitions(BeanDefinitionRegistry registry) { |
111 | 104 | assertThat(registry.getBeanDefinitionCount()).isEqualTo(24);
|
112 | 105 | assertThat(registry.getBeanDefinitionNames()).hasSize(24);
|
113 |
| - assertThat(Arrays.asList(registry.getBeanDefinitionNames()).contains("rod")).isTrue(); |
114 |
| - assertThat(Arrays.asList(registry.getBeanDefinitionNames()).contains("aliased")).isTrue(); |
| 106 | + assertThat(registry.getBeanDefinitionNames()).contains("rod", "aliased"); |
115 | 107 | assertThat(registry.containsBeanDefinition("rod")).isTrue();
|
116 | 108 | assertThat(registry.containsBeanDefinition("aliased")).isTrue();
|
117 | 109 | assertThat(registry.getBeanDefinition("rod").getBeanClassName()).isEqualTo(TestBean.class.getName());
|
118 | 110 | assertThat(registry.getBeanDefinition("aliased").getBeanClassName()).isEqualTo(TestBean.class.getName());
|
119 | 111 | assertThat(registry.isAlias("youralias")).isTrue();
|
120 |
| - String[] aliases = registry.getAliases("aliased"); |
121 |
| - assertThat(aliases).hasSize(2); |
122 |
| - assertThat(ObjectUtils.containsElement(aliases, "myalias")).isTrue(); |
123 |
| - assertThat(ObjectUtils.containsElement(aliases, "youralias")).isTrue(); |
| 112 | + assertThat(registry.getAliases("aliased")).containsExactly("myalias", "youralias"); |
124 | 113 | }
|
125 | 114 |
|
126 | 115 | @Test
|
127 |
| - public void dtdValidationAutodetect() { |
| 116 | + void dtdValidationAutodetect() { |
128 | 117 | doTestValidation("validateWithDtd.xml");
|
129 | 118 | }
|
130 | 119 |
|
131 | 120 | @Test
|
132 |
| - public void xsdValidationAutodetect() { |
| 121 | + void xsdValidationAutodetect() { |
133 | 122 | doTestValidation("validateWithXsd.xml");
|
134 | 123 | }
|
135 | 124 |
|
136 | 125 | private void doTestValidation(String resourceName) {
|
137 | 126 | DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
138 | 127 | Resource resource = new ClassPathResource(resourceName, getClass());
|
139 | 128 | new XmlBeanDefinitionReader(factory).loadBeanDefinitions(resource);
|
140 |
| - TestBean bean = (TestBean) factory.getBean("testBean"); |
141 |
| - assertThat(bean).isNotNull(); |
| 129 | + assertThat((TestBean) factory.getBean("testBean")).isNotNull(); |
142 | 130 | }
|
143 | 131 |
|
144 | 132 | }
|
0 commit comments