001    /*
002     * (c) Copyright 2009 University of Bristol
003     * All rights reserved.
004     * [See end of file]
005     */
006    
007    package net.rootdev.javardfa;
008    
009    import com.hp.hpl.jena.query.Query;
010    import com.hp.hpl.jena.query.QueryFactory;
011    import com.hp.hpl.jena.rdf.model.Model;
012    import com.hp.hpl.jena.rdf.model.ModelFactory;
013    import com.hp.hpl.jena.sparql.algebra.Algebra;
014    import com.hp.hpl.jena.util.FileManager;
015    import java.io.BufferedReader;
016    import java.io.IOException;
017    import java.io.InputStream;
018    import java.io.InputStreamReader;
019    import java.net.URISyntaxException;
020    import java.net.URL;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Map;
024    import javax.xml.parsers.ParserConfigurationException;
025    import javax.xml.stream.XMLInputFactory;
026    import javax.xml.stream.XMLStreamException;
027    import net.rootdev.javardfa.ParserFactory.Format;
028    import net.rootdev.javardfa.jena.JenaStatementSink;
029    import net.rootdev.javardfa.query.QueryUtilities;
030    import static org.junit.Assert.assertEquals;
031    import static org.junit.Assert.assertTrue;
032    import org.junit.Test;
033    import org.junit.runner.RunWith;
034    import org.junit.runners.Parameterized;
035    import org.junit.runners.Parameterized.Parameters;
036    import org.xml.sax.SAXException;
037    import org.xml.sax.XMLReader;
038    
039    /**
040     * @author Damian Steer <pldms@mac.com>
041     */
042    
043    @RunWith(Parameterized.class)
044    public class FileBasedTests {
045    
046        static final String BASE = "/query-tests/";
047    
048        @Parameters
049        public static Collection<String[]> testFiles()
050                throws URISyntaxException, IOException {
051            InputStream stream = FileBasedTests.class.getClassLoader().getResourceAsStream("test-manifest");
052            if (stream == null) {
053                throw new Error("Couldn't find test-manifest");
054            }
055            BufferedReader reader =
056                    new BufferedReader(new InputStreamReader(stream, "UTF-8"));
057            Collection<String[]> filePairs = new ArrayList<String[]>();
058            String nextLine;
059            while ((nextLine = reader.readLine()) != null) {
060                if (nextLine.startsWith("#")) continue;
061                filePairs.add(nextLine.split("\\s+"));
062            }
063            return filePairs;
064        }
065        private final String htmlFile;
066        private final String compareFile;
067        private final XMLInputFactory xmlFactory;
068    
069        public FileBasedTests(String htmlFile, String compareFile) {
070            this.htmlFile = BASE + htmlFile;
071            this.compareFile = BASE + compareFile;
072            xmlFactory = XMLInputFactory.newInstance();
073        }
074    
075        @Test
076        public void compare() throws XMLStreamException, IOException, ParserConfigurationException, SAXException {
077            URL htmlURL = this.getClass().getResource(htmlFile);
078            URL compareURL = this.getClass().getResource(compareFile);
079            
080            if (compareFile.endsWith(".rq")) compareQuery(htmlURL, compareURL);
081            else compareRDF(htmlURL, compareURL);
082        }
083    
084        private void compareRDF(URL htmlURL, URL compareURL) throws SAXException, IOException {
085            String cf = compareURL.toExternalForm();
086            if (cf.matches("file:/[^/][^/].*")) cf = cf.replaceFirst("file:/", "file:///");
087            String hf = htmlURL.toExternalForm();
088            if (hf.matches("file:/[^/][^/].*")) hf = hf.replaceFirst("file:/", "file:///");
089            Model c = FileManager.get().loadModel(compareURL.toExternalForm());
090            Model m = ModelFactory.createDefaultModel();
091            StatementSink sink = new JenaStatementSink(m);
092            XMLReader parser = ParserFactory.createReaderForFormat(sink, Format.XHTML, Setting.OnePointOne);
093            parser.parse(hf);
094            boolean result = c.isIsomorphicWith(m);
095            if (!result) m.write(System.err, "TTL");
096            assertTrue("Files match (" + htmlURL + ")", result);
097        }
098    
099        private void compareQuery(URL htmlURL, URL compareURL) throws SAXException, IOException {
100            Query query = QueryFactory.read(compareURL.toExternalForm());
101            
102            Map<String, Query> qs = QueryUtilities.makeQueries(ParserFactory.Format.XHTML,
103                    htmlURL.toExternalForm());
104    
105            assertTrue("We have a query", qs.size() != 0);
106    
107            Query qFromHTML = qs.get(qs.keySet().toArray()[0]);
108    
109            assertEquals("Query matches (" + htmlURL + ")",
110                    Algebra.compile(query),
111                    Algebra.compile(qFromHTML));
112        }
113    
114    }
115    
116    /*
117     * (c) Copyright 2009 University of Bristol
118     * All rights reserved.
119     *
120     * Redistribution and use in source and binary forms, with or without
121     * modification, are permitted provided that the following conditions
122     * are met:
123     * 1. Redistributions of source code must retain the above copyright
124     *    notice, this list of conditions and the following disclaimer.
125     * 2. Redistributions in binary form must reproduce the above copyright
126     *    notice, this list of conditions and the following disclaimer in the
127     *    documentation and/or other materials provided with the distribution.
128     * 3. The name of the author may not be used to endorse or promote products
129     *    derived from this software without specific prior written permission.
130     *
131     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
132     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
133     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
134     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
135     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
136     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
137     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
138     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
139     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
140     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141     */