create tests for resolvers

This commit is contained in:
Andreas Penski (init) 2020-05-04 16:27:55 +02:00
parent 7671977249
commit edb8427dec
11 changed files with 187 additions and 94 deletions

View file

@ -19,7 +19,6 @@
package de.kosit.validationtool.impl;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
@ -84,7 +83,6 @@ public class Helper {
return new ContentRepository(strategy, Simple.REPOSITORY_URI);
}
public static URI getSchemaLocation() {
return SCHEMA;
}
@ -99,6 +97,15 @@ public class Helper {
public static final URI SCENARIOS_ILLFORMED = ROOT.resolve("scenarios-illformed.xml");
}
public static class Resolving {
public static final URI ROOT = EXAMPLES_DIR.resolve("resolving/");
public static final URI SCHEMA_WITH_REMOTE_REFERENCE = ROOT.resolve("withRemote.xsd");
public static final URI SCHEMA_WITH_REFERENCE = ROOT.resolve("main.xsd");
}
public static final URI MODEL_ROOT = Paths.get("src/main/model").toUri();
public static final URI ASSERTION_SCHEMA = MODEL_ROOT.resolve("xsd/assertions.xsd");
@ -135,16 +142,6 @@ public class Helper {
return c.readXml(url.toURI(), type);
}
/**
* Lädt das default test repository mit Artefacten für Unit-Tests
*
* @return ein {@link ContentRepository}
*/
public static ContentRepository loadTestRepository() {
return new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(),
new File("src/test/resources/examples/repository").toURI());
}
public static String serialize(final XdmNode node) {
try ( final StringWriter writer = new StringWriter() ) {
final Processor processor = Helper.getTestProcessor();

View file

@ -1,94 +1,19 @@
package de.kosit.validationtool.impl;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import de.kosit.validationtool.impl.xml.StrictLocalResolvingStrategy;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerException;
import net.sf.saxon.Configuration;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.CollectionFinder;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.lib.OutputURIResolver;
import net.sf.saxon.lib.ResourceCollection;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.trans.XPathException;
/**
* @author Andreas Penski
*/
public class TestObjectFactory {
private static class SecureUriResolver implements CollectionFinder, OutputURIResolver, UnparsedTextURIResolver {
public static final String MESSAGE = "Configuration error. Resolving ist not allowed";
@Override
public OutputURIResolver newInstance() {
return this;
}
@Override
public Result resolve(final String href, final String base) throws TransformerException {
throw new IllegalStateException(MESSAGE);
}
@Override
public void close(final Result result) throws TransformerException {
throw new IllegalStateException(MESSAGE);
}
@Override
public Reader resolve(final URI absoluteURI, final String encoding, final Configuration config) throws XPathException {
throw new IllegalStateException(MESSAGE);
}
@Override
public ResourceCollection findCollection(final XPathContext context, final String collectionURI) throws XPathException {
throw new IllegalStateException(MESSAGE);
}
}
private static final String DISSALLOW_DOCTYPE_DECL_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
private static final String LOAD_EXTERNAL_DTD_FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
private static final String FEATURE_SECURE_PROCESSING = "http://javax.xml.XMLConstants/feature/secure-processing";
private static Processor processor;
private static String encode(final String input) {
try {
return URLEncoder.encode(input, StandardCharsets.UTF_8.name());
} catch (final UnsupportedEncodingException e) {
throw new IllegalStateException("Error encoding property while initializing saxon", e);
}
}
public static Processor createProcessor() {
if (processor == null) {
processor = new Processor(false);
// verhindere global im Prinzip alle resolving strategien
final SecureUriResolver resolver = new SecureUriResolver();
processor.getUnderlyingConfiguration().setCollectionFinder(resolver);
processor.getUnderlyingConfiguration().setOutputURIResolver(resolver);
processor.getUnderlyingConfiguration().setUnparsedTextURIResolver(resolver);
// grundsätzlich Feature-konfiguration:
processor.setConfigurationProperty(FeatureKeys.DTD_VALIDATION, false);
processor.setConfigurationProperty(FeatureKeys.ENTITY_RESOLVER_CLASS, "");
processor.setConfigurationProperty(FeatureKeys.XINCLUDE, false);
processor.setConfigurationProperty(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS, false);
// Konfiguration des zu verwendenden Parsers, wenn Saxon selbst einen erzeugen muss, bspw. beim XSL parsen
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(FEATURE_SECURE_PROCESSING), true);
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(DISSALLOW_DOCTYPE_DECL_FEATURE), true);
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(LOAD_EXTERNAL_DTD_FEATURE), false);
processor = new StrictLocalResolvingStrategy().getProcessor();
}
return processor;
}

View file

@ -23,7 +23,7 @@ import lombok.RequiredArgsConstructor;
*
* @author Andreas Penski
*/
public class BaseResolverTest {
public class BaseResolverConfigurationTest {
@RequiredArgsConstructor
private class TestResolvingStrategy extends StrictRelativeResolvingStrategy {

View file

@ -0,0 +1,41 @@
package de.kosit.validationtool.impl.xml;
import static org.assertj.core.api.Assertions.assertThat;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
import de.kosit.validationtool.impl.Helper.Resolving;
/**
* Tests {@link RemoteResolvingStrategy}.
*
* @author Andreas Penski
*/
public class RemoteResolvingStrategyTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testRemoteSchemaResolving() throws Exception {
final ResolvingConfigurationStrategy s = new RemoteResolvingStrategy();
final SchemaFactory schemaFactory = s.createSchemaFactory();
final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REMOTE_REFERENCE.toURL());
assertThat(schema).isNotNull();
}
@Test
public void testLocalSchemaResolving() throws Exception {
final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy();
final SchemaFactory schemaFactory = s.createSchemaFactory();
final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REFERENCE.toURL());
assertThat(schema).isNotNull();
}
}

View file

@ -17,7 +17,7 @@
* under the License.
*/
package de.kosit.validationtool.impl;
package de.kosit.validationtool.impl.xml;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@ -35,9 +35,10 @@ import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.impl.Helper;
import de.kosit.validationtool.impl.Helper.Simple;
import de.kosit.validationtool.impl.TestObjectFactory;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.impl.xml.RelativeUriResolver;
import de.kosit.validationtool.model.reportInput.XMLSyntaxError;
import net.sf.saxon.s9api.Processor;

View file

@ -0,0 +1,43 @@
package de.kosit.validationtool.impl.xml;
import static org.assertj.core.api.Assertions.assertThat;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.xml.sax.SAXParseException;
import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
import de.kosit.validationtool.impl.Helper.Resolving;
/**
* Tests {@link StrictLocalResolvingStrategy}
*
* @author Andreas Penski
*/
public class StrictLocalResolvingTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testRemoteSchemaResolving() throws Exception {
this.expectedException.expect(SAXParseException.class);
this.expectedException.expectMessage(Matchers.containsString("schema_reference"));
final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy();
final SchemaFactory schemaFactory = s.createSchemaFactory();
schemaFactory.newSchema(Resolving.SCHEMA_WITH_REMOTE_REFERENCE.toURL());
}
@Test
public void testLocalSchemaResolving() throws Exception {
final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy();
final SchemaFactory schemaFactory = s.createSchemaFactory();
final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REFERENCE.toURL());
assertThat(schema).isNotNull();
}
}

View file

@ -0,0 +1,45 @@
package de.kosit.validationtool.impl.xml;
import static org.assertj.core.api.Assertions.assertThat;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.xml.sax.SAXParseException;
import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
import de.kosit.validationtool.impl.Helper.Resolving;
/**
* Tests {@link StrictRelativeResolvingStrategy}.
*
* @author Andreas Penski
*/
public class StrictRelativeResolvingTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testRemoteSchemaResolving() throws Exception {
this.expectedException.expect(SAXParseException.class);
this.expectedException.expectMessage(Matchers.containsString("schema_reference"));
final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy();
final SchemaFactory schemaFactory = s.createSchemaFactory();
schemaFactory.newSchema(Resolving.SCHEMA_WITH_REMOTE_REFERENCE.toURL());
}
@Test
public void testLocalSchemaResolving() throws Exception {
final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy();
final SchemaFactory schemaFactory = s.createSchemaFactory();
final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REFERENCE.toURL());
assertThat(schema).isNotNull();
}
// TODO loading schema from location outside of the repository - this is still possible yet
}