|
10 | 10 |
|
11 | 11 | package org.junit.platform.console.tasks;
|
12 | 12 |
|
| 13 | +import static org.junit.jupiter.api.Assertions.fail; |
| 14 | + |
13 | 15 | import java.io.StringReader;
|
14 | 16 | import java.net.URL;
|
15 |
| -import java.util.concurrent.atomic.AtomicReference; |
16 | 17 |
|
17 | 18 | import javax.xml.XMLConstants;
|
18 | 19 | import javax.xml.transform.stream.StreamSource;
|
19 | 20 | import javax.xml.validation.Schema;
|
20 | 21 | import javax.xml.validation.SchemaFactory;
|
21 | 22 | import javax.xml.validation.Validator;
|
22 | 23 |
|
23 |
| -import org.opentest4j.AssertionFailedError; |
24 | 24 | import org.xml.sax.SAXException;
|
25 | 25 |
|
26 | 26 | /**
|
27 | 27 | * @since 1.0
|
28 | 28 | */
|
29 | 29 | class XmlReportAssertions {
|
30 | 30 |
|
31 |
| - private static AtomicReference<Schema> schema = new AtomicReference<>(); |
32 |
| - |
33 | 31 | static void assertValidAccordingToJenkinsSchema(String content) throws Exception {
|
34 | 32 | try {
|
35 | 33 | // Schema is thread-safe, Validator is not
|
36 |
| - Validator validator = getSchema().newValidator(); |
| 34 | + Validator validator = CachedSchema.JENKINS.newValidator(); |
37 | 35 | validator.validate(new StreamSource(new StringReader(content)));
|
38 | 36 | }
|
39 | 37 | catch (SAXException e) {
|
40 |
| - throw new AssertionFailedError("Invalid XML document: " + content, e); |
| 38 | + fail("Invalid XML document: " + content, e); |
41 | 39 | }
|
42 | 40 | }
|
43 | 41 |
|
44 |
| - private static Schema getSchema() throws SAXException { |
45 |
| - if (schema.get() == null) { |
46 |
| - URL schemaFile = XmlReportsWritingListener.class.getResource("/jenkins-junit.xsd"); |
| 42 | + private enum CachedSchema { |
| 43 | + |
| 44 | + JENKINS("/jenkins-junit.xsd"); |
| 45 | + |
| 46 | + private final Schema schema; |
| 47 | + |
| 48 | + CachedSchema(String resourcePath) { |
| 49 | + URL schemaFile = XmlReportsWritingListener.class.getResource(resourcePath); |
47 | 50 | SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
48 |
| - Schema newSchema = schemaFactory.newSchema(schemaFile); |
49 |
| - schema.compareAndSet(null, newSchema); |
| 51 | + try { |
| 52 | + this.schema = schemaFactory.newSchema(schemaFile); |
| 53 | + } |
| 54 | + catch (SAXException e) { |
| 55 | + throw new RuntimeException("Failed to create schema using " + schemaFile, e); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Validator newValidator() { |
| 60 | + return schema.newValidator(); |
50 | 61 | }
|
51 |
| - return schema.get(); |
52 | 62 | }
|
53 | 63 |
|
54 | 64 | }
|
0 commit comments