Skip to content

Commit

Permalink
Reduce boilerplate further
Browse files Browse the repository at this point in the history
This adds more convenience methods to reduce boilerplate.

See: io7m/blackthorne#2
  • Loading branch information
io7m committed Oct 15, 2020
1 parent 85de503 commit 430b3d1
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 46 deletions.
2 changes: 1 addition & 1 deletion com.io7m.jxe.core/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.io7m.jxe</groupId>
<artifactId>com.io7m.jxe</artifactId>
<version>0.0.2</version>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.jxe.core</artifactId>
Expand Down
@@ -0,0 +1,103 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> 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<URI, JXESchemaDefinition> 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<URI, JXESchemaDefinition> mapOfList(
final List<JXESchemaDefinition> 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<JXESchemaDefinition> schemas)
{
Objects.requireNonNull(schemas, "schemas");
return JXESchemaResolutionMappings.builder()
.putAllMappings(mapOfList(schemas))
.build();
}
}
2 changes: 1 addition & 1 deletion com.io7m.jxe.documentation/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.io7m.jxe</groupId>
<artifactId>com.io7m.jxe</artifactId>
<version>0.0.2</version>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.jxe.documentation</artifactId>
Expand Down
11 changes: 8 additions & 3 deletions com.io7m.jxe.tests/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.io7m.jxe</groupId>
<artifactId>com.io7m.jxe</artifactId>
<version>0.0.2</version>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.jxe.tests</artifactId>
Expand All @@ -32,8 +32,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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));
});
}
}

Expand All @@ -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")
);
}
}

Expand All @@ -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")
);
}
}

Expand All @@ -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'")
);
}
}

Expand Down Expand Up @@ -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")
);
}
}

Expand Down Expand Up @@ -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")
);
}
}

Expand All @@ -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")
);
}
}

Expand Down
Expand Up @@ -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
{
Expand Down
Expand Up @@ -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
{
Expand All @@ -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")));
}
}
}

0 comments on commit 430b3d1

Please sign in to comment.