001    /*
002     * (c) Copyright 2009 University of Bristol
003     * All rights reserved.
004     * [See end of file]
005     */
006    package net.rootdev.javardfa.conformance;
007    
008    import com.hp.hpl.jena.query.Query;
009    import com.hp.hpl.jena.query.QueryExecution;
010    import com.hp.hpl.jena.query.QueryExecutionFactory;
011    import com.hp.hpl.jena.query.QueryFactory;
012    import com.hp.hpl.jena.query.QuerySolution;
013    import com.hp.hpl.jena.query.ResultSet;
014    import com.hp.hpl.jena.rdf.model.Model;
015    import com.hp.hpl.jena.rdf.model.ModelFactory;
016    import com.hp.hpl.jena.util.FileManager;
017    import java.io.IOException;
018    import java.io.InputStream;
019    import java.net.URISyntaxException;
020    import java.util.ArrayList;
021    import java.util.Arrays;
022    import java.util.Collection;
023    import java.util.HashSet;
024    import java.util.Set;
025    import org.junit.Test;
026    import static org.junit.Assert.*;
027    import org.junit.runner.RunWith;
028    import org.junit.runners.Parameterized;
029    import org.junit.runners.Parameterized.Parameters;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    import org.xml.sax.InputSource;
033    import org.xml.sax.SAXException;
034    import org.xml.sax.XMLReader;
035    
036    /**
037     * @author Damian Steer <pldms@mac.com>
038     */
039    @RunWith(Parameterized.class)
040    public abstract class RDFaConformance {
041    
042        final static Logger log = LoggerFactory.getLogger(RDFaConformance.class);
043        
044        public static Collection<String[]> testFiles(String manifestURI, String... excludes)
045                throws URISyntaxException, IOException {
046    
047            Set<String> toExclude = new HashSet(Arrays.asList(excludes));
048    
049            FileManager fm = FileManager.get();
050    
051            Model manifest = fm.loadModel(manifestURI);
052    
053            Query manifestExtract = QueryFactory.read("manifest-extract.rq");
054    
055            Collection<String[]> tests = new ArrayList<String[]>();
056    
057            QueryExecution qe = QueryExecutionFactory.create(manifestExtract, manifest);
058    
059            ResultSet results = qe.execSelect();
060    
061            if (!results.hasNext()) {
062                throw new RuntimeException("No results");
063            }
064            while (results.hasNext()) {
065    
066                QuerySolution soln = results.next();
067                String[] params = new String[6];
068                params[0] = soln.getResource("test").getURI();
069                params[1] = soln.getLiteral("title").getString();
070                params[2] = soln.getLiteral("purpose").getString();
071                params[3] = soln.getResource("input").getURI();
072                params[4] = soln.getResource("query").getURI();
073                // getBoolean not working??
074                //boolean expected = (soln.contains("expect")) ?
075                //    soln.getLiteral("expect").getBoolean() : true;
076                params[5] = soln.contains("expect") ? soln.getLiteral("expect").getLexicalForm() : "true";
077                if (toExclude.contains(params[0]) ||
078                        toExclude.contains(params[3]) ||
079                        toExclude.contains(params[4]) ) {
080                    log.warn("Skipping test <" + params[0] + ">");
081                    continue;
082                }
083                tests.add(params);
084            }
085    
086            return tests;
087        }
088        private final String test;
089        private final String title;
090        private final String purpose;
091        private final String input;
092        private final String query;
093        private final boolean expected;
094    
095        public RDFaConformance(String test, String title,
096                String purpose, String input, String query, String expected) {
097            this.test = test;
098            this.title = title;
099            this.purpose = purpose;
100            this.input = input;
101            this.query = query;
102            this.expected = Boolean.valueOf(expected);
103        }
104    
105        public abstract XMLReader getParser(Model model) throws SAXException;
106    
107        @Test
108        public void compare() throws SAXException, IOException {
109            Model model = ModelFactory.createDefaultModel();
110            InputStream in = FileManager.get().open(input);
111            XMLReader reader = getParser(model);
112            try {
113                InputSource ins = new InputSource(in);
114                ins.setEncoding("utf-8");
115                ins.setSystemId(input);
116                reader.parse(ins);
117            } catch (NullPointerException e) {
118                fail("NPE <" + test + ">");
119            }
120            Query theQuery = QueryFactory.read(query);
121            QueryExecution qe = QueryExecutionFactory.create(theQuery, model);
122            boolean result = qe.execAsk();
123            if (result != expected) {
124                System.err.println("------ " + test + " ------");
125                model.write(System.err, "TTL");
126                System.err.println("------ Query ------");
127                System.err.println(theQuery);
128                System.err.println("-----------------------");
129            }
130            if (expected) {
131                assertTrue(title + " <" + test + ">", result);
132            } else {
133                assertFalse(title + " <" + test + ">", result);
134            }
135        }
136    }
137    
138    /*
139     * (c) Copyright 2009 University of Bristol
140     * All rights reserved.
141     *
142     * Redistribution and use in source and binary forms, with or without
143     * modification, are permitted provided that the following conditions
144     * are met:
145     * 1. Redistributions of source code must retain the above copyright
146     *    notice, this list of conditions and the following disclaimer.
147     * 2. Redistributions in binary form must reproduce the above copyright
148     *    notice, this list of conditions and the following disclaimer in the
149     *    documentation and/or other materials provided with the distribution.
150     * 3. The name of the author may not be used to endorse or promote products
151     *    derived from this software without specific prior written permission.
152     *
153     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
154     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
155     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
156     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
157     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
158     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
159     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
160     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
161     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
162     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
163     */