📄 testnode.java
字号:
{
Node n = Node.create( "\"chat\"xy-AB" );
assertEquals( "chat", n.getLiteralLexicalForm() );
assertEquals( "xy-AB", n.getLiteralLanguage() );
assertEquals( null, n.getLiteralDatatypeURI() );
}
public void testCreateTypedLiteralInteger()
{
Node n = Node.create( "'42'xsd:integer" );
assertEquals( "42", n.getLiteralLexicalForm() );
assertEquals( "", n.getLiteralLanguage() );
assertEquals( expand( "xsd:integer" ), n.getLiteralDatatypeURI() );
}
public void testCreateTypedLiteralBoolean()
{
Node n = Node.create( "\"true\"xsd:boolean" );
assertEquals( "true", n.getLiteralLexicalForm() );
assertEquals( "", n.getLiteralLanguage() );
assertEquals( expand( "xsd:boolean" ), n.getLiteralDatatypeURI() );
}
public void testGetPlainLiteralLexicalForm()
{
Node n = Node.create( "'stuff'" );
assertEquals( "stuff", n.getLiteralLexicalForm() );
}
public void testGetNumericLiteralLexicalForm()
{
Node n = Node.create( "17" );
assertEquals( "17", n.getLiteralLexicalForm() );
}
public void testTypesExpandPrefix()
{
testTypeExpandsPrefix( "rdf:spoo" );
testTypeExpandsPrefix( "rdfs:bar" );
testTypeExpandsPrefix( "owl:henry" );
testTypeExpandsPrefix( "xsd:bool" );
testTypeExpandsPrefix( "unknown:spoo" );
}
private void testTypeExpandsPrefix( String type )
{
Node n = Node.create( "'stuff'" + type );
String wanted = PrefixMapping.Extended.expandPrefix( type );
assertEquals( wanted, n.getLiteralDatatypeURI() );
}
public void testCreateURI()
{
String uri = "http://www.electric-hedgehog.net/";
testCreateURI( uri );
testCreateURI( "rdf:trinket", "http://www.w3.org/1999/02/22-rdf-syntax-ns#trinket" );
testCreateURI( "rdfs:device", "http://www.w3.org/2000/01/rdf-schema#device" );
testCreateURI( "dc:creator", DC.getURI() + "creator" );
testCreateURI( "rss:something", RSS.getURI() + "something" );
testCreateURI( "vcard:TITLE", VCARD.getURI() + "TITLE" );
testCreateURI( "owl:wol", OWL.NAMESPACE + "wol" );
}
public void testCreateURIOtherMap()
{
String myNS = "eh:foo/bar#", suffix = "something";
PrefixMapping mine = PrefixMapping.Factory.create().setNsPrefix( "mine", myNS );
Node n = Node.create( mine, "mine:" + suffix );
assertEquals( myNS + suffix, n.getURI() );
}
private void testCreateURI( String inOut )
{ testCreateURI( inOut, inOut ); }
private void testCreateURI( String in, String wanted )
{
String got = Node.create( in ).getURI();
if (!wanted.equals( got ))
{
if (in.equals( wanted )) fail( "should preserve " + in );
else fail( "should translate " + in + " to " + wanted + " not " + got );
}
}
public void testCreatePrefixed()
{
PrefixMapping pm = PrefixMapping.Factory.create();
/* TODO Node n = */ Node.create( pm, "xyz" );
}
public void testToStringWithPrefixMapping()
{
PrefixMapping pm = PrefixMapping.Factory.create();
String prefix = "spoo", ns = "abc:def/ghi#";
pm.setNsPrefix( prefix, ns );
String suffix = "bamboozle";
assertEquals( prefix + ":" + suffix, Node.create( ns + suffix ).toString( pm ) );
}
public void testNodeHelp()
{
assertTrue( "node() making URIs", node( "hello" ).isURI() );
assertTrue( "node() making literals", node( "123" ).isLiteral() );
assertTrue( "node() making literals", node( "'hello'" ).isLiteral() );
assertTrue( "node() making hyphens", node( "_x" ).isBlank() );
assertTrue( "node() making variables", node( "?x" ).isVariable() );
}
public void testVisitorPatternNode()
{
NodeVisitor returnNode = new NodeVisitor()
{
public Object visitAny( Node_ANY it ) { return it; }
public Object visitBlank( Node_Blank it, AnonId id ) { return it; }
public Object visitLiteral( Node_Literal it, LiteralLabel lit ) { return it; }
public Object visitURI( Node_URI it, String uri ) { return it; }
public Object visitVariable( Node_Variable it, String name ) { return it; }
};
testVisitorPatternNode( "sortOfURI", returnNode );
testVisitorPatternNode( "?variable", returnNode );
testVisitorPatternNode( "_anon", returnNode );
testVisitorPatternNode( "11", returnNode );
testVisitorPatternNode( "??", returnNode );
}
private void testVisitorPatternNode( String ns, NodeVisitor v )
{
Node n = node( ns );
assertEquals( n, n.visitWith( v ) );
}
private void visitExamples( NodeVisitor nv )
{
node( "sortOfURI" ).visitWith( nv );
node( "?variableI" ).visitWith( nv );
node( "_anon" ).visitWith( nv );
node( "11" ).visitWith( nv );
node( "??" ).visitWith( nv );
}
public void testVisitorPatternValue()
{
NodeVisitor checkValue = new NodeVisitor()
{
public Object visitAny( Node_ANY it )
{ return null; }
public Object visitBlank( Node_Blank it, AnonId id )
{ assertTrue( it.getBlankNodeId() == id ); return null; }
public Object visitLiteral( Node_Literal it, LiteralLabel lit )
{ assertTrue( it.getLiteral() == lit ); return null; }
public Object visitURI( Node_URI it, String uri )
{ assertTrue( it.getURI() == uri ); return null; }
public Object visitVariable( Node_Variable it, String name )
{ assertEquals( it.getName(), name ); return null; }
};
visitExamples( checkValue );
}
/**
Test that the appropriate elements of the visitor are called exactly once;
this relies on the order of the visits in visitExamples.
*/
public void testVisitorPatternCalled()
{
final String [] strings = new String [] { "" };
NodeVisitor checkCalled = new NodeVisitor()
{
public Object visitAny( Node_ANY it )
{ strings[0] += " any"; return null; }
public Object visitBlank( Node_Blank it, AnonId id )
{ strings[0] += " blank"; return null; }
public Object visitLiteral( Node_Literal it, LiteralLabel lit )
{ strings[0] += " literal"; return null; }
public Object visitURI( Node_URI it, String uri )
{ strings[0] += " uri"; return null; }
public Object visitVariable( Node_Variable it, String name )
{ strings[0] += " variable"; return null; }
};
String desired = " uri variable blank literal any";
visitExamples( checkCalled );
assertEquals( "all vists must have been made", desired, strings[0] );
}
public void testSimpleMatches()
{
assertTrue( Node.create( "S").matches( Node.create( "S" ) ) );
assertFalse( "", Node.create( "S").matches( Node.create( "T" ) ) );
assertFalse( "", Node.create( "S" ).matches( null ) );
assertTrue( Node.create( "_X").matches( Node.create( "_X" ) ) );
assertFalse( "", Node.create( "_X").matches( Node.create( "_Y" ) ) );
assertFalse( "", Node.create( "_X").matches( null ) );
assertTrue( Node.create( "10" ).matches( Node.create( "10" ) ) );
assertFalse( "", Node.create( "10" ).matches( Node.create( "11" ) ) );
assertFalse( "", Node.create( "10" ).matches( null ) );
assertTrue( Node.ANY.matches( Node.create( "S" ) ) );
assertTrue( Node.ANY.matches( Node.create( "_X" ) ) );
assertTrue( Node.ANY.matches( Node.create( "10" ) ) );
assertFalse( "", Node.ANY.matches( null ) );
}
public void testDataMatches()
{
TypeMapper tm = TypeMapper.getInstance();
RDFDatatype dt1 = tm.getTypeByValue( new Integer( 10 ) );
RDFDatatype dt2 = tm.getTypeByValue( new Short( (short) 10 ) );
Node a = Node.createLiteral( "10", "", dt1 );
Node b = Node.createLiteral( "10", "", dt2 );
assertDiffer( "types must make a difference", a, b );
assertTrue( "A and B must express the same value", a.sameValueAs( b ) );
assertTrue( "matching literals must respect sameValueAs", a.matches( b ) );
}
public void testLiteralToString()
{
TypeMapper tm = TypeMapper.getInstance();
RDFDatatype dtInt = tm.getTypeByValue( new Integer( 10 ) );
Node plain = Node.createLiteral( "rhubarb", "", false );
Node english = Node.createLiteral( "eccentric", "en_UK", false );
Node typed = Node.createLiteral( "10", "", dtInt );
assertEquals( "\"rhubarb\"", plain.toString() );
assertEquals( "rhubarb", plain.toString( false ) );
assertEquals( "\"eccentric\"@en_UK", english.toString() );
assertEquals( "10^^http://www.w3.org/2001/XMLSchema#int", typed.toString( false ) );
}
public void testGetIndexingValueURI()
{
Node u = Node.create( "eh:/telephone" );
assertSame( u, u.getIndexingValue() );
}
public void testGetIndexingValueBlank()
{
Node b = Node.create( "_television" );
assertSame( b, b.getIndexingValue() );
}
public void testGetIndexingValuePlainString()
{
Node s = Node.create( "'literally'" );
assertEquals( s.getLiteral().getIndexingValue(), s.getIndexingValue() );
}
public void testGetIndexingValueLanguagedString()
{
Node s = Node.create( "'chat'fr" );
assertEquals( s, s.getIndexingValue() );
}
public void testGetIndexingValueXSDString()
{
Node s = Node.create( "'string'xsd:string" );
assertEquals( s.getLiteral().getIndexingValue(), s.getIndexingValue() );
}
// TODO should have more of these
public void testGetLiteralValuePlainString()
{
Node s = Node.create( "'aString'" );
assertSame( s.getLiteral().getValue(), s.getLiteralValue() );
}
public void testGetLiteralDatatypeNull()
{
assertEquals( null, Node.create( "'plain'" ).getLiteralDatatype() );
}
public void testLiteralIsXML()
{
assertFalse( Node.create( "'notXML'" ).getLiteralIsXML() );
assertFalse( Node.create( "17" ).getLiteralIsXML() );
assertFalse( Node.create( "'joke'xsd:Joke" ).getLiteralIsXML() );
assertTrue( Node.createLiteral( "lit", "lang", true ).getLiteralIsXML() );
assertFalse( Node.createLiteral( "lit", "lang", false ).getLiteralIsXML() );
}
public void testConcrete()
{
assertTrue( Node.create( "S" ).isConcrete() );
assertTrue( Node.create( "_P" ).isConcrete() );
assertTrue( Node.create( "11" ).isConcrete() );
assertTrue( Node.create( "'hello'" ).isConcrete() );
assertFalse( Node.create( "??" ).isConcrete() );
assertFalse( Node.create( "?x" ).isConcrete() );
}
static String [] someURIs = new String []
{
"http://domainy.thing/stuff/henry",
"http://whatever.com/stingy-beast/bee",
"ftp://erewhon/12345",
"potatoe:rhubarb"
};
/**
test that URI nodes have namespace/localname splits which are consistent
with Util.splitNamepace.
*/
public void testNamespace()
{
for (int i = 0; i < someURIs.length; i += 1)
{
String uri = someURIs[i];
int split = Util.splitNamespace( uri );
Node n = Node.create( uri );
assertEquals( "check namespace", uri.substring( 0, split ), n.getNameSpace() );
assertEquals( "check localname", uri.substring( split ), n.getLocalName() );
}
}
protected static String [] someNodes =
{
"42",
"'hello'",
"_anon",
"'robotic'tick",
"'teriffic'abc:def"
};
public void testHasURI()
{
for (int i = 0; i < someURIs.length; i += 1) testHasURI( someURIs[i] );
for (int i = 0; i < someNodes.length; i += 1) testHasURI( someNodes[i] );
}
protected void testHasURI( String uri )
{
Node n = Node.create( uri );
assertTrue( uri, !n.isURI() || n.hasURI( uri ) );
assertFalse( uri, n.hasURI( uri + "x" ) );
}
/**
Answer the string <code>s</code> prefix-expanded using the built-in
PrefixMapping.Extended.
*/
private String expand( String s )
{ return PrefixMapping.Extended.expandPrefix( s ); }
}
/*
(c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -