Package org.openrdf.query.parser.sparql

Source Code of org.openrdf.query.parser.sparql.ManifestTest

/*
* Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2008.
*
* Licensed under the Aduna BSD-style license.
*/
package org.openrdf.query.parser.sparql;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.jar.JarFile;

import junit.framework.TestSuite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import info.aduna.io.ZipUtil;
import info.aduna.io.FileUtil;

import org.openrdf.model.Resource;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.util.RDFInserter;
import org.openrdf.result.TupleResult;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.turtle.TurtleParser;
import org.openrdf.sail.memory.MemoryStore;
import org.openrdf.store.StoreException;

public class ManifestTest {

  static final Logger logger = LoggerFactory.getLogger(ManifestTest.class);

  private static final boolean REMOTE = false;

  public static final String MANIFEST_FILE;

  static {
    if (REMOTE) {
      MANIFEST_FILE = "http://www.w3.org/2001/sw/DataAccess/tests/data-r2/manifest-evaluation.ttl";
    }
    else {
      URL url = ManifestTest.class.getResource("/testcases-dawg/data-r2/manifest-evaluation.ttl");

      if ("jar".equals(url.getProtocol())) {
        // Extract manifest files to a temporary directory
        try {
          File destDir = FileUtil.createTempDir("sparql");

          JarURLConnection con = (JarURLConnection)url.openConnection();
          JarFile jar = con.getJarFile();

          ZipUtil.extract(jar, destDir);

          destDir.deleteOnExit();
          File localFile = new File(destDir, con.getEntryName());
          destDir.deleteOnExit();
          MANIFEST_FILE = localFile.toURI().toURL().toString();
        }
        catch (IOException e) {
          throw new AssertionError(e);
        }
      }
      else {
        MANIFEST_FILE = url.toString();
      }
    }
  }

  public static TestSuite suite(SPARQLQueryTest.Factory factory)
    throws Exception
  {
    TestSuite suite = new TestSuite(factory.getClass().getName());

    Repository manifestRep = new SailRepository(new MemoryStore());
    manifestRep.initialize();
    RepositoryConnection con = manifestRep.getConnection();

    addTurtle(con, new URL(MANIFEST_FILE), MANIFEST_FILE);

    String query = "SELECT DISTINCT manifestFile FROM {x} rdf:first {manifestFile} "
        + "USING NAMESPACE mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
        + "  qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";

    TupleResult manifestResults = con.prepareTupleQuery(QueryLanguage.SERQL, query, MANIFEST_FILE).evaluate();

    while (manifestResults.hasNext()) {
      BindingSet bindingSet = manifestResults.next();
      String manifestFile = bindingSet.getValue("manifestFile").toString();
      suite.addTest(SPARQLQueryTest.suite(manifestFile, factory));
    }

    manifestResults.close();
    con.close();
    manifestRep.shutDown();

    logger.info("Created aggregated test suite with " + suite.countTestCases() + " test cases.");
    return suite;
  }

  static void addTurtle(RepositoryConnection con, URL url, String baseURI, Resource... contexts)
    throws IOException, StoreException, RDFParseException
  {
    if (baseURI == null) {
      baseURI = url.toExternalForm();
    }

    InputStream in = url.openStream();

    try {
      final ValueFactory vf = con.getValueFactory();
      RDFParser rdfParser = new TurtleParser();
      rdfParser.setValueFactory(vf);

      rdfParser.setVerifyData(false);
      rdfParser.setStopAtFirstError(true);
      rdfParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

      RDFInserter rdfInserter = new RDFInserter(con);
      rdfInserter.enforceContext(contexts);
      rdfParser.setRDFHandler(rdfInserter);

      con.begin();
      try {
        rdfParser.parse(in, baseURI);
        con.commit();
      }
      catch (RDFHandlerException e) {
        con.rollback();
        // RDFInserter only throws wrapped StoreExceptions
        throw (StoreException)e.getCause();
      }
      catch (RuntimeException e) {
        con.rollback();
        throw e;
      }
    }
    finally {
      in.close();
    }
  }
}
TOP

Related Classes of org.openrdf.query.parser.sparql.ManifestTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.