Updated Branches: refs/heads/develop baa6c4d11 -> ad9841782
Added tests for MARMOTTA-221, MARMOTTA-197 Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/9629d75c Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/9629d75c Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/9629d75c Branch: refs/heads/develop Commit: 9629d75ccdbd059326f66e57b11433fb2fcb8831 Parents: baa6c4d Author: Jakob Frank <[email protected]> Authored: Tue May 7 16:06:31 2013 +0200 Committer: Jakob Frank <[email protected]> Committed: Tue May 7 16:06:31 2013 +0200 ---------------------------------------------------------------------- .../marmotta/ldpath/backend/file/ParserTest.java | 15 ++-- libraries/ldpath/ldpath-core/pom.xml | 4 + .../marmotta/ldpath/parser/SelectorsTest.java | 83 +++++++++++++++ .../ldpath/parser/StringTestingBackend.java | 81 ++++++++++++++ .../apache/marmotta/ldpath/parser/TestsTest.java | 78 ++++++++++++++ 5 files changed, 253 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/9629d75c/libraries/ldpath/ldpath-backend-file/src/test/java/org/apache/marmotta/ldpath/backend/file/ParserTest.java ---------------------------------------------------------------------- diff --git a/libraries/ldpath/ldpath-backend-file/src/test/java/org/apache/marmotta/ldpath/backend/file/ParserTest.java b/libraries/ldpath/ldpath-backend-file/src/test/java/org/apache/marmotta/ldpath/backend/file/ParserTest.java index 4d23c14..6872c4c 100644 --- a/libraries/ldpath/ldpath-backend-file/src/test/java/org/apache/marmotta/ldpath/backend/file/ParserTest.java +++ b/libraries/ldpath/ldpath-backend-file/src/test/java/org/apache/marmotta/ldpath/backend/file/ParserTest.java @@ -31,6 +31,7 @@ import org.apache.marmotta.ldpath.model.selectors.UnionSelector; import org.apache.marmotta.ldpath.model.transformers.StringTransformer; import org.apache.marmotta.ldpath.parser.ParseException; import org.apache.marmotta.ldpath.parser.RdfPathParser; +import org.hamcrest.CoreMatchers; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -60,7 +61,7 @@ public class ParserTest { String path1 = "rdfs:label"; NodeSelector<Value> s1 = parseSelector(path1, null); - Assert.assertTrue(s1 instanceof PropertySelector); + Assert.assertThat(s1, CoreMatchers.instanceOf(PropertySelector.class)); Assert.assertEquals("<http://www.w3.org/2000/01/rdf-schema#label>",s1.getPathExpression(backend)); @@ -70,17 +71,15 @@ public class ParserTest { ); String path2 = "(*[rdf:type is dbp-ont:Person]) | (dct:subject/^dct:subject[rdf:type is dbp-ont:Person]) | (dct:subject/^skos:broader/^dct:subject[rdf:type is dbp-ont:Person])"; NodeSelector<Value> s2 = parseSelector(path2,namespaces2); - Assert.assertTrue(s2 instanceof UnionSelector); + Assert.assertThat(s2, CoreMatchers.instanceOf(UnionSelector.class)); String path3 = "*[rdf:type is dbp-ont:Person] | dct:subject/^dct:subject[rdf:type is dbp-ont:Person] | dct:subject/^skos:broader/^dct:subject[rdf:type is dbp-ont:Person]"; NodeSelector<Value> s3 = parseSelector(path3,namespaces2); - Assert.assertTrue(s3 instanceof UnionSelector); + Assert.assertThat(s3, CoreMatchers.instanceOf(UnionSelector.class)); - Assert.assertEquals(s2,s3); - String path4 = "(* | dct:subject/^dct:subject | dct:subject/^skos:broader/^dct:subject)[rdf:type is dbp-ont:Person]"; NodeSelector<Value> s4 = parseSelector(path4,namespaces2); - Assert.assertTrue(s4 instanceof TestingSelector); + Assert.assertThat(s4, CoreMatchers.instanceOf(TestingSelector.class)); } private NodeSelector<Value> parseSelector(String selector, Map<String,String> namespaces) throws ParseException { @@ -109,8 +108,8 @@ public class ParserTest { Assert.assertNotNull(p3.getFilter()); Assert.assertEquals(5, p3.getNamespaces().size()); Assert.assertNotNull(p3.getField("person")); - Assert.assertTrue(p3.getField("person").getSelector() instanceof PathSelector); - Assert.assertTrue(p3.getField("person").getTransformer() instanceof StringTransformer); + Assert.assertThat(p3.getField("person").getSelector() , CoreMatchers.instanceOf(PathSelector.class)); + Assert.assertThat(p3.getField("person").getTransformer(), CoreMatchers.instanceOf( StringTransformer.class)); } http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/9629d75c/libraries/ldpath/ldpath-core/pom.xml ---------------------------------------------------------------------- diff --git a/libraries/ldpath/ldpath-core/pom.xml b/libraries/ldpath/ldpath-core/pom.xml index 00a8dbf..289c21d 100644 --- a/libraries/ldpath/ldpath-core/pom.xml +++ b/libraries/ldpath/ldpath-core/pom.xml @@ -68,6 +68,10 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-library</artifactId> + </dependency> + <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/9629d75c/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/SelectorsTest.java ---------------------------------------------------------------------- diff --git a/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/SelectorsTest.java b/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/SelectorsTest.java new file mode 100644 index 0000000..3f1ec3a --- /dev/null +++ b/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/SelectorsTest.java @@ -0,0 +1,83 @@ +package org.apache.marmotta.ldpath.parser; + +import java.io.StringReader; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.marmotta.ldpath.api.backend.NodeBackend; +import org.apache.marmotta.ldpath.api.selectors.NodeSelector; +import org.hamcrest.CoreMatchers; +import org.hamcrest.text.IsEqualIgnoringWhiteSpace; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class SelectorsTest { + + @Parameters(name = "{1}Selector") + public static List<String[]> testCases() { + return Arrays.asList(new String[][] { + {"*", "Wildcard"}, + {"<http://www.example.com/>", "Property"}, + {"<http://foo.bar> / <http://bar.foo>", "Path"}, + {"<http://foo.bar> & <http://bar.foo>", "Intersection"}, + {"<http://foo.bar> | <http://bar.foo>", "Union"}, + {"<http://foo.bar>[<http://bar.foo>]", "Testing"}, + {"(<http://www.example.com/>[@en])", "Grouped"}, + {"(<http://www.example.com/>)*", "RecursivePath"}, + {"(<http://www.example.com/>)+", "RecursivePath"}, + {"^<http://www.example.com/>", "ReverseProperty"}, + {"fn:count(\"foo\")", "Function"}, + // Not implemented yet: {"^*", "ReverseProperty"}, + {".", "Self"}, + {"\"Hello World\"", "StringConstant"}, + }); + } + + @Parameter + public String expr; + + @Parameter(1) + public String name; + + private NodeSelector<String> selector; + + private static NodeBackend<String> backend; + + @BeforeClass + public static void beforeClass() { + backend = new StringTestingBackend(); + } + + @Before + public void before() throws ParseException { + RdfPathParser<String> rdfPathParser = new RdfPathParser<String>(backend,new StringReader(expr)); + selector = rdfPathParser.parseSelector(Collections.<String,String>emptyMap()); + } + + @Test + public void testGetPathExpression() { + Assert.assertThat(selector.getPathExpression(backend), IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace(expr)); + } + + @Test + public void testParseSelector() { + final String className = "org.apache.marmotta.ldpath.model.selectors." + name + "Selector"; + try { + final Class<?> cls = Class.forName(className); + Assert.assertThat(selector, CoreMatchers.instanceOf(cls)); + } catch (ClassNotFoundException e) { + Assert.fail("Could not load class: " + className); + } + + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/9629d75c/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/StringTestingBackend.java ---------------------------------------------------------------------- diff --git a/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/StringTestingBackend.java b/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/StringTestingBackend.java new file mode 100644 index 0000000..47a1032 --- /dev/null +++ b/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/StringTestingBackend.java @@ -0,0 +1,81 @@ +package org.apache.marmotta.ldpath.parser; + +import java.net.URI; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.marmotta.ldpath.model.backend.AbstractBackend; + +public class StringTestingBackend extends AbstractBackend<String> { + + private static final Pattern LANG_PATTERN = Pattern.compile("@(\\w+)"), + TYPE_PATTERN = Pattern.compile("\\^\\^([\\w:/.#%-]+)"); + + @Override + public boolean isLiteral(String n) { + return n.startsWith("\""); + } + + @Override + public boolean isURI(String n) { + return n.startsWith("<"); + } + + @Override + public boolean isBlank(String n) { + return n.startsWith("_"); + } + + @Override + public Locale getLiteralLanguage(String n) { + final Matcher m = LANG_PATTERN.matcher(n); + if (m.find()) { + return new Locale(m.group(1)); + } + return null; + } + + @Override + public URI getLiteralType(String n) { + final Matcher m = TYPE_PATTERN.matcher(n); + if (m.find()) { + return URI.create(m.group(1)); + } + return null; + } + + @Override + public String createLiteral(String content) { + return "\""+content+"\""; + } + + @Override + public String createLiteral(String content, Locale language, URI type) { + StringBuilder sb = new StringBuilder('"'); + sb.append(content).append('"'); + if (language != null) { + sb.append("@").append(language.getLanguage()); + } + if (type != null) { + sb.append("^^").append(type.toString()); + } + return sb.toString(); + } + + @Override + public String createURI(String uri) { + return "<" + uri + ">"; + } + + @Override + public String stringValue(String node) { + if (node.startsWith("<")) { + return node.substring(1, node.length()-1); + } else if (node.startsWith("\"")) { + return node.substring(1, node.indexOf('"', 1)); + } else + return node; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/9629d75c/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/TestsTest.java ---------------------------------------------------------------------- diff --git a/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/TestsTest.java b/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/TestsTest.java new file mode 100644 index 0000000..845f138 --- /dev/null +++ b/libraries/ldpath/ldpath-core/src/test/java/org/apache/marmotta/ldpath/parser/TestsTest.java @@ -0,0 +1,78 @@ +package org.apache.marmotta.ldpath.parser; + +import java.io.StringReader; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.marmotta.ldpath.api.backend.NodeBackend; +import org.apache.marmotta.ldpath.api.tests.NodeTest; +import org.hamcrest.CoreMatchers; +import org.hamcrest.text.IsEqualIgnoringWhiteSpace; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class TestsTest { + + @Parameters(name = "{1}Test") + public static List<String[]> testCases() { + return Arrays.asList(new String[][] { + {"<http://foo.bar> & <http://bar.foo>", "And"}, + {"fn:eq(\"1\", \"2\")", "Function"}, + {"@en", "LiteralLanguage"}, + {"^^<http://foo.bar>", "LiteralType"}, + {"!@en", "Not"}, + {"<http://foo.bar> | <http://bar.foo>", "Or"}, + {"<http://www.example.com/> is <http://foo.bar>", "PathEquality"}, + {"is-a <http://foo.bar>", "PathEquality"}, + {"<http://www.example.com/>", "Path"}, + }); + } + + @Parameter + public String expr; + + @Parameter(1) + public String name; + + private NodeTest<String> test; + + private static NodeBackend<String> backend; + + @BeforeClass + public static void beforeClass() { + backend = new StringTestingBackend(); + } + + @Before + public void before() throws ParseException { + RdfPathParser<String> rdfPathParser = new RdfPathParser<String>(backend,new StringReader(expr)); + test = rdfPathParser.parseTest(Collections.<String,String>emptyMap()); + } + + @Test + public void testGetPathExpression() { + Assert.assertThat(test.getPathExpression(backend), IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace(expr)); + } + + @Test + public void testParseSelector() { + final String className = "org.apache.marmotta.ldpath.model.tests." + name + "Test"; + try { + final Class<?> cls = Class.forName(className); + Assert.assertThat(test, CoreMatchers.instanceOf(cls)); + } catch (ClassNotFoundException e) { + Assert.fail("Could not load class: " + className); + } + + } + + +}
