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.query.parser.sparql;
007:
008: import java.io.File;
009: import java.io.FileOutputStream;
010: import java.text.SimpleDateFormat;
011: import java.util.Date;
012:
013: import junit.framework.AssertionFailedError;
014: import junit.framework.Test;
015: import junit.framework.TestListener;
016: import junit.framework.TestResult;
017:
018: import org.openrdf.model.BNode;
019: import org.openrdf.model.Resource;
020: import org.openrdf.model.ValueFactory;
021: import org.openrdf.model.vocabulary.RDF;
022: import org.openrdf.model.vocabulary.XMLSchema;
023: import org.openrdf.repository.Repository;
024: import org.openrdf.repository.RepositoryConnection;
025: import org.openrdf.repository.RepositoryException;
026: import org.openrdf.repository.sail.SailRepository;
027: import org.openrdf.rio.RDFFormat;
028: import org.openrdf.rio.RDFWriterFactory;
029: import org.openrdf.rio.RDFWriterRegistry;
030: import org.openrdf.sail.memory.MemoryStore;
031:
032: /**
033: * @author Arjohn Kampman
034: */
035: public class EarlReport {
036:
037: protected static Repository earlRepository;
038:
039: protected static ValueFactory vf;
040:
041: protected static RepositoryConnection con;
042:
043: protected static Resource projectNode;
044:
045: protected static Resource asserterNode;
046:
047: public static void main(String[] args) throws Exception {
048: earlRepository = new SailRepository(new MemoryStore());
049: earlRepository.initialize();
050: vf = earlRepository.getValueFactory();
051: con = earlRepository.getConnection();
052: con.setAutoCommit(false);
053:
054: con.setNamespace("rdf", RDF.NAMESPACE);
055: con.setNamespace("xsd", XMLSchema.NAMESPACE);
056: con.setNamespace("doap", DOAP.NAMESPACE);
057: con.setNamespace("earl", EARL.NAMESPACE);
058: con.setNamespace("dc", DC.NAMESPACE);
059:
060: projectNode = vf.createBNode();
061: BNode releaseNode = vf.createBNode();
062: con.add(projectNode, RDF.TYPE, DOAP.PROJECT);
063: con.add(projectNode, DOAP.NAME, vf
064: .createLiteral("OpenRDF Sesame"));
065: con.add(projectNode, DOAP.RELEASE, releaseNode);
066: con.add(releaseNode, RDF.TYPE, DOAP.VERSION);
067: con.add(releaseNode, DOAP.NAME, vf
068: .createLiteral("Sesame 2.0-SNAPSHOT"));
069: SimpleDateFormat xsdDataFormat = new SimpleDateFormat(
070: "yyyy-MM-dd");
071: String currentDate = xsdDataFormat.format(new Date());
072: con.add(releaseNode, DOAP.CREATED, vf.createLiteral(
073: currentDate, XMLSchema.DATE));
074:
075: asserterNode = vf.createBNode();
076: con.add(asserterNode, RDF.TYPE, EARL.SOFTWARE);
077: con.add(asserterNode, DC.TITLE, vf
078: .createLiteral("OpenRDF SPARQL compliance test"));
079:
080: TestResult testResult = new TestResult();
081: EarlTestListener listener = new EarlTestListener();
082: testResult.addListener(listener);
083:
084: ManifestTest.suite().run(testResult);
085: SPARQLSyntaxTest.suite().run(testResult);
086:
087: con.setAutoCommit(true);
088:
089: RDFWriterFactory factory = RDFWriterRegistry.getInstance().get(
090: RDFFormat.TURTLE);
091: File outFile = File.createTempFile("sesame-sparql-compliance",
092: "." + RDFFormat.TURTLE.getDefaultFileExtension());
093: FileOutputStream out = new FileOutputStream(outFile);
094: try {
095: con.export(factory.getWriter(out));
096: } finally {
097: out.close();
098: }
099:
100: con.close();
101: earlRepository.shutDown();
102:
103: System.out.println("EARL output written to " + outFile);
104: }
105:
106: protected static class EarlTestListener implements TestListener {
107:
108: private int errorCount;
109:
110: private int failureCount;
111:
112: public void startTest(Test test) {
113: errorCount = failureCount = 0;
114: }
115:
116: public void endTest(Test test) {
117: String testURI = null;
118: ;
119: if (test instanceof SPARQLQueryTest) {
120: testURI = ((SPARQLQueryTest) test).testURI;
121: } else if (test instanceof SPARQLSyntaxTest) {
122: testURI = ((SPARQLSyntaxTest) test).testURI;
123: } else {
124: throw new RuntimeException("Unexpected test type: "
125: + test.getClass());
126: }
127:
128: try {
129: BNode testNode = vf.createBNode();
130: BNode resultNode = vf.createBNode();
131: con.add(testNode, RDF.TYPE, EARL.ASSERTION);
132: con.add(testNode, EARL.ASSERTEDBY, asserterNode);
133: con.add(testNode, EARL.MODE, EARL.AUTOMATIC);
134: con.add(testNode, EARL.SUBJECT, projectNode);
135: con.add(testNode, EARL.TEST, vf.createURI(testURI));
136: con.add(testNode, EARL.RESULT, resultNode);
137: con.add(resultNode, RDF.TYPE, EARL.TESTRESULT);
138:
139: if (errorCount > 0) {
140: con.add(resultNode, EARL.OUTCOME, EARL.FAIL);
141: } else if (failureCount > 0) {
142: con.add(resultNode, EARL.OUTCOME, EARL.FAIL);
143: } else {
144: con.add(resultNode, EARL.OUTCOME, EARL.PASS);
145: }
146: } catch (RepositoryException e) {
147: throw new RuntimeException(e);
148: }
149: }
150:
151: public void addError(Test test, Throwable t) {
152: errorCount++;
153: }
154:
155: public void addFailure(Test test, AssertionFailedError error) {
156: failureCount++;
157: }
158: }
159: }
|