http://git-wip-us.apache.org/repos/asf/jena/blob/74c4bd06/src/test/java/org/apache/jena/propertytable/impl/GraphCSVTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/jena/propertytable/impl/GraphCSVTest.java b/src/test/java/org/apache/jena/propertytable/impl/GraphCSVTest.java deleted file mode 100644 index c5fa87e..0000000 --- a/src/test/java/org/apache/jena/propertytable/impl/GraphCSVTest.java +++ /dev/null @@ -1,142 +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.propertytable.impl; - -import org.junit.Assert; -import org.junit.Test; - -import com.hp.hpl.jena.query.ARQ; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.sparql.engine.main.StageBuilder; -import com.hp.hpl.jena.sparql.engine.main.StageGenerator; -import com.hp.hpl.jena.util.PrintUtil; - -public class GraphCSVTest extends Assert { - - @Test - public void testGraphCSV() throws Exception { - //String file = "src/test/resources/HEFCE_organogram_senior_data_31032011.csv";test.csv - String file = "src/test/resources/test.csv"; - - Model csv = ModelFactory.createModelForGraph(new GraphCSV(file)); - assertEquals(12, csv.size()); - - Query query = QueryFactory - .create("PREFIX : <src/test/resources/test.csv#> SELECT ?townName ?pop {?x :Town ?townName ; :Population ?pop ; :Predicate%20With%20Space 'PredicateWithSpace2' . FILTER(?pop > 500000)}"); - - QueryExecution qexec = QueryExecutionFactory.create(query, csv); - ResultSet results = qexec.execSelect(); - - assertTrue(results.hasNext()); - QuerySolution soln = results.nextSolution(); - assertEquals( "Northville", soln.getLiteral("townName").getString()); - assertTrue( 654000 == soln.getLiteral("pop").getInt()); - - assertFalse(results.hasNext()); - } - - @Test - public void stageGeneratorTest() throws Exception{ - wireIntoExecution(); - testGraphCSV(); - } - - private static void wireIntoExecution() { - StageGenerator orig = (StageGenerator)ARQ.getContext().get(ARQ.stageGenerator) ; - StageGenerator stageGenerator = new StageGeneratorPropertyTable(orig) ; - StageBuilder.setGenerator(ARQ.getContext(), stageGenerator) ; - } - - //http://www.w3.org/TR/csvw-ucr/#UC-OrganogramData - //2.4 Use Case #4 - Publication of public sector roles and salaries - @Test - public void testUseCase4(){ - String file = "src/test/resources/HEFCE_organogram_senior_data_31032011.csv"; - - Model csv = ModelFactory.createModelForGraph(new GraphCSV(file)); - assertEquals(72, csv.size()); - - Query query = QueryFactory - .create("PREFIX : <src/test/resources/HEFCE_organogram_senior_data_31032011.csv#> SELECT ?name ?unit {?x :Name ?name ; :Unit ?unit ; :Actual%20Pay%20Floor%20%28%A3%29 ?floor ; :Actual%20Pay%20Ceiling%20%28%A3%29 ?ceiling . FILTER(?floor > 100000 && ?ceiling <120000 )}"); - - QueryExecution qexec = QueryExecutionFactory.create(query, csv); - ResultSet results = qexec.execSelect(); - - assertTrue(results.hasNext()); - QuerySolution soln = results.nextSolution(); - assertEquals( "David Sweeney", soln.getLiteral("name").getString()); - assertEquals( "Research, Innovation and Skills", soln.getLiteral("unit").getString()); - - assertFalse(results.hasNext()); - } - - - //http://www.w3.org/TR/csvw-ucr/#UC-JournalArticleSearch - //2.6 Use Case #6 - Journal Article Solr Search Results - @Test - public void testUseCase6(){ - String file = "src/test/resources/PLOSone-search-results.csv"; - - Model csv = ModelFactory.createModelForGraph(new GraphCSV(file)); - assertEquals(30, csv.size()); - - Query query = QueryFactory - .create("PREFIX : <src/test/resources/PLOSone-search-results.csv#> SELECT ?author {?x :author ?author ; :doi '10.1371/journal.pone.0095156' }"); - - QueryExecution qexec = QueryExecutionFactory.create(query, csv); - ResultSet results = qexec.execSelect(); - - assertTrue(results.hasNext()); - QuerySolution soln = results.nextSolution(); - assertEquals( "Oshrat Raz,Dorit L Lev,Alexander Battler,Eli I Lev", soln.getLiteral("author").getString()); - - assertFalse(results.hasNext()); - } - - //http://www.w3.org/TR/csvw-ucr/#UC-PaloAltoTreeData - //2.11 Use Case #11 - City of Palo Alto Tree Data - @Test - public void testUseCase11(){ - String file = "src/test/resources/Palo_Alto_Trees.csv"; - - Model csv = ModelFactory.createModelForGraph(new GraphCSV(file)); - assertEquals(199, csv.size()); - - Query query = QueryFactory - .create("PREFIX : <src/test/resources/Palo_Alto_Trees.csv#> SELECT ?longitude ?latitude {?x :Longitude ?longitude ; :Latitude ?latitude ; :Distance%20from%20Property ?distance . FILTER(?distance > 50 )}"); - - QueryExecution qexec = QueryExecutionFactory.create(query, csv); - ResultSet results = qexec.execSelect(); - - assertTrue(results.hasNext()); - QuerySolution soln = results.nextSolution(); - assertEquals( -122.1566921, soln.getLiteral("longitude").getDouble(), 0); - assertEquals( 37.4408948, soln.getLiteral("latitude").getDouble(), 0); - - assertFalse(results.hasNext()); - } - -}
http://git-wip-us.apache.org/repos/asf/jena/blob/74c4bd06/src/test/java/org/apache/jena/propertytable/lang/TestLangCSV.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/jena/propertytable/lang/TestLangCSV.java b/src/test/java/org/apache/jena/propertytable/lang/TestLangCSV.java new file mode 100644 index 0000000..0e0204d --- /dev/null +++ b/src/test/java/org/apache/jena/propertytable/lang/TestLangCSV.java @@ -0,0 +1,107 @@ +/* + * 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.propertytable.lang; + +import java.io.StringReader; + +import org.apache.jena.atlas.junit.BaseTest; +import org.apache.jena.atlas.lib.StrUtils; +import org.apache.jena.riot.Lang; +import org.apache.jena.riot.RDFDataMgr; +import org.apache.jena.riot.RDFLanguages; +import org.apache.jena.riot.system.IRIResolver; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; + +public class TestLangCSV extends BaseTest { + private static final String FILE_NAME = "src/test/resources/test.csv"; + private static final String FILE_URI = IRIResolver.resolveString(FILE_NAME); + + @BeforeClass + public static void init(){ + LangCSV.register(); + } + + @Test + public void testPredicateWithSpace() { + String[] s1 = { "Predicate With Space", "PredicateWithSpace" }; + String[] s2 = { + //"<"+ LangCSV.caculateSubject(1, FILE_NAME) + "> <" + FILE_URI + "#Predicate+With+Space> 'PredicateWithSpace' ; ", + " [] <" + FILE_URI + "#Predicate%20With%20Space> 'PredicateWithSpace' ; ", + " <http://w3c/future-csv-vocab/row> 1 ." }; + assertIsomorphicWith(s1, s2); + } + + @Test + public void testNonURICharacters() { + String[] s1 = { "`~!@#$%^&*()-_=+[{]}|\\;:'\"<.>/?", "NonURICharacters" }; + String[] s2 = { + //"<"+ LangCSV.caculateSubject(1, FILE_NAME) + "> <" + FILE_URI + "#%60%7E%21%40%23%24%25%5E%26*%28%29-_%3D%2B%5B%7B%5D%7D%7C%5C%3B%3A%27%22%3C.%3E%2F%3F> 'NonURICharacters' ; ", + " [] <" + FILE_URI + "#%60~%21%40%23%24%25%5E%26%2A%28%29-_%3D%2B%5B%7B%5D%7D%7C%5C%3B%3A%27%22%3C.%3E%2F%3F> 'NonURICharacters' ; ", + " <http://w3c/future-csv-vocab/row> 1 ." }; + assertIsomorphicWith(s1, s2); + } + + @Test + public void testDigitalLocalName() { + String[] s1 = { "1234", "DigitalLocalName" }; + String[] s2 = { + //"<"+ LangCSV.caculateSubject(1, FILE_NAME) + "> <" + FILE_URI + "#1234> 'DigitalLocalName' ; ", + " [] <" + FILE_URI + "#1234> 'DigitalLocalName' ; ", + " <http://w3c/future-csv-vocab/row> 1 ." }; + assertIsomorphicWith(s1, s2); + } + + @Test + public void testMoney() { + String[] s1 = { "£", "£" }; + String[] s2 = { + //"<"+ LangCSV.caculateSubject(1, FILE_NAME) + "> <" + FILE_URI + "#1234> 'DigitalLocalName' ; ", + " [] <" + FILE_URI + "#%A3> '£' ; ", + " <http://w3c/future-csv-vocab/row> 1 ." }; + assertIsomorphicWith(s1, s2); + } + + @Test + public void RDFDataMgrReadTest() { + Model m1 = RDFDataMgr.loadModel(FILE_NAME, RDFLanguages.CSV); + Model m2 = ModelFactory.createDefaultModel(); + m2.read(FILE_NAME, "CSV"); + assertEquals(12, m1.size()); + assertTrue(m1.isIsomorphicWith(m2)); + } + + private Model parseToModel(String[] strings, Lang lang) { + String string = StrUtils.strjoin("\n", strings); + StringReader r = new StringReader(string); + Model model = ModelFactory.createDefaultModel(); + RDFDataMgr.read(model, r, FILE_NAME, lang); + return model; + } + + private void assertIsomorphicWith(String[] s1, String[] s2){ + Model m1 = parseToModel(s1, RDFLanguages.CSV); + Model m2 = parseToModel(s2, RDFLanguages.TURTLE); + assertTrue(m1.isIsomorphicWith(m2)); + } + +}
