16
16
17
17
package org .springframework .beans .factory .xml ;
18
18
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 ;
22
21
import org .mockito .Mockito ;
23
22
import org .xml .sax .InputSource ;
24
- import org .xml .sax .SAXException ;
25
23
26
24
import org .springframework .core .io .Resource ;
27
25
import org .springframework .core .io .ResourceLoader ;
28
26
import org .springframework .lang .Nullable ;
29
27
30
28
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 ;
32
30
33
31
/**
32
+ * Unit tests for ResourceEntityResolver.
33
+ *
34
34
* @author Simon Baslé
35
+ * @author Sam Brannen
36
+ * @since 6.0.4
35
37
*/
36
38
class ResourceEntityResolverTests {
37
39
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 );
49
44
50
- assertThat (resolver .resolveEntity ("testPublicId" , "https://example.org/exampleschema.xsd" ))
51
- . isNull ();
45
+ assertThat (resolver .resolveEntity ("testPublicId" , systemId )). isNull ();
46
+ assertThat ( resolver . fallbackInvoked ). isFalse ();
52
47
}
53
48
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 );
57
53
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 ();
61
56
}
62
57
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 );
66
62
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 ( );
70
66
}
71
67
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 {
74
71
InputSource expected = Mockito .mock (InputSource .class );
75
- ResourceEntityResolver resolver = new FallingBackEntityResolver ( false , expected );
72
+ ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver ( expected );
76
73
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 ();
80
76
}
81
77
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
- }
99
78
100
79
private static final class NoOpResourceLoader implements ResourceLoader {
80
+
101
81
@ Override
102
82
public Resource getResource (String location ) {
103
83
return null ;
@@ -109,23 +89,40 @@ public ClassLoader getClassLoader() {
109
89
}
110
90
}
111
91
112
- private static class FallingBackEntityResolver extends ResourceEntityResolver {
92
+ private static class ConfigurableFallbackEntityResolver extends ResourceEntityResolver {
113
93
114
94
private final boolean shouldThrow ;
95
+
115
96
@ Nullable
116
97
private final InputSource returnValue ;
117
98
118
- private FallingBackEntityResolver (boolean shouldThrow , @ Nullable InputSource returnValue ) {
99
+ boolean fallbackInvoked = false ;
100
+
101
+
102
+ private ConfigurableFallbackEntityResolver (boolean shouldThrow ) {
119
103
super (new NoOpResourceLoader ());
120
104
this .shouldThrow = shouldThrow ;
105
+ this .returnValue = null ;
106
+ }
107
+
108
+ private ConfigurableFallbackEntityResolver (@ Nullable InputSource returnValue ) {
109
+ super (new NoOpResourceLoader ());
110
+ this .shouldThrow = false ;
121
111
this .returnValue = returnValue ;
122
112
}
123
113
114
+
124
115
@ Nullable
125
116
@ Override
126
117
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
+ }
128
122
return this .returnValue ;
129
123
}
130
124
}
125
+
126
+ static class ResolutionRejectedException extends RuntimeException {}
127
+
131
128
}
0 commit comments