package org.openrdf.repository.object;

import junit.framework.Test;

import org.openrdf.repository.object.base.ObjectRepositoryTestCase;
import org.openrdf.repository.object.concepts.Person;

public class ObjectQueryTest extends ObjectRepositoryTestCase {

	private static final String PREFIX = "PREFIX foaf: <urn:foaf:>\n";

	public static Test suite() throws Exception {
		return ObjectRepositoryTestCase.suite(ObjectQueryTest.class);
	}

	private static final String QUERY_PERSON_SMITH = PREFIX
			+ "SELECT ?person WHERE { ?person foaf:family_name \"Smith\" }";

	private static final String QUERY_PERSON_NAME_SMITH = PREFIX
			+ "SELECT ?person ?name WHERE { ?person foaf:family_name \"Smith\" ; foaf:name ?name }";

	private static final String QUERY_PERSON_NAME_GENDER_SMITH = PREFIX
			+ "SELECT ?person ?name ?gender WHERE { ?person foaf:family_name \"Smith\" ; foaf:name ?name OPTIONAL { ?person foaf:gender ?gender } }";

	private static final String QUERY_NAME_SMITH = PREFIX
			+ "SELECT ?name WHERE { ?person foaf:family_name \"Smith\" ; foaf:name ?name }";

	private static final String QUERY_FRIENDS_SMITH = PREFIX
			+ "SELECT ?friend WHERE { ?person foaf:family_name \"Smith\" OPTIONAL { ?person foaf:knows ?friend } }";

	public void testBeanQuery() throws Exception {
		ObjectQuery query = con.prepareObjectQuery(QUERY_PERSON_SMITH);
		int count = 0;
		for (Object bean : query.evaluate().asList()) {
			Person person = (Person) bean;
			count++;
			assertTrue(person.getFoafNames().contains("Bob")
					|| person.getFoafNames().contains("John"));
		}
		assertEquals(2, count);
	}

	public void testOptionalBeanQuery() throws Exception {
		ObjectQuery query = con.prepareObjectQuery(QUERY_FRIENDS_SMITH);
		assertTrue(query.evaluate().asList().isEmpty());
	}

	public void testTupleQuery() throws Exception {
		ObjectQuery query = con.prepareObjectQuery(QUERY_PERSON_NAME_SMITH);
		int count = 0;
		for (Object row : query.evaluate().asList()) {
			Person person = (Person) ((Object[]) row)[0];
			String name = (String) ((Object[]) row)[1];
			count++;
			assertTrue(person.getFoafNames().contains("Bob")
					|| person.getFoafNames().contains("John"));
			assertTrue(name.equals("Bob") || name.equals("John"));
		}
		assertEquals(2, count);
	}

	public void testTupleOptionalQuery() throws Exception {
		ObjectQuery query = con.prepareObjectQuery(QUERY_PERSON_NAME_GENDER_SMITH);
		int count = 0;
		for (Object row : query.evaluate().asList()) {
			Person person = (Person) ((Object[]) row)[0];
			String name = (String) ((Object[]) row)[1];
			String gender = (String) ((Object[]) row)[2];
			count++;
			assertTrue(person.getFoafNames().contains("Bob")
					|| person.getFoafNames().contains("John"));
			assertTrue(name.equals("Bob") || name.equals("John"));
			assertNull(gender);
		}
		assertEquals(2, count);
	}

	public void testLiteralQuery() throws Exception {
		ObjectQuery query = con.prepareObjectQuery(QUERY_NAME_SMITH);
		int count = 0;
		for (Object result : query.evaluate().asList()) {
			String name = (String) result;
			count++;
			assertTrue(name.equals("Bob") || name.equals("John"));
		}
		assertEquals(2, count);
	}

	public void testResourceQuery() throws Exception {
		ObjectQuery query = con.prepareObjectQuery(QUERY_PERSON_SMITH);
		int count = 0;
		for (Object bean : query.evaluate().asList()) {
			Person person = (Person) bean;
			count++;
			assertTrue(person.getFoafNames().contains("Bob")
					|| person.getFoafNames().contains("John"));
		}
		assertEquals(2, count);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		Person bob = con.addDesignation(con.getObjectFactory().createObject(),
				Person.class);
		bob.getFoafNames().add("Bob");
		bob.getFoafFamily_names().add("Smith");
		Person john = con.addDesignation(con.getObjectFactory().createObject(),
				Person.class);
		john.getFoafNames().add("John");
		john.getFoafFamily_names().add("Smith");
	}
}

@@ -59,24 +59,104 @@
 work.Test;%0A%0A
+import org.openrdf.query.TupleQuery;%0Aimport org.openrdf.query.TupleQueryResult;%0A
 import org.o
@@ -3603,16 +3603,601 @@
 t);%0A%09%7D%0A%0A
+%09public void testTupleQueryBinding() throws Exception %7B%0A%09%09Person jamie = con.addDesignation(con.getObjectFactory().createObject(),%0A%09%09%09%09Person.class);%0A%09%09jamie.getFoafNames().add(%22Jamie%22);%0A%09%09jamie.getFoafFamily_names().add(%22Leigh%22);%0A%09%09String q = PREFIX + %22Select ?name where %7B ?person foaf:name ?name %7D%22;%0A%09%09TupleQuery query = con.prepareTupleQuery(q);%0A%09%09query.setBinding(%22person%22, ((RDFObject)jamie).getResource());%0A%09%09TupleQueryResult result = query.evaluate();%0A%09%09assertTrue(result.hasNext());%0A%09%09assertEquals(%22Jamie%22, result.next().getValue(%22name%22).stringValue());%0A%09%09result.close();%0A%09%7D%0A%0A
 %09@Overri

Add tuple binding test