Skip to content

Commit

Permalink
Stop using Constants utility in XmlBeanDefinitionReader
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 16, 2023
1 parent d6e05dd commit 6f73351
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -68,6 +68,7 @@
* @author Juergen Hoeller
* @author Rob Harrop
* @author Chris Beams
* @author Sam Brannen
* @since 26.11.2003
* @see #setDocumentReaderClass
* @see BeanDefinitionDocumentReader
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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<Field> streamValidationModeConstants() {
return Arrays.stream(XmlBeanDefinitionReader.class.getFields())
.filter(ReflectionUtils::isPublicStaticFinal)
.filter(field -> field.getName().startsWith("VALIDATION_"));
}

}

0 comments on commit 6f73351

Please sign in to comment.