001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.resultio;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.List;
011:
012: import junit.framework.TestCase;
013:
014: import org.openrdf.query.QueryLanguage;
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.rio.RDFFormat;
021: import org.openrdf.rio.RDFParseException;
022: import org.openrdf.rio.UnsupportedRDFormatException;
023: import org.openrdf.sail.memory.MemoryStore;
024:
025: public class TupleQueryResultTest extends TestCase {
026:
027: private Repository rep;
028:
029: private RepositoryConnection con;
030:
031: private String emptyResultQuery;
032:
033: private String singleResultQuery;
034:
035: private String multipleResultQuery;
036:
037: public TupleQueryResultTest(String name)
038: throws RepositoryException, IOException,
039: UnsupportedRDFormatException, RDFParseException {
040: super (name);
041:
042: rep = new SailRepository(new MemoryStore());
043: rep.initialize();
044: con = rep.getConnection();
045:
046: buildQueries();
047: addData();
048: }
049:
050: /*
051: * build some simple SeRQL queries to use for testing the result set object.
052: */
053: private void buildQueries() {
054: StringBuilder query = new StringBuilder();
055:
056: query.append("SELECT * ");
057: query.append("FROM {X} P {Y} ");
058: query.append("WHERE X != X ");
059:
060: emptyResultQuery = query.toString();
061:
062: query = new StringBuilder();
063:
064: query.append("SELECT DISTINCT P ");
065: query.append("FROM {} dc:publisher {P} ");
066: query.append("USING NAMESPACE ");
067: query.append(" dc = <http://purl.org/dc/elements/1.1/>");
068:
069: singleResultQuery = query.toString();
070:
071: query = new StringBuilder();
072: query.append("SELECT DISTINCT P, D ");
073: query.append("FROM {} dc:publisher {P}; ");
074: query.append(" dc:date {D} ");
075: query.append("USING NAMESPACE ");
076: query.append(" dc = <http://purl.org/dc/elements/1.1/>");
077:
078: multipleResultQuery = query.toString();
079: }
080:
081: private void addData() throws IOException,
082: UnsupportedRDFormatException, RDFParseException,
083: RepositoryException {
084: InputStream defaultGraph = TupleQueryResultTest.class
085: .getResourceAsStream("/testcases/default-graph-1.ttl");
086: try {
087: con.add(defaultGraph, "", RDFFormat.TURTLE);
088: } finally {
089: defaultGraph.close();
090: }
091: }
092:
093: public void testGetBindingNames() throws Exception {
094: TupleQueryResult result = con.prepareTupleQuery(
095: QueryLanguage.SERQL, multipleResultQuery).evaluate();
096: try {
097: List<String> headers = result.getBindingNames();
098:
099: if (!headers.get(0).equals("P")) {
100: fail("first header element should be 'P' but is '"
101: + headers.get(0) + "'");
102: }
103: if (!headers.get(1).equals("D")) {
104: fail("second header element should be 'D' but is '"
105: + headers.get(1) + "'");
106: }
107: } finally {
108: result.close();
109: }
110: }
111:
112: /*
113: * deprecated
114: public void testIsDistinct()
115: throws Exception
116: {
117: TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SERQL, emptyResultQuery).evaluate();
118:
119: try {
120: if (result.isDistinct()) {
121: fail("query result should not be distinct.");
122: }
123: }
124: finally {
125: result.close();
126: }
127:
128: result = con.prepareTupleQuery(QueryLanguage.SERQL, singleResultQuery).evaluate();
129:
130: try {
131: if (!result.isDistinct()) {
132: fail("query result should be distinct.");
133: }
134: }
135: finally {
136: result.close();
137: }
138: }
139: */
140:
141: public void testIterator() throws Exception {
142: TupleQueryResult result = con.prepareTupleQuery(
143: QueryLanguage.SERQL, multipleResultQuery).evaluate();
144:
145: try {
146: int count = 0;
147: while (result.hasNext()) {
148: result.next();
149: count++;
150: }
151:
152: if (count <= 1) {
153: fail("query should have multiple results.");
154: }
155: } finally {
156: result.close();
157: }
158: }
159:
160: public void testIsEmpty() throws Exception {
161: TupleQueryResult result = con.prepareTupleQuery(
162: QueryLanguage.SERQL, emptyResultQuery).evaluate();
163:
164: try {
165: if (result.hasNext()) {
166: fail("Query result should be empty");
167: }
168: } finally {
169: result.close();
170: }
171: }
172: }
|