📄 graphimpltest.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.model.impl;import java.io.IOException;import java.net.URL;import junit.framework.TestCase;import org.openrdf.model.BNode;import org.openrdf.model.Graph;import org.openrdf.model.GraphException;import org.openrdf.model.Literal;import org.openrdf.model.Statement;import org.openrdf.model.URI;import org.openrdf.model.ValueFactory;import org.openrdf.sesame.Sesame;import org.openrdf.sesame.admin.AdminMsg;import org.openrdf.sesame.admin.AdminMsgCollector;import org.openrdf.sesame.constants.QueryLanguage;import org.openrdf.sesame.repository.SesameRepository;import org.openrdf.sesame.repository.SesameService;import org.openrdf.sesame.sail.StatementIterator;/** * JUnit test for the Graph API. * * @author Jeen Broekstra * @author Arjohn Kampman * @version $Revision: 1.11.4.1 $ */public class GraphImplTest extends TestCase { private static final String SERVER_URL = "http://localhost:8080/sesame"; private static final String REMOTE_RDF_REPOSITORY_ID = "mem-rdf-db"; private static final String USERNAME = "testuser"; private static final String PASSWORD = "opensesame"; private static final String SUBJECT1_URI = "http://www.foo.com/subject1"; private static final String PREDICATE1_URI = "http://www.foo.com/predicate1"; private static final String PREDICATE2_URI = "http://www.foo.com/predicate2"; private static final String OBJECT1_URI = "http://www.foo.com/object1"; private static final String OBJECT2_LITERAL = "literal object 2"; private Graph _graph; private ValueFactory _factory; protected void setUp() { _graph = new GraphImpl(); _factory = _graph.getValueFactory(); } public void testAddStatement() { URI subject1 = _factory.createURI(SUBJECT1_URI); URI predicate1 = _factory.createURI(PREDICATE1_URI); URI predicate2 = _factory.createURI(PREDICATE1_URI); URI object1 = _factory.createURI(OBJECT1_URI); _graph.add(subject1, predicate1, object1); Literal object2 = _factory.createLiteral(OBJECT2_LITERAL); _graph.add(subject1, predicate1, object2); assertTrue(_graph.contains(subject1, predicate1, object1)); assertTrue(_graph.contains(subject1, predicate1, object2)); StatementIterator iter1 = null; try { iter1 = object2.getObjectStatements(); } catch (GraphException e) { fail("Graph exception occurred :" + e.getMessage()); } while (iter1.hasNext()) { Statement st = iter1.next(); assertTrue(st.getObject().equals(object2)); } try { subject1.addProperty(predicate2, object2); } catch (GraphException e) { fail("Graph exception occurred :" + e.getMessage()); } assertTrue(_graph.contains(subject1, predicate2, object2)); } public void testBNodeIdentity() { BNode node1 = _factory.createBNode(); BNode node2 = _factory.createBNode(); URI predicate1 = _factory.createURI(PREDICATE1_URI); URI predicate2 = _factory.createURI(PREDICATE1_URI); URI object1 = _factory.createURI(OBJECT1_URI); _graph.add(node1, predicate1, object1); assertTrue(_graph.contains(node1, predicate1, object1)); assertFalse(_graph.contains(node2, predicate1, object1)); _graph.add(node2, predicate1, object1); assertTrue(_graph.contains(node1, predicate1, object1)); assertTrue(_graph.contains(node2, predicate1, object1)); assertFalse(node1.equals(node2)); StatementIterator iter = _graph.getStatements(node1, null, null); // there should be only one statement if (iter.hasNext()) { Statement st = iter.next(); assertFalse(iter.hasNext()); } } public void testRemoveStatement() { URI subject1 = _factory.createURI(SUBJECT1_URI); URI predicate1 = _factory.createURI(PREDICATE1_URI); URI object1 = _factory.createURI(OBJECT1_URI); _graph.add(subject1, predicate1, object1); Literal object2 = _factory.createLiteral(OBJECT2_LITERAL); _graph.add(subject1, predicate1, object2); _graph.remove(subject1, predicate1, object1); assertFalse(_graph.contains(subject1, predicate1, object1)); assertTrue(_graph.contains(subject1, predicate1, object2)); } public void testGraphComparison() { URI subject1 = _factory.createURI(SUBJECT1_URI); URI predicate1 = _factory.createURI(PREDICATE1_URI); URI object1 = _factory.createURI(OBJECT1_URI); _graph.add(subject1, predicate1, object1); Graph graph2 = new GraphImpl(); ValueFactory factory2 = graph2.getValueFactory(); URI subject2 = factory2.createURI(SUBJECT1_URI); URI predicate2 = factory2.createURI(PREDICATE1_URI); URI object2 = factory2.createURI(OBJECT1_URI); graph2.add(subject2, predicate2, object2); StatementIterator iter = graph2.getStatements(); while (iter.hasNext()) { Statement st = iter.next(); assertTrue(_graph.contains(st)); } iter.close(); iter = _graph.getStatements(); while (iter.hasNext()) { Statement st = iter.next(); assertTrue(graph2.contains(st)); } iter.close(); } public void testRemoteQueryGraphComparison() throws Exception { URI subject1 = _factory.createURI(SUBJECT1_URI); URI predicate1 = _factory.createURI(PREDICATE1_URI); URI object1 = _factory.createURI(OBJECT1_URI); _graph.add(subject1, predicate1, object1); SesameService service = Sesame.getService(new URL(SERVER_URL)); service.login(USERNAME, PASSWORD); SesameRepository rep = service.getRepository(REMOTE_RDF_REPOSITORY_ID); AdminMsgCollector listener = new AdminMsgCollector(); rep.clear(listener); if (listener.hasErrors()) { AdminMsg firstErrMsg = (AdminMsg)listener.getErrors().get(0); throw new IOException(firstErrMsg.getMessage()); } rep.addGraph(_graph); Graph graph2 = rep.performGraphQuery(QueryLanguage.SERQL, "CONSTRUCT * FROM {X} <" + PREDICATE1_URI + "> {Y}"); StatementIterator iter = _graph.getStatements(); while (iter.hasNext()) { Statement st = iter.next(); assertTrue(graph2.contains(st)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -