001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.util;
007:
008: import java.io.InputStream;
009: import java.net.URL;
010: import java.util.LinkedHashSet;
011: import java.util.Set;
012:
013: import junit.framework.TestCase;
014:
015: import org.openrdf.model.Statement;
016: import org.openrdf.model.util.ModelUtil;
017: import org.openrdf.rio.RDFFormat;
018: import org.openrdf.rio.RDFParser;
019: import org.openrdf.rio.Rio;
020: import org.openrdf.rio.RDFParser.DatatypeHandling;
021: import org.openrdf.rio.helpers.StatementCollector;
022:
023: /**
024: * @author Arjohn Kampman
025: */
026: public class ModelEqualityTest extends TestCase {
027:
028: public static final String TESTCASES_DIR = "/testcases/model/equality/";
029:
030: public void testTest001() throws Exception {
031: testFilesEqual("test001a.ttl", "test001b.ttl");
032: }
033:
034: public void testFoafExampleAdvanced() throws Exception {
035: testFilesEqual("foaf-example-advanced.rdf",
036: "foaf-example-advanced.rdf");
037: }
038:
039: public void testSparqlGraph11() throws Exception {
040: testFilesEqual("sparql-graph-11.ttl", "sparql-graph-11.ttl");
041: }
042:
043: // public void testSparqlGraph11Shuffled()
044: // throws Exception
045: // {
046: // testFilesEqual("sparql-graph-11.ttl", "sparql-graph-11-shuffled.ttl");
047: // }
048:
049: // public void testSparqlGraph11Shuffled2()
050: // throws Exception
051: // {
052: // testFilesEqual("sparql-graph-11-shuffled.ttl", "sparql-graph-11.ttl");
053: // }
054:
055: // public void testPhotoData()
056: // throws Exception
057: // {
058: // testFilesEqual("photo-data.rdf", "photo-data.rdf");
059: // }
060:
061: private void testFilesEqual(String file1, String file2)
062: throws Exception {
063: Set<Statement> model1 = loadModel(file1);
064: Set<Statement> model2 = loadModel(file2);
065:
066: // long startTime = System.currentTimeMillis();
067: boolean modelsEqual = ModelUtil.equals(model1, model2);
068: // long endTime = System.currentTimeMillis();
069: // System.out.println("Model equality checked in " + (endTime - startTime)
070: // + "ms (" + file1 + ", " + file2
071: // + ")");
072:
073: assertTrue(modelsEqual);
074: }
075:
076: private Set<Statement> loadModel(String fileName) throws Exception {
077: URL modelURL = this .getClass().getResource(
078: TESTCASES_DIR + fileName);
079: assertNotNull("Test file not found: " + fileName, modelURL);
080:
081: Set<Statement> model = new LinkedHashSet<Statement>();
082:
083: RDFFormat rdfFormat = Rio.getParserFormatForFileName(fileName);
084: assertNotNull("Unable to determine RDF format for file: "
085: + fileName, rdfFormat);
086:
087: RDFParser parser = Rio.createParser(rdfFormat);
088: parser.setDatatypeHandling(DatatypeHandling.IGNORE);
089: parser.setPreserveBNodeIDs(true);
090: parser.setRDFHandler(new StatementCollector(model));
091:
092: InputStream in = modelURL.openStream();
093: try {
094: parser.parse(in, modelURL.toString());
095: return model;
096: } finally {
097: in.close();
098: }
099: }
100: }
|