You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/05/23 21:46:48 UTC
[3/3] jena git commit: JENA-907 : Tests for splitting IRIs.
JENA-907 : Tests for splitting IRIs.
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/575a2b41
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/575a2b41
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/575a2b41
Branch: refs/heads/master
Commit: 575a2b41746b67603bb125b60ee6cd8353077f67
Parents: a3907db
Author: Andy Seaborne <an...@apache.org>
Authored: Sat May 23 20:21:58 2015 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Sat May 23 20:21:58 2015 +0100
----------------------------------------------------------------------
.../java/org/apache/jena/util/TestPackage.java | 7 +-
.../org/apache/jena/util/TestSplitIRI_TTL.java | 160 +++++++++++++++++++
.../org/apache/jena/util/TestSplitIRI_XML.java | 138 ++++++++++++++++
.../java/org/apache/jena/util/TestUtil.java | 138 ----------------
4 files changed, 302 insertions(+), 141 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/575a2b41/jena-core/src/test/java/org/apache/jena/util/TestPackage.java
----------------------------------------------------------------------
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestPackage.java b/jena-core/src/test/java/org/apache/jena/util/TestPackage.java
index dc213e6..22d5a58 100644
--- a/jena-core/src/test/java/org/apache/jena/util/TestPackage.java
+++ b/jena-core/src/test/java/org/apache/jena/util/TestPackage.java
@@ -40,10 +40,11 @@ public class TestPackage extends TestSuite {
addTest( "TestHashUtils", TestCollectionFactory.suite() );
addTest( "TestLocationMapper", TestLocationMapper.suite() ) ;
addTest( "TestFileManager", TestFileManager.suite()) ;
- addTest( "TestMonitors", TestMonitors.suite()) ;
- addTest( "TestPrintUtil", TestPrintUtil.suite()) ;
+ addTest( "TestMonitors", TestMonitors.suite()) ;
+ addTest( "TestPrintUtil", TestPrintUtil.suite()) ;
addTest( TestIteratorCollection.suite() );
- addTest( "TestUtil", TestUtil.suite()) ;
+ addTest( "TestSplitIRI_XML", TestSplitIRI_XML.suite()) ;
+ addTest( "TestSplitIRI_TTL", TestSplitIRI_TTL.suite()) ;
addTestSuite( TestLocators.class );
addTestSuite( TestOneToManyMap.class );
}
http://git-wip-us.apache.org/repos/asf/jena/blob/575a2b41/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
----------------------------------------------------------------------
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
new file mode 100644
index 0000000..a698e6e
--- /dev/null
+++ b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.util;
+
+import static org.apache.jena.util.SplitIRI.* ;
+
+import java.util.Objects ;
+
+import junit.framework.JUnit4TestAdapter ;
+import org.junit.Assert ;
+import org.junit.Test ;
+
+/** Test splitting IRI strings using Turtle rules.
+ */
+public class TestSplitIRI_TTL {
+ public static junit.framework.Test suite() {
+ return new JUnit4TestAdapter(TestSplitIRI_TTL.class) ;
+ }
+
+ // Basics
+ @Test public void split_basic_00() { testSplit("http://example/foo", "http://example/".length()) ; }
+ @Test public void split_basic_01() { testPrefixLocalname("http://example/foo", "http://example/", "foo" ) ; }
+ @Test public void split_basic_02() { testPrefixLocalname("http://example/foo#bar", "http://example/foo#", "bar" ) ; }
+ @Test public void split_basic_03() { testPrefixLocalname("http://example/foo#", "http://example/foo#", "" ) ; }
+ @Test public void split_basic_04() { testPrefixLocalname("http://example/", "http://example/", "" ) ; }
+ @Test public void split_basic_05() { testPrefixLocalname("http://example/1abc", "http://example/", "1abc" ) ; }
+ @Test public void split_basic_06() { testPrefixLocalname("http://example/1.2.3.4", "http://example/", "1.2.3.4" ) ; }
+ @Test public void split_basic_07() { testPrefixLocalname("http://example/xyz#1.2.3.4", "http://example/xyz#", "1.2.3.4" ) ; }
+ @Test public void split_basic_08() { testPrefixLocalname("http://example/xyz#_abc", "http://example/xyz#", "_abc" ) ; }
+ @Test public void split_basic_09() { testPrefixLocalname("http://example/xyz/_1.2.3.4", "http://example/xyz/", "_1.2.3.4" ) ; }
+
+ // Relative URIs
+ @Test public void split_rel_1() { testPrefixLocalname("xyz/_1.2.3.4", "xyz/", "_1.2.3.4" ) ; }
+ @Test public void split_rel_2() { testPrefixLocalname("xyz", "", "xyz" ) ; }
+ @Test public void split_rel_3() { testPrefixLocalname("", "", "" ) ; }
+
+ // Bizarre but legal URIs
+ @Test public void split_weird_1() { testPrefixLocalname("abc:def", "abc:", "def" ) ; }
+
+
+ // Turtle details.
+ // "." leading dot is not legal.
+ @Test public void split_ttl_01() { testPrefixLocalname("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
+ @Test public void split_ttl_02() { testPrefixLocalname("http://example/a:b:c", "http://example/", "a:b:c" ) ; }
+ @Test public void split_ttl_03() { testPrefixLocalname("http://example/.2.3.4", "http://example/.", "2.3.4" ) ; }
+
+ // "." leading dot is not legal.
+ @Test public void split_ttl_04() { testPrefixLocalname("abc:xyz/.def", "abc:xyz/.", "def" ) ; }
+ // "-" leading dash is not legal.
+ @Test public void split_ttl_05() { testPrefixLocalname("abc:xyz/-def", "abc:xyz/-", "def" ) ; }
+ @Test public void split_ttl_06() { testPrefixLocalname("abc:xyz/-.-.-def", "abc:xyz/-.-.-", "def" ) ; }
+ @Test public void split_ttl_07() { testPrefixLocalname("http://example/id=89", "http://example/", "id=89" ) ; }
+ // Trailing '.'
+ @Test public void split_ttl_08() { testPrefixLocalname("http://example/2.3.", "http://example/", "2.3." ) ; }
+
+ // Turtle details, including escaping.
+ // Test for PrefixLocalnameEsc
+ @Test public void split_ttl_esc_01() { testPrefixLocalnameEsc("http://example/id=89", "id\\=89" ) ; }
+ @Test public void split_ttl_esc_02() { testPrefixLocalnameEsc("http://example/a,b", "a\\,b" ) ; }
+ // Trailing '.'
+ @Test public void split_ttl_esc_033() { testPrefixLocalnameEsc("http://example/2.3.", "2.3\\." ) ; }
+
+ // URNs split differently.
+ @Test public void split_urn_01() { testPrefixLocalname("urn:foo:bar", "urn:foo:", "bar" ) ; }
+
+ // Anti-tests
+ @Test public void split_51() { testPrefixLocalnameNot("http://example/foo#bar:baz", "http://example/foo#bar", "baz" ) ; }
+
+
+
+ private void testSplit(String string, int expected) {
+ int i = splitpoint(string) ;
+ Assert.assertEquals(expected, i) ;
+ }
+
+ // ??
+ private void testTurtle(String string, String expectedPrefix, String expectedLocalname) {
+ int i = splitpoint(string) ;
+ String ns = string ;
+ String ln = "" ;
+ if ( i > 0 ) {
+ ns = string.substring(0, i) ;
+ ln = string.substring(i) ;
+ }
+
+ if ( expectedPrefix != null )
+ Assert.assertEquals(expectedPrefix, ns);
+ if ( expectedLocalname != null )
+ Assert.assertEquals(expectedLocalname, ln);
+ if ( expectedPrefix != null && expectedLocalname != null ) {
+ String x = ns+ln ;
+ Assert.assertEquals(string, x) ;
+ }
+ }
+
+ private void testPrefixLocalnameNoSplit(String string) {
+ int i = splitpoint(string) ;
+ String msg = string ;
+ if ( i != -1 ) {
+ // Better error message.
+ String ns = namespace(string) ;
+ String ln = localname(string) ;
+ msg = "Unexpected split of '"+string+"' into ("+ns+", "+ln+") [index="+i+"]" ;
+ }
+ Assert.assertEquals(msg, -1, i) ;
+ }
+
+ private void testPrefixLocalname(String string, String expectedPrefix, String expectedLocalname) {
+ String ns = namespace(string) ;
+ String ln = localname(string) ;
+
+ if ( expectedPrefix != null )
+ Assert.assertEquals(expectedPrefix, ns);
+ if ( expectedLocalname != null )
+ Assert.assertEquals(expectedLocalname, ln);
+ if ( expectedPrefix != null && expectedLocalname != null ) {
+ String x = ns+ln ;
+ Assert.assertEquals(string, x) ;
+ }
+ }
+
+ private void testPrefixLocalnameEsc(String string, String expectedLocalname) {
+ String ln = localnameTTL(string) ;
+ Assert.assertEquals(expectedLocalname, ln);
+ }
+
+ private void testPrefixLocalnameNot(String string, String expectedPrefix, String expectedLocalname) {
+// Node n = NodeFactory.createURI(string) ;
+// String ns = n.getNameSpace() ;
+// String ln = n.getLocalName() ;
+ String ns = namespace(string) ;
+ String ln = localname(string) ;
+
+ boolean b1 = Objects.equals(expectedPrefix, ns) ;
+ boolean b2 = Objects.equals(expectedLocalname, ln) ;
+
+ // Test not both true.
+ Assert.assertFalse("Wrong: ("+ns+","+ln+")", b1&&b2);
+ // But it still combines.
+ String x = ns+ln ;
+ Assert.assertEquals(string, x) ;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/575a2b41/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_XML.java
----------------------------------------------------------------------
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_XML.java b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_XML.java
new file mode 100644
index 0000000..5719886
--- /dev/null
+++ b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_XML.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.util;
+
+import junit.framework.JUnit4TestAdapter;
+import org.apache.jena.rdf.model.impl.Util ;
+import org.junit.Test;
+import static org.junit.Assert.*;
+public class TestSplitIRI_XML
+{
+ public static junit.framework.Test suite() {
+ return new JUnit4TestAdapter(TestSplitIRI_XML.class) ;
+ }
+
+ // Intermediate : JUnit 3 and JUnit 4.
+
+ @Test public void splitNS_01()
+ { split("http://example/xyz", "http://example/", "xyz") ; }
+
+ @Test public void splitNS_02()
+ { split("http://example/ns#xyz", "http://example/ns#", "xyz") ; }
+
+ @Test public void splitNS_03()
+ { no_split("http://example/ns#") ; }
+
+ @Test public void splitNS_04()
+ { no_split("http://example/") ; }
+
+ @Test public void splitNS_05() // Illegal URI
+ { split("http://example", "http://", "example") ; }
+
+ @Test public void splitNS_06() // localname must be at least the NCStartChar - not empty
+ { split("mailto:me", "mailto:m", "e") ; }
+
+ @Test public void splitNS_07()
+ { split("urn:abc:xyz","urn:abc:", "xyz") ; }
+
+ @Test public void splitNS_08()
+ { no_split("urn:abc:xyz:") ; }
+
+ @Test public void splitNS_09()
+ { split("http://bio2rdf.org/pdb:Pentane-3%2C4-diol-5-phosphate", "http://bio2rdf.org/pdb:Pentane-3%2C4-", "diol-5-phosphate") ; }
+
+ @Test public void splitNS_10()
+ { split("http://bio2rdf.org/pdb:Pentane-3,4-diol-5-phosphate", "http://bio2rdf.org/pdb:Pentane-3,4-", "diol-5-phosphate") ; }
+
+ // Don't split inside a %encoding.
+ @Test public void splitNS_11()
+ { split("http://host/abc%AAdef", "http://host/abc%AA", "def") ; }
+
+ @Test public void splitNS_12()
+ { split("http://host/abc%1Adef", "http://host/abc%1A", "def") ; }
+
+ @Test public void splitNS_13()
+ { split("http://host/abc%A1def", "http://host/abc%A1", "def") ; }
+
+ @Test public void splitNS_14()
+ { split("http://host/abc%AA22def", "http://host/abc%AA22", "def") ; }
+
+ @Test public void splitNS_15()
+ { no_split("http://host/abc%AA22") ; }
+
+ // Other schemes
+
+ @Test public void splitNS_50()
+ { split("file:///x/y", "file:///x/", "y") ; }
+
+ @Test public void splitNS_51()
+ { split("file:///x", "file:///", "x") ; }
+
+ @Test public void splitNS_52()
+ { split("file:x", "file:", "x") ; }
+
+ @Test public void splitNS_53()
+ // Not ideal but some URI schemes dislike a URI with just the scheme
+ { split("file:foo", "file:", "foo") ; }
+
+ @Test public void splitNS_54()
+ { split("file:c:/foo", "file:c:/", "foo") ; }
+
+ // urn:uuid:d871c7f4-2926-11b2-8073-a5e169788449 - legal type 1 uuid as urn
+ // uuid:3cf3e43a-3a5d-40d8-a93c-8697b162a1c0 - legal type 4 uuid as uri
+
+ @Test public void splitNS_55()
+ { split("urn:uuid:d871c7f4-2926-11b2-8073-a5e169788449", "urn:uuid:", "d871c7f4-2926-11b2-8073-a5e169788449") ; }
+
+ @Test public void splitNS_56()
+ { split("uuid:3cf3e43a-3a5d-40d8-a93c-8697b162a1c0", "uuid:3", "cf3e43a-3a5d-40d8-a93c-8697b162a1c0") ; }
+
+ @Test public void splitNS_57()
+ { split("urn:abc:def", "urn:abc:", "def") ; }
+
+ // --------
+
+ static void no_split(String string)
+ { split(string, null, null) ; }
+
+ static void split(String uriStr, String namespace, String localname)
+ {
+ if ( namespace == null && localname != null )
+ fail("Bad test - namespace is null but local name is not") ;
+ if ( namespace != null && localname == null )
+ fail("Bad test - namespace is not null but local name is") ;
+
+ int idx = Util.splitNamespaceXML(uriStr) ;
+ if ( idx == uriStr.length() )
+ {
+ // No split.
+ if ( namespace != null )
+ fail("Expected a split ("+namespace+","+localname+") - but none found") ;
+ return ;
+
+ }
+ // Split
+ String ns = uriStr.substring(0,idx) ;
+ String ln = uriStr.substring(idx) ;
+ assertEquals(namespace, ns) ;
+ assertEquals(localname, ln) ;
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/575a2b41/jena-core/src/test/java/org/apache/jena/util/TestUtil.java
----------------------------------------------------------------------
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestUtil.java b/jena-core/src/test/java/org/apache/jena/util/TestUtil.java
deleted file mode 100644
index 9eb0538..0000000
--- a/jena-core/src/test/java/org/apache/jena/util/TestUtil.java
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.jena.util;
-
-import junit.framework.JUnit4TestAdapter;
-import org.apache.jena.rdf.model.impl.Util ;
-import org.junit.Test;
-import static org.junit.Assert.*;
-public class TestUtil
-{
- public static junit.framework.Test suite() {
- return new JUnit4TestAdapter(TestUtil.class) ;
- }
-
- // Intermediate : JUnit 3 and JUnit 4.
-
- @Test public void splitNS_01()
- { split("http://example/xyz", "http://example/", "xyz") ; }
-
- @Test public void splitNS_02()
- { split("http://example/ns#xyz", "http://example/ns#", "xyz") ; }
-
- @Test public void splitNS_03()
- { no_split("http://example/ns#") ; }
-
- @Test public void splitNS_04()
- { no_split("http://example/") ; }
-
- @Test public void splitNS_05() // Illegal URI
- { split("http://example", "http://", "example") ; }
-
- @Test public void splitNS_06() // localname must be at least the NCStartChar - not empty
- { split("mailto:me", "mailto:m", "e") ; }
-
- @Test public void splitNS_07()
- { split("urn:abc:xyz","urn:abc:", "xyz") ; }
-
- @Test public void splitNS_08()
- { no_split("urn:abc:xyz:") ; }
-
- @Test public void splitNS_09()
- { split("http://bio2rdf.org/pdb:Pentane-3%2C4-diol-5-phosphate", "http://bio2rdf.org/pdb:Pentane-3%2C4-", "diol-5-phosphate") ; }
-
- @Test public void splitNS_10()
- { split("http://bio2rdf.org/pdb:Pentane-3,4-diol-5-phosphate", "http://bio2rdf.org/pdb:Pentane-3,4-", "diol-5-phosphate") ; }
-
- // Don't split inside a %encoding.
- @Test public void splitNS_11()
- { split("http://host/abc%AAdef", "http://host/abc%AA", "def") ; }
-
- @Test public void splitNS_12()
- { split("http://host/abc%1Adef", "http://host/abc%1A", "def") ; }
-
- @Test public void splitNS_13()
- { split("http://host/abc%A1def", "http://host/abc%A1", "def") ; }
-
- @Test public void splitNS_14()
- { split("http://host/abc%AA22def", "http://host/abc%AA22", "def") ; }
-
- @Test public void splitNS_15()
- { no_split("http://host/abc%AA22") ; }
-
- // Other schemes
-
- @Test public void splitNS_50()
- { split("file:///x/y", "file:///x/", "y") ; }
-
- @Test public void splitNS_51()
- { split("file:///x", "file:///", "x") ; }
-
- @Test public void splitNS_52()
- { split("file:x", "file:", "x") ; }
-
- @Test public void splitNS_53()
- // Not ideal but some URI schemes dislike a URI with just the scheme
- { split("file:foo", "file:", "foo") ; }
-
- @Test public void splitNS_54()
- { split("file:c:/foo", "file:c:/", "foo") ; }
-
- // urn:uuid:d871c7f4-2926-11b2-8073-a5e169788449 - legal type 1 uuid as urn
- // uuid:3cf3e43a-3a5d-40d8-a93c-8697b162a1c0 - legal type 4 uuid as uri
-
- @Test public void splitNS_55()
- { split("urn:uuid:d871c7f4-2926-11b2-8073-a5e169788449", "urn:uuid:", "d871c7f4-2926-11b2-8073-a5e169788449") ; }
-
- @Test public void splitNS_56()
- { split("uuid:3cf3e43a-3a5d-40d8-a93c-8697b162a1c0", "uuid:3", "cf3e43a-3a5d-40d8-a93c-8697b162a1c0") ; }
-
- @Test public void splitNS_57()
- { split("urn:abc:def", "urn:abc:", "def") ; }
-
- // --------
-
- static void no_split(String string)
- { split(string, null, null) ; }
-
- static void split(String uriStr, String namespace, String localname)
- {
- if ( namespace == null && localname != null )
- fail("Bad test - namespace is null but local name is not") ;
- if ( namespace != null && localname == null )
- fail("Bad test - namespace is not null but local name is") ;
-
- int idx = Util.splitNamespaceXML(uriStr) ;
- if ( idx == uriStr.length() )
- {
- // No split.
- if ( namespace != null )
- fail("Expected a split ("+namespace+","+localname+") - but none found") ;
- return ;
-
- }
- // Split
- String ns = uriStr.substring(0,idx) ;
- String ln = uriStr.substring(idx) ;
- assertEquals(namespace, ns) ;
- assertEquals(localname, ln) ;
- }
-
-
-}