/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
package com.hp.jena3.rules.retelike.impl.tests;
import static com.hp.jena.shell.test.utils.NodeCreateUtils.node;
import static org.junit.Assert.*;
import org.junit.Test;
import com.hp.jena.graph.Node;
import com.hp.jena.rules.retelike.impl.Bindings;
import com.hp.jena.shell.test.utils.NodeCreateUtils;
public class TestBindings
{
protected static final String A = "?A";
protected static final String B = "?B";
protected static final String C = "eh:/C";
protected static final String D = "'d'";
@Test public void ensureEmptyBindingsMapsNothing()
{
Bindings<String, Node> b = new Bindings<String, Node>();
assertNull( b.get( A ) );
assertNull( b.get( B ) );
assertNull( b.get( C ) );
assertNull( b.get( D ) );
}
@Test public void ensureCreateIsGeneric()
{
Bindings<String, String> bss = Bindings.create();
Bindings<String, Node> bsn = Bindings.create();
Bindings<Node, String> bns = Bindings.create();
Bindings<Integer, String> bis = Bindings.create();
Bindings<String, Integer> bsi = Bindings.create();
}
@Test public void ensureRemembersABinding()
{
Bindings<String, Node> b = new Bindings<String, Node>();
Node n = node( "eh:/whatever" );
b.put( A, n );
assertEquals( n, b.get( A ) );
assertNull( b.get( B ) );
assertNull( b.get( C ) );
assertNull( b.get( D ) );
}
@Test public void ensureRemembersBBinding()
{
Bindings<String, Node> b = new Bindings<String, Node>();
Node n = node( "eh:/wossit" );
b.put( B, n );
assertNull( b.get( A ) );
assertEquals( n, b.get( B ) );
assertNull( b.get( C ) );
assertNull( b.get( D ) );
}
@Test public void ensureCopyIsEqual()
{
Bindings<String, Node> b = bindings( "?x=A, ?y=B" );
assertEquals( b, b.copy() );
}
@Test public void ensureSetReturnsSelf()
{
Bindings<String, Node> b = new Bindings<String, Node>();
assertSame( b, b.set( "spoo", node( "x" ) ) );
}
@Test public void ensureSetUpdatesBindings()
{
Bindings<String, Node> b = new Bindings<String, Node>();
b.set( "spoo", node( "x" ) );
assertEquals( node( "x" ), b.get( "spoo" ) );
}
@Test public void ensureEmptyBindingsCompatibleWithAny()
{
assertTrue( bindings( "" ).compatible( bindings( "" ) ) );
assertTrue( bindings( "" ).compatible( bindings( "?x=A" ) ) );
assertTrue( bindings( "" ).compatible( bindings( "?x=A, ?y=B" ) ) );
}
@Test public void ensureAnyCompatibleWithEmpty()
{
assertTrue( bindings( "" ).compatible( bindings( "" ) ) );
assertTrue( bindings( "?x=A" ).compatible( bindings( "" ) ) );
assertTrue( bindings( "?x=A, ?y=B" ).compatible( bindings( "" ) ) );
}
@Test public void ensureCompatibleWithNonOverlapping()
{
assertTrue( bindings( "?x=A" ).compatible( bindings( "?y=B" ) ) );
assertTrue( bindings( "?x=A, ?y=B" ).compatible( bindings( "?z=C" ) ) );
}
@Test public void ensureCompatibleWithSameBindings()
{
assertTrue( bindings( "?x=A" ).compatible( bindings( "?x=A" ) ) );
assertTrue( bindings( "?x=A, ?y=B" ).compatible( bindings( "?x=A" ) ) );
assertTrue( bindings( "?x=A, ?y=B" ).compatible( bindings( "?x=A, ?y=B" ) ) );
assertTrue( bindings( "?x=A, ?y=B" ).compatible( bindings( "?x=A, ?z=C" ) ) );
}
@Test public void ensureNotCompatibleWithDifferentBindings()
{
assertFalse( bindings( "?x=A" ).compatible( bindings( "?x=B" ) ) );
assertFalse( bindings( "?x=A, ?y=B" ).compatible( bindings( "?x=A, ?y=C" ) ) );
assertFalse( bindings( "?x=A" ).compatible( bindings( "?x=B, ?y=C" ) ) );
assertFalse( bindings( "?x=A, ?y=C" ).compatible( bindings( "?x=B, ?y=C" ) ) );
}
@Test public void ensureEmptyAddsOther()
{
assertEquals( bindings( "" ), bindings( "" ).merge( bindings( "" ) ) );
assertEquals( bindings( "?x=A" ), bindings( "" ).merge( bindings( "?x=A" ) ) );
assertEquals( bindings( "?y=B, ?z=C" ), bindings( "" ).merge( bindings( "?y=B, ?z=C" ) ) );
}
@Test public void ensureMergeUnitesBindings()
{
assertEquals( bindings( "?x=A, ?y=B" ), bindings( "?y=B" ).merge( bindings( "?x=A" ) ) );
assertEquals( bindings( "?x=A, ?y=B, ?z=C" ), bindings( "?y=B, ?z=C" ).merge( bindings( "?x=A" ) ) );
assertEquals( bindings( "?x=A, ?y=B, ?z=C" ), bindings( "?y=B" ).merge( bindings( "?x=A, ?z=C" ) ) );
}
public static Bindings<Node, Node> nodeBindings( String template )
{ return bindings( parseNode, parseNode, template ); }
public static Bindings<String, Node> bindings( String template )
{ return bindings( parseString, parseNode, template ); }
public static Parse<String> parseString = new Parse<String>()
{ public String parse( String s ) { return s; } };
public static Parse<Node> parseNode = new Parse<Node>()
{ public Node parse( String s ) { return node( s ); } };
interface Parse<T>
{ public T parse( String s ); }
public static <K, V> Bindings<K, V> bindings( Parse<K> key, Parse<V> value, String template )
{
Bindings<K, V> result = new Bindings<K, V>();
if (template.length() > 0)
for (String binding : template.split( " *, *", 0 ))
{
String [] elements = binding.trim().split( " *= *" );
result.put( key.parse( elements[0] ), value.parse( elements[1] ) );
}
return result;
}
}