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.parser.sparql;
007:
008: import java.io.InputStream;
009: import java.io.InputStreamReader;
010: import java.net.URL;
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: import junit.framework.Test;
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: import org.slf4j.Logger;
019: import org.slf4j.LoggerFactory;
020:
021: import info.aduna.io.IOUtil;
022:
023: import org.openrdf.query.BindingSet;
024: import org.openrdf.query.MalformedQueryException;
025: import org.openrdf.query.QueryLanguage;
026: import org.openrdf.query.TupleQueryResult;
027: import org.openrdf.query.parser.QueryParserUtil;
028: import org.openrdf.repository.Repository;
029: import org.openrdf.repository.RepositoryConnection;
030: import org.openrdf.repository.sail.SailRepository;
031: import org.openrdf.rio.RDFFormat;
032: import org.openrdf.sail.memory.MemoryStore;
033:
034: public class SPARQLSyntaxTest extends TestCase {
035:
036: /*-----------*
037: * Constants *
038: *-----------*/
039:
040: private static final Logger logger = LoggerFactory
041: .getLogger(SPARQLSyntaxTest.class);
042:
043: private static final boolean REMOTE = false;
044:
045: private static final String HOST, MANIFEST_FILE;
046:
047: private static final String SUBMANIFEST_QUERY, TESTCASE_QUERY;
048:
049: static {
050: // manifest of W3C Data Access Working Group SPARQL syntax tests
051: if (REMOTE) {
052: HOST = "http://www.w3.org/2001/sw/DataAccess/tests/data-r2/";
053: } else {
054: HOST = SPARQLSyntaxTest.class.getResource(
055: "/testcases-dawg/data-r2/").toString();
056: }
057:
058: MANIFEST_FILE = HOST + "manifest-syntax.ttl";
059:
060: StringBuilder sb = new StringBuilder(512);
061:
062: sb.append("SELECT subManifest ");
063: sb.append("FROM {} rdf:first {subManifest} ");
064: sb.append("USING NAMESPACE");
065: sb
066: .append(" mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>,");
067: sb
068: .append(" qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>");
069: SUBMANIFEST_QUERY = sb.toString();
070:
071: sb.setLength(0);
072: sb.append("SELECT TestURI, Name, Action, Type ");
073: sb.append("FROM {TestURI} rdf:type {Type};");
074: sb.append(" mf:name {Name};");
075: sb.append(" mf:action {Action} ");
076: sb
077: .append("WHERE Type = mf:PositiveSyntaxTest or Type = mf:NegativeSyntaxTest ");
078: sb.append("USING NAMESPACE");
079: sb
080: .append(" mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>,");
081: sb
082: .append(" qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>");
083: TESTCASE_QUERY = sb.toString();
084: }
085:
086: /*-----------*
087: * Variables *
088: *-----------*/
089:
090: protected String testURI;
091:
092: protected String queryFileURL;
093:
094: protected boolean positiveTest;
095:
096: /*--------------*
097: * Constructors *
098: *--------------*/
099:
100: public SPARQLSyntaxTest(String testURI, String name,
101: String queryFileURL, boolean positiveTest) {
102: super (name);
103: this .testURI = testURI;
104: this .queryFileURL = queryFileURL;
105: this .positiveTest = positiveTest;
106: }
107:
108: /*---------*
109: * Methods *
110: *---------*/
111:
112: @Override
113: protected void runTest() throws Exception {
114: // Read query from file
115: InputStream stream = new URL(queryFileURL).openStream();
116: String query = IOUtil.readString(new InputStreamReader(stream,
117: "UTF-8"));
118: stream.close();
119:
120: try {
121: QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query,
122: queryFileURL);
123:
124: if (!positiveTest) {
125: fail("Negative test case should have failed to parse");
126: }
127: } catch (MalformedQueryException e) {
128: if (positiveTest) {
129: e.printStackTrace();
130: fail("Positive test case failed: " + e.getMessage());
131: }
132: }
133: }
134:
135: public static Test suite() throws Exception {
136: TestSuite suite = new TestSuite();
137:
138: // Read manifest and create declared test cases
139: Repository manifestRep = new SailRepository(new MemoryStore());
140: manifestRep.initialize();
141:
142: RepositoryConnection con = manifestRep.getConnection();
143:
144: logger.debug("Loading manifest data");
145: URL manifest = new URL(MANIFEST_FILE);
146: RDFFormat rdfFormat = RDFFormat.forFileName(MANIFEST_FILE,
147: RDFFormat.TURTLE);
148: con.add(manifest, MANIFEST_FILE, rdfFormat);
149:
150: logger.info("Searching for sub-manifests");
151: List<String> subManifestList = new ArrayList<String>();
152:
153: TupleQueryResult subManifests = con.prepareTupleQuery(
154: QueryLanguage.SERQL, SUBMANIFEST_QUERY).evaluate();
155: while (subManifests.hasNext()) {
156: BindingSet bindings = subManifests.next();
157: subManifestList.add(bindings.getValue("subManifest")
158: .toString());
159: }
160: subManifests.close();
161:
162: logger.info("Found {} sub-manifests", subManifestList.size());
163:
164: for (String subManifest : subManifestList) {
165: logger.info("Loading sub manifest {}", subManifest);
166: con.clear();
167:
168: URL subManifestURL = new URL(subManifest);
169: rdfFormat = RDFFormat.forFileName(subManifest,
170: RDFFormat.TURTLE);
171: con.add(subManifestURL, subManifest, rdfFormat);
172:
173: TestSuite subSuite = new TestSuite(subManifest
174: .substring(HOST.length()));
175:
176: logger.info("Creating test cases for {}", subManifest);
177: TupleQueryResult tests = con.prepareTupleQuery(
178: QueryLanguage.SERQL, TESTCASE_QUERY).evaluate();
179: while (tests.hasNext()) {
180: BindingSet bindingSet = tests.next();
181:
182: String testURI = bindingSet.getValue("TestURI")
183: .toString();
184: String testName = bindingSet.getValue("Name")
185: .toString();
186: String testAction = bindingSet.getValue("Action")
187: .toString();
188: boolean positiveTest = bindingSet
189: .getValue("Type")
190: .toString()
191: .equals(
192: "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#PositiveSyntaxTest");
193:
194: subSuite.addTest(new SPARQLSyntaxTest(testURI,
195: testName, testAction, positiveTest));
196: }
197: tests.close();
198:
199: suite.addTest(subSuite);
200: }
201:
202: con.close();
203: manifestRep.shutDown();
204:
205: logger.info("Created test suite containing "
206: + suite.countTestCases() + " test cases");
207: return suite;
208: }
209: }
|