(fix) tests

This commit is contained in:
Andreas Penski (init) 2020-04-29 13:55:12 +02:00
parent 7ca3ef90f3
commit 5b1d0cd467
12 changed files with 92 additions and 56 deletions

View file

@ -31,13 +31,13 @@ public interface ResolvingConfigurationStrategy {
SchemaFactory createSchemaFactory();
/**
* Creates a preconfigured {@link Processor Saxon Processor} for various tasks within the Validator. The validator
* Returns a preconfigured {@link Processor Saxon Processor} for various tasks within the Validator. The validator
* leverages the saxon s9api for internal processing e.g. xml reading and writing. So this is the main object to secure
* for reading, transforming and writing xml files.
*
* @return a preconfigured {@link Processor}
*/
Processor createProcessor();
Processor getProcessor();
/**
* Creates a specific implementation for resolving referenced objects in XML files. The URIResolver, it is used for

View file

@ -170,9 +170,9 @@ public class ConfigurationBuilder {
public Configuration build() {
final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy();
if (this.processor == null) {
this.processor = resolving.createProcessor();
this.processor = resolving.getProcessor();
}
final ContentRepository contentRepository = new ContentRepository(this.resolvingConfigurationStrategy, this.repository);
final ContentRepository contentRepository = new ContentRepository(resolving, this.repository);
final List<Scenario> list = initializeScenarios(contentRepository);
final Scenario fallbackScenario = initializeFallback(contentRepository);

View file

@ -117,7 +117,7 @@ public class ConfigurationLoader {
public Configuration build() {
final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy();
final Processor processor = resolving.createProcessor();
final Processor processor = resolving.getProcessor();
final ContentRepository contentRepository = new ContentRepository(resolving, getScenarioRepository());
final Scenarios def = loadScenarios(contentRepository.getScenarioSchema(), processor);

View file

@ -92,7 +92,7 @@ public class ContentRepository {
public ContentRepository(final ResolvingConfigurationStrategy strategy, final URI repository) {
this.repository = repository;
this.resolvingConfigurationStrategy = strategy;
this.processor = this.resolvingConfigurationStrategy.createProcessor();
this.processor = this.resolvingConfigurationStrategy.getProcessor();
this.resolver = this.resolvingConfigurationStrategy.createResolver(repository);
this.schemaFactory = this.resolvingConfigurationStrategy.createSchemaFactory();
}

View file

@ -58,8 +58,8 @@ public class ComputeAcceptanceAction implements CheckAction {
selector.setContextItem(results.getReport());
results.setAcceptStatus(selector.effectiveBooleanValue() ? AcceptRecommendation.ACCEPTABLE : AcceptRecommendation.REJECT);
} catch (final SaxonApiException e) {
final String msg = "Error evaluating accept recommendation: %s";
log.error(msg);
final String msg = String.format("Error evaluating accept recommendation: %s", selector.getUnderlyingXPathContext().toString());
log.error(msg, e);
results.addProcessingError(msg);
}
}

View file

@ -13,6 +13,8 @@ import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
import net.sf.saxon.s9api.Processor;
/**
* @author Andreas Penski
*/
@ -27,6 +29,18 @@ public abstract class BaseResolvingStrategy implements ResolvingConfigurationStr
private static final String ORACLE_XERCES_CLASS = "com.sun.org.apache.xerces.internal.impl.Constants";
private Processor processor;
@Override
public Processor getProcessor() {
if (this.processor == null) {
this.processor = createProcessor();
}
return this.processor;
}
protected abstract Processor createProcessor();
public static void forceOpenJdkXmlImplementation() {
if (!isOpenJdkXmlImplementationAvailable()) {
throw new IllegalStateException("No OpenJDK version of XERCES found");

View file

@ -9,8 +9,6 @@ import javax.xml.validation.Validator;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.s9api.Processor;
/**
*
*
@ -30,11 +28,6 @@ public class StrictLocalResolvingStrategy extends StrictRelativeResolvingStrateg
return schemaFactory;
}
@Override
public Processor createProcessor() {
return super.createProcessor();
}
@Override
public URIResolver createResolver(final URI repository) {
// intentionally return 'null', since all resolving is configured with the other objects

View file

@ -77,7 +77,7 @@ public class StrictRelativeResolvingStrategy extends BaseResolvingStrategy {
}
@Override
public Processor createProcessor() {
protected Processor createProcessor() {
final Processor processor = new Processor(false);
// verhindere global im Prinzip alle resolving strategien
final SecureUriResolver resolver = new SecureUriResolver();

View file

@ -0,0 +1,31 @@
package de.kosit.validationtool.config;
import java.util.List;
import java.util.Map;
import lombok.Data;
import de.kosit.validationtool.api.Configuration;
import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.Scenario;
/**
* @author Andreas Penski
*/
@Data
public class TestConfiguration implements Configuration {
private List<Scenario> scenarios;
private Scenario fallbackScenario;
private String author;
private String name;
private String date;
private ContentRepository contentRepository;
private Map<String, Object> additionalParameters;
}

View file

@ -22,12 +22,14 @@ package de.kosit.validationtool.impl;
import static de.kosit.validationtool.api.InputFactory.read;
import static org.assertj.core.api.Assertions.assertThat;
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;
import net.sf.saxon.s9api.XdmNode;
@ -37,14 +39,21 @@ import net.sf.saxon.s9api.XdmNode;
*
* @author Andreas Penski
*/
public class DocumentParserTest {
public class DocumentParseActionTest {
@Rule
public ExpectedException exception = ExpectedException.none();
private DocumentParseAction action;
@Before
public void setup() {
this.action = new DocumentParseAction(Helper.createProcessor());
}
@Test
public void testSimple() {
final Result<XdmNode, XMLSyntaxError> result = Helper.parseDocument(read(Simple.SIMPLE_VALID));
final Result<XdmNode, XMLSyntaxError> result = this.action.parseDocument(read(Simple.SIMPLE_VALID));
assertThat(result).isNotNull();
assertThat(result.getObject()).isNotNull();
assertThat(result.getErrors()).isEmpty();
@ -53,7 +62,7 @@ public class DocumentParserTest {
@Test
public void testIllformed() {
final Result<XdmNode, XMLSyntaxError> result = Helper.parseDocument(read(Simple.NOT_WELLFORMED));
final Result<XdmNode, XMLSyntaxError> result = this.action.parseDocument(read(Simple.NOT_WELLFORMED));
assertThat(result).isNotNull();
assertThat(result.getErrors()).isNotEmpty();
assertThat(result.getObject()).isNull();
@ -63,7 +72,7 @@ public class DocumentParserTest {
@Test
public void testNullInput() {
this.exception.expect(IllegalArgumentException.class);
Helper.parseDocument(null);
this.action.parseDocument(null);
}

View file

@ -43,6 +43,7 @@ import de.kosit.validationtool.impl.tasks.DocumentParseAction;
import de.kosit.validationtool.model.reportInput.XMLSyntaxError;
import net.sf.saxon.dom.NodeOverNodeInfo;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
@ -161,7 +162,20 @@ public class Helper {
return serialize((Document) NodeOverNodeInfo.wrap(node.getUnderlyingNode()));
}
public static Result<XdmNode, XMLSyntaxError> parseDocument(final Processor processor, final Input input) {
return new DocumentParseAction(processor).parseDocument(input);
}
public static Result<XdmNode, XMLSyntaxError> parseDocument(final Input input) {
return new DocumentParseAction(TestObjectFactory.createProcessor()).parseDocument(input);
return new DocumentParseAction(getTestProcessor()).parseDocument(input);
}
public static Processor getTestProcessor() {
// is always the same at the moment
return createProcessor();
}
public static Processor createProcessor() {
return ResolvingMode.STRICT_RELATIVE.getStrategy().getProcessor();
}
}

View file

@ -26,22 +26,17 @@ import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import lombok.Data;
import de.kosit.validationtool.api.Configuration;
import de.kosit.validationtool.config.TestConfiguration;
import de.kosit.validationtool.impl.Helper.Simple;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.model.scenarios.ScenarioType;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XdmNode;
@ -53,45 +48,25 @@ import net.sf.saxon.s9api.XdmNode;
public class ScenarioRepositoryTest {
@Data
private static class DummyConfiguration implements Configuration {
private List<Scenario> scenarios;
private Scenario fallbackScenario;
private String author;
private String name;
private String date;
private Processor processor;
private ContentRepository contentRepository;
private Map<String, Object> additionalParameters;
}
@Rule
public ExpectedException expectedException = ExpectedException.none();
private ScenarioRepository repository;
private DummyConfiguration configInstance;
private TestConfiguration configInstance;
@Before
public void setup() {
final Scenario s = createScenario();
this.configInstance = new TestConfiguration();
this.configInstance.setContentRepository(new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), null));
this.configInstance = new DummyConfiguration();
final Scenario s = createScenario();
this.configInstance.setScenarios(new ArrayList<>());
this.configInstance.getScenarios().add(s);
this.repository = new ScenarioRepository(this.configInstance);
}
private static Scenario createScenario() {
private Scenario createScenario() {
final Scenario s = new Scenario(new ScenarioType());
s.setMatchExecutable(createXpath("//*:name"));
return s;
@ -134,11 +109,11 @@ public class ScenarioRepositoryTest {
assertThat(scenario.getObject().getName()).isEqualTo("fallback");
}
private static XdmNode load(final URI uri) throws IOException {
return Helper.parseDocument(read(uri.toURL())).getObject();
private XdmNode load(final URI uri) throws IOException {
return Helper.parseDocument(this.configInstance.getContentRepository().getProcessor(), read(uri.toURL())).getObject();
}
private static XPathExecutable createXpath(final String expression) {
return new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), null).createXPath(expression, new HashMap<>());
private XPathExecutable createXpath(final String expression) {
return this.configInstance.getContentRepository().createXPath(expression, new HashMap<>());
}
}