📄 serqlcfwquerytest.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.query.serql;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import org.openrdf.util.io.IOUtil;import org.openrdf.util.log.ThreadLog;import org.openrdf.model.Graph;import org.openrdf.model.Statement;import org.openrdf.sesame.Sesame;import org.openrdf.sesame.admin.AdminListener;import org.openrdf.sesame.admin.DummyAdminListener;import org.openrdf.sesame.config.AccessDeniedException;import org.openrdf.sesame.config.ConfigurationException;import org.openrdf.sesame.config.RepositoryConfig;import org.openrdf.sesame.constants.QueryLanguage;import org.openrdf.sesame.constants.RDFFormat;import org.openrdf.sesame.query.MalformedQueryException;import org.openrdf.sesame.query.QueryEvaluationException;import org.openrdf.sesame.query.QueryResultsTable;import org.openrdf.sesame.repository.local.LocalRepository;import org.openrdf.sesame.repository.local.LocalService;import org.openrdf.sesame.sail.RdfSource;import org.openrdf.sesame.sail.SailUtil;import org.openrdf.sesame.sail.StatementIterator;import org.openrdf.sesame.sailimpl.rdbms.RdfSchemaRepositoryConfig;public class SeRQLCfwQueryTest extends TestCase { /*--------------------------------------+ | Constants | +--------------------------------------*/ private static final String TEST_DIR_PREFIX = "/files/testcases/SeRQL/CfwQuery"; private static final String MANIFEST_FILE = TEST_DIR_PREFIX + "/manifest.ttl"; private static final boolean USE_RDBMS = false; private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; private static final String JDBC_URL = "jdbc:mysql://localhost:3306/testdb"; private static final String JDBC_USER = "sesame"; private static final String JDBC_PASSWORD = ""; /*--------------------------------------+ | Variables | +--------------------------------------*/ private static AdminListener _listener; private static LocalService _service; private static LocalRepository _dataRep; private static LocalRepository _expectedResultRep; private static LocalRepository _actualResultRep; private String _dataFile; private String _queryFile; private String _resultFile; /*--------------------------------------+ | Constructors | +--------------------------------------*/ /** * Creates a new SeRQL CfwQuery test. **/ public SeRQLCfwQueryTest( String name, String dataFile, String queryFile, String resultFile) { super(name); _dataFile = dataFile; _queryFile = queryFile; _resultFile = resultFile; } /*--------------------------------------+ | Methods | +--------------------------------------*/ protected void runTest() { try { // Remove any existing data from repositories _dataRep.clear(_listener); _expectedResultRep.clear(_listener); _actualResultRep.clear(_listener); // Read query from file InputStream stream = _openResourceStream(_queryFile); String query = IOUtil.readFully( new InputStreamReader(stream, "UTF-8") ); stream.close(); // Upload input data stream = _openResourceStream(_dataFile); _dataRep.addData(stream, _dataFile, _getFormatForFileName(_dataFile), true, _listener); stream.close(); // Upload expected result data stream = _openResourceStream(_resultFile); _expectedResultRep.addData(stream, _resultFile, _getFormatForFileName(_resultFile), true, _listener); stream.close(); try { _actualResultRep.addGraph( _dataRep.performGraphQuery(QueryLanguage.SERQL, query)); } catch (MalformedQueryException e) { fail("Unable to parse query: " + e.getMessage()); } catch (org.openrdf.sesame.query.QueryEvaluationException e) { fail("Unable to evaluate query: " + e.getMessage()); } // Compare query result to expected data RdfSource expectedResultSail = (RdfSource)_expectedResultRep.getSail(); RdfSource actualResultSail = (RdfSource)_actualResultRep.getSail(); if (!SailUtil.modelsEqual(expectedResultSail, actualResultSail)) { // Found differences between expected and actual results StringBuffer message = new StringBuffer(128); Graph expectedGraph = _expectedResultRep.getGraph(); Graph actualGraph = _actualResultRep.getGraph(); StatementIterator actualIter = actualGraph.getStatements(); while (actualIter.hasNext()) { Statement st = actualIter.next(); if (!expectedGraph.contains(st)) { message.append("unexpected statement in result: "); message.append(st.toString()); message.append("\n"); } } actualIter.close(); StatementIterator expectedIter = expectedGraph.getStatements(); while (expectedIter.hasNext()) { Statement st = expectedIter.next(); if (!actualGraph.contains(st)) { message.append("expected statement not in result: "); message.append(st.toString()); message.append("\n"); } } expectedIter.close(); ThreadLog.error(message.toString()); fail(message.toString()); } } catch (IOException e) { ThreadLog.error("IOException: " + e.getMessage()); fail("IOException:" + e.getMessage()); } catch (AccessDeniedException e) { ThreadLog.error("AccessDeniedException: " + e.getMessage()); fail("AccessDeniedException: " + e.getMessage()); } } /** * Determines the RDF format for a file based on the filename's extension. **/ private static RDFFormat _getFormatForFileName(String fileName) { if (fileName.endsWith(".ttl")) { return RDFFormat.TURTLE; } else if (fileName.endsWith(".nt")) { return RDFFormat.NTRIPLES; } else { return RDFFormat.RDFXML; } } /** * Opens a stream to a resource from the classpath. **/ private static InputStream _openResourceStream(String resource) { return SeRQLCfwQueryTest.class.getResourceAsStream(resource); } /*--------------------------------------+ | Test methods | +--------------------------------------*/ public static Test suite() throws ConfigurationException, IOException, AccessDeniedException, MalformedQueryException, QueryEvaluationException { ThreadLog.registerThread(null, ThreadLog.STATUS); TestSuite suite = new TestSuite(); _listener = new DummyAdminListener(); _service = Sesame.getService(); if (USE_RDBMS) { ThreadLog.log("Using RDBMS for data storage"); RepositoryConfig repConfig = new RepositoryConfig("junit-data", true, true); repConfig.addSail(new RdfSchemaRepositoryConfig( JDBC_DRIVER, JDBC_URL, JDBC_USER, JDBC_PASSWORD)); _dataRep = _service.createRepository(repConfig); } else { ThreadLog.log("Using main memory store for data storage"); _dataRep = _service.createRepository("junit-data", true); } _expectedResultRep = _service.createRepository("expected", false); _actualResultRep = _service.createRepository("actual", false); // Read manifest and create declared test cases LocalRepository manifestRep = _service.createRepository("manifest", false); InputStream stream = _openResourceStream(MANIFEST_FILE); manifestRep.addData(stream, MANIFEST_FILE, _getFormatForFileName(MANIFEST_FILE), true, _listener); stream.close(); QueryResultsTable tests = manifestRep.performTableQuery( QueryLanguage.SERQL, "SELECT testName, input, query, result " + "FROM {} mf:name {testName}; " + " mf:result {result}; " + " mf:action {} qt:query {query}; " + " qt:data {input} " + "USING NAMESPACE " + " mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, " + " qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>"); System.out.println("Retrieved " + tests.getRowCount() + " test cases."); for (int i = 0; i < tests.getRowCount(); i++) { String testName = tests.getValue(i, 0).toString(); String inputFile = tests.getValue(i, 1).toString(); String queryFile = tests.getValue(i, 2).toString(); String resultFile = tests.getValue(i, 3).toString(); suite.addTest(_createTestCase( testName, inputFile, queryFile, resultFile)); } _service.removeRepository("manifest"); manifestRep.shutDown(); return suite; } private static TestCase _createTestCase( String testName, String input, String query, String output) { return new SeRQLCfwQueryTest(testName, input, query, output); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -