001package org.w3.ldp.testsuite.reporter;
002
003import java.io.BufferedWriter;
004import java.io.File;
005import java.io.FileWriter;
006import java.io.IOException;
007import java.util.HashMap;
008import java.util.Map.Entry;
009
010import org.w3.ldp.testsuite.vocab.LDP;
011
012import com.hp.hpl.jena.rdf.model.Model;
013import com.hp.hpl.jena.rdf.model.ModelFactory;
014
015public abstract class AbstractEarlReporter {
016
017        protected BufferedWriter writerTurtle;
018        protected BufferedWriter writerJson;
019        protected Model model;
020        protected static final String TURTLE = "TURTLE";
021        protected static final String JSON_LD = "JSON-LD";
022        protected static final HashMap<String, String> prefixes = new HashMap<String, String>();
023
024        protected String outputDirectory;
025
026        public void setOutputDirectory(String outputDirectory) {
027                this.outputDirectory = outputDirectory;
028        }
029
030        static {
031                prefixes.put("doap", "http://usefulinc.com/ns/doap#");
032                prefixes.put("foaf", "http://xmlns.com/foaf/0.1/");
033                prefixes.put("earl", "http://www.w3.org/ns/earl#");
034                prefixes.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
035                prefixes.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
036                prefixes.put("mf", "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#");
037                prefixes.put("rdft", "http://www.w3.org/ns/rdftest#");
038                prefixes.put("dcterms", "http://purl.org/dc/terms/");
039                prefixes.put("td", "http://www.w3.org/2006/03/test-description#");
040                prefixes.put(LDP.LDPT_PREFIX, LDP.LDPT_NAMESPACE);
041        }
042
043        protected abstract String getFilename();
044
045        protected void createWriter(String directory, String title) throws IOException {
046                File dir = new File(directory);
047                dir.mkdirs();
048                System.out.println("Writing EARL results:");
049                String fileName = getFilename() + title + ".ttl";
050                File file = new File(dir, fileName);
051                writerTurtle = new BufferedWriter(new FileWriter(file));
052                System.out.println("\t"+file.getAbsolutePath());
053                fileName = getFilename() + title + ".jsonld";
054                file = new File(dir, fileName);
055                writerJson = new BufferedWriter(new FileWriter(file));
056                System.out.println("\t"+file.getAbsolutePath());
057        }
058
059        protected void write() {
060                model.write(writerTurtle, TURTLE);
061                model.write(writerJson, JSON_LD);
062        }
063
064        protected void endWriter() throws IOException {
065                writerTurtle.flush();
066                writerTurtle.close();
067                writerJson.flush();
068                writerJson.close();
069        }
070
071        protected void createModel() {
072                model = ModelFactory.createDefaultModel();
073                writePrefixes(model);
074        }
075
076        public void writePrefixes(Model model) {
077                for (Entry<String, String> prefix : prefixes.entrySet()) {
078                        model.setNsPrefix(prefix.getKey(), prefix.getValue());
079                }
080        }
081
082        public static String createTestCaseName(String className, String methodName) {
083
084                className = className.substring(className.lastIndexOf(".") + 1);
085
086                if (className.endsWith("Test")) {
087                        className = className.substring(0, className.length()-4);
088                }
089                if (methodName.startsWith("test")) {
090                        methodName = methodName.substring(4, methodName.length());
091                }
092                return className + "-" + methodName;
093        }
094
095        public static String createTestCaseURL(String className, String methodName) {
096                return LDP.LDPT_NAMESPACE + createTestCaseName(className, methodName);
097        }
098}