#39 The supplied Input unnecessarily is not written into memory

This commit is contained in:
Andreas Penski 2019-12-18 15:57:44 +01:00
parent d7ee019194
commit efd4fd5fff
63 changed files with 1111 additions and 18196 deletions

View file

@ -36,6 +36,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.impl.Helper.Simple;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XsltExecutable;
@ -53,7 +55,7 @@ public class ContentRepositoryTest {
@Before
public void setup() {
this.repository = new ContentRepository(ObjectFactory.createProcessor(), Helper.REPOSITORY);
this.repository = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY);
}
@Test
@ -71,19 +73,19 @@ public class ContentRepositoryTest {
@Test
public void testCreateSchemaNotExisting() throws Exception {
this.exception.expect(IllegalStateException.class);
ContentRepository.createSchema(Helper.ASSERTION_SCHEMA.resolve("noexisting").toURL());
ContentRepository.createSchema(Simple.NOT_EXISTING.toURL());
}
@Test
public void testLoadXSLT() {
final XsltExecutable executable = this.repository.loadXsltScript(Helper.SAMPLE_XSLT);
final XsltExecutable executable = this.repository.loadXsltScript(Simple.REPORT_XSL);
assertThat(executable).isNotNull();
}
@Test
public void testLoadXSLTNotExisting() {
this.exception.expect(IllegalStateException.class);
this.repository.loadXsltScript(Helper.SAMPLE_XSLT.resolve("notexisting"));
this.repository.loadXsltScript(Simple.NOT_EXISTING);
}
@Test
@ -119,8 +121,8 @@ public class ContentRepositoryTest {
@Test
public void testLoadSchema() {
final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("simple/main.xsd");
final Schema schema = ContentRepository.createSchema(main, new ClassPathResourceResolver("/simple"));
final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("loading/main.xsd");
final Schema schema = ContentRepository.createSchema(main, new ClassPathResourceResolver("/loading"));
assertThat(schema).isNotNull();
}

View file

@ -31,6 +31,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.impl.Helper.Invalid;
import de.kosit.validationtool.impl.Helper.Simple;
import de.kosit.validationtool.model.scenarios.Scenarios;
/**
@ -40,12 +42,6 @@ import de.kosit.validationtool.model.scenarios.Scenarios;
*/
public class ConversionServiceTest {
private static final URL VALID_XML = ConversionServiceTest.class.getResource("/valid/scenarios.xml");
private static final URL INVALID_XML = ConversionServiceTest.class.getResource("/invalid/scenarios-invalid.xml");
private static final URL ILLFORMED_XML = ConversionServiceTest.class.getResource("/invalid/scenarios-illformed.xml");
private static final URL SCHEMA = ConversionServiceTest.class.getResource("/xsd/scenarios.xsd");
@Rule
@ -77,28 +73,28 @@ public class ConversionServiceTest {
@Test
public void testUnmarshal() throws URISyntaxException {
final Scenarios s = this.service.readXml(VALID_XML.toURI(), Scenarios.class);
final Scenarios s = this.service.readXml(Simple.SCENARIOS, Scenarios.class);
assertThat(s).isNotNull();
assertThat(s.getName()).isEqualToIgnoringCase("XInneres");
assertThat(s.getName()).isEqualToIgnoringCase("HTML-TestSuite");
}
@Test
public void testUnmarshalWithSchema() throws URISyntaxException {
final Scenarios s = this.service.readXml(VALID_XML.toURI(), Scenarios.class, ContentRepository.createSchema(SCHEMA));
public void testUnmarshalWithSchema() {
final Scenarios s = this.service.readXml(Simple.SCENARIOS, Scenarios.class, ContentRepository.createSchema(SCHEMA));
assertThat(s).isNotNull();
assertThat(s.getName()).isEqualToIgnoringCase("XInneres");
assertThat(s.getName()).isEqualToIgnoringCase("HTML-TestSuite");
}
@Test
public void testUnmarshalInvalidXml() throws URISyntaxException {
public void testUnmarshalInvalidXml() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.service.readXml(INVALID_XML.toURI(), Scenarios.class, ContentRepository.createSchema(SCHEMA));
this.service.readXml(Invalid.SCENARIOS, Scenarios.class, ContentRepository.createSchema(SCHEMA));
}
@Test
public void testUnmarshalIllFormed() throws URISyntaxException {
public void testUnmarshalIllFormed() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.service.readXml(ILLFORMED_XML.toURI(), Scenarios.class, ContentRepository.createSchema(SCHEMA));
this.service.readXml(Invalid.SCENARIOS_ILLFORMED, Scenarios.class, ContentRepository.createSchema(SCHEMA));
}
@Test
@ -110,13 +106,13 @@ public class ConversionServiceTest {
@Test
public void testUnmarshalUnknownType() throws URISyntaxException {
this.exception.expect(ConversionService.ConversionExeption.class);
this.service.readXml(VALID_XML.toURI(), ConversionService.class);
this.service.readXml(Simple.SCENARIOS, ConversionService.class);
}
@Test
public void testUnmarshalWithoutType() throws URISyntaxException {
this.exception.expect(ConversionService.ConversionExeption.class);
this.service.readXml(VALID_XML.toURI(), null);
this.service.readXml(Simple.SCENARIOS, null);
}
}

View file

@ -26,7 +26,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -39,6 +38,7 @@ import de.kosit.validationtool.api.AcceptRecommendation;
import de.kosit.validationtool.api.CheckConfiguration;
import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.api.Result;
import de.kosit.validationtool.impl.Helper.Simple;
/**
* Test das Check-Interface
@ -47,10 +47,7 @@ import de.kosit.validationtool.api.Result;
*/
public class DefaultCheckTest {
private static final URL SCENARIO_DEFINITION = ScenarioRepositoryTest.class.getResource("/examples/UBLReady/scenarios-2.xml");
private static final URL VALID_EXAMPLE = ScenarioRepositoryTest.class
.getResource("/examples/UBLReady/UBLReady_EU_UBL-NL_20170102_FULL.xml");
public static final int MULTI_COUNT = 5;
@ -58,14 +55,23 @@ public class DefaultCheckTest {
@Before
public void setup() throws URISyntaxException {
final CheckConfiguration d = new CheckConfiguration(SCENARIO_DEFINITION.toURI());
d.setScenarioRepository(new File("src/test/resources/examples/repository").toURI());
final CheckConfiguration d = new CheckConfiguration(Simple.SCENARIOS);
d.setScenarioRepository(new File(Simple.REPOSITORY).toURI());
this.implementation = new DefaultCheck(d);
}
@Test
public void testHappyCase() {
final Result doc = this.implementation.checkInput(read(VALID_EXAMPLE));
final Result doc = this.implementation.checkInput(read(Simple.SIMPLE_VALID));
assertThat(doc).isNotNull();
assertThat(doc.getReport()).isNotNull();
assertThat(doc.isAcceptable()).isTrue();
assertThat(doc.getAcceptRecommendation()).isEqualTo(AcceptRecommendation.ACCEPTABLE);
}
@Test
public void testWithoutAcceptMatch() {
final Result doc = this.implementation.checkInput(read(Simple.FOO));
assertThat(doc).isNotNull();
assertThat(doc.getReport()).isNotNull();
assertThat(doc.isAcceptable()).isFalse();
@ -74,13 +80,13 @@ public class DefaultCheckTest {
@Test
public void testHappyCaseDocument() {
final Document doc = this.implementation.check(read(VALID_EXAMPLE));
final Document doc = this.implementation.check(read(Simple.SIMPLE_VALID));
assertThat(doc).isNotNull();
}
@Test
public void testMultipleCase() {
final List<Input> input = IntStream.range(0, MULTI_COUNT).mapToObj(i -> read(VALID_EXAMPLE)).collect(Collectors.toList());
final List<Input> input = IntStream.range(0, MULTI_COUNT).mapToObj(i -> read(Simple.SIMPLE_VALID)).collect(Collectors.toList());
final List<Result> docs = this.implementation.checkInput(input);
assertThat(docs).isNotNull();
assertThat(docs).hasSize(MULTI_COUNT);
@ -88,7 +94,7 @@ public class DefaultCheckTest {
@Test
public void testMultipleCaseDocument() {
final List<Input> input = IntStream.range(0, MULTI_COUNT).mapToObj(i -> read(VALID_EXAMPLE)).collect(Collectors.toList());
final List<Input> input = IntStream.range(0, MULTI_COUNT).mapToObj(i -> read(Simple.SIMPLE_VALID)).collect(Collectors.toList());
final List<Document> docs = this.implementation.check(input);
assertThat(docs).isNotNull();
assertThat(docs).hasSize(MULTI_COUNT);
@ -96,10 +102,10 @@ public class DefaultCheckTest {
@Test
public void testExtractHtml() {
final DefaultResult doc = (DefaultResult) this.implementation.checkInput(read(VALID_EXAMPLE));
final DefaultResult doc = (DefaultResult) this.implementation.checkInput(read(Simple.SIMPLE_VALID));
assertThat(doc).isNotNull();
assertThat(doc.getReport()).isNotNull();
assertThat(doc.isAcceptable()).isFalse();
assertThat(doc.isAcceptable()).isTrue();
assertThat(doc.extractHtmlAsString()).isNotEmpty();
assertThat(doc.extractHtmlAsElement()).isNotEmpty();
assertThat(doc.extractHtml()).isNotEmpty();

View file

@ -22,13 +22,11 @@ package de.kosit.validationtool.impl;
import static de.kosit.validationtool.api.InputFactory.read;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.URL;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.impl.Helper.Simple;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.impl.tasks.DocumentParseAction;
import de.kosit.validationtool.model.reportInput.XMLSyntaxError;
@ -42,25 +40,12 @@ import net.sf.saxon.s9api.XdmNode;
*/
public class DocumentParserTest {
private static final URL CONTENT = ConversionServiceTest.class.getResource("/valid/scenarios.xml");
private static final URL ILLFORMED = ConversionServiceTest.class.getResource("/invalid/scenarios-illformed.xml");
private static final URL NOT_EXISTING = ConversionServiceTest.class.getResource("/does not exist.xml");
@Rule
public ExpectedException exception = ExpectedException.none();
private DocumentParseAction parser;
@Before
public void setup() {
this.parser = new DocumentParseAction();
}
@Test
public void testSimple() {
final Result<XdmNode, XMLSyntaxError> result = DocumentParseAction.parseDocument(read(CONTENT));
final Result<XdmNode, XMLSyntaxError> result = DocumentParseAction.parseDocument(read(Simple.SIMPLE_VALID));
assertThat(result).isNotNull();
assertThat(result.getObject()).isNotNull();
assertThat(result.getErrors()).isEmpty();
@ -69,7 +54,7 @@ public class DocumentParserTest {
@Test
public void testIllformed() {
final Result<XdmNode, XMLSyntaxError> result = DocumentParseAction.parseDocument(read(ILLFORMED));
final Result<XdmNode, XMLSyntaxError> result = DocumentParseAction.parseDocument(read(Simple.NOT_WELLFORMED));
assertThat(result).isNotNull();
assertThat(result.getErrors()).isNotEmpty();
assertThat(result.getObject()).isNull();

View file

@ -37,12 +37,14 @@ import net.sf.saxon.s9api.XdmNode;
*
* @author Andreas Penski
*/
public class Helper {
public class Helper {
public static class Simple {
public static final URI ROOT = EXAMPLES_DIR.resolve("simple/");
public static final URI EXAMPLES = ROOT.resolve("input/");
public static final URI SIMPLE_VALID = Simple.ROOT.resolve("input/simple.xml");
public static final URI FOO = Simple.ROOT.resolve("input/foo.xml");
@ -58,15 +60,30 @@ public class Helper {
public static final URI UNKNOWN = ROOT.resolve("input/unknown.xml");
public static final URI GARBAGE = ROOT.resolve("input/no-xml.file");
public static final URI NOT_EXISTING = EXAMPLES_DIR.resolve("doesnotexist");
public static final URI REPORT_XSL = REPOSITORY.resolve("report.xsl");
public static URI getSchemaLocation() {
return ROOT.resolve("repository/simple.xsd");
}
}
public static class Invalid {
public static final URI ROOT = EXAMPLES_DIR.resolve("invaid/");
public static final URI SCENARIOS = ROOT.resolve("scenarios.xml");
public static final URI SCENARIOS_ILLFORMED = ROOT.resolve("scenarios-illformed.xml");
}
public static final URI SOURCE_ROOT = Paths.get("src/main/resources").toUri();
public static final URI MODEL_ROOT = Paths.get("src/main/model").toUri();
public static final URI ASSERTION_SCHEMA = MODEL_ROOT.resolve("xsd/assertions.xsd");
public static final URI SCENARIO_SCHEMA = MODEL_ROOT.resolve("xsd/scenarios.xsd");
public static final URI TEST_ROOT = Paths.get("src/test/resources").toUri();
@ -74,21 +91,14 @@ public class Helper {
public static final URI ASSERTIONS = EXAMPLES_DIR.resolve("assertions/tests-xrechnung.xml");
public static final URI SCENARIO_FILE = EXAMPLES_DIR.resolve("UBLReady/scenarios-2.xml");
public static final URI REPOSITORY = EXAMPLES_DIR.resolve("repository/");
public static final URL JAR_REPOSITORY = Helper.class.getClassLoader().getResource("xrechnung/repository/");
public static final URI NOT_EXISTING = EXAMPLES_DIR.resolve("doesnotexist");
public static final URI SAMPLE_DIR = EXAMPLES_DIR.resolve("UBLReady/");
public static final URI SAMPLE_XSLT = EXAMPLES_DIR.resolve("repository/resources/eRechnung/report.xsl");
public static final URI SAMPLE = SAMPLE_DIR.resolve("UBLReady_EU_UBL-NL_20170102_FULL.xml");
public static final URI SAMPLE2 = SAMPLE_DIR.resolve("UBLReady_EU_UBL-NL_20170102_FULL-invalid.xml");
/**
* Lädt ein XML-Dokument von der gegebenen URL

View file

@ -75,8 +75,8 @@ public class RelativeUriResolverTest {
@Test
public void testClasspathLocal() throws URISyntaxException, TransformerException {
this.resolver = new RelativeUriResolver(RelativeUriResolver.class.getClassLoader().getResource("simple").toURI());
final URL moz = RelativeUriResolverTest.class.getClassLoader().getResource("simple/main.xsd");
this.resolver = new RelativeUriResolver(RelativeUriResolver.class.getClassLoader().getResource("loading").toURI());
final URL moz = RelativeUriResolverTest.class.getClassLoader().getResource("loading/main.xsd");
final Source resolved = this.resolver.resolve("./resources/reference.xsd", moz.toURI().toASCIIString());
assertThat(resolved).isNotNull();
}

View file

@ -33,7 +33,14 @@ import org.w3c.dom.Document;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.s9api.*;
import de.kosit.validationtool.impl.Helper.Simple;
import net.sf.saxon.s9api.DOMDestination;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;
/**
* Testet verschiedene Saxon Security Einstellungen.
@ -45,18 +52,18 @@ public class SaxonSecurityTest {
@Test
public void testEvilStylesheets() throws IOException {
Processor p = ObjectFactory.createProcessor();
final Processor p = ObjectFactory.createProcessor();
for (int i = 1; i <= 5; i++) {
try {
URL resource = SaxonSecurityTest.class.getResource(String.format("/evil/evil%s.xsl", i));
final URL resource = SaxonSecurityTest.class.getResource(String.format("/evil/evil%s.xsl", i));
final XsltCompiler compiler = p.newXsltCompiler();
final RelativeUriResolver resolver = new RelativeUriResolver(Helper.REPOSITORY);
final RelativeUriResolver resolver = new RelativeUriResolver(Simple.REPOSITORY);
compiler.setURIResolver(resolver);
final XsltExecutable exetuable = compiler.compile(new StreamSource(resource.openStream()));
final XsltTransformer transformer = exetuable.load();
final Document document = ObjectFactory.createDocumentBuilder(false).newDocument();
document.createElement("root");
Document result = ObjectFactory.createDocumentBuilder(false).newDocument();
final Document result = ObjectFactory.createDocumentBuilder(false).newDocument();
transformer.getUnderlyingController().setUnparsedTextURIResolver(resolver);
transformer.setURIResolver(resolver);
transformer.setSource(new DOMSource(document));
@ -68,7 +75,7 @@ public class SaxonSecurityTest {
fail(String.format("Saxon configuration should prevent expansion within %s", resource));
}
} catch (SaxonApiException | RuntimeException e) {
} catch (final SaxonApiException | RuntimeException e) {
log.info("Expected exception detected", e.getMessage());
}
}

View file

@ -22,10 +22,9 @@ package de.kosit.validationtool.impl;
import static de.kosit.validationtool.api.InputFactory.read;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.junit.Before;
import org.junit.Rule;
@ -33,6 +32,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.api.CheckConfiguration;
import de.kosit.validationtool.impl.Helper.Simple;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.impl.tasks.DocumentParseAction;
import de.kosit.validationtool.model.scenarios.ScenarioType;
@ -48,8 +48,6 @@ import net.sf.saxon.s9api.XdmNode;
public class ScenarioRepositoryTest {
private static final URL SAMPLE = ScenarioRepositoryTest.class.getResource("/valid/scenarios.xml");
@Rule
public ExpectedException expectedException = ExpectedException.none();
@ -59,7 +57,7 @@ public class ScenarioRepositoryTest {
@Before
public void setup() {
this.content = new ContentRepository(ObjectFactory.createProcessor(), new File("src/test/resources/examples/repository").toURI());
this.content = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY);
final Scenarios def = new Scenarios();
final ScenarioType t = new ScenarioType();
t.setMatch("//*:name");
@ -72,7 +70,7 @@ public class ScenarioRepositoryTest {
@Test
public void testHappyCase() throws Exception {
final Result<ScenarioType, String> scenario = this.repository.selectScenario(load(SAMPLE));
final Result<ScenarioType, String> scenario = this.repository.selectScenario(load(Simple.SCENARIOS));
assertThat(scenario).isNotNull();
assertThat(scenario.isValid()).isTrue();
}
@ -83,7 +81,7 @@ public class ScenarioRepositoryTest {
final ScenarioType fallback = new ScenarioType();
fallback.setName("fallback");
this.repository.setFallbackScenario(fallback);
final Result<ScenarioType, String> scenario = this.repository.selectScenario(load(SAMPLE));
final Result<ScenarioType, String> scenario = this.repository.selectScenario(load(Simple.SCENARIOS));
assertThat(scenario).isNotNull();
assertThat(scenario.isValid()).isFalse();
assertThat(scenario.getObject().getName()).isEqualTo("fallback");
@ -100,15 +98,15 @@ public class ScenarioRepositoryTest {
final ScenarioType fallback = new ScenarioType();
fallback.setName("fallback");
this.repository.setFallbackScenario(fallback);
final Result<ScenarioType, String> scenario = this.repository.selectScenario(load(SAMPLE));
final Result<ScenarioType, String> scenario = this.repository.selectScenario(load(Simple.SCENARIOS));
assertThat(scenario).isNotNull();
assertThat(scenario.isValid()).isFalse();
assertThat(scenario.getObject().getName()).isEqualTo("fallback");
}
private static XdmNode load(final URL url) throws IOException {
private static XdmNode load(final URI uri) throws IOException {
final DocumentParseAction p = new DocumentParseAction();
return DocumentParseAction.parseDocument(read(url)).getObject();
return DocumentParseAction.parseDocument(read(uri.toURL())).getObject();
}
@Test

View file

@ -1,113 +0,0 @@
/*
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
* one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. KoSIT licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package de.kosit.validationtool.impl;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Collections;
import javax.xml.validation.Schema;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.impl.tasks.CheckAction;
import de.kosit.validationtool.impl.tasks.SchemaValidationAction;
import de.kosit.validationtool.model.reportInput.CreateReportInput;
import de.kosit.validationtool.model.scenarios.ResourceType;
import de.kosit.validationtool.model.scenarios.ScenarioType;
import de.kosit.validationtool.model.scenarios.ValidateWithXmlSchema;
/**
* Testet die {@linkSchemaValidatorAction}.
*
* @author Andreas Penski
*/
public class SchemaValidatorActionTest {
private static final URL VALID_EXAMPLE = SchemaValidatorActionTest.class
.getResource("/examples/UBLReady/UBLReady_EU_UBL-NL_20170102_FULL.xml");
private static final URI INVALID_EXAMPLE = Helper.TEST_ROOT.resolve("invalid/scenarios-invalid.xml");
public ExpectedException expectedException = ExpectedException.none();
private SchemaValidationAction service;
private ContentRepository repository;
@Before
public void setup() {
service = new SchemaValidationAction();
repository = new ContentRepository(ObjectFactory.createProcessor(), Helper.REPOSITORY);
}
@Test
public void testSimple() {
CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(VALID_EXAMPLE), new CreateReportInput());
ScenarioType t = new ScenarioType();
ValidateWithXmlSchema v = new ValidateWithXmlSchema();
ResourceType r = new ResourceType();
r.setLocation("resources/eRechnung/UBL-2.1/xsdrt/maindoc/UBL-Invoice-2.1.xsd");
r.setName("invoice");
v.getResource().add(r);
t.setValidateWithXmlSchema(v);
t.initialize(repository, true);
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
service.check(bag);
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
}
@Test
public void testValidationFailure() throws MalformedURLException {
CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(INVALID_EXAMPLE.toURL()), new CreateReportInput());
ScenarioType t = new ScenarioType();
ValidateWithXmlSchema v = new ValidateWithXmlSchema();
ResourceType r = new ResourceType();
r.setLocation(Helper.REPOSITORY.relativize(Helper.SCENARIO_SCHEMA).getRawPath());
r.setName("invoice");
v.getResource().add(r);
t.setValidateWithXmlSchema(v);
t.initialize(repository, true);
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
service.check(bag);
assertThat(bag.getSchemaValidationResult().isValid()).isFalse();
bag.getSchemaValidationResult().getErrors().forEach(e->{
assertThat(e.getRowNumber()).isGreaterThan(0);
assertThat(e.getColumnNumber()).isGreaterThan(0);
});
}
@Test
public void testSchemaReferences() {
final Schema reportInputSchema = repository.getReportInputSchema();
assertThat(reportInputSchema).isNotNull();
}
}

View file

@ -49,7 +49,7 @@ public class SimpleScenarioCheck {
public void testUnknown() throws MalformedURLException {
final Result result = this.implementation.checkInput(InputFactory.read(Simple.UNKNOWN.toURL()));
assertThat(result).isNotNull();
assertThat(result.getAcceptRecommendation()).isEqualTo(AcceptRecommendation.REJECT);
assertThat(result.isProcessingSuccessful()).isFalse();
}
@Test

View file

@ -0,0 +1,174 @@
/*
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
* one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. KoSIT licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package de.kosit.validationtool.impl.tasks;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URI;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.Helper;
import de.kosit.validationtool.impl.Helper.Simple;
import de.kosit.validationtool.impl.ObjectFactory;
import de.kosit.validationtool.impl.input.SourceInput;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.impl.tasks.CheckAction.Bag;
import de.kosit.validationtool.model.reportInput.CreateReportInput;
import de.kosit.validationtool.model.scenarios.ResourceType;
import de.kosit.validationtool.model.scenarios.ScenarioType;
import de.kosit.validationtool.model.scenarios.ValidateWithXmlSchema;
/**
* Tests die {@link SchemaValidationAction}.
*
* @author Andreas Penski
*/
public class SchemaValidatorActionTest {
public ExpectedException expectedException = ExpectedException.none();
private SchemaValidationAction service;
private ContentRepository repository;
@Before
public void setup() {
this.service = new SchemaValidationAction();
this.repository = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY);
}
@Test
public void testSimple() throws MalformedURLException {
final CheckAction.Bag bag = createBag(InputFactory.read(Simple.SIMPLE_VALID.toURL()));
this.service.check(bag);
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
}
@Test
public void testValidationFailure() throws MalformedURLException {
final Input input = InputFactory.read(Simple.INVALID.toURL());
final CheckAction.Bag bag = createBag(input);
this.service.check(bag);
assertThat(bag.getSchemaValidationResult().isValid()).isFalse();
bag.getSchemaValidationResult().getErrors().forEach(e->{
assertThat(e.getRowNumber()).isGreaterThan(0);
assertThat(e.getColumnNumber()).isGreaterThan(0);
});
}
private Bag createBag(final Input input) {
final Bag bag = new Bag(input, new CreateReportInput());
bag.setScenarioSelectionResult(new Result<>(createScenario(Helper.Simple.getSchemaLocation())));
return bag;
}
private ScenarioType createScenario(final URI schemafile) {
final ScenarioType t = new ScenarioType();
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
final ResourceType r = new ResourceType();
r.setLocation(schemafile.getRawPath());
r.setName("invoice");
v.getResource().add(r);
t.setValidateWithXmlSchema(v);
t.initialize(this.repository, true);
return t;
}
@Test
public void testSchemaReferences() {
final Schema reportInputSchema = this.repository.getReportInputSchema();
assertThat(reportInputSchema).isNotNull();
}
@Test
public void testNoRepeatableRead() throws Exception {
try ( final InputStream inputStream = Simple.SIMPLE_VALID.toURL().openStream() ) {
final Bag bag = createBag(InputFactory.read(new StreamSource(inputStream)));
// don't read the real inputstream here!
bag.setParserResult(DocumentParseAction.parseDocument(InputFactory.read(Simple.SIMPLE_VALID.toURL())));
this.service.check(bag);
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
}
}
@Test
public void testNoRepeatableReadBigFile() throws Exception {
try ( final InputStream inputStream = Simple.SIMPLE_VALID.toURL().openStream() ) {
final SourceInput input = (SourceInput) InputFactory.read(new StreamSource(inputStream));
final Bag bag = createBag(input);
// set limit and length for serialization to 5 bytes
this.service.setInMemoryLimit(5L);
input.setLength(6L);
bag.setParserResult(DocumentParseAction.parseDocument(InputFactory.read(Simple.SIMPLE_VALID.toURL())));
this.service.check(bag);
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
}
}
@Test
public void testNoRepeatableReaderInput() throws Exception {
try ( final InputStream inputStream = Simple.SIMPLE_VALID.toURL().openStream();
final Reader reader = new InputStreamReader(inputStream) ) {
final SourceInput input = (SourceInput) InputFactory.read(new StreamSource(reader));
final Bag bag = createBag(input);
bag.setParserResult(DocumentParseAction.parseDocument(InputFactory.read(Simple.SIMPLE_VALID.toURL())));
this.service.check(bag);
this.service.check(bag);
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
}
}
@Test
public void testNoRepeatableReaderInputBigFile() throws Exception {
try ( final InputStream inputStream = Simple.SIMPLE_VALID.toURL().openStream();
final Reader reader = new InputStreamReader(inputStream) ) {
final SourceInput input = (SourceInput) InputFactory.read(new StreamSource(reader));
final Bag bag = createBag(input);
// set limit and length for serialization to 5 bytes
this.service.setInMemoryLimit(5L);
bag.setParserResult(DocumentParseAction.parseDocument(InputFactory.read(Simple.SIMPLE_VALID.toURL())));
this.service.check(bag);
this.service.check(bag);
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
}
}
}