mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
Merge remote-tracking branch 'origin/configuration_api' into configuration_api
This commit is contained in:
commit
62550c42d8
20 changed files with 493 additions and 113 deletions
|
|
@ -0,0 +1,116 @@
|
|||
package de.kosit.validationtool.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
|
||||
import net.sf.saxon.s9api.XPathExecutable;
|
||||
|
||||
/**
|
||||
* Tests {@link XPathBuilder}.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public class XPathBuilderTest {
|
||||
|
||||
@Test
|
||||
public void testSimpleString() {
|
||||
final String name = RandomStringUtils.randomAlphanumeric(5);
|
||||
final XPathBuilder b = new XPathBuilder(name);
|
||||
b.setXpath("//*");
|
||||
final Result<XPathExecutable, String> result = b.build(Simple.createContentRepository());
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isTrue();
|
||||
assertThat(b.getNamespaces()).isNotNull();
|
||||
assertThat(b.getNamespaces()).isEmpty();
|
||||
assertThat(b.getXPath()).isNotEmpty();
|
||||
assertThat(b.getName()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringWithNamespace() {
|
||||
final String name = RandomStringUtils.randomAlphanumeric(5);
|
||||
final XPathBuilder b = new XPathBuilder(name);
|
||||
final Map<String, String> ns = new HashMap<>();
|
||||
ns.put("p", "http://somens");
|
||||
b.setNamespaces(ns);
|
||||
b.setXpath("//p:*");
|
||||
final Result<XPathExecutable, String> result = b.build(Simple.createContentRepository());
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isTrue();
|
||||
assertThat(b.getNamespaces()).isNotEmpty();
|
||||
assertThat(b.getXPath()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringWithUnknownNamespace() {
|
||||
final String name = RandomStringUtils.randomAlphanumeric(5);
|
||||
final XPathBuilder b = new XPathBuilder(name);
|
||||
final Map<String, String> ns = new HashMap<>();
|
||||
ns.put("p", "http://somens");
|
||||
b.setNamespaces(ns);
|
||||
b.setXpath("//u:*");
|
||||
final Result<XPathExecutable, String> result = b.build(Simple.createContentRepository());
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutable() {
|
||||
final String name = RandomStringUtils.randomAlphanumeric(5);
|
||||
final ContentRepository repository = Simple.createContentRepository();
|
||||
final XPathExecutable xpath = repository.createXPath("//*", Collections.emptyMap());
|
||||
final XPathBuilder b = new XPathBuilder(name);
|
||||
b.setExecutable(xpath);
|
||||
final Result<XPathExecutable, String> result = b.build(repository);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isTrue();
|
||||
assertThat(b.getNamespaces()).isEmpty();
|
||||
assertThat(b.getXPath()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutableWithNamespace() {
|
||||
final String name = RandomStringUtils.randomAlphanumeric(5);
|
||||
final ContentRepository repository = Simple.createContentRepository();
|
||||
final Map<String, String> ns = new HashMap<>();
|
||||
ns.put("p", "http://somens");
|
||||
final XPathExecutable xpath = repository.createXPath("//p:*", ns);
|
||||
final XPathBuilder b = new XPathBuilder(name);
|
||||
b.setExecutable(xpath);
|
||||
final Result<XPathExecutable, String> result = b.build(repository);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isTrue();
|
||||
assertThat(b.getNamespaces()).isNotEmpty();
|
||||
assertThat(b.getNamespaces()).containsKey("p");
|
||||
assertThat(b.getXPath()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoName() {
|
||||
final XPathBuilder b = new XPathBuilder(null);
|
||||
b.setXpath("//*");
|
||||
final Result<XPathExecutable, String> result = b.build(Simple.createContentRepository());
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isTrue();
|
||||
assertThat(b.getName()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoConfig() {
|
||||
final String name = RandomStringUtils.randomAlphanumeric(5);
|
||||
final XPathBuilder b = new XPathBuilder(name);
|
||||
final Result<XPathExecutable, String> result = b.build(Simple.createContentRepository());
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.isValid()).isFalse();
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import lombok.RequiredArgsConstructor;
|
|||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public class BaseResolverTest {
|
||||
public class BaseResolverConfigurationTest {
|
||||
|
||||
@RequiredArgsConstructor
|
||||
private class TestResolvingStrategy extends StrictRelativeResolvingStrategy {
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue