Skip to content

Commit 6f73351

Browse files
committed
Stop using Constants utility in XmlBeanDefinitionReader
See gh-30851
1 parent d6e05dd commit 6f73351

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.util.HashSet;
22+
import java.util.Map;
2223
import java.util.Set;
2324

2425
import javax.xml.parsers.ParserConfigurationException;
@@ -40,7 +41,6 @@
4041
import org.springframework.beans.factory.parsing.SourceExtractor;
4142
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
4243
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
43-
import org.springframework.core.Constants;
4444
import org.springframework.core.NamedThreadLocal;
4545
import org.springframework.core.io.DescriptiveResource;
4646
import org.springframework.core.io.Resource;
@@ -68,6 +68,7 @@
6868
* @author Juergen Hoeller
6969
* @author Rob Harrop
7070
* @author Chris Beams
71+
* @author Sam Brannen
7172
* @since 26.11.2003
7273
* @see #setDocumentReaderClass
7374
* @see BeanDefinitionDocumentReader
@@ -99,8 +100,16 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
99100
public static final int VALIDATION_XSD = XmlValidationModeDetector.VALIDATION_XSD;
100101

101102

102-
/** Constants instance for this class. */
103-
private static final Constants constants = new Constants(XmlBeanDefinitionReader.class);
103+
/**
104+
* Map of constant names to constant values for the validation constants defined
105+
* in this class.
106+
*/
107+
private static final Map<String, Integer> constants = Map.of(
108+
"VALIDATION_NONE", VALIDATION_NONE,
109+
"VALIDATION_AUTO", VALIDATION_AUTO,
110+
"VALIDATION_DTD", VALIDATION_DTD,
111+
"VALIDATION_XSD", VALIDATION_XSD
112+
);
104113

105114
private int validationMode = VALIDATION_AUTO;
106115

@@ -163,7 +172,10 @@ public void setValidating(boolean validating) {
163172
* @see #setValidationMode
164173
*/
165174
public void setValidationModeName(String validationModeName) {
166-
setValidationMode(constants.asNumber(validationModeName).intValue());
175+
Assert.hasText(validationModeName, "'validationModeName' must not be null or blank");
176+
Integer mode = constants.get(validationModeName);
177+
Assert.notNull(mode, "Only validation mode constants allowed");
178+
setValidationMode(mode);
167179
}
168180

169181
/**

spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.beans.factory.xml;
1818

19+
import java.lang.reflect.Field;
20+
import java.util.Arrays;
21+
import java.util.stream.Stream;
22+
1923
import org.junit.jupiter.api.Test;
2024
import org.xml.sax.InputSource;
2125

@@ -27,12 +31,16 @@
2731
import org.springframework.core.io.ClassPathResource;
2832
import org.springframework.core.io.InputStreamResource;
2933
import org.springframework.core.io.Resource;
34+
import org.springframework.util.ReflectionUtils;
3035

3136
import static org.assertj.core.api.Assertions.assertThat;
3237
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3339
import static org.assertj.core.api.Assertions.assertThatNoException;
3440

3541
/**
42+
* Tests for {@link XmlBeanDefinitionReader}.
43+
*
3644
* @author Rick Evans
3745
* @author Juergen Hoeller
3846
* @author Sam Brannen
@@ -129,4 +137,28 @@ private void doTestValidation(String resourceName) {
129137
assertThat((TestBean) factory.getBean("testBean")).isNotNull();
130138
}
131139

140+
@Test
141+
void setValidationModeNameToUnsupportedValues() {
142+
assertThatIllegalArgumentException().isThrownBy(() -> reader.setValidationModeName(null));
143+
assertThatIllegalArgumentException().isThrownBy(() -> reader.setValidationModeName(" "));
144+
assertThatIllegalArgumentException().isThrownBy(() -> reader.setValidationModeName("bogus"));
145+
}
146+
147+
/**
148+
* This test effectively verifies that the internal 'constants' map is properly
149+
* configured for all VALIDATION_ constants defined in {@link XmlBeanDefinitionReader}.
150+
*/
151+
@Test
152+
void setValidationModeNameToAllSupportedValues() {
153+
streamValidationModeConstants()
154+
.map(Field::getName)
155+
.forEach(name -> assertThatNoException().as(name).isThrownBy(() -> reader.setValidationModeName(name)));
156+
}
157+
158+
private static Stream<Field> streamValidationModeConstants() {
159+
return Arrays.stream(XmlBeanDefinitionReader.class.getFields())
160+
.filter(ReflectionUtils::isPublicStaticFinal)
161+
.filter(field -> field.getName().startsWith("VALIDATION_"));
162+
}
163+
132164
}

0 commit comments

Comments
 (0)