/**
   * Read object.
   *
   * @param in the in
   * @throws IOException Signals that an I/O exception has occurred.
   * @throws ClassNotFoundException the class not found exception
   */
  @SuppressWarnings("unchecked")
  private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    // note, here we don't need in.defaultReadObject();
    // because Dataset has no other state to deserialize
    this.defaultGraphs = (Set<URI>) in.readObject();
    this.namedGraphs = (Set<URI>) in.readObject();

    for (URI dg : this.defaultGraphs) {
      super.addDefaultGraph(dg);
    }
    for (URI ng : this.namedGraphs) {
      super.addNamedGraph(ng);
    }
  }
  protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(
      final TupleExpr tupleExpr,
      final Dataset dataSet,
      final BindingSet bindingSet,
      final boolean includeInferred)
      throws SailException {

    // ignore the given dataset and restrict everything to the single context we have been setup
    // with
    DatasetImpl singleContextDataset = new DatasetImpl();
    if (singleContext instanceof URI) {
      singleContextDataset.setDefaultInsertGraph((URI) singleContext);
      singleContextDataset.addDefaultGraph((URI) singleContext);
      singleContextDataset.addNamedGraph((URI) singleContext);
      singleContextDataset.addDefaultRemoveGraph((URI) singleContext);
    }

    return baseSailConnection.evaluate(
        tupleExpr, singleContextDataset, bindingSet, includeInferred);
  }
  public static TestSuite suite(String manifestFileURL, Factory factory, boolean approvedOnly)
      throws Exception {
    logger.info("Building test suite for {}", manifestFileURL);

    TestSuite suite = new TestSuite(factory.getClass().getName());

    // Read manifest and create declared test cases
    Repository manifestRep = new SailRepository(new MemoryStore());
    manifestRep.initialize();
    RepositoryConnection con = manifestRep.getConnection();

    ManifestTest.addTurtle(con, new URL(manifestFileURL), manifestFileURL);

    suite.setName(getManifestName(manifestRep, con, manifestFileURL));

    // Extract test case information from the manifest file. Note that we
    // only
    // select those test cases that are mentioned in the list.
    StringBuilder query = new StringBuilder(512);
    query.append(
        " SELECT DISTINCT testURI, testName, resultFile, action, queryFile, defaultGraph, ordered ");
    query.append(" FROM {} rdf:first {testURI} ");
    if (approvedOnly) {
      query.append("                          dawgt:approval {dawgt:Approved}; ");
    }
    query.append("                             mf:name {testName}; ");
    query.append("                             mf:result {resultFile}; ");
    query.append("                             [ mf:checkOrder {ordered} ]; ");
    query.append("                             [ mf:requires {Requirement} ];");
    query.append("                             mf:action {action} qt:query {queryFile}; ");
    query.append("                                               [qt:data {defaultGraph}]; ");
    query.append("                                               [sd:entailmentRegime {Regime} ]");

    // skip tests involving CSV result files, these are not query tests
    query.append(" WHERE NOT resultFile LIKE \"*.csv\" ");
    // skip tests involving JSON, sesame currently does not have a
    // SPARQL/JSON
    // parser.
    query.append(" AND NOT resultFile LIKE \"*.srj\" ");
    // skip tests involving entailment regimes
    query.append(" AND NOT BOUND(Regime) ");
    // skip test involving basic federation, these are tested separately.
    query.append(" AND (NOT BOUND(Requirement) OR (Requirement != mf:BasicFederation)) ");
    query.append(" USING NAMESPACE ");
    query.append("  mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, ");
    query.append("  dawgt = <http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#>, ");
    query.append("  qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>, ");
    query.append("  sd = <http://www.w3.org/ns/sparql-service-description#>, ");
    query.append("  ent = <http://www.w3.org/ns/entailment/> ");
    TupleQuery testCaseQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString());

    query.setLength(0);
    query.append(" SELECT graph ");
    query.append(" FROM {action} qt:graphData {graph} ");
    query.append(" USING NAMESPACE ");
    query.append(" qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>");
    TupleQuery namedGraphsQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString());

    query.setLength(0);
    query.append("SELECT 1 ");
    query.append(" FROM {testURI} mf:resultCardinality {mf:LaxCardinality}");
    query.append(
        " USING NAMESPACE mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>");
    TupleQuery laxCardinalityQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString());

    logger.debug("evaluating query..");
    TupleQueryResult testCases = testCaseQuery.evaluate();
    while (testCases.hasNext()) {
      BindingSet bindingSet = testCases.next();

      URI testURI = (URI) bindingSet.getValue("testURI");
      String testName = bindingSet.getValue("testName").toString();
      String resultFile = bindingSet.getValue("resultFile").toString();
      String queryFile = bindingSet.getValue("queryFile").toString();
      URI defaultGraphURI = (URI) bindingSet.getValue("defaultGraph");
      Value action = bindingSet.getValue("action");
      Value ordered = bindingSet.getValue("ordered");

      logger.debug("found test case : {}", testName);

      // Query named graphs
      namedGraphsQuery.setBinding("action", action);
      TupleQueryResult namedGraphs = namedGraphsQuery.evaluate();

      DatasetImpl dataset = null;

      if (defaultGraphURI != null || namedGraphs.hasNext()) {
        dataset = new DatasetImpl();

        if (defaultGraphURI != null) {
          dataset.addDefaultGraph(defaultGraphURI);
        }

        while (namedGraphs.hasNext()) {
          BindingSet graphBindings = namedGraphs.next();
          URI namedGraphURI = (URI) graphBindings.getValue("graph");
          logger.debug(" adding named graph : {}", namedGraphURI);
          dataset.addNamedGraph(namedGraphURI);
        }
      }

      // Check for lax-cardinality conditions
      boolean laxCardinality = false;
      laxCardinalityQuery.setBinding("testURI", testURI);
      TupleQueryResult laxCardinalityResult = laxCardinalityQuery.evaluate();
      try {
        laxCardinality = laxCardinalityResult.hasNext();
      } finally {
        laxCardinalityResult.close();
      }

      // if this is enabled, Sesame passes all tests, showing that the
      // only
      // difference is the semantics of arbitrary-length
      // paths
      /*
       * if (!laxCardinality) { // property-path tests always with lax
       * cardinality because Sesame filters out duplicates by design if
       * (testURI.stringValue().contains("property-path")) {
       * laxCardinality = true; } }
       */

      // check if we should test for query result ordering
      boolean checkOrder = false;
      if (ordered != null) {
        checkOrder = Boolean.parseBoolean(ordered.stringValue());
      }

      SPARQLQueryTest test =
          factory.createSPARQLQueryTest(
              testURI.toString(),
              testName,
              queryFile,
              resultFile,
              dataset,
              laxCardinality,
              checkOrder);
      if (test != null) {
        suite.addTest(test);
      }
    }

    testCases.close();
    con.close();

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