diff --git a/src/main/java/de/kosit/validationtool/config/XPathBuilder.java b/src/main/java/de/kosit/validationtool/config/XPathBuilder.java index 83cbe5a..c5f60b8 100644 --- a/src/main/java/de/kosit/validationtool/config/XPathBuilder.java +++ b/src/main/java/de/kosit/validationtool/config/XPathBuilder.java @@ -13,7 +13,6 @@ import org.apache.commons.lang3.StringUtils; import lombok.AccessLevel; import lombok.Data; -import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @@ -42,9 +41,15 @@ class XPathBuilder implements Builder { private XPathExecutable executable; @Setter(AccessLevel.PACKAGE) - @Getter(AccessLevel.PACKAGE) private Map namespaces; + Map getNamespaces() { + if (this.namespaces == null) { + this.namespaces = new HashMap<>(); + } + return this.namespaces; + } + /** * Returns the xpath expression. * @@ -66,7 +71,7 @@ class XPathBuilder implements Builder { } try { if (this.executable == null) { - this.executable = repository.createXPath(this.xpath, this.namespaces); + this.executable = repository.createXPath(this.xpath, getNamespaces()); } else { this.xpath = extractExpression(); extractNamespaces(); @@ -81,19 +86,15 @@ class XPathBuilder implements Builder { } private void extractNamespaces() { - if (this.namespaces == null) { - this.namespaces = new HashMap<>(); - } + final Map ns = new HashMap<>(); final Iterator iterator = this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext() .iteratePrefixes(); final Iterable iterable = () -> iterator; 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); + .filter(StringUtils::isNotBlank).forEach(e -> ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression() + .getRetainedStaticContext().getURIForPrefix(e, false))); + getNamespaces().putAll(ns); } diff --git a/src/main/java/de/kosit/validationtool/impl/ContentRepository.java b/src/main/java/de/kosit/validationtool/impl/ContentRepository.java index 6fd7a5b..84757d4 100644 --- a/src/main/java/de/kosit/validationtool/impl/ContentRepository.java +++ b/src/main/java/de/kosit/validationtool/impl/ContentRepository.java @@ -143,7 +143,7 @@ public class ContentRepository { throw new IllegalStateException("Can not compile xslt executable for uri " + uri, e); } finally { 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)); } } diff --git a/src/test/java/de/kosit/validationtool/config/XPathBuilderTest.java b/src/test/java/de/kosit/validationtool/config/XPathBuilderTest.java new file mode 100644 index 0000000..30d3af1 --- /dev/null +++ b/src/test/java/de/kosit/validationtool/config/XPathBuilderTest.java @@ -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 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 ns = new HashMap<>(); + ns.put("p", "http://somens"); + b.setNamespaces(ns); + b.setXpath("//p:*"); + final Result 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 ns = new HashMap<>(); + ns.put("p", "http://somens"); + b.setNamespaces(ns); + b.setXpath("//u:*"); + final Result 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 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 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 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 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 result = b.build(Simple.createContentRepository()); + assertThat(result).isNotNull(); + assertThat(result.isValid()).isFalse(); + } +}