示例#1
0
  // TEMPORARY
  private boolean checkResultsByModel(
      Query query, Model expectedModel, ResultSetRewindable results) {
    // Fudge - can't cope with ordered results properly.  The output writer for ResultSets does nto
    // add rs:index.

    results.reset();
    Model actualModel = ResultSetFormatter.toModel(results);
    // Tidy the models.
    // Very regretable.

    expectedModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSet);
    expectedModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSolution);
    expectedModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultBinding);
    expectedModel.removeAll(null, ResultSetGraphVocab.size, (RDFNode) null);
    expectedModel.removeAll(null, ResultSetGraphVocab.index, (RDFNode) null);

    actualModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSet);
    actualModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSolution);
    actualModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultBinding);
    actualModel.removeAll(null, ResultSetGraphVocab.size, (RDFNode) null);
    actualModel.removeAll(null, ResultSetGraphVocab.index, (RDFNode) null);

    boolean b = expectedModel.isIsomorphicWith(actualModel);
    if (!b) {
      System.out.println("---- Expected");
      expectedModel.write(System.out, "TTL");
      System.out.println("---- Actual");
      actualModel.write(System.out, "TTL");
      System.out.println("----");
    }
    return b;
  }
 public void testBlankNodes2() {
   readIntoModel("blankNodes2.xml", "TRIX");
   Model expected = ModelFactory.createDefaultModel();
   InputStream stream = this.getClass().getResourceAsStream("tests/blankNodes.nt");
   expected.read(stream, "file:/test", "N-TRIPLE");
   assertFalse(expected.isIsomorphicWith(this.model));
 }
示例#3
0
  @Test
  /**
   * Reads from TRIG with Jena API into Dataset 1, transforms one named Model from that Dataset into
   * Signingframework's API GraphCollection with one NamedGraph, transforms (converts) that
   * NamedGraph into Jena's Model, and checks if the resulting Model is the same as original Model.
   */
  public void namedGraphToModelTest() throws Exception {

    for (String resourceFile : RESOURCE_FILES) {

      // prepare GraphCollection with NamedGraph to be converted:
      InputStream is = this.getClass().getResourceAsStream(resourceFile);
      Dataset dataset = DatasetFactory.createMem();
      RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang());
      is.close();
      String modelName = dataset.listNames().next();
      Model model1 = dataset.getNamedModel(modelName);
      // this method is not tested here and used just for input
      // generation and to make it easier Namedgraph<->Model comparison
      // (but it's tested in other method, see modelToGraphCollectionTest())
      GraphCollection gc = ModelConverter.modelToGraphCollection(modelName, dataset);
      LinkedList<NamedGraph> graphs = gc.getGraphs();
      String graphName = null;
      for (NamedGraph g : graphs) {
        if (!g.getName().isEmpty() && g.getName().contains(modelName)) {
          graphName = g.getName();
          break;
        }
      }
      // use this when debugging:
      // File outFile0 = File.createTempFile("won", ".trig");
      // System.out.println(outFile0);
      // OutputStream os0 = new FileOutputStream(outFile0);
      // TriGPlusWriter.writeFile(gc, outFile0.getAbsolutePath(), false);
      // os0.close();

      // test convert from NamedGraph of GraphCollection into Model
      Model model2 = ModelConverter.namedGraphToModel(graphName, gc);
      Dataset dataset2 = DatasetFactory.createMem();
      dataset2.addNamedModel(modelName, model2);
      // TODO maybe chng the API so that the prefix map is taken care of in the converter:
      // if it makes sense from the the usage of this in Assembler point of view
      dataset2.getDefaultModel().setNsPrefixes(dataset2.getNamedModel(modelName).getNsPrefixMap());

      File outFile = testFolder.newFile();
      // use this when debugging:
      // File outFile = File.createTempFile("won", ".trig");
      // System.out.println(outFile);
      OutputStream os = new FileOutputStream(outFile);
      RDFDataMgr.write(os, dataset2, RDFFormat.TRIG.getLang());
      os.close();

      // make sure that the original Model that was used to generate test input
      // GraphCollection with NamedGraph is isomorphic with the Model after
      // conversion is applied:
      Assert.assertTrue(model1.listStatements().hasNext() && model2.listStatements().hasNext());
      Assert.assertTrue(model1.isIsomorphicWith(model2));
    }
  }
示例#4
0
 /**
  * test that two models are isomorphic and fail if they are not.
  *
  * @param title a String appearing at the beginning of the failure message
  * @param wanted the model value that is expected
  * @param got the model value to check
  * @exception if the models are not isomorphic
  */
 public static void assertIsoModels(String title, Model wanted, Model got) {
   if (wanted.isIsomorphicWith(got) == false) {
     Map<Node, Object> map = CollectionFactory.createHashedMap();
     fail(
         title
             + ": expected "
             + nice(wanted.getGraph(), map)
             + "\n but had "
             + nice(got.getGraph(), map));
   }
 }
示例#5
0
  @Test
  /**
   * Reads from TRIG with Jena API into Dataset 1, transforms one named graph from that Dataset into
   * Signingframework's API GraphCollection and writes it with Signingframework's API, reads the
   * result with Jena API into Dataset 2, and checks if the specified named graph model from Dataset
   * 1 is isomorphic with the same named graph model from Dataset 2.
   */
  public void modelToGraphCollectionTest() throws Exception {

    for (String resourceFile : RESOURCE_FILES) {

      // prepare the input Dataset containg the Model to be converted
      InputStream is = this.getClass().getResourceAsStream(resourceFile);
      File outFile = testFolder.newFile();
      // use this when debugging:
      // File outFile = File.createTempFile("won", ".trig");
      // System.out.println(outFile);
      Dataset dataset = DatasetFactory.createMem();
      RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang());
      is.close();

      // test the convertion from the Model to the NamedGraph
      String modelName = dataset.listNames().next();
      Model model = dataset.getNamedModel(modelName);
      // the method to be tested
      GraphCollection gc = ModelConverter.modelToGraphCollection(modelName, dataset);
      TriGPlusWriter.writeFile(gc, outFile.getAbsolutePath(), false);

      // check that the resulting graph collection is a representation
      // of the converted model. For this, read the resulting graph collection
      // as a Model with Jena API
      InputStream is2 = new FileInputStream(outFile);
      Dataset dataset2 = DatasetFactory.createMem();
      RDFDataMgr.read(dataset2, is2, RDFFormat.TRIG.getLang());
      is2.close();
      Model model2 = dataset2.getNamedModel(modelName);
      File outFile2 = testFolder.newFile();
      // use this when debugging:
      // File outFile2 = File.createTempFile("won", ".trig");
      // System.out.println(outFile2);
      OutputStream os = new FileOutputStream(outFile2);
      RDFDataMgr.write(os, dataset2, RDFFormat.TRIG.getLang());
      os.close();

      // check that the model obtained from resulting graph collection is
      // a representation of the original converted model.
      Assert.assertTrue(model.listStatements().hasNext() && model2.listStatements().hasNext());
      Assert.assertTrue(model.isIsomorphicWith(model2));
    }
  }
示例#6
0
 private void compareRDF(URL htmlURL, URL compareURL) throws SAXException, IOException {
   String cf = compareURL.toExternalForm();
   if (cf.matches("file:/[^/][^/].*")) cf = cf.replaceFirst("file:/", "file:///");
   String hf = htmlURL.toExternalForm();
   if (hf.matches("file:/[^/][^/].*")) hf = hf.replaceFirst("file:/", "file:///");
   Model c = FileManager.get().loadModel(compareURL.toExternalForm());
   Model m = ModelFactory.createDefaultModel();
   StatementSink sink = new JenaStatementSink(m);
   XMLReader parser = ParserFactory.createReaderForFormat(sink, Format.XHTML, Setting.OnePointOne);
   parser.parse(hf);
   boolean result = c.isIsomorphicWith(m);
   if (!result) m.write(System.err, "TTL");
   assertTrue("Files match (" + htmlURL + ")", result);
 }
示例#7
0
  private void compareGraphResults(Model resultsActual, Query query) {
    if (results != null) {
      try {
        if (!results.isGraph()) fail("Expected results are not a graph: " + testItem.getName());

        Model resultsExpected = results.getModel();
        if (!resultsExpected.isIsomorphicWith(resultsActual)) {
          printFailedModelTest(query, resultsExpected, resultsActual);
          fail("Results do not match: " + testItem.getName());
        }
      } catch (Exception ex) {
        String typeName = (query.isConstructType() ? "construct" : "describe");
        fail("Exception in result testing (" + typeName + "): " + ex);
      }
    }
  }
  private static void getExisting(String graph, String mediaType, String lang)
      throws IllegalStateException, IOException, URISyntaxException {
    URI uri = uri(graph);

    HttpGet httpget = new HttpGet(uri);
    httpget.setHeader("Accept", mediaType);

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    assertEquals(200, response.getStatusLine().getStatusCode());

    Model m = ModelFactory.createDefaultModel();
    m.read(entity.getContent(), "", lang); // TODO: fix base

    assertNotNull(m);
    assertTrue(model.isIsomorphicWith(m));
  }
 private void assertSameStatements(String nTriplesFile) {
   Model expected = ModelFactory.createDefaultModel();
   InputStream stream = this.getClass().getResourceAsStream("tests/" + nTriplesFile);
   assertNotNull("tests/" + nTriplesFile + " not found", stream);
   expected.read(stream, "file:/test", "N-TRIPLE");
   if (!expected.isIsomorphicWith(this.model)) {
     System.out.println("=== Expected ===");
     StmtIterator it = expected.listStatements();
     while (it.hasNext()) {
       System.out.println(it.next());
     }
     System.out.println("=== Actual ===");
     it = this.model.listStatements();
     while (it.hasNext()) {
       System.out.println(it.next());
     }
     fail("models are not isomorphic; see stdout");
   }
 }