示例#1
0
  private void compareGraphs(Set<Statement> queryResult, Set<Statement> expectedResult)
      throws Exception {
    if (!ModelUtil.equals(expectedResult, queryResult)) {
      // Don't use RepositoryUtil.difference, it reports incorrect diffs
      /*
       * Collection<? extends Statement> unexpectedStatements =
       * RepositoryUtil.difference(queryResult, expectedResult);
       * Collection<? extends Statement> missingStatements =
       * RepositoryUtil.difference(expectedResult, queryResult);
       * StringBuilder message = new StringBuilder(128);
       * message.append("\n=======Diff: "); message.append(getName());
       * message.append("========================\n"); if
       * (!unexpectedStatements.isEmpty()) { message.append("Unexpected
       * statements in result: \n"); for (Statement st :
       * unexpectedStatements) { message.append(st.toString());
       * message.append("\n"); } message.append("============="); for (int
       * i = 0; i < getName().length(); i++) { message.append("="); }
       * message.append("========================\n"); } if
       * (!missingStatements.isEmpty()) { message.append("Statements
       * missing in result: \n"); for (Statement st : missingStatements) {
       * message.append(st.toString()); message.append("\n"); }
       * message.append("============="); for (int i = 0; i <
       * getName().length(); i++) { message.append("="); }
       * message.append("========================\n"); }
       */
      StringBuilder message = new StringBuilder(128);
      message.append("\n============ ");
      message.append(getName());
      message.append(" =======================\n");
      message.append("Expected result: \n");
      for (Statement st : expectedResult) {
        message.append(st.toString());
        message.append("\n");
      }
      message.append("=============");
      StringUtil.appendN('=', getName().length(), message);
      message.append("========================\n");

      message.append("Query result: \n");
      for (Statement st : queryResult) {
        message.append(st.toString());
        message.append("\n");
      }
      message.append("=============");
      StringUtil.appendN('=', getName().length(), message);
      message.append("========================\n");

      logger.error(message.toString());
      fail(message.toString());
    }
  }
    @Override
    protected void runTest() throws Exception {
      // Parse input data
      RDFParser turtleParser = createRDFParser();
      turtleParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

      Set<Statement> inputCollection = new LinkedHashSet<Statement>();
      StatementCollector inputCollector = new StatementCollector(inputCollection);
      turtleParser.setRDFHandler(inputCollector);

      InputStream in = inputURL.openStream();
      turtleParser.parse(in, base(inputURL.toExternalForm()));
      in.close();

      // Parse expected output data
      NTriplesParser ntriplesParser = new NTriplesParser();
      ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

      Set<Statement> outputCollection = new LinkedHashSet<Statement>();
      StatementCollector outputCollector = new StatementCollector(outputCollection);
      ntriplesParser.setRDFHandler(outputCollector);

      in = outputURL.openStream();
      ntriplesParser.parse(in, base(outputURL.toExternalForm()));
      in.close();

      // Check equality of the two models
      if (!ModelUtil.equals(inputCollection, outputCollection)) {
        System.err.println("===models not equal===");
        // System.err.println("Expected: " + outputCollection);
        // System.err.println("Actual : " + inputCollection);
        // System.err.println("======================");

        List<Statement> missing = new LinkedList<Statement>(outputCollection);
        missing.removeAll(inputCollection);

        List<Statement> unexpected = new LinkedList<Statement>(inputCollection);
        unexpected.removeAll(outputCollection);

        if (!missing.isEmpty()) {
          System.err.println("Missing   : " + missing);
        }
        if (!unexpected.isEmpty()) {
          System.err.println("Unexpected: " + unexpected);
        }

        fail("models not equal");
      }
    }
  /**
   * Compares the models of the default context of two repositories and returns true if rep1 is a
   * subset of rep2. Note that the method pulls the entire default context of both repositories into
   * main memory. Use with caution.
   */
  public static boolean isSubset(Repository rep1, Repository rep2) throws RepositoryException {
    Set<Statement> model1, model2;

    RepositoryConnection con1 = rep1.getConnection();
    try {
      model1 = Iterations.asSet(con1.getStatements(null, null, null, true));
    } finally {
      con1.close();
    }

    RepositoryConnection con2 = rep2.getConnection();
    try {
      model2 = Iterations.asSet(con2.getStatements(null, null, null, true));
    } finally {
      con2.close();
    }

    return ModelUtil.isSubset(model1, model2);
  }