001: package org.openrdf.query.algebra.evaluation.impl;
002:
003: import java.util.Arrays;
004: import java.util.List;
005:
006: import junit.framework.TestCase;
007:
008: import org.openrdf.model.Literal;
009: import org.openrdf.model.Value;
010: import org.openrdf.model.ValueFactory;
011: import org.openrdf.query.MalformedQueryException;
012: import org.openrdf.query.QueryEvaluationException;
013: import org.openrdf.query.QueryLanguage;
014: import org.openrdf.query.TupleQuery;
015: import org.openrdf.query.TupleQueryResult;
016: import org.openrdf.repository.Repository;
017: import org.openrdf.repository.RepositoryConnection;
018: import org.openrdf.repository.RepositoryException;
019: import org.openrdf.repository.sail.SailRepository;
020: import org.openrdf.sail.memory.MemoryStore;
021:
022: public class SparqlOrderByTest extends TestCase {
023:
024: private String query1 = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
025: + "SELECT ?name\n"
026: + "WHERE { ?x foaf:name ?name }\n"
027: + "ORDER BY ?name\n";
028:
029: private String query2 = "PREFIX : <http://example.org/ns#>\n"
030: + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
031: + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n"
032: + "SELECT ?name\n"
033: + "WHERE { ?x foaf:name ?name ; :empId ?emp }\n"
034: + "ORDER BY DESC(?emp)\n";
035:
036: private String query3 = "PREFIX : <http://example.org/ns#>\n"
037: + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
038: + "SELECT ?name\n"
039: + "WHERE { ?x foaf:name ?name ; :empId ?emp }\n"
040: + "ORDER BY ?name DESC(?emp)\n";
041:
042: private Repository repository;
043:
044: private RepositoryConnection conn;
045:
046: public void testQuery1() throws Exception {
047: assertTrue("James Leigh".compareTo("James Leigh Hunt") < 0);
048: assertResult(query1, Arrays.asList("James Leigh",
049: "James Leigh", "James Leigh Hunt", "Megan Leigh"));
050: }
051:
052: public void testQuery2() throws Exception {
053: assertResult(query2, Arrays.asList("Megan Leigh",
054: "James Leigh", "James Leigh Hunt", "James Leigh"));
055: }
056:
057: public void testQuery3() throws Exception {
058: assertResult(query3, Arrays.asList("James Leigh",
059: "James Leigh", "James Leigh Hunt", "Megan Leigh"));
060: }
061:
062: @Override
063: protected void setUp() throws Exception {
064: repository = new SailRepository(new MemoryStore());
065: repository.initialize();
066: createEmployee("james", "James Leigh", 123);
067: createEmployee("jim", "James Leigh", 244);
068: createEmployee("megan", "Megan Leigh", 1234);
069: createEmployee("hunt", "James Leigh Hunt", 243);
070: conn = repository.getConnection();
071: }
072:
073: @Override
074: protected void tearDown() throws Exception {
075: conn.close();
076: repository.shutDown();
077: }
078:
079: private void createEmployee(String id, String name, int empId)
080: throws RepositoryException {
081: ValueFactory vf = repository.getValueFactory();
082: String foafName = "http://xmlns.com/foaf/0.1/name";
083: String exEmpId = "http://example.org/ns#empId";
084: RepositoryConnection conn = repository.getConnection();
085: conn.add(vf.createURI("http://example.org/ns#" + id), vf
086: .createURI(foafName), vf.createLiteral(name));
087: conn.add(vf.createURI("http://example.org/ns#" + id), vf
088: .createURI(exEmpId), vf.createLiteral(empId));
089: conn.close();
090: }
091:
092: private void assertResult(String queryStr, List<String> names)
093: throws RepositoryException, MalformedQueryException,
094: QueryEvaluationException {
095: TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL,
096: queryStr);
097: TupleQueryResult result = query.evaluate();
098: for (String name : names) {
099: Value value = result.next().getValue("name");
100: assertEquals(name, ((Literal) value).getLabel());
101: }
102: assertFalse(result.hasNext());
103: result.close();
104: }
105: }
|