more tests

This commit is contained in:
Andreas Penski (init) 2020-05-04 14:56:36 +02:00
parent 7dc62012a6
commit 01ae592862
3 changed files with 129 additions and 12 deletions

View file

@ -13,7 +13,6 @@ import org.apache.commons.lang3.StringUtils;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -42,9 +41,15 @@ class XPathBuilder implements Builder<XPathExecutable> {
private XPathExecutable executable; private XPathExecutable executable;
@Setter(AccessLevel.PACKAGE) @Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PACKAGE)
private Map<String, String> namespaces; private Map<String, String> namespaces;
Map<String, String> getNamespaces() {
if (this.namespaces == null) {
this.namespaces = new HashMap<>();
}
return this.namespaces;
}
/** /**
* Returns the xpath expression. * Returns the xpath expression.
* *
@ -66,7 +71,7 @@ class XPathBuilder implements Builder<XPathExecutable> {
} }
try { try {
if (this.executable == null) { if (this.executable == null) {
this.executable = repository.createXPath(this.xpath, this.namespaces); this.executable = repository.createXPath(this.xpath, getNamespaces());
} else { } else {
this.xpath = extractExpression(); this.xpath = extractExpression();
extractNamespaces(); extractNamespaces();
@ -81,19 +86,15 @@ class XPathBuilder implements Builder<XPathExecutable> {
} }
private void extractNamespaces() { private void extractNamespaces() {
if (this.namespaces == null) {
this.namespaces = new HashMap<>();
}
final Map<String, String> ns = new HashMap<>(); final Map<String, String> ns = new HashMap<>();
final Iterator<String> iterator = this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext() final Iterator<String> iterator = this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
.iteratePrefixes(); .iteratePrefixes();
final Iterable<String> iterable = () -> iterator; final Iterable<String> iterable = () -> iterator;
StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e)) StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e))
.filter(StringUtils::isNotBlank).forEach(e -> { .filter(StringUtils::isNotBlank).forEach(e -> ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression()
ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext() .getRetainedStaticContext().getURIForPrefix(e, false)));
.getURIForPrefix(e, false)); getNamespaces().putAll(ns);
});
this.namespaces.putAll(ns);
} }

View file

@ -143,7 +143,7 @@ public class ContentRepository {
throw new IllegalStateException("Can not compile xslt executable for uri " + uri, e); throw new IllegalStateException("Can not compile xslt executable for uri " + uri, e);
} finally { } finally {
if (!listener.hasErrors() && listener.hasEvents()) { if (!listener.hasErrors() && listener.hasEvents()) {
log.warn("Received warnings while loading a xslt script {}", uri); log.warn("Received warnings or errors while loading a xslt script {}", uri);
listener.getErrors().forEach(e -> e.log(log)); listener.getErrors().forEach(e -> e.log(log));
} }
} }

View file

@ -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();
}
}