Strabon

changeset 1028:e0f078b158b3

Changed the way we test if the results of the query are the expected (not finished yet).
author Panayiotis Smeros <psmeros@di.uoa.gr>
date Thu Apr 11 20:33:38 2013 +0300 (2013-04-11)
parents 0faf06eeb77f
children bcc5fff82a71
files testsuite/src/test/java/eu/earthobservatory/runtime/postgis/Utils.java
line diff
     1.1 --- a/testsuite/src/test/java/eu/earthobservatory/runtime/postgis/Utils.java	Thu Apr 11 20:30:58 2013 +0300
     1.2 +++ b/testsuite/src/test/java/eu/earthobservatory/runtime/postgis/Utils.java	Thu Apr 11 20:33:38 2013 +0300
     1.3 @@ -10,10 +10,18 @@
     1.4  package eu.earthobservatory.runtime.postgis;
     1.5  
     1.6  import static org.junit.Assert.assertEquals;
     1.7 +import static org.junit.Assert.assertTrue;
     1.8 +import static org.junit.Assert.assertFalse;
     1.9  import java.io.BufferedReader;
    1.10 +import java.io.ByteArrayInputStream;
    1.11 +import java.io.ByteArrayOutputStream;
    1.12 +import java.io.File;
    1.13  import java.io.IOException;
    1.14  import java.io.InputStream;
    1.15  import java.io.InputStreamReader;
    1.16 +import java.io.OutputStream;
    1.17 +import java.net.URISyntaxException;
    1.18 +import java.net.URL;
    1.19  import java.sql.Connection;
    1.20  import java.sql.DriverManager;
    1.21  import java.sql.PreparedStatement;
    1.22 @@ -21,15 +29,25 @@
    1.23  import java.sql.SQLException;
    1.24  import java.util.ArrayList;
    1.25  import java.util.Iterator;
    1.26 +import java.util.List;
    1.27  import java.util.Properties;
    1.28 +import org.apache.commons.io.FileUtils;
    1.29 +import org.openrdf.model.Value;
    1.30 +import org.openrdf.query.BindingSet;
    1.31  import org.openrdf.query.MalformedQueryException;
    1.32  import org.openrdf.query.QueryEvaluationException;
    1.33 +import org.openrdf.query.TupleQueryResult;
    1.34  import org.openrdf.query.TupleQueryResultHandlerException;
    1.35 +import org.openrdf.query.resultio.QueryResultIO;
    1.36 +import org.openrdf.query.resultio.QueryResultParseException;
    1.37 +import org.openrdf.query.resultio.TupleQueryResultFormat;
    1.38 +import org.openrdf.query.resultio.UnsupportedQueryResultFormatException;
    1.39  import org.openrdf.repository.RepositoryException;
    1.40  import org.openrdf.rio.RDFHandlerException;
    1.41  import org.openrdf.rio.RDFParseException;
    1.42  import eu.earthobservatory.runtime.generaldb.InvalidDatasetFormatFault;
    1.43  import eu.earthobservatory.runtime.postgis.Strabon;
    1.44 +import eu.earthobservatory.utils.Format;
    1.45  
    1.46  /**
    1.47   * A class with useful methods for the tests.
    1.48 @@ -132,39 +150,51 @@
    1.49  	    	strabon.storeInRepo(datasetFile, "NQUADS");
    1.50  	}
    1.51  	
    1.52 -	public static void testQuery(String queryFile, String resultsFile) throws IOException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException
    1.53 +	public static void testQuery(String queryFile, String resultsFile) throws IOException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException, URISyntaxException, QueryResultParseException, UnsupportedQueryResultFormatException
    1.54  	{
    1.55 -		BufferedReader queryReader = new BufferedReader(new InputStreamReader(Utils.class.getResourceAsStream(queryFile)));
    1.56 -		BufferedReader resultsReader = new BufferedReader(new InputStreamReader(Utils.class.getResourceAsStream(resultsFile)));
    1.57 -		String query="";
    1.58 -		ArrayList<String> actualResults = new ArrayList<String>();
    1.59 -		ArrayList<String> expectedResults = new ArrayList<String>();
    1.60 +		ByteArrayOutputStream resultsStream = new ByteArrayOutputStream();
    1.61 +		String query = FileUtils.readFileToString(new File(Utils.class.getResource(queryFile).toURI()));
    1.62  		
    1.63 -		while (queryReader.ready())
    1.64 +		//Pose the query
    1.65 +		strabon.query(query, Format.XML, strabon.getSailRepoConnection(), resultsStream);
    1.66 +
    1.67 +		//Check if the results of the query are the expected
    1.68 +		TupleQueryResult expectedResults = QueryResultIO.parse(Utils.class.getResourceAsStream(resultsFile), TupleQueryResultFormat.SPARQL);
    1.69 +		TupleQueryResult actualResults = QueryResultIO.parse((new ByteArrayInputStream(resultsStream.toByteArray())), TupleQueryResultFormat.SPARQL);
    1.70 +
    1.71 +		List<String> eBindingNames = expectedResults.getBindingNames();
    1.72 +		List<String> aBindingNames = actualResults.getBindingNames();
    1.73 +		assertTrue("Results are not the expected.", aBindingNames.containsAll(aBindingNames) && eBindingNames.containsAll(aBindingNames));
    1.74 +		
    1.75 +		
    1.76 +		//Sort expected and actual results' bindings by binding name
    1.77 +		List<String> eBindingList = new ArrayList<String>();
    1.78 +		List<String> aBindingList = new ArrayList<String>();
    1.79 +
    1.80 +		while(expectedResults.hasNext() && actualResults.hasNext())
    1.81  		{
    1.82 -			query+=queryReader.readLine()+"\n";
    1.83 -		}
    1.84 -		actualResults = (ArrayList<String>) strabon.query(query,strabon.getSailRepoConnection());
    1.85 -		
    1.86 -		while (resultsReader.ready())
    1.87 -		{
    1.88 -			expectedResults.add(resultsReader.readLine());
    1.89 +			BindingSet eBinding = expectedResults.next();
    1.90 +			BindingSet aBinding = actualResults.next();
    1.91 +			
    1.92 +			String eBindingValues="";
    1.93 +			String aBindingValues="";
    1.94 +			for(String bindingName : eBindingNames)
    1.95 +			{
    1.96 +				eBindingValues+=eBinding.getValue(bindingName).stringValue();
    1.97 +				aBindingValues+=aBinding.getValue(bindingName).stringValue();
    1.98 +			}
    1.99 +			
   1.100 +			eBindingList.add(eBindingValues);
   1.101 +			aBindingList.add(aBindingValues);
   1.102  		}
   1.103  		
   1.104 -		//Check if the actual result set is the same as the expected one
   1.105 -		assertEquals("Result set is not the expected one.", expectedResults.size(), actualResults.size());
   1.106 -		Iterator<String> expectedResultsIterator = expectedResults.iterator();
   1.107 -		Iterator<String> actualResultsIterator = actualResults.iterator();
   1.108 +		assertFalse("Results are not the expected.", expectedResults.hasNext() || actualResults.hasNext());
   1.109  		
   1.110 -		while(expectedResultsIterator.hasNext() && actualResultsIterator.hasNext())
   1.111 -		{
   1.112 -			String eResults = expectedResultsIterator.next();
   1.113 -			String aResults = actualResultsIterator.next();	
   1.114 -			
   1.115 -			//Replace all the names of the variables with "?"
   1.116 -			aResults = aResults.replaceAll("[[A-Z][a-z][0-9]]*=", "?=");
   1.117 -			assertEquals("Result set is not the expected one.", eResults, aResults);
   1.118 -		}
   1.119 +
   1.120 +		
   1.121 +		actualResults.close();
   1.122 +		expectedResults.close();
   1.123 +
   1.124  	}
   1.125  	
   1.126  	public static void dropdb() throws SQLException