📄 rdfschemarepositorytest.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 java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.HashSet;import java.util.Set;import junit.framework.TestCase;import org.openrdf.model.Resource;import org.openrdf.model.URI;import org.openrdf.model.impl.URIImpl;import org.openrdf.sesame.admin.RdfAdmin;import org.openrdf.sesame.admin.StdOutAdminListener;import org.openrdf.sesame.admin.UpdateException;import org.openrdf.sesame.constants.RDFFormat;/** * A JUnit test for testing the methods of RdfSchemaRepository. * * @author Jeen Broekstra * @author Arjohn Kampman */public abstract class RdfSchemaRepositoryTest extends TestCase implements SailChangedListener {/*-------------------------------+| Constants |+-------------------------------*/ private static String SCHEMA1_FILE = "/files/museum/schema1.rdf"; private static String SCHEMA1_URL = "http://www.icom.com/schema.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 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 RdfSchemaRepository _testRep; private RdfRepository _closureRep; private int _removedEvents; private int _addedEvents; /*-------------------------------+| Constructors |+-------------------------------*/ public RdfSchemaRepositoryTest(String name) { super(name); }/*-------------------------------+| Methods |+-------------------------------*/ protected void setUp() { try { // Create and initialize _closureRep _closureRep = new org.openrdf.sesame.sailimpl.memory.RdfRepository(); _closureRep.initialize(new HashMap()); // Upload the closure file _upload(_closureRep, CLOSURE_FILE, CLOSURE_URL, RDFFormat.RDFXML); // Create and initialize _testRep _testRep = _getTestRepository(); // Upload the RDF files _upload(_testRep, SCHEMA1_FILE, SCHEMA1_URL, RDFFormat.RDFXML); _upload(_testRep, SCHEMA2_FILE, SCHEMA2_URL, RDFFormat.RDFXML); _upload(_testRep, CULTURE_FILE, CULTURE_URL, RDFFormat.RDFXML); // add self as listener // note: this happens _after_ the initial upload to avoid counting them. _testRep.addListener(this); } catch (SailInitializationException e) { fail("unable to initialize repository: " + e.getMessage()); } catch (IOException e) { fail("unable to upload file: " + e.getMessage()); } catch (UpdateException e) { fail("unable add data: " + e.getMessage()); } } protected abstract RdfSchemaRepository _getTestRepository() throws SailInitializationException; protected void tearDown() { try { _testRep.startTransaction(); _testRep.clearRepository(); _testRep.commitTransaction(); } catch (SailUpdateException e) { fail("sail update exception while clearing:" + e.getMessage()); } _testRep.shutDown(); _testRep = null; try { _closureRep.startTransaction(); _closureRep.clearRepository(); _closureRep.commitTransaction(); } catch (SailUpdateException e) { fail("sail update exception while clearing:" + e.getMessage()); } _closureRep.shutDown(); _closureRep = null; } private void _upload(RdfRepository repository, String fileName, String baseURL, RDFFormat format) throws IOException, UpdateException { RdfAdmin admin = new RdfAdmin(repository); InputStream rdfStream = this.getClass().getResourceAsStream(fileName); admin.addRdfModel(rdfStream, baseURL, new StdOutAdminListener(), format, false); rdfStream.close(); }/*-------------------------+| test methods |+-------------------------*/ public void testClosureCorrectness() { assertTrue(SailUtil.modelsEqual(_testRep, _closureRep)); } public void testIsClass() { assertTrue( "Painter should be a class", _testRep.isClass(PAINTER)); assertTrue( "Artist should be a class", _testRep.isClass(ARTIST)); assertTrue( "Artifact should be a class", _testRep.isClass(ARTIFACT)); assertFalse( "paints should not be a class", _testRep.isClass(PAINTS)); assertFalse( "creates should not be a class", _testRep.isClass(CREATES)); } public void testIsProperty() { assertTrue( "paints should be a property", _testRep.isProperty(PAINTS)); assertTrue( "creates should be a property", _testRep.isProperty(CREATES)); assertFalse( "Painter should not be a property", _testRep.isProperty(PAINTER)); } public void testIsSubClassOf() { assertTrue( "Painter should be a subclass of Artist", _testRep.isSubClassOf(PAINTER, ARTIST)); assertTrue( "Cubist should be a subclass of Artist", _testRep.isSubClassOf(CUBIST, ARTIST)); assertTrue( "Artist should be a subclass of itself", _testRep.isSubClassOf(ARTIST, ARTIST)); assertFalse( "Artist should not be a subclass of Cubist", _testRep.isSubClassOf(ARTIST, CUBIST)); } public void testIsDirectSubClassOf() { assertTrue( "Painter should be a direct subclass of Artist", _testRep.isDirectSubClassOf(PAINTER, ARTIST)); assertFalse( "Artist should not be a direct subclass of itself", _testRep.isDirectSubClassOf(ARTIST, ARTIST)); assertFalse( "Cubist should not be a direct subclass of Artist", _testRep.isDirectSubClassOf(CUBIST, ARTIST)); } public void testGetSubClassOf() { StatementIterator statIter = _testRep.getSubClassOf(null, ARTIST); assertNotNull(statIter); assertTrue("there should be subclasses of Artist", statIter.hasNext()); Set subClasses = new HashSet(); while (statIter.hasNext()) { // no double elements allowed Resource subClass = statIter.next().getSubject(); assertTrue( "the list of subclasses of Artist should not contain duplicates", subClasses.add(subClass)); } statIter.close(); assertTrue( "Artist should be returned as a subclass of Artist", subClasses.contains(ARTIST)); assertTrue( "Painter should be returned as a subclass of Artist", subClasses.contains(PAINTER)); assertTrue( "Cubist should be returned as a subclass of Artist", subClasses.contains(CUBIST)); } public void testIsSubPropertyOf() { assertTrue( "paints should be a subproperty of creates", _testRep.isSubPropertyOf(PAINTS, CREATES)); assertTrue( "paints should be a subproperty of itself", _testRep.isSubPropertyOf(PAINTS, PAINTS)); assertFalse( "creates should not be a subproperty of paints", _testRep.isSubPropertyOf(CREATES, PAINTS)); } public void testIsDirectSubPropertyOf() { assertTrue( "paints should be a direct subproperty of creates", _testRep.isDirectSubPropertyOf(PAINTS, CREATES)); assertFalse( "paints should not be a direct subproperty of itself", _testRep.isDirectSubPropertyOf(PAINTS, PAINTS)); assertFalse( "creates should not be a direct subproperty of paints", _testRep.isDirectSubPropertyOf(CREATES, PAINTS)); } public void testAddStatement() { URI newCubist = new URIImpl("http://www.foo.com/someCubist"); try { _testRep.startTransaction(); _testRep.addStatement(newCubist, URIImpl.RDF_TYPE, CUBIST); _testRep.commitTransaction(); } catch (SailUpdateException e) { fail("Unable to add statement: " + e.getMessage()); } assertEquals("There should be one add statement event", 1, _addedEvents); assertEquals("There should be zero remove statement events", 0, _removedEvents); assertTrue( "repository should have statement (someCubist, rdf:type, Cubist)", _testRep.hasStatement(newCubist, URIImpl.RDF_TYPE, CUBIST)); assertTrue( "repository should have statement (someCubist, rdf:type, Artist)", _testRep.hasStatement(newCubist, URIImpl.RDF_TYPE, ARTIST)); } public void testRemoveStatement() { int result = 0; try { _testRep.startTransaction(); result = _testRep.removeStatements(PICASSO, null, GUERNICA); _testRep.commitTransaction(); } catch (SailUpdateException e) { fail("Unable to remove statement: " + e.getMessage()); } assertEquals("There should be zero add statement events", 0, _addedEvents); assertEquals("There should be one remove statement event", 1, _removedEvents); // one (explicit) statement should have been removed: // (picasso, paints, guernica) assertEquals( "The number of removed statements should be 1", 1, result); // no statements relating picasso to guernica should remain. assertFalse( "repository should not have statement (Picasso, paints, Guernica)", _testRep.hasStatement(PICASSO, PAINTS, GUERNICA)); assertFalse( "repository should not have statement (Picasso, creates, Guernica)", _testRep.hasStatement(PICASSO, CREATES, GUERNICA)); assertFalse( "repository should not have statements matching (Picasso, null, Guernica)", _testRep.hasStatement(PICASSO, null, GUERNICA)); } public void sailChanged(SailChangedEvent event) { if (event.statementsRemoved()) { _removedEvents++; } if (event.statementsAdded()) { _addedEvents++; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -