Stop using Constants utility in XmlBeanDefinitionReader

See gh-30851
This commit is contained in:
Sam Brannen 2023-07-16 13:23:02 +02:00
parent d6e05ddf2d
commit 6f733512b5
2 changed files with 49 additions and 5 deletions

View File

@ -19,6 +19,7 @@ package org.springframework.beans.factory.xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
@ -40,7 +41,6 @@ import org.springframework.beans.factory.parsing.ReaderEventListener;
import org.springframework.beans.factory.parsing.SourceExtractor;
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.Constants;
import org.springframework.core.NamedThreadLocal;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
@ -68,6 +68,7 @@ import org.springframework.util.xml.XmlValidationModeDetector;
* @author Juergen Hoeller
* @author Rob Harrop
* @author Chris Beams
* @author Sam Brannen
* @since 26.11.2003
* @see #setDocumentReaderClass
* @see BeanDefinitionDocumentReader
@ -99,8 +100,16 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
public static final int VALIDATION_XSD = XmlValidationModeDetector.VALIDATION_XSD;
/** Constants instance for this class. */
private static final Constants constants = new Constants(XmlBeanDefinitionReader.class);
/**
* Map of constant names to constant values for the validation constants defined
* in this class.
*/
private static final Map<String, Integer> constants = Map.of(
"VALIDATION_NONE", VALIDATION_NONE,
"VALIDATION_AUTO", VALIDATION_AUTO,
"VALIDATION_DTD", VALIDATION_DTD,
"VALIDATION_XSD", VALIDATION_XSD
);
private int validationMode = VALIDATION_AUTO;
@ -163,7 +172,10 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
* @see #setValidationMode
*/
public void setValidationModeName(String validationModeName) {
setValidationMode(constants.asNumber(validationModeName).intValue());
Assert.hasText(validationModeName, "'validationModeName' must not be null or blank");
Integer mode = constants.get(validationModeName);
Assert.notNull(mode, "Only validation mode constants allowed");
setValidationMode(mode);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,10 @@
package org.springframework.beans.factory.xml;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.xml.sax.InputSource;
@ -27,12 +31,16 @@ import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link XmlBeanDefinitionReader}.
*
* @author Rick Evans
* @author Juergen Hoeller
* @author Sam Brannen
@ -129,4 +137,28 @@ class XmlBeanDefinitionReaderTests {
assertThat((TestBean) factory.getBean("testBean")).isNotNull();
}
@Test
void setValidationModeNameToUnsupportedValues() {
assertThatIllegalArgumentException().isThrownBy(() -> reader.setValidationModeName(null));
assertThatIllegalArgumentException().isThrownBy(() -> reader.setValidationModeName(" "));
assertThatIllegalArgumentException().isThrownBy(() -> reader.setValidationModeName("bogus"));
}
/**
* This test effectively verifies that the internal 'constants' map is properly
* configured for all VALIDATION_ constants defined in {@link XmlBeanDefinitionReader}.
*/
@Test
void setValidationModeNameToAllSupportedValues() {
streamValidationModeConstants()
.map(Field::getName)
.forEach(name -> assertThatNoException().as(name).isThrownBy(() -> reader.setValidationModeName(name)));
}
private static Stream<Field> streamValidationModeConstants() {
return Arrays.stream(XmlBeanDefinitionReader.class.getFields())
.filter(ReflectionUtils::isPublicStaticFinal)
.filter(field -> field.getName().startsWith("VALIDATION_"));
}
}