From 430b3d18eba927bc5cf8fd66cbd8986732222cbd Mon Sep 17 00:00:00 2001 From: Mark Raynsford Date: Thu, 15 Oct 2020 13:31:19 +0000 Subject: [PATCH] Reduce boilerplate further This adds more convenience methods to reduce boilerplate. See: https://github.com/io7m/blackthorne/issues/2 --- com.io7m.jxe.core/pom.xml | 2 +- .../io7m/jxe/core/JXESchemaDefinitions.java | 103 ++++++++++++++++++ com.io7m.jxe.documentation/pom.xml | 2 +- com.io7m.jxe.tests/pom.xml | 11 +- .../tests/core/JXEHardenedSAXParsersTest.java | 87 +++++++++------ .../tests/core/JXESchemaDefinitionTest.java | 2 +- .../core/JXESchemaResolutionMappingsTest.java | 60 +++++++++- pom.xml | 14 ++- 8 files changed, 235 insertions(+), 46 deletions(-) create mode 100644 com.io7m.jxe.core/src/main/java/com/io7m/jxe/core/JXESchemaDefinitions.java diff --git a/com.io7m.jxe.core/pom.xml b/com.io7m.jxe.core/pom.xml index 3e226a6..df2a2ce 100644 --- a/com.io7m.jxe.core/pom.xml +++ b/com.io7m.jxe.core/pom.xml @@ -9,7 +9,7 @@ com.io7m.jxe com.io7m.jxe - 0.0.2 + 1.0.0-SNAPSHOT com.io7m.jxe.core diff --git a/com.io7m.jxe.core/src/main/java/com/io7m/jxe/core/JXESchemaDefinitions.java b/com.io7m.jxe.core/src/main/java/com/io7m/jxe/core/JXESchemaDefinitions.java new file mode 100644 index 0000000..c4b9774 --- /dev/null +++ b/com.io7m.jxe.core/src/main/java/com/io7m/jxe/core/JXESchemaDefinitions.java @@ -0,0 +1,103 @@ +/* + * Copyright © 2020 Mark Raynsford http://io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +package com.io7m.jxe.core; + +import java.net.URI; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * Functions over schema definitions. + */ + +public final class JXESchemaDefinitions +{ + private JXESchemaDefinitions() + { + + } + + /** + * Make a map of schema definitions from the given list of schemas. + * + * @param schemas The list of schemas + * + * @return The schemas + */ + + public static Map mapOf( + final JXESchemaDefinition... schemas) + { + Objects.requireNonNull(schemas, "schemas"); + return mapOfList(List.of(schemas)); + } + + /** + * Make a map of schema definitions from the given list of schemas. + * + * @param schemas The list of schemas + * + * @return The schemas + */ + + public static Map mapOfList( + final List schemas) + { + Objects.requireNonNull(schemas, "schemas"); + return schemas.stream().collect(Collectors.toMap( + JXESchemaDefinition::namespace, + Function.identity() + )); + } + + /** + * Make a map of schema definitions from the given list of schemas. + * + * @param schemas The list of schemas + * + * @return The schemas + */ + + public static JXESchemaResolutionMappings mappingsOf( + final JXESchemaDefinition... schemas) + { + Objects.requireNonNull(schemas, "schemas"); + return JXESchemaResolutionMappings.builder() + .putAllMappings(mapOf(schemas)) + .build(); + } + + /** + * Make a map of schema definitions from the given list of schemas. + * + * @param schemas The list of schemas + * + * @return The schemas + */ + + public static JXESchemaResolutionMappings mappingsOfList( + final List schemas) + { + Objects.requireNonNull(schemas, "schemas"); + return JXESchemaResolutionMappings.builder() + .putAllMappings(mapOfList(schemas)) + .build(); + } +} diff --git a/com.io7m.jxe.documentation/pom.xml b/com.io7m.jxe.documentation/pom.xml index 1615ac8..6578cad 100644 --- a/com.io7m.jxe.documentation/pom.xml +++ b/com.io7m.jxe.documentation/pom.xml @@ -9,7 +9,7 @@ com.io7m.jxe com.io7m.jxe - 0.0.2 + 1.0.0-SNAPSHOT com.io7m.jxe.documentation diff --git a/com.io7m.jxe.tests/pom.xml b/com.io7m.jxe.tests/pom.xml index 0ca186a..411b059 100644 --- a/com.io7m.jxe.tests/pom.xml +++ b/com.io7m.jxe.tests/pom.xml @@ -9,7 +9,7 @@ com.io7m.jxe com.io7m.jxe - 0.0.2 + 1.0.0-SNAPSHOT com.io7m.jxe.tests @@ -32,8 +32,13 @@ test - junit - junit + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-api test diff --git a/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXEHardenedSAXParsersTest.java b/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXEHardenedSAXParsersTest.java index b1bbe41..e1ca869 100644 --- a/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXEHardenedSAXParsersTest.java +++ b/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXEHardenedSAXParsersTest.java @@ -20,11 +20,9 @@ import com.io7m.jxe.core.JXESchemaDefinition; import com.io7m.jxe.core.JXESchemaResolutionMappings; import com.io7m.jxe.core.JXEXInclude; -import org.hamcrest.core.StringContains; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -45,9 +43,7 @@ public final class JXEHardenedSAXParsersTest private JXEHardenedSAXParsers parsers; private Path tmpdir; - @Rule public ExpectedException expected = ExpectedException.none(); - - @Before + @BeforeEach public void setUp() throws IOException { @@ -99,8 +95,9 @@ public void testParseNonValidatingIllFormed() try (InputStream input = Files.newInputStream(this.copyResource("simple_ill_formed.xml"))) { - this.expected.expect(SAXParseException.class); - reader.parse(new InputSource(input)); + Assertions.assertThrows(SAXParseException.class, () -> { + reader.parse(new InputSource(input)); + }); } } @@ -116,10 +113,14 @@ public void testParseNonValidatingRefuseTraversal() try (InputStream input = Files.newInputStream(this.copyResource("simple_refuse_traversal.xml"))) { - this.expected.expect(SAXException.class); - this.expected.expectMessage(StringContains.containsString( - "Refusing to allow access to files above the base directory")); - reader.parse(new InputSource(input)); + final var ex = + Assertions.assertThrows(SAXException.class, () -> { + reader.parse(new InputSource(input)); + }); + Assertions.assertTrue( + ex.getMessage().contains( + "Refusing to allow access to files above the base directory") + ); } } @@ -135,10 +136,13 @@ public void testParseNonValidatingRefuseNetwork() try (InputStream input = Files.newInputStream(this.copyResource("simple_refuse_network.xml"))) { - this.expected.expect(SAXException.class); - this.expected.expectMessage(StringContains.containsString( - "Refusing to resolve a non-file URI")); - reader.parse(new InputSource(input)); + final var ex = + Assertions.assertThrows(SAXException.class, () -> { + reader.parse(new InputSource(input)); + }); + Assertions.assertTrue( + ex.getMessage().contains("Refusing to resolve a non-file URI") + ); } } @@ -161,10 +165,14 @@ public void testParseValidatingNoSchemas() try (InputStream input = Files.newInputStream(this.copyResource("simple.xml"))) { - this.expected.expect(SAXException.class); - this.expected.expectMessage(StringContains.containsString( - "Cannot find the declaration of element 'simple'")); - reader.parse(new InputSource(input)); + final var ex = + Assertions.assertThrows(SAXParseException.class, () -> { + reader.parse(new InputSource(input)); + }); + Assertions.assertTrue( + ex.getMessage().contains( + "Cannot find the declaration of element 'simple'") + ); } } @@ -212,10 +220,14 @@ public void testParseNonValidatingNotAFile() try (InputStream input = Files.newInputStream(this.copyResource("simple_invalid_not_file.xml"))) { - this.expected.expect(SAXParseException.class); - this.expected.expectMessage(StringContains.containsString( - "File does not exist or is not a regular file")); - reader.parse(new InputSource(input)); + + final var ex = + Assertions.assertThrows(SAXParseException.class, () -> { + reader.parse(new InputSource(input)); + }); + Assertions.assertTrue( + ex.getMessage().contains("File does not exist or is not a regular file") + ); } } @@ -252,9 +264,13 @@ public void testParseNonValidatingRegularFileNoFilesystem() try (InputStream input = Files.newInputStream(this.copyResource("simple_regular_file.xml"))) { - this.expected.expect(SAXException.class); - this.expected.expectMessage(StringContains.containsString("Refusing to allow access to the filesystem")); - reader.parse(new InputSource(input)); + final var ex = + Assertions.assertThrows(SAXException.class, () -> { + reader.parse(new InputSource(input)); + }); + Assertions.assertTrue( + ex.getMessage().contains("Refusing to allow access to the filesystem") + ); } } @@ -271,10 +287,15 @@ public void testBillionLaughs() try (InputStream input = Files.newInputStream(this.copyResource("billion.xml"))) { - this.expected.expect(SAXException.class); - this.expected.expectMessage(StringContains.containsString( - "External subsets are explicitly forbidden by this parser configuration")); - reader.parse(new InputSource(input)); + + final var ex = + Assertions.assertThrows(SAXException.class, () -> { + reader.parse(new InputSource(input)); + }); + Assertions.assertTrue( + ex.getMessage().contains( + "External subsets are explicitly forbidden by this parser configuration") + ); } } diff --git a/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaDefinitionTest.java b/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaDefinitionTest.java index 59c42e2..d55f902 100644 --- a/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaDefinitionTest.java +++ b/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaDefinitionTest.java @@ -16,11 +16,11 @@ package com.io7m.jxe.tests.core; -import org.junit.Test; import com.io7m.jxe.core.JXESchemaDefinition; import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; public final class JXESchemaDefinitionTest { diff --git a/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaResolutionMappingsTest.java b/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaResolutionMappingsTest.java index 77db8a4..017aa20 100644 --- a/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaResolutionMappingsTest.java +++ b/com.io7m.jxe.tests/src/test/java/com/io7m/jxe/tests/core/JXESchemaResolutionMappingsTest.java @@ -16,12 +16,16 @@ package com.io7m.jxe.tests.core; -import org.junit.Test; - import com.io7m.jxe.core.JXESchemaDefinition; +import com.io7m.jxe.core.JXESchemaDefinitions; import com.io7m.jxe.core.JXESchemaResolutionMappings; - import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.net.URL; +import java.util.List; public final class JXESchemaResolutionMappingsTest { @@ -32,4 +36,54 @@ public void testEqualsHashCode() .withNonnullFields("mappings") .verify(); } + + @Test + public void testOf() + throws Exception + { + final var schema0 = + JXESchemaDefinition.of( + URI.create("urn:test0"), + "test0.xsd", + new URL("http://www.example.com/test0.xsd") + ); + final var schema1 = + JXESchemaDefinition.of( + URI.create("urn:test1"), + "test1.xsd", + new URL("http://www.example.com/test1.xsd") + ); + + { + final var mappings = + JXESchemaDefinitions.mapOf(schema0, schema1); + Assertions.assertEquals(2, mappings.size()); + Assertions.assertEquals(schema0, mappings.get(URI.create("urn:test0"))); + Assertions.assertEquals(schema1, mappings.get(URI.create("urn:test1"))); + } + + { + final var mappings = + JXESchemaDefinitions.mapOfList(List.of(schema0, schema1)); + Assertions.assertEquals(2, mappings.size()); + Assertions.assertEquals(schema0, mappings.get(URI.create("urn:test0"))); + Assertions.assertEquals(schema1, mappings.get(URI.create("urn:test1"))); + } + + { + final var mappings = + JXESchemaDefinitions.mappingsOfList(List.of(schema0, schema1)); + Assertions.assertEquals(2, mappings.mappings().size()); + Assertions.assertEquals(schema0, mappings.mappings().get(URI.create("urn:test0"))); + Assertions.assertEquals(schema1, mappings.mappings().get(URI.create("urn:test1"))); + } + + { + final var mappings = + JXESchemaDefinitions.mappingsOf(schema0, schema1); + Assertions.assertEquals(2, mappings.mappings().size()); + Assertions.assertEquals(schema0, mappings.mappings().get(URI.create("urn:test0"))); + Assertions.assertEquals(schema1, mappings.mappings().get(URI.create("urn:test1"))); + } + } } diff --git a/pom.xml b/pom.xml index d8a6742..7a64cea 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.io7m.jxe com.io7m.jxe - 0.0.2 + 1.0.0-SNAPSHOT pom com.io7m.jxe @@ -28,6 +28,7 @@ 6e6f69d7eb5d41e9a2ceb3fa28ab7b0a 2.6.0 0.0.1 + 5.7.0 2017 @@ -113,9 +114,14 @@ - junit - junit - 4.13 + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + + + org.junit.jupiter + junit-jupiter-api + ${junit.version} nl.jqno.equalsverifier