📄 repositorytest.java
字号:
/* * Sesame - Storage and Querying architecture for RDF and RDF Schema Copyright * (C) 2001-2005 Aduna * * Contact: Aduna Prinses Julianaplein 14 b 3817 CS Amersfoort The Netherlands * tel. +33 (0)33 465 99 87 fax. +33 (0)33 465 99 87 * * http://aduna.biz/ http://www.openrdf.org/ * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.repository;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import junit.framework.TestCase;import org.openrdf.model.Graph;import org.openrdf.model.GraphException;import org.openrdf.model.Resource;import org.openrdf.model.Statement;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.model.ValueFactory;import org.openrdf.model.impl.URIImpl;import org.openrdf.sesame.admin.StdOutAdminListener;import org.openrdf.sesame.config.AccessDeniedException;import org.openrdf.sesame.constants.QueryLanguage;import org.openrdf.sesame.constants.RDFFormat;import org.openrdf.sesame.query.MalformedQueryException;import org.openrdf.sesame.query.QueryEvaluationException;import org.openrdf.sesame.query.QueryResultsTable;import org.openrdf.sesame.sail.StatementIterator;public abstract class RepositoryTest extends TestCase {/*----------+| Constants |+----------*/ private static final String OUTPUT_PATH = "/tmp/"; private static final String FILE_SERVER = "http://www.openrdf.org/sesame/";/*----------+| Variables |+----------*/ protected SesameRepository _repository;/*-------------+| Constructors |+-------------*/ /** * Creates a new HTTP repository test. */ public RepositoryTest(String name) { super(name); } protected void setUp() throws Exception { _repository = _createRepository(); }/*--------+| Methods |+--------*/ /** * Creates the repository to perform the tests on. **/ protected abstract SesameRepository _createRepository(); public void testPerformTableQuery() { try { QueryResultsTable resultsTable = _repository.performTableQuery( QueryLanguage.SERQL, "select * from {X} rdf:type {rdfs:Resource}"); assertTrue(resultsTable != null); } catch (Exception e) { fail("[" + e.getClass() + "]: " + e.getMessage()); } } public void testPerformMalformedTableQuery() { try { QueryResultsTable resultsTable = _repository.performTableQuery( QueryLanguage.SERQL, "foobar"); fail("Expected a MalformedQueryException"); } catch (MalformedQueryException e) { // This is correct } catch (Exception e) { fail("[" + e.getClass() + "]: " + e.getMessage()); } } public void testPerformGraphQuery() { try { Graph graph = _repository.performGraphQuery(QueryLanguage.SERQL, "construct * from {X} rdf:type {rdfs:Resource}"); } catch (Exception e) { fail("[" + e.getClass() + "]: " + e.getMessage()); } } public void testPerformMalformedGraphQuery() { try { Graph graph = _repository.performGraphQuery(QueryLanguage.SERQL, "foobar"); fail("Expected a MalformedQueryException"); } catch (MalformedQueryException e) { // This is correct } catch (Exception e) { fail("[" + e.getClass() + "]: " + e.getMessage()); } } public void testExtractRDF() { try { URL dataURL = new URL(FILE_SERVER + "files/museum/schema1.rdf"); String baseURI = "http://www.icom.com/schema.rdf"; _repository.clear(new StdOutAdminListener()); _repository.addData(dataURL, baseURI, RDFFormat.RDFXML, true, new StdOutAdminListener()); InputStream in = _repository.extractRDF(RDFFormat.RDFXML, true, true, false, true); FileOutputStream os = new FileOutputStream(OUTPUT_PATH + "extractRDF-test.rdf"); for(int i = in.read(); i != -1; i = in.read()) { os.write(i); } in.close(); os.close(); } catch (Exception e) { e.printStackTrace(); fail("[" + e.getClass() + "]: " + e.getMessage()); } } public void testAddData_URL() { try { URL dataURL = new URL(FILE_SERVER + "files/museum/schema1.rdf"); String baseURI = "http://www.icom.com/schema.rdf"; _repository.clear(new StdOutAdminListener()); _repository.addData(dataURL, baseURI, RDFFormat.RDFXML, true, new StdOutAdminListener()); } catch (MalformedURLException e) { fail("malformed url: " + e); } catch (IOException e) { fail("I/O error: " + e.getMessage()); } catch (AccessDeniedException e) { fail("Access denied: " + e.getMessage()); } } public void testAddData_File() { try { String workingDir = System.getProperty("user.dir"); File dataFile = new File(workingDir + "/test/files/museum/schema1.rdf"); String baseURI = "http://www.icom.com/schema.rdf"; _repository.clear(new StdOutAdminListener()); _repository.addData(dataFile, baseURI, RDFFormat.RDFXML, true, new StdOutAdminListener()); } catch (MalformedURLException e) { fail("malformed url: " + e); } catch (IOException e) { fail("I/O error: " + e); } catch (AccessDeniedException e) { fail("Access denied: " + e.getMessage()); } } public void testAddGraph() { try { String workingDir = System.getProperty("user.dir"); File dataFile = new File(workingDir + "/test/files/museum/schema1.rdf"); String baseURI = "http://www.icom.com/schema.rdf"; _repository.clear(new StdOutAdminListener()); _repository.addData(dataFile, baseURI, RDFFormat.RDFXML, true, new StdOutAdminListener()); } catch (MalformedURLException e) { fail("malformed url: " + e); } catch (IOException e) { fail("I/O error: " + e); } catch (AccessDeniedException e) { fail("Access denied: " + e.getMessage()); } try { Graph transformedGraph = _repository.performGraphQuery(QueryLanguage.SERQL, "construct {Y} <http://www.foo.com/bar> {X} from {X} rdfs:subClassOf {Y} where Y != X"); _repository.addGraph(transformedGraph); Graph otherGraph = _repository.performGraphQuery(QueryLanguage.SERQL, "construct * from {X} <http://www.foo.com/bar> {Y}"); ValueFactory factory = otherGraph.getValueFactory(); URI predicate = factory.createURI("http://www.foo.com/bar"); assertTrue(otherGraph.contains(null, predicate, null)); Statement st = null; try { StatementIterator iter = otherGraph.getStatements(null, predicate, null);//predicate.getPredicateStatements(); st = iter.next(); iter.close(); } catch (Exception e) { fail("Graph error: " + e); } Statement st2 = factory.createStatement(st.getSubject(), st.getPredicate(), st.getObject()); assertTrue(otherGraph.contains(st)); assertTrue(otherGraph.contains(st2)); } catch (QueryEvaluationException e) { fail("query evaluation exception: " + e.getMessage()); } catch (IOException e) { fail("I/O error: " + e.getMessage()); } catch (MalformedQueryException e) { fail("Malformed query: " + e.getMessage()); } catch (AccessDeniedException e) { fail("Access denied: " + e.getMessage()); } } public void testAddGraph_bNodes_notJoined() { try { String workingDir = System.getProperty("user.dir"); File dataFile = new File(workingDir + "/test/files/testcases/bnodes/foaf.rdf"); _repository.clear(new StdOutAdminListener()); _repository.addData(dataFile, "", RDFFormat.RDFXML, true, new StdOutAdminListener()); Graph toBeAdded = _repository.performGraphQuery(QueryLanguage.SERQL, " construct {y} foaf:knows {x} from {x} foaf:knows {y} " + " using namespace foaf = <http://xmlns.com/foaf/0.1/>"); _repository.addGraph(toBeAdded, false); Graph result = _repository.performGraphQuery(QueryLanguage.SERQL, " construct * from {x} foaf:knows {y} foaf:knows {x} " + " using namespace foaf = <http://xmlns.com/foaf/0.1/>"); ValueFactory factory = result.getValueFactory(); URI predicate = factory.createURI("http://xmlns.com/foaf/0.1/knows"); // this add should create new bnodes, so the relations should not be symmetric. assertFalse(result.contains(null, predicate, null)); } catch (QueryEvaluationException e) { fail("query evaluation exception: " + e.getMessage()); } catch (IOException e) { fail("I/O error: " + e.getMessage()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -