Skip to content

Commit 8df4f92

Browse files
committed
Polish contribution
See gh-29697
1 parent 5965917 commit 8df4f92

File tree

2 files changed

+59
-60
lines changed

2 files changed

+59
-60
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,20 @@ else if (systemId.endsWith(DTD_SUFFIX) || systemId.endsWith(XSD_SUFFIX)) {
120120
/**
121121
* A fallback method for {@link #resolveEntity(String, String)} that is used when a
122122
* "schema" entity (DTD or XSD) cannot be resolved as a local resource. The default
123-
* behavior is to perform a remote resolution over HTTPS.
123+
* behavior is to perform remote resolution over HTTPS.
124124
* <p>Subclasses can override this method to change the default behavior.
125125
* <ul>
126126
* <li>Return {@code null} to fall back to the parser's
127127
* {@linkplain org.xml.sax.EntityResolver#resolveEntity(String, String) default behavior}.</li>
128-
* <li>Throw an exception to prevent remote resolution of the XSD or DTD.</li>
128+
* <li>Throw an exception to prevent remote resolution of the DTD or XSD.</li>
129129
* </ul>
130130
* @param publicId the public identifier of the external entity being referenced,
131131
* or null if none was supplied
132-
* @param systemId the system identifier of the external entity being referenced
132+
* @param systemId the system identifier of the external entity being referenced,
133+
* representing the URL of the DTD or XSD
133134
* @return an InputSource object describing the new input source, or null to request
134-
* that the parser open a regular URI connection to the system identifier.
135+
* that the parser open a regular URI connection to the system identifier
136+
* @since 6.0.4
135137
*/
136138
@Nullable
137139
protected InputSource resolveSchemaEntity(@Nullable String publicId, String systemId) {

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

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,88 +16,68 @@
1616

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

19-
import java.io.IOException;
20-
21-
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
2221
import org.mockito.Mockito;
2322
import org.xml.sax.InputSource;
24-
import org.xml.sax.SAXException;
2523

2624
import org.springframework.core.io.Resource;
2725
import org.springframework.core.io.ResourceLoader;
2826
import org.springframework.lang.Nullable;
2927

3028
import static org.assertj.core.api.Assertions.assertThat;
31-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
29+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3230

3331
/**
32+
* Unit tests for ResourceEntityResolver.
33+
*
3434
* @author Simon Baslé
35+
* @author Sam Brannen
36+
* @since 6.0.4
3537
*/
3638
class ResourceEntityResolverTests {
3739

38-
@Test
39-
void resolveEntityCallsFallbackWithNullOnDtd() throws IOException, SAXException {
40-
ResourceEntityResolver resolver = new FallingBackEntityResolver(false, null);
41-
42-
assertThat(resolver.resolveEntity("testPublicId", "https://example.org/exampleschema.dtd"))
43-
.isNull();
44-
}
45-
46-
@Test
47-
void resolveEntityCallsFallbackWithNullOnXsd() throws IOException, SAXException {
48-
ResourceEntityResolver resolver = new FallingBackEntityResolver(false, null);
40+
@ParameterizedTest
41+
@ValueSource(strings = { "https://example.org/schema/", "https://example.org/schema.xml" })
42+
void resolveEntityDoesNotCallFallbackIfNotSchema(String systemId) throws Exception {
43+
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(true);
4944

50-
assertThat(resolver.resolveEntity("testPublicId", "https://example.org/exampleschema.xsd"))
51-
.isNull();
45+
assertThat(resolver.resolveEntity("testPublicId", systemId)).isNull();
46+
assertThat(resolver.fallbackInvoked).isFalse();
5247
}
5348

54-
@Test
55-
void resolveEntityCallsFallbackWithThrowOnDtd() {
56-
ResourceEntityResolver resolver = new FallingBackEntityResolver(true, null);
49+
@ParameterizedTest
50+
@ValueSource(strings = { "https://example.org/schema.dtd", "https://example.org/schema.xsd" })
51+
void resolveEntityCallsFallbackThatReturnsNull(String systemId) throws Exception {
52+
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(null);
5753

58-
assertThatIllegalStateException().isThrownBy(
59-
() -> resolver.resolveEntity("testPublicId", "https://example.org/exampleschema.dtd"))
60-
.withMessage("FallingBackEntityResolver that throws");
54+
assertThat(resolver.resolveEntity("testPublicId", systemId)).isNull();
55+
assertThat(resolver.fallbackInvoked).isTrue();
6156
}
6257

63-
@Test
64-
void resolveEntityCallsFallbackWithThrowOnXsd() {
65-
ResourceEntityResolver resolver = new FallingBackEntityResolver(true, null);
58+
@ParameterizedTest
59+
@ValueSource(strings = { "https://example.org/schema.dtd", "https://example.org/schema.xsd" })
60+
void resolveEntityCallsFallbackThatThrowsException(String systemId) {
61+
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(true);
6662

67-
assertThatIllegalStateException().isThrownBy(
68-
() -> resolver.resolveEntity("testPublicId", "https://example.org/exampleschema.xsd"))
69-
.withMessage("FallingBackEntityResolver that throws");
63+
assertThatExceptionOfType(ResolutionRejectedException.class)
64+
.isThrownBy(() -> resolver.resolveEntity("testPublicId", systemId));
65+
assertThat(resolver.fallbackInvoked).isTrue();
7066
}
7167

72-
@Test
73-
void resolveEntityCallsFallbackWithInputSourceOnDtd() throws IOException, SAXException {
68+
@ParameterizedTest
69+
@ValueSource(strings = { "https://example.org/schema.dtd", "https://example.org/schema.xsd" })
70+
void resolveEntityCallsFallbackThatReturnsInputSource(String systemId) throws Exception {
7471
InputSource expected = Mockito.mock(InputSource.class);
75-
ResourceEntityResolver resolver = new FallingBackEntityResolver(false, expected);
72+
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(expected);
7673

77-
assertThat(resolver.resolveEntity("testPublicId", "https://example.org/exampleschema.dtd"))
78-
.isNotNull()
79-
.isSameAs(expected);
74+
assertThat(resolver.resolveEntity("testPublicId", systemId)).isSameAs(expected);
75+
assertThat(resolver.fallbackInvoked).isTrue();
8076
}
8177

82-
@Test
83-
void resolveEntityCallsFallbackWithInputSourceOnXsd() throws IOException, SAXException {
84-
InputSource expected = Mockito.mock(InputSource.class);
85-
ResourceEntityResolver resolver = new FallingBackEntityResolver(false, expected);
86-
87-
assertThat(resolver.resolveEntity("testPublicId", "https://example.org/exampleschema.xsd"))
88-
.isNotNull()
89-
.isSameAs(expected);
90-
}
91-
92-
@Test
93-
void resolveEntityDoesntCallFallbackIfNotSchema() throws IOException, SAXException {
94-
ResourceEntityResolver resolver = new FallingBackEntityResolver(true, null);
95-
96-
assertThat(resolver.resolveEntity("testPublicId", "https://example.org/example.xml"))
97-
.isNull();
98-
}
9978

10079
private static final class NoOpResourceLoader implements ResourceLoader {
80+
10181
@Override
10282
public Resource getResource(String location) {
10383
return null;
@@ -109,23 +89,40 @@ public ClassLoader getClassLoader() {
10989
}
11090
}
11191

112-
private static class FallingBackEntityResolver extends ResourceEntityResolver {
92+
private static class ConfigurableFallbackEntityResolver extends ResourceEntityResolver {
11393

11494
private final boolean shouldThrow;
95+
11596
@Nullable
11697
private final InputSource returnValue;
11798

118-
private FallingBackEntityResolver(boolean shouldThrow, @Nullable InputSource returnValue) {
99+
boolean fallbackInvoked = false;
100+
101+
102+
private ConfigurableFallbackEntityResolver(boolean shouldThrow) {
119103
super(new NoOpResourceLoader());
120104
this.shouldThrow = shouldThrow;
105+
this.returnValue = null;
106+
}
107+
108+
private ConfigurableFallbackEntityResolver(@Nullable InputSource returnValue) {
109+
super(new NoOpResourceLoader());
110+
this.shouldThrow = false;
121111
this.returnValue = returnValue;
122112
}
123113

114+
124115
@Nullable
125116
@Override
126117
protected InputSource resolveSchemaEntity(String publicId, String systemId) {
127-
if (shouldThrow) throw new IllegalStateException("FallingBackEntityResolver that throws");
118+
this.fallbackInvoked = true;
119+
if (this.shouldThrow) {
120+
throw new ResolutionRejectedException();
121+
}
128122
return this.returnValue;
129123
}
130124
}
125+
126+
static class ResolutionRejectedException extends RuntimeException {}
127+
131128
}

0 commit comments

Comments
 (0)