From 6f733512b59543dc0bf79792d9c4aea3fb866ff0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 16 Jul 2023 13:23:02 +0200 Subject: [PATCH] Stop using Constants utility in XmlBeanDefinitionReader See gh-30851 --- .../factory/xml/XmlBeanDefinitionReader.java | 20 ++++++++--- .../xml/XmlBeanDefinitionReaderTests.java | 34 ++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java index 1804e3b48168..6951b078d86f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java @@ -19,6 +19,7 @@ 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.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 @@ * @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 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 void setValidating(boolean validating) { * @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); } /** diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java index d8ed2682554e..6807e24d9dfe 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java @@ -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.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 @@ private void doTestValidation(String resourceName) { 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 streamValidationModeConstants() { + return Arrays.stream(XmlBeanDefinitionReader.class.getFields()) + .filter(ReflectionUtils::isPublicStaticFinal) + .filter(field -> field.getName().startsWith("VALIDATION_")); + } + }