📄 testnode.java
字号:
/*
(c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
[See end of file]
$Id: TestNode.java,v 1.47 2007/07/04 15:21:57 chris-dollin Exp $
*/
package com.hp.hpl.jena.graph.test;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.impl.*;
import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.impl.Util;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.datatypes.*;
import com.hp.hpl.jena.vocabulary.*;
import junit.framework.*;
/**
@author bwm out of kers
Exercise nodes. Make sure that the different node types do not overlap
and that the test predicates work properly on the different node kinds.
*/
public class TestNode extends GraphTestBase
{
public TestNode( String name )
{ super( name ); }
public static TestSuite suite()
{ return new TestSuite( TestNode.class ); }
private static final String U = "http://some.domain.name/magic/spells.incant";
private static final String N = "Alice";
private static final LiteralLabel L = new LiteralLabel( "ashes are burning", "en", false );
private static final AnonId A = AnonId.create();
public void testBlanks()
{
assertTrue( "anonymous nodes are blank", Node.createAnon().isBlank() );
assertFalse( "anonymous nodes aren't literal", Node.createAnon().isLiteral() );
assertFalse( "anonymous nodes aren't URIs", Node.createAnon().isURI() );
assertFalse( "anonymous nodes aren't variables", Node.createAnon().isVariable() );
assertEquals( "anonymous nodes have the right id", Node.createAnon(A).getBlankNodeId(), A );
}
public void testLiterals()
{
assertFalse( "literal nodes aren't blank", Node.createLiteral( L ).isBlank() );
assertTrue( "literal nodes are literal", Node.createLiteral( L ).isLiteral() );
assertFalse( "literal nodes aren't variables", Node.createLiteral( L ).isVariable() );
assertFalse( "literal nodes aren't URIs", Node.createLiteral( L ).isURI() );
assertEquals( "literal nodes preserve value", Node.createLiteral( L ).getLiteral(), L );
}
public void testURIs()
{
assertFalse( "URI nodes aren't blank", Node.createURI( U ).isBlank() );
assertFalse( "URI nodes aren't literal", Node.createURI( U ).isLiteral() );
assertFalse( "URI nodes aren't variables", Node.createURI( U ).isVariable() );
assertTrue( "URI nodes are URIs", Node.createURI( U ).isURI() );
assertEquals( "URI nodes preserve URI", Node.createURI( U ).getURI(), U );
}
public void testVariables()
{
assertFalse( "variable nodes aren't blank", Node.createVariable( N ).isBlank() );
assertFalse( "variable nodes aren't literal", Node.createVariable( N ).isLiteral() );
assertFalse( "variable nodes aren't URIs", Node.createVariable( N ).isURI() );
assertTrue( "variable nodes are variable", Node.createVariable( N ).isVariable() );
assertEquals( "variable nodes keep their name", N, Node.createVariable( N ).getName() );
assertEquals( "variable nodes keep their name", N + "x", Node.createVariable( N + "x" ).getName() );
}
public void testANY()
{
assertFalse( "ANY nodes aren't blank", Node.ANY.isBlank() );
assertFalse( "ANY nodes aren't literals", Node.ANY.isLiteral() );
assertFalse( "ANY nodes aren't URIs", Node.ANY.isURI() );
assertFalse( "ANY nodes aren't variables", Node.ANY.isVariable() );
assertFalse( "ANY nodes aren't blank", Node.ANY.isBlank() );
assertFalse( "ANY nodes aren't blank", Node.ANY.isBlank() );
}
public void testNodeVariableConstructor()
{
assertEquals( Node.createVariable( "hello" ), new Node_Variable( "hello" ) );
assertEquals( Node.createVariable( "world" ), new Node_Variable( "world" ) );
assertDiffer( Node.createVariable( "hello" ), new Node_Variable( "world" ) );
assertEquals( "myName", new Node_Variable( "myName" ).getName() );
}
/**
test cases for equality: an array of (Node, String) pairs. [It's not worth
making a special class for these pairs.] The nodes are created with caching
off, to make sure that caching effects don't hide the effect of using .equals().
The strings are "equality groups": the nodes should test equal iff their
associated strings test equal.
*/
private Object [][] eqTestCases()
{
try
{
Node.cache( false );
AnonId id = AnonId.create();
LiteralLabel L2 = new LiteralLabel( id.toString(), "", false );
LiteralLabel LLang1 = new LiteralLabel( "xyz", "en", null) ;
LiteralLabel LLang2 = new LiteralLabel( "xyz", "EN", null) ;
String U2 = id.toString();
String N2 = id.toString();
return new Object [][]
{
{ Node.ANY, "0" },
{ Node.createAnon( id ), "1" },
{ Node.createAnon(), "2" },
{ Node.createAnon( id ), "1" },
{ Node.createLiteral( L ), "3" },
{ Node.createLiteral( L2 ), "4" },
{ Node.createLiteral( L ), "3" },
{ Node.createURI( U ), "5" },
{ Node.createURI( U2 ), "6" },
{ Node.createURI( U ), "5" },
{ Node.createVariable( N ), "7" },
{ Node.createVariable( N2 ), "8" },
{ Node.createVariable( N ), "7" } ,
{ Node.createLiteral( LLang1 ), "9" },
{ Node.createLiteral( LLang2 ), "10" },
};
}
finally
{ Node.cache( true ); }
}
public void testNodeEquals()
{
Object [][] tests = eqTestCases();
for (int i = 0; i < tests.length; i += 1)
{
Object [] I = tests[i];
assertFalse( I[0] + " should not equal null", I[0].equals( null ) );
assertFalse( I[0] + "should not equal 'String'", I[0].equals( "String" ) );
for (int j = 0; j < tests.length; j += 1)
{
Object [] J = tests[j];
testEquality( I[1].equals( J[1] ), I[0], J[0] );
}
}
}
private void testEquality( boolean testEq, Object x, Object y )
{
String testName = getType( x ) + " " + x + " and " + getType( y ) + " " + y;
if (testEq)
assertEquals( testName + "should be equal", x, y );
else
assertDiffer( testName + " should differ", x, y );
}
private String getType( Object x )
{
String fullName = x.getClass().getName();
return fullName.substring( fullName.lastIndexOf( '.' ) + 1 );
}
public void testEquals()
{
try
{
Node.cache( false );
assertDiffer( "different variables", Node.createVariable( "xx" ), Node.createVariable( "yy" ) );
assertEquals( "same vars", Node.createVariable( "aa" ), Node.createVariable( "aa" ) );
assertEquals( "same URI", Node.createURI( U ), Node.createURI( U ) );
assertEquals( "same anon", Node.createAnon( A ), Node.createAnon( A ) );
assertEquals( "same literal", Node.createLiteral( L ), Node.createLiteral( L ) );
assertFalse( "distinct URIs", Node.createURI( U ) == Node.createURI( U ) );
assertFalse( "distinct hyphens", Node.createAnon( A ) == Node.createAnon( A ) );
assertFalse( "distinct literals", Node.createLiteral( L ) == Node.createLiteral( L ) );
assertFalse( "distinct vars", Node.createVariable( "aa" ) == Node.createVariable( "aa" ) );
}
finally
{ Node.cache( true ); }
}
/**
test that the label of a Node can be retrieved from that Node in
a way appropriate to that Node.
*/
public void testLabels()
{
AnonId id = AnonId.create();
assertEquals( "get URI value", U, Node.createURI( U ).getURI() );
assertEquals( "get blank value", id, Node.createAnon( id ).getBlankNodeId() );
assertEquals( "get literal value", L, Node.createLiteral( L ).getLiteral() );
assertEquals( "get variable name", N, Node.createVariable( N ).getName() );
}
/**
this is where we test that using the wrong accessor on a Node gets you
an exception.
*/
public void testFailingLabels()
{
Node u = Node.createURI( U ), b = Node.createAnon();
Node l = Node.createLiteral( L ), v = Node.createVariable( N );
Node a = Node.ANY;
/* */
testGetURIFails( a );
testGetURIFails( b );
testGetURIFails( l );
testGetURIFails( v );
/* */
testGetLiteralFails( a );
testGetLiteralFails( u );
testGetLiteralFails( b );
testGetLiteralFails( v );
/* */
testGetNameFails( a );
testGetNameFails( u );
testGetNameFails( b );
testGetNameFails( l );
/* */
testGetBlankNodeIdFails( a );
testGetBlankNodeIdFails( u );
testGetBlankNodeIdFails( l );
testGetBlankNodeIdFails( v );
}
public void testGetBlankNodeIdFails( Node n )
{ try { n.getBlankNodeId(); fail( n.getClass() + " should fail getName()" ); } catch (UnsupportedOperationException e) {} }
public void testGetURIFails( Node n )
{ try { n.getURI(); fail( n.getClass() + " should fail getURI()" ); } catch (UnsupportedOperationException e) {} }
public void testGetNameFails( Node n )
{ try { n.getName(); fail( n.getClass() + " should fail getName()" ); } catch (UnsupportedOperationException e) {} }
public void testGetLiteralFails( Node n )
{ try { n.getLiteral(); fail( n.getClass() + " should fail getLiteral()" ); } catch (UnsupportedOperationException e) {} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -