1616
1717package org .springframework .xml .xsd ;
1818
19- import javax .xml .parsers .DocumentBuilder ;
19+ import java .util .ArrayList ;
20+ import java .util .List ;
21+ import java .util .concurrent .CountDownLatch ;
22+ import java .util .concurrent .ExecutorService ;
23+ import java .util .concurrent .Executors ;
24+ import java .util .concurrent .Future ;
25+
2026import javax .xml .parsers .DocumentBuilderFactory ;
21- import javax .xml .transform .Transformer ;
2227import javax .xml .transform .TransformerFactory ;
2328import javax .xml .transform .dom .DOMResult ;
2429
25- import org .junit .jupiter .api .BeforeEach ;
2630import org .junit .jupiter .api .Test ;
2731import org .w3c .dom .Document ;
2832import org .xmlunit .assertj .XmlAssert ;
3842
3943public abstract class AbstractXsdSchemaTests {
4044
41- private DocumentBuilder documentBuilder ;
42-
43- protected Transformer transformer ;
44-
45- @ BeforeEach
46- public final void setUp () throws Exception {
47-
48- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtils .newInstance ();
49- documentBuilderFactory .setNamespaceAware (true );
50- this .documentBuilder = documentBuilderFactory .newDocumentBuilder ();
51- TransformerFactory transformerFactory = TransformerFactoryUtils .newInstance ();
52- this .transformer = transformerFactory .newTransformer ();
53- }
54-
5545 @ Test
5646 void testSingle () throws Exception {
5747
@@ -61,12 +51,10 @@ void testSingle() throws Exception {
6151 assertThat (single .getTargetNamespace ()).isEqualTo ("http://www.springframework.org/spring-ws/single/schema" );
6252
6353 resource = new ClassPathResource ("single.xsd" , AbstractXsdSchemaTests .class );
64- Document expected = this .documentBuilder .parse (SaxUtils .createInputSource (resource ));
65- DOMResult domResult = new DOMResult ();
66- this .transformer .transform (single .getSource (), domResult );
67- Document result = (Document ) domResult .getNode ();
54+ Document expected = createDocument (resource );
55+ Document actual = createDocument (single );
6856
69- XmlAssert .assertThat (result ).and (expected ).ignoreWhitespace ().areIdentical ();
57+ XmlAssert .assertThat (actual ).and (expected ).ignoreWhitespace ().areIdentical ();
7058 }
7159
7260 @ Test
@@ -78,12 +66,10 @@ void testIncludes() throws Exception {
7866 assertThat (including .getTargetNamespace ()).isEqualTo ("http://www.springframework.org/spring-ws/include/schema" );
7967
8068 resource = new ClassPathResource ("including.xsd" , AbstractXsdSchemaTests .class );
81- Document expected = this .documentBuilder .parse (SaxUtils .createInputSource (resource ));
82- DOMResult domResult = new DOMResult ();
83- this .transformer .transform (including .getSource (), domResult );
84- Document result = (Document ) domResult .getNode ();
69+ Document expected = createDocument (resource );
70+ Document actual = createDocument (including );
8571
86- XmlAssert .assertThat (result ).and (expected ).ignoreWhitespace ().areIdentical ();
72+ XmlAssert .assertThat (actual ).and (expected ).ignoreWhitespace ().areIdentical ();
8773 }
8874
8975 @ Test
@@ -96,12 +82,10 @@ void testImports() throws Exception {
9682 .isEqualTo ("http://www.springframework.org/spring-ws/importing/schema" );
9783
9884 resource = new ClassPathResource ("importing.xsd" , AbstractXsdSchemaTests .class );
99- Document expected = this .documentBuilder .parse (SaxUtils .createInputSource (resource ));
100- DOMResult domResult = new DOMResult ();
101- this .transformer .transform (importing .getSource (), domResult );
102- Document result = (Document ) domResult .getNode ();
85+ Document expected = createDocument (resource );
86+ Document actual = createDocument (importing );
10387
104- XmlAssert .assertThat (result ).and (expected ).ignoreWhitespace ().areIdentical ();
88+ XmlAssert .assertThat (actual ).and (expected ).ignoreWhitespace ().areIdentical ();
10589 }
10690
10791 @ Test
@@ -113,24 +97,63 @@ void testXmlNamespace() throws Exception {
11397 assertThat (importing .getTargetNamespace ()).isEqualTo ("http://www.springframework.org/spring-ws/xmlNamespace" );
11498
11599 resource = new ClassPathResource ("xmlNamespace.xsd" , AbstractXsdSchemaTests .class );
116- Document expected = this .documentBuilder .parse (SaxUtils .createInputSource (resource ));
117- DOMResult domResult = new DOMResult ();
118- this .transformer .transform (importing .getSource (), domResult );
119- Document result = (Document ) domResult .getNode ();
100+ Document expected = createDocument (resource );
101+ Document actual = createDocument (importing );
120102
121- XmlAssert .assertThat (result ).and (expected ).ignoreWhitespace ().areIdentical ();
103+ XmlAssert .assertThat (actual ).and (expected ).ignoreWhitespace ().areIdentical ();
122104 }
123105
124106 @ Test
125107 void testCreateValidator () throws Exception {
126-
127108 Resource resource = new ClassPathResource ("single.xsd" , AbstractXsdSchemaTests .class );
128109 XsdSchema single = createSchema (resource );
129110 XmlValidator validator = single .createValidator ();
130111
131112 assertThat (validator ).isNotNull ();
132113 }
133114
115+ @ Test
116+ void testLoadXsdSchemaConcurrently () throws Exception {
117+ ClassPathResource xsdResource = new ClassPathResource ("single.xsd" , AbstractXsdSchemaTests .class );
118+ XsdSchema xsdSchema = createSchema (xsdResource );
119+ CountDownLatch signal = new CountDownLatch (1 );
120+ CountDownLatch threadSignal = new CountDownLatch (4 );
121+ ExecutorService executorService = Executors .newFixedThreadPool (4 );
122+ try {
123+ List <Future <Document >> futures = new ArrayList <>();
124+ for (int i = 0 ; i < 4 ; i ++) {
125+ futures .add (executorService .submit (() -> {
126+ threadSignal .countDown ();
127+ signal .await ();
128+ return createDocument (xsdSchema );
129+ }));
130+ }
131+ threadSignal .await ();
132+ signal .countDown ();
133+ for (Future <Document > future : futures ) {
134+ Document actual = future .get ();
135+ Document expected = createDocument (xsdResource );
136+ XmlAssert .assertThat (actual ).and (expected ).ignoreWhitespace ().areIdentical ();
137+ }
138+ }
139+ finally {
140+ executorService .shutdownNow ();
141+ }
142+ }
143+
134144 protected abstract XsdSchema createSchema (Resource resource ) throws Exception ;
135145
146+ protected Document createDocument (Resource resource ) throws Exception {
147+ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtils .newInstance ();
148+ documentBuilderFactory .setNamespaceAware (true );
149+ return documentBuilderFactory .newDocumentBuilder ().parse (SaxUtils .createInputSource (resource ));
150+ }
151+
152+ private Document createDocument (XsdSchema schema ) throws Exception {
153+ DOMResult domResult = new DOMResult ();
154+ TransformerFactory transformerFactory = TransformerFactoryUtils .newInstance ();
155+ transformerFactory .newTransformer ().transform (schema .getSource (), domResult );
156+ return (Document ) domResult .getNode ();
157+ }
158+
136159}
0 commit comments