📄 abstracttestgraph.java
字号:
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
[See end of file]
$Id: AbstractTestGraph.java,v 1.72 2007/01/02 11:50:08 andy_seaborne Exp $i
*/
package com.hp.hpl.jena.graph.test;
import com.hp.hpl.jena.util.CollectionFactory;
import com.hp.hpl.jena.util.iterator.*;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.query.*;
import com.hp.hpl.jena.shared.*;
import java.util.*;
/**
AbstractTestGraph provides a bunch of basic tests for something that
purports to be a Graph. The abstract method getGraph must be overridden
in subclasses to deliver a Graph of interest.
@author kers
*/
public/* abstract */class AbstractTestGraph extends GraphTestBase
{
public AbstractTestGraph( String name )
{ super( name ); }
/**
Returns a Graph to take part in the test. Must be overridden in
a subclass.
*/
// public abstract Graph getGraph();
public Graph getGraph() { return Factory.createGraphMem(); }
public Graph getGraphWith( String facts )
{
Graph g = getGraph();
graphAdd( g, facts );
return g;
}
public void testCloseSetsIsClosed()
{
Graph g = getGraph();
assertFalse( "unclosed Graph shouild not be isClosed()", g.isClosed() );
g.close();
assertTrue( "closed Graph should be isClosed()", g.isClosed() );
}
/**
This test case was generated by Ian and was caused by GraphMem
not keeping up with changes to the find interface.
*/
public void testFindAndContains()
{
Graph g = getGraph();
Node r = Node.create( "r" ), s = Node.create( "s" ), p = Node.create( "P" );
g.add( Triple.create( r, p, s ) );
assertTrue( g.contains( r, p, Node.ANY ) );
assertEquals( 1, g.find( r, p, Node.ANY ).toList().size() );
}
public void testRepeatedSubjectDoesNotConceal()
{
Graph g = getGraphWith( "s P o; s Q r" );
assertTrue( g.contains( triple( "s P o" ) ) );
assertTrue( g.contains( triple( "s Q r" ) ) );
assertTrue( g.contains( triple( "?? P o" ) ) );
assertTrue( g.contains( triple( "?? Q r" ) ) );
assertTrue( g.contains( triple( "?? P ??" ) ) );
assertTrue( g.contains( triple( "?? Q ??" ) ) );
}
public void testFindByFluidTriple()
{
Graph g = getGraphWith( "x y z " );
Set expect = tripleSet( "x y z" );
assertEquals( expect, g.find( triple( "?? y z" ) ).toSet() );
assertEquals( expect, g.find( triple( "x ?? z" ) ).toSet() );
assertEquals( expect, g.find( triple( "x y ??" ) ).toSet() );
}
public void testContainsConcrete()
{
Graph g = getGraphWith( "s P o; _x _R _y; x S 0" );
assertTrue( g.contains( triple( "s P o" ) ) );
assertTrue( g.contains( triple( "_x _R _y" ) ) );
assertTrue( g.contains( triple( "x S 0" ) ) );
/* */
assertFalse( g.contains( triple( "s P Oh" ) ) );
assertFalse( g.contains( triple( "S P O" ) ) );
assertFalse( g.contains( triple( "s p o" ) ) );
assertFalse( g.contains( triple( "_x _r _y" ) ) );
assertFalse( g.contains( triple( "x S 1" ) ) );
}
public void testContainsFluid()
{
Graph g = getGraphWith( "x R y; a P b" );
assertTrue( g.contains( triple( "?? R y" ) ) );
assertTrue( g.contains( triple( "x ?? y" ) ) );
assertTrue( g.contains( triple( "x R ??" ) ) );
assertTrue( g.contains( triple( "?? P b" ) ) );
assertTrue( g.contains( triple( "a ?? b" ) ) );
assertTrue( g.contains( triple( "a P ??" ) ) );
assertTrue( g.contains( triple( "?? R y" ) ) );
/* */
assertFalse( g.contains( triple( "?? R b" ) ) );
assertFalse( g.contains( triple( "a ?? y" ) ) );
assertFalse( g.contains( triple( "x P ??" ) ) );
assertFalse( g.contains( triple( "?? R x" ) ) );
assertFalse( g.contains( triple( "x ?? R" ) ) );
assertFalse( g.contains( triple( "a S ??" ) ) );
}
/**
Check that contains respects by-value semantics.
*/
public void testContainsByValue()
{
if (getGraph().getCapabilities().handlesLiteralTyping())
{ // TODO fix the adhocness of this
Graph g1 = getGraphWith( "x P '1'xsd:integer" );
assertTrue( g1.contains( triple( "x P '01'xsd:int" ) ) );
//
Graph g2 = getGraphWith( "x P '1'xsd:int" );
assertTrue( g2.contains( triple( "x P '1'xsd:integer" ) ) );
//
Graph g3 = getGraphWith( "x P '123'xsd:string" );
assertTrue( g3.contains( triple( "x P '123'" ) ) );
}
}
/**
test isEmpty - moved from the QueryHandler code.
*/
public void testIsEmpty()
{
Graph g = getGraph();
if (canBeEmpty( g ))
{
assertTrue( g.isEmpty() );
g.add( Triple.create( "S P O" ) );
assertFalse( g.isEmpty() );
g.add( Triple.create( "A B C" ) );
assertFalse( g.isEmpty() );
g.add( Triple.create( "S P O" ) );
assertFalse( g.isEmpty() );
g.delete( Triple.create( "S P O" ) );
assertFalse( g.isEmpty() );
g.delete( Triple.create( "A B C" ) );
assertTrue( g.isEmpty() );
}
}
public void testAGraph()
{
String title = this.getClass().getName();
Graph g = getGraph();
int baseSize = g.size();
graphAdd( g, "x R y; p S q; a T b" );
/* */
assertContainsAll( title + ": simple graph", g, "x R y; p S q; a T b" );
assertEquals( title + ": size", baseSize + 3, g.size() );
graphAdd( g, "spindizzies lift cities; Diracs communicate instantaneously" );
assertEquals( title + ": size after adding", baseSize + 5, g.size() );
g.delete( triple( "x R y" ) );
g.delete( triple( "a T b" ) );
assertEquals( title + ": size after deleting", baseSize + 3, g.size() );
assertContainsAll( title + ": modified simple graph", g, "p S q; spindizzies lift cities; Diracs communicate instantaneously" );
assertOmitsAll( title + ": modified simple graph", g, "x R y; a T b" );
/* */
ClosableIterator it = g.find( Node.ANY, node("lift"), Node.ANY );
assertTrue( title + ": finds some triple(s)", it.hasNext() );
assertEquals( title + ": finds a 'lift' triple", triple("spindizzies lift cities"), it.next() );
assertFalse( title + ": finds exactly one triple", it.hasNext() );
it.close();
}
// public void testStuff()
// {
//// testAGraph( "StoreMem", new GraphMem() );
//// testAGraph( "StoreMemBySubject", new GraphMem() );
//// String [] empty = new String [] {};
//// Graph g = graphWith( "x R y; p S q; a T b" );
//// /* */
//// assertContainsAll( "simple graph", g, "x R y; p S q; a T b" );
//// graphAdd( g, "spindizzies lift cities; Diracs communicate instantaneously" );
//// g.delete( triple( "x R y" ) );
//// g.delete( triple( "a T b" ) );
//// assertContainsAll( "modified simple graph", g, "p S q; spindizzies lift cities; Diracs communicate instantaneously" );
//// assertOmitsAll( "modified simple graph", g, "x R y; a T b" );
// }
/**
Test that Graphs have transaction support methods, and that if they fail
on some g they fail because they do not support the operation.
*/
public void testHasTransactions()
{
Graph g = getGraph();
TransactionHandler th = g.getTransactionHandler();
th.transactionsSupported();
try { th.begin(); } catch (UnsupportedOperationException x) {}
try { th.abort(); } catch (UnsupportedOperationException x) {}
try { th.commit(); } catch (UnsupportedOperationException x) {}
/* */
Command cmd = new Command()
{ public Object execute() { return null; } };
try { th.executeInTransaction( cmd ); }
catch (UnsupportedOperationException x) {}
}
public void testExecuteInTransactionCatchesThrowable()
{Graph g = getGraph();
TransactionHandler th = g.getTransactionHandler();
if (th.transactionsSupported())
{
Command cmd = new Command()
{ public Object execute() throws Error { throw new Error(); } };
try { th.executeInTransaction( cmd ); }
catch (JenaException x) {}
}
}
static final Triple [] tripleArray = tripleArray( "S P O; A R B; X Q Y" );
static final List tripleList = Arrays.asList( tripleArray( "i lt j; p equals q" ) );
static final Triple [] setTriples = tripleArray
( "scissors cut paper; paper wraps stone; stone breaks scissors" );
static final Set tripleSet = CollectionFactory.createHashedSet( Arrays.asList( setTriples ) );
public void testBulkUpdate()
{
Graph g = getGraph();
BulkUpdateHandler bu = g.getBulkUpdateHandler();
Graph items = graphWith( "pigs might fly; dead can dance" );
int initialSize = g.size();
/* */
bu.add( tripleArray );
testContains( g, tripleArray );
testOmits( g, tripleList );
/* */
bu.add( tripleList );
testContains( g, tripleList );
testContains( g, tripleArray );
/* */
bu.add( tripleSet.iterator() );
testContains( g, tripleSet.iterator() );
testContains( g, tripleList );
testContains( g, tripleArray );
/* */
bu.add( items );
testContains( g, items );
testContains( g, tripleSet.iterator() );
testContains( g, tripleArray );
testContains( g, tripleList );
/* */
bu.delete( tripleArray );
testOmits( g, tripleArray );
testContains( g, tripleList );
testContains( g, tripleSet.iterator() );
testContains( g, items );
/* */
bu.delete( tripleSet.iterator() );
testOmits( g, tripleSet.iterator() );
testOmits( g, tripleArray );
testContains( g, tripleList );
testContains( g, items );
/* */
bu.delete( items );
testOmits( g, tripleSet.iterator() );
testOmits( g, tripleArray );
testContains( g, tripleList );
testOmits( g, items );
/* */
bu.delete( tripleList );
assertEquals( "graph has original size", initialSize, g.size() );
}
public void testBulkAddWithReification()
{
testBulkAddWithReification( false );
testBulkAddWithReification( true );
}
public void testBulkAddWithReificationPreamble()
{
Graph g = getGraph();
xSPO( g.getReifier() );
assertFalse( getReificationTriples( g.getReifier() ).isEmpty() );
}
public void testBulkAddWithReification( boolean withReifications )
{
Graph graphToUpdate = getGraph();
BulkUpdateHandler bu = graphToUpdate.getBulkUpdateHandler();
Graph graphToAdd = graphWith( "pigs might fly; dead can dance" );
Reifier updatedReifier = graphToUpdate.getReifier();
Reifier addedReifier = graphToAdd.getReifier();
xSPOyXYZ( addedReifier );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -