/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
package com.hp.jena.rules.ast.tests;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.hp.jena.rules.ast.Expr;
import com.hp.jena.rules.ast.Item;
public class TestItem
{
@Test public void testItemFunctorAccessors()
{
List<Expr> empty = new ArrayList<Expr>();
Item functor = Item.createFunctor( "f", empty );
assertFalse( functor.isURI() );
assertFalse( functor.isVariable() );
assertFalse( functor.isLiteral() );
assertFalse( functor.isBlank() );
assertFalse( functor.isExplicitBlank() );
assertTrue( functor.isFunctor() );
assertFalse( functor.isExpr() );
assertEquals( "f", functor.getFunctorName() );
assertSame( empty, functor.getFunctorArgs() );
}
@Test public void testItemURIAccessors()
{
Item itemURI = Item.createURI( "eh:/x" );
assertTrue( itemURI.isURI() );
assertFalse( itemURI.isVariable() );
assertFalse( itemURI.isLiteral() );
assertFalse( itemURI.isBlank() );
assertFalse( itemURI.isExplicitBlank() );
assertFalse( itemURI.isFunctor() );
assertFalse( itemURI.isExpr() );
}
@Test public void testItemVariableAccessors()
{
Item itemVariable = Item.createVariable( "spoo" );
assertFalse( itemVariable.isURI() );
assertTrue( itemVariable.isVariable() );
assertFalse( itemVariable.isLiteral() );
assertFalse( itemVariable.isBlank() );
assertFalse( itemVariable.isFunctor() );
assertFalse( itemVariable.isExpr() );
assertEquals( "spoo", itemVariable.getVarName() );
}
@Test public void testPlainItemLiteralAccessors()
{
Item itemLiteral = Item.createPlainLiteral( "spoo" );
testLiteralPredicates( itemLiteral );
assertEquals( "spoo", itemLiteral.getLiteralSpelling() );
assertEquals( null, itemLiteral.getLiteralLanguage() );
assertEquals( null, itemLiteral.getLiteralType() );
}
@Test public void testTaggedItemLiteralAccessors()
{
Item itemLiteral = Item.createTaggedLiteral( "chat", "en-uk" );
testLiteralPredicates( itemLiteral );
assertEquals( "chat", itemLiteral.getLiteralSpelling() );
assertEquals( "en-uk", itemLiteral.getLiteralLanguage() );
assertEquals( null, itemLiteral.getLiteralType() );
}
@Test public void testTypedtemLiteralAccessors()
{
Item itemLiteral = Item.createTypedLiteral( "17", "xsd:integer" );
testLiteralPredicates( itemLiteral );
assertEquals( "17", itemLiteral.getLiteralSpelling() );
assertEquals( null, itemLiteral.getLiteralLanguage() );
assertEquals( "xsd:integer", itemLiteral.getLiteralType() );
}
private void testLiteralPredicates( Item itemLiteral )
{
assertFalse( itemLiteral.isURI() );
assertFalse( itemLiteral.isVariable() );
assertFalse( itemLiteral.isBlank() );
assertFalse( itemLiteral.isExplicitBlank() );
assertFalse( itemLiteral.isFunctor() );
assertFalse( itemLiteral.isExplicitBlank() );
assertTrue( itemLiteral.isLiteral() );
assertFalse( itemLiteral.isExpr() );
}
@Test public void testBlankNodeAccessors()
{
Item itemBlank = Item.createBNode();
assertFalse( itemBlank.isURI() );
assertFalse( itemBlank.isVariable() );
assertFalse( itemBlank.isExplicitBlank() );
assertTrue( itemBlank.isBlank() );
assertFalse( itemBlank.isLiteral() );
assertFalse( itemBlank.isFunctor() );
assertFalse( itemBlank.isExpr() );
}
@Test public void testExplicitBlankNodeAccessors()
{
Item itemBlank = Item.createExplicitBNode( "_blank" );
assertFalse( itemBlank.isURI() );
assertFalse( itemBlank.isVariable() );
assertFalse( itemBlank.isBlank() );
assertTrue( itemBlank.isExplicitBlank() );
assertFalse( itemBlank.isLiteral() );
assertFalse( itemBlank.isFunctor() );
assertFalse( itemBlank.isExpr() );
}
@Test public void testExprAccessors()
{
Expr e = new Expr( "f", new ArrayList<Expr>() );
Item item = Item.createExpr( e );
assertSame( e, item.getExpr() );
assertFalse( item.isURI() );
assertFalse( item.isVariable() );
assertFalse( item.isBlank() );
assertFalse( item.isExplicitBlank() );
assertFalse( item.isLiteral() );
assertFalse( item.isFunctor() );
assertTrue( item.isExpr() );
}
@Test public void testVisitExpr()
{
Item.Visitor<Expr> iv = new Item.VisitorBoom<Expr>()
{
@Override public Expr visitExpr( Expr e )
{ return e; }
};
Expr expected = new Expr( "spoo:thingy", new ArrayList<Expr>() );
assertSame( expected, Item.createExpr( expected ).visit( iv ) );
}
@Test public void testVisitURI()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitURI( String uri )
{ return uri; }
};
Object actual = Item.createURI( "spoo:thingy" ).visit( iv );
assertEquals( "spoo:thingy", actual );
}
@Test public void testVisitVariable()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitVariable( String name )
{ return name; }
};
Object actual = Item.createVariable( "varying" ).visit( iv );
assertEquals( "varying", actual );
}
@Test public void testVisitBNode()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitBNode( String label )
{ return label; }
};
String actual = Item.createBNode().visit( iv );
assertEquals( "_:B", actual.substring( 0, 3 ) );
}
@Test public void testVisitExplicitBNode()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitExplicitBNode( String uri )
{ return uri; }
};
Object actual = Item.createExplicitBNode( "_label" ).visit( iv );
assertEquals( "_label", actual );
}
@Test public void testVisitPlainLiteral()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitPlainLiteral( String spelling )
{ return spelling; }
};
Object actual = Item.createPlainLiteral( "hello, world" ).visit( iv );
assertEquals( "hello, world", actual );
}
@Test public void testVisitTaggedLiteral()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitTaggedLiteral( String spelling, String tag )
{ return spelling + "@@" + tag; }
};
Object actual = Item.createTaggedLiteral( "hello, world", "en" ).visit( iv );
assertEquals( "hello, world@@en", actual );
}
@Test public void testVisitTypedLiteral()
{
Item.Visitor<String> iv = new Item.VisitorBoom<String>()
{
@Override public String visitTypedLiteral( String spelling, String type )
{ return spelling + "::" + type; }
};
Object actual = Item.createTypedLiteral( "hello, world", "xsd:boolean" ).visit( iv );
assertEquals( "hello, world::xsd:boolean", actual );
}
@Test public void testVisitFunctor()
{
Item.Visitor<Item.Functor> iv = new Item.VisitorBoom<Item.Functor>()
{
@Override public Item.Functor visitFunctor( Item.Functor f )
{ return f; }
};
Item functor = Item.createFunctor( "someF", new ArrayList<Expr>() );
Object actual = functor.visit( iv );
assertSame( functor, actual );
}
}