📄 rdfrepositorytest.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.sail;import junit.framework.TestCase;import org.openrdf.model.URI;import org.openrdf.model.impl.URIImpl;/** * A JUnit test for testing the methods of RdfRepository. * * @author Jeen Broekstra * @author Arjohn Kampman */public abstract class RdfRepositoryTest extends TestCase implements SailChangedListener {/*-------------------------------+| Constants |+-------------------------------*/ private static String SCHEMA1_URL = "http://www.icom.com/schema.rdf";/* private static String SCHEMA1_FILE = "/files/museum/schema1.rdf"; private static String SCHEMA2_FILE = "/files/museum/schema2.rdf"; private static String SCHEMA2_URL = "http://www.oclc.org/schema.rdf"; private static String CULTURE_FILE = "/files/museum/culture.rdf"; private static String CULTURE_URL = ""; private static String CLOSURE_FILE = "/files/museum/fullclosure.rdf"; private static String CLOSURE_URL = "";*/ private static URI CUBIST = new URIImpl(SCHEMA1_URL + "#", "Cubist"); private static URI PAINTER = new URIImpl(SCHEMA1_URL + "#", "Painter"); private static URI PAINTS = new URIImpl(SCHEMA1_URL + "#", "paints"); private static URI CREATES = new URIImpl(SCHEMA1_URL + "#", "creates"); private static URI ARTIST = new URIImpl(SCHEMA1_URL + "#", "Artist"); private static URI ARTIFACT = new URIImpl(SCHEMA1_URL + "#", "Artifact"); private static URI PAINTING = new URIImpl(SCHEMA1_URL + "#", "Painting"); private static URI PICASSO = new URIImpl("http://www.european-history.com/picasso.html"); private static URI GUERNICA = new URIImpl("http://www.european-history.com/jpg/guernica03.jpg");/*-------------------------------+| Variables |+-------------------------------*/ private RdfRepository _repository; private int _removedEvents; private int _addedEvents; /*-------------------------------+| Constructors |+-------------------------------*/ public RdfRepositoryTest(String name) { super(name); }/*-------------------------------+| Methods |+-------------------------------*/ protected void setUp() { try { // Create and initialize _repository _repository = _getTestRepository(); // set self as listener _repository.addListener(this); } catch (SailInitializationException e) { fail("unable to initialize repository: " + e.getMessage()); } } protected void tearDown() { _repository.shutDown(); _repository = null; } /** * Gets an instance of the RdfRepository that should be tested. The * returned repository should already have been initialized. * * @return an initialized RdfRepository. * @exception SailInitializationException If the initialization of the * repository failed. **/ protected abstract RdfRepository _getTestRepository() throws SailInitializationException;/*-------------------------+| test methods |+-------------------------*/ public void testAllMethods() { try { // repository should be empty StatementIterator statIter = _repository.getStatements(null, null, null); assertFalse( "Empty repository should not return any statements", statIter.hasNext()); // Add some data to the repository _repository.startTransaction(); _repository.addStatement(PAINTER, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS); _repository.addStatement(PICASSO, URIImpl.RDF_TYPE, PAINTER); _repository.addStatement(PAINTING, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS); _repository.addStatement(GUERNICA, URIImpl.RDF_TYPE, PAINTING); _repository.addStatement(PICASSO, PAINTS, GUERNICA); _repository.commitTransaction(); // Check for presence of the added statements assertTrue( "Statement (Painter, type, Class) should be in the repository", _repository.hasStatement(PAINTER, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS)); assertTrue( "Statement (picasso, type, Painter) should be in the repository", _repository.hasStatement(PICASSO, URIImpl.RDF_TYPE, PAINTER)); assertTrue( "Statement (Painting, type, Class) should be in the repository", _repository.hasStatement(PAINTING, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS)); assertTrue( "Statement (guernica, type, Painting) should be in the repository", _repository.hasStatement(GUERNICA, URIImpl.RDF_TYPE, PAINTING)); assertTrue( "Statement (picasso, paints, geurnica) should be in the repository", _repository.hasStatement(PICASSO, PAINTS, GUERNICA)); // Check for absense of non-added statements assertFalse( "Statement (Painter, paints, Painting) should not be in the repository", _repository.hasStatement(PAINTER, PAINTS, PAINTING)); assertFalse( "Statement (Painter, paints, Painting) should not be in the repository", _repository.hasStatement(PICASSO, CREATES, GUERNICA)); // repository should contain 5 statements in total statIter = _repository.getStatements(null, null, null); int statCount = _countStatements(statIter); assertEquals( "Repository should contain 5 statements in total", 5, statCount); // repository should contain 2 statements with picasso as subject statIter = _repository.getStatements(PICASSO, null, null); statCount = _countStatements(statIter); assertEquals( "Repository should contain 2 statements with picasso as subject", 2, statCount); // repository should contain 1 statements with picasso as subject // and paints as predicate statIter = _repository.getStatements(PICASSO, PAINTS, null); statCount = _countStatements(statIter); assertEquals( "Repository should contain 1 statements with picasso as subject " + "and paints as predicate", 1, statCount); // repository should contain 4 statements with rdf:type as predicate statIter = _repository.getStatements(null, URIImpl.RDF_TYPE, null); statCount = _countStatements(statIter); assertEquals( "Repository should contain 4 statements with paints as predicate", 4, statCount); // repository should contain 2 statements with rdfs:Class as object statIter = _repository.getStatements(null, null, URIImpl.RDFS_CLASS); statCount = _countStatements(statIter); assertEquals( "Repository should contain 2 statements with rdfs:Class as object", 2, statCount); // repository should contain 0 statements with rdf:type as object statIter = _repository.getStatements(null, null, URIImpl.RDF_TYPE); statCount = _countStatements(statIter); assertEquals( "Repository should contain 0 statements with rdf:type as object", 0, statCount); // Test removal of statements int removeCount = 0; _repository.startTransaction(); removeCount = _repository.removeStatements(PAINTING, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS); _repository.commitTransaction(); assertEquals( "Exactly 1 statement should have been removed", 1, removeCount); assertFalse( "Statement (Painting, type, Class) should no longer be in the repository", _repository.hasStatement(PAINTING, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS)); _repository.startTransaction(); removeCount = _repository.removeStatements(null, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS); _repository.commitTransaction(); assertEquals( "Exactly 1 statement should have been removed", 1, removeCount); assertFalse( "No statement matching (null, type, Class) should be in the repository", _repository.hasStatement(null, URIImpl.RDF_TYPE, URIImpl.RDFS_CLASS)); _repository.startTransaction(); removeCount = _repository.removeStatements(null, URIImpl.RDF_TYPE, null); _repository.commitTransaction(); assertEquals( "Exactly 2 statement should have been removed", 2, removeCount); assertFalse( "No statement matching (null, type, null) should be in the repository", _repository.hasStatement(null, URIImpl.RDF_TYPE, null)); _repository.startTransaction(); removeCount = _repository.removeStatements(null, null, URIImpl.RDFS_CLASS); _repository.commitTransaction(); assertEquals( "No statements should have been removed", 0, removeCount); // only one statements should be left statIter = _repository.getStatements(null, null, null); statCount = _countStatements(statIter); assertEquals( "Repository should contain 1 statements in total", 1, statCount); assertTrue( "Statement (picasso, paints, guernica) should still be in the repository", _repository.hasStatement(PICASSO, PAINTS, GUERNICA)); _repository.startTransaction(); _repository.clearRepository(); _repository.commitTransaction(); // Statement should be empty again statIter = _repository.getStatements(null, null, null); statCount = _countStatements(statIter); assertEquals( "Repository should contain 0 statements", 0, statCount); // test if event listener works properly. // one add event should have registered. assertEquals("There should have been 1 event in which statements were added", 1, _addedEvents); // four (not five since one of the removes makes no change!) // removal events should have registered. assertEquals("There should have been 4 events in which statements were removed", 4, _removedEvents); } catch (SailUpdateException e) { fail("Unable to add statement: " + e.getMessage()); } } public void sailChanged(SailChangedEvent event) { if (event.statementsRemoved()) { _removedEvents++; } if (event.statementsAdded()) { _addedEvents++; } } protected int _countStatements(StatementIterator statIter) { int statCount = 0; while (statIter.hasNext()) { statIter.next(); statCount++; } statIter.close(); return statCount; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -