001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.parser.serql;
007:
008: import java.io.InputStream;
009: import java.io.InputStreamReader;
010: import java.net.URL;
011:
012: import junit.framework.Test;
013: import junit.framework.TestCase;
014: import junit.framework.TestSuite;
015:
016: import org.slf4j.Logger;
017: import org.slf4j.LoggerFactory;
018:
019: import info.aduna.io.IOUtil;
020:
021: import org.openrdf.model.URI;
022: import org.openrdf.model.Value;
023: import org.openrdf.model.impl.URIImpl;
024: import org.openrdf.query.BindingSet;
025: import org.openrdf.query.MalformedQueryException;
026: import org.openrdf.query.QueryLanguage;
027: import org.openrdf.query.TupleQueryResult;
028: import org.openrdf.query.parser.QueryParser;
029: import org.openrdf.repository.Repository;
030: import org.openrdf.repository.RepositoryConnection;
031: import org.openrdf.repository.sail.SailRepository;
032: import org.openrdf.rio.RDFFormat;
033: import org.openrdf.sail.memory.MemoryStore;
034:
035: public class SeRQLParserTest extends TestCase {
036:
037: static final Logger logger = LoggerFactory
038: .getLogger(SeRQLParserTest.class);
039:
040: /*-----------*
041: * Constants *
042: *-----------*/
043:
044: private static final String MANIFEST_FILE = "/testcases/SeRQL/syntax/manifest.ttl";
045:
046: /* Variables */
047:
048: private String queryFile;
049:
050: private Value result;
051:
052: /* constants */
053:
054: private static String MFX = "http://www.openrdf.org/test-manifest-extensions#";
055:
056: private static URI MFX_CORRECT = new URIImpl(MFX + "Correct");
057:
058: private static URI MFX_PARSE_ERROR = new URIImpl(MFX + "ParseError");
059:
060: /* Constructors */
061:
062: /**
063: * Creates a new SeRQL Parser test.
064: */
065: public SeRQLParserTest(String name, String queryFile, Value result) {
066: super (name);
067:
068: this .queryFile = queryFile;
069:
070: if (!(MFX_CORRECT.equals(result) || MFX_PARSE_ERROR
071: .equals(result))) {
072: throw new IllegalArgumentException("unknown result type: "
073: + result);
074: }
075: this .result = result;
076: }
077:
078: /*---------*
079: * Methods *
080: *---------*/
081:
082: @Override
083: protected void runTest() throws Exception {
084: // Read query from file
085: InputStream stream = new URL(queryFile).openStream();
086: String query = IOUtil.readString(new InputStreamReader(stream,
087: "UTF-8"));
088: stream.close();
089:
090: try {
091: QueryParser parser = new SeRQLParser();
092: parser.parseQuery(query, null);
093: if (MFX_PARSE_ERROR.equals(result)) {
094: fail("Negative syntax test failed. Malformed query caused no error.");
095: }
096: } catch (MalformedQueryException e) {
097: if (MFX_CORRECT.equals(result)) {
098: fail("Positive syntax test failed: " + e.getMessage());
099: } else {
100: return;
101: }
102: }
103: }
104:
105: /*--------------*
106: * Test methods *
107: *--------------*/
108:
109: public static Test suite() throws Exception {
110: TestSuite suite = new TestSuite();
111: suite.setName("SeRQL Syntax Tests");
112:
113: TestSuite positiveTests = new TestSuite();
114: positiveTests.setName("Positive Syntax Tests");
115:
116: TestSuite negativeTests = new TestSuite();
117: negativeTests.setName("Negative Syntax Tests");
118:
119: // Read manifest and create declared test cases
120: Repository manifestRep = new SailRepository(new MemoryStore());
121: manifestRep.initialize();
122: RepositoryConnection con = manifestRep.getConnection();
123:
124: URL manifestURL = SeRQLParserTest.class
125: .getResource(MANIFEST_FILE);
126: con
127: .add(manifestURL, null, RDFFormat
128: .forFileName(MANIFEST_FILE));
129:
130: String query = "SELECT testName, query, result "
131: + "FROM {} mf:name {testName}; "
132: + " mf:action {query}; "
133: + " mf:result {result} "
134: + "USING NAMESPACE "
135: + " mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
136: + " mfx = <http://www.openrdf.org/test-manifest-extensions#>, "
137: + " qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";
138:
139: TupleQueryResult tests = con.prepareTupleQuery(
140: QueryLanguage.SERQL, query).evaluate();
141: while (tests.hasNext()) {
142: BindingSet testBindings = tests.next();
143: String testName = testBindings.getValue("testName")
144: .toString();
145: String queryFile = testBindings.getValue("query")
146: .toString();
147: Value result = testBindings.getValue("result");
148: if (MFX_CORRECT.equals(result)) {
149: positiveTests.addTest(new SeRQLParserTest(testName,
150: queryFile, result));
151: } else if (MFX_PARSE_ERROR.equals(result)) {
152: negativeTests.addTest(new SeRQLParserTest(testName,
153: queryFile, result));
154: } else {
155: logger.warn("Unexpected result value for test \""
156: + testName + "\": " + result);
157: }
158: }
159:
160: tests.close();
161: con.close();
162: manifestRep.shutDown();
163:
164: suite.addTest(positiveTests);
165: suite.addTest(negativeTests);
166: return suite;
167: }
168: }
|