some more tests

This commit is contained in:
Andreas Penski (init) 2020-04-30 15:41:39 +02:00
parent 16dc45ab46
commit 0bb0f6671d
5 changed files with 131 additions and 19 deletions

View file

@ -186,7 +186,16 @@ public class ConfigurationBuilder {
* @return the scenario configuration builder
*/
public static ScenarioBuilder scenario(final String name) {
return new ScenarioBuilder(name);
return new ScenarioBuilder().name(name);
}
/**
* Create a new scenario configuration.
*
* @return the scenario configuration builder
*/
public static ScenarioBuilder scenario() {
return scenario(null);
}
/**

View file

@ -39,6 +39,7 @@ import net.sf.saxon.s9api.XPathExecutable;
*/
@RequiredArgsConstructor
@Slf4j
@Getter(AccessLevel.PACKAGE)
public class ScenarioBuilder implements Builder<Scenario> {
private static int nameCount = 0;
@ -51,8 +52,7 @@ public class ScenarioBuilder implements Builder<Scenario> {
private final XPathBuilder acceptConfig = new XPathBuilder("accept");
@Getter(AccessLevel.PACKAGE)
private final String name;
private String name;
private SchemaBuilder schemaBuilder;
@ -273,4 +273,8 @@ public class ScenarioBuilder implements Builder<Scenario> {
return type;
}
public ScenarioBuilder name(final String name) {
this.name = name;
return this;
}
}

View file

@ -9,6 +9,7 @@ import java.util.Map;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import lombok.AccessLevel;
import lombok.Data;
@ -32,7 +33,7 @@ import net.sf.saxon.s9api.XPathExecutable;
@Slf4j
class XPathBuilder implements Builder<XPathExecutable> {
private static final String[] IGNORED_PREFIXES = new String[] { "xsd" };
private static final String[] IGNORED_PREFIXES = new String[] { "xsd", "saxon", "xsl", "xs" };
private final String name;
@ -66,10 +67,9 @@ class XPathBuilder implements Builder<XPathExecutable> {
try {
if (this.executable == null) {
this.executable = repository.createXPath(this.xpath, this.namespaces);
} else {
this.xpath = extractExpression();
this.namespaces = extractNamespaces();
extractNamespaces();
}
} catch (final IllegalStateException e) {
final String msg = String.format("Error creating %s xpath", this.name, e);
@ -80,16 +80,21 @@ class XPathBuilder implements Builder<XPathExecutable> {
return new Result<>(this.executable);
}
private Map<String, String> extractNamespaces() {
private void extractNamespaces() {
if (this.namespaces == null) {
this.namespaces = new HashMap<>();
}
final Map<String, String> ns = new HashMap<>();
final Iterator<String> iterator = this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
.iteratePrefixes();
final Iterable<String> iterable = () -> iterator;
StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e)).forEach(e -> {
ns.put(e,
this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext().getURIForPrefix(e, false));
});
return ns;
StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e))
.filter(StringUtils::isNotBlank).forEach(e -> {
ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
.getURIForPrefix(e, false));
});
this.namespaces.putAll(ns);
}
private String extractExpression() {

View file

@ -1,7 +1,10 @@
package de.kosit.validationtool.daemon;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.HttpServer;
@ -13,7 +16,6 @@ import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.Configuration;
import de.kosit.validationtool.impl.ConversionService;
import de.kosit.validationtool.impl.DefaultCheck;
import de.kosit.validationtool.model.daemon.HealthType;
/**
* HTTP-Daemon für die Bereitstellung der Prüf-Funktionalität via http.
@ -25,6 +27,12 @@ import de.kosit.validationtool.model.daemon.HealthType;
@Slf4j
public class Daemon {
private static final String DEFAULT_HOST = "localhost";
private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_THREAD_COUNT = Runtime.getRuntime().availableProcessors();
private final String hostName;
private final int port;
@ -40,18 +48,25 @@ public class Daemon {
HttpServer server = null;
try {
final ConversionService converter = new ConversionService();
converter.initialize(HealthType.class.getPackage());
server = HttpServer.create(new InetSocketAddress(this.hostName, this.port), 0);
server = HttpServer.create(getSocket(), 0);
final DefaultCheck check = new DefaultCheck(config);
server.createContext("/", new CheckHandler(check, config.getContentRepository().getProcessor()));
server.createContext("/server/health", new HealthHandler(config, converter));
server.createContext("/server/config", new ConfigHandler(config, new ConversionService()));
server.setExecutor(Executors.newFixedThreadPool(this.threadCount));
server.createContext("/server/config", new ConfigHandler(config, converter));
server.setExecutor(createExecutor());
server.start();
log.info("Server unter Port {} ist erfolgreich gestartet", this.port);
log.info("Server {} started", server.getAddress());
} catch (final IOException e) {
log.error("Fehler beim HttpServer erstellen: {}", e.getMessage(), e);
log.error("Error starting HttpServer for Valdidator: {}", e.getMessage(), e);
}
}
private ExecutorService createExecutor() {
return Executors.newFixedThreadPool(this.threadCount > 0 ? this.threadCount : DEFAULT_THREAD_COUNT);
}
private InetSocketAddress getSocket() {
return new InetSocketAddress(defaultIfBlank(this.hostName, DEFAULT_HOST), this.port > 0 ? this.port : DEFAULT_PORT);
}
}