Strabon

changeset 151:3b8c73978d32

Add incomplete version of SimpleTests
author George Garbis <ggarbis@di.uoa.gr>
date Thu May 17 15:07:24 2012 +0300 (2012-05-17)
parents 450e41a1a7ce
children 362c84669d89
files runtime/src/test/java/eu/earthobservatory/runtime/postgis/SimpleTests.java runtime/src/test/resources/simple-tests.ntriples
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/runtime/src/test/java/eu/earthobservatory/runtime/postgis/SimpleTests.java	Thu May 17 15:07:24 2012 +0300
     1.3 @@ -0,0 +1,352 @@
     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 +
     1.9 +import java.io.IOException;
    1.10 +import java.net.URL;
    1.11 +import java.sql.Date;
    1.12 +import java.sql.DriverManager;
    1.13 +import java.sql.ResultSet;
    1.14 +import java.sql.SQLException;
    1.15 +import java.sql.Statement;
    1.16 +import java.util.ArrayList;
    1.17 +
    1.18 +import org.junit.After;
    1.19 +import org.junit.AfterClass;
    1.20 +import org.junit.Before;
    1.21 +import org.junit.BeforeClass;
    1.22 +import org.junit.Test;
    1.23 +import org.openrdf.model.vocabulary.RDF;
    1.24 +import org.openrdf.model.vocabulary.XMLSchema;
    1.25 +import org.openrdf.query.MalformedQueryException;
    1.26 +import org.openrdf.query.QueryEvaluationException;
    1.27 +import org.openrdf.query.TupleQueryResultHandlerException;
    1.28 +import org.openrdf.repository.RepositoryException;
    1.29 +import org.openrdf.rio.RDFHandlerException;
    1.30 +import org.openrdf.rio.RDFParseException;
    1.31 +
    1.32 +import eu.earthobservatory.runtime.generaldb.InvalidDatasetFormatFault;
    1.33 +
    1.34 +/**
    1.35 + * A set of simple tests on SPARQL query functionality 
    1.36 + * 
    1.37 + * @author George Garbis
    1.38 + */
    1.39 +
    1.40 +public class SimpleTests {
    1.41 +
    1.42 +	private static Strabon strabon;
    1.43 +
    1.44 +	private static String jdbcDriver= "org.postgresql.Driver";  
    1.45 +	private static String serverName = "localhost";
    1.46 +	private static String username = "postgres";
    1.47 +	private static String password = "postgres";
    1.48 +	private static Integer port = 5432;
    1.49 +	private static java.sql.Connection conn = null;
    1.50 +	private static String databaseName = null; 
    1.51 +
    1.52 +	protected static final String 
    1.53 +		STRDF_NS = "http://strdf.di.uoa.gr/ontology#",
    1.54 +		EX_NS = "http://example.org/",
    1.55 +		NOA_NS = "http://teleios.di.uoa.gr/ontologies/noaOntology.owl#";
    1.56 +
    1.57 +
    1.58 +	protected static final String prefixes = 
    1.59 +		"PREFIX rdf: <"+RDF.NAMESPACE+"> \n" +
    1.60 +		"PREFIX strdf: <"+STRDF_NS+"> \n" +
    1.61 +		"PREFIX xsd: <"+XMLSchema.NAMESPACE+"> \n" +
    1.62 +		"PREFIX ex: <"+EX_NS+"> \n" +
    1.63 +		"PREFIX noa: <"+NOA_NS+"> \n";
    1.64 +	
    1.65 +	@BeforeClass
    1.66 +	public static void beforeClass() throws SQLException, ClassNotFoundException, RDFParseException, RepositoryException, RDFHandlerException, IOException, InvalidDatasetFormatFault
    1.67 +	{
    1.68 +		// Create database
    1.69 +		Date date = new Date(0);
    1.70 +		databaseName = "strabon-test-"+date.getTime();
    1.71 +		Class.forName(jdbcDriver);
    1.72 +		String url = "jdbc:postgresql://"+serverName+":"+port+"/template1";
    1.73 +		conn = DriverManager.getConnection(url, username, password);
    1.74 +		Statement stmt = conn.createStatement();
    1.75 +	    String sql = "CREATE DATABASE \""+databaseName+"\" WITH TEMPLATE = template_postgis";
    1.76 +	    stmt.executeUpdate(sql);
    1.77 +	    stmt.close();
    1.78 +		conn.close();
    1.79 +		
    1.80 +	    // Connect to database
    1.81 +		url = "jdbc:postgresql://"+serverName+":"+port+"/"+databaseName;
    1.82 +		System.out.println("open database");
    1.83 +		conn = DriverManager.getConnection(url, username, password);
    1.84 +	    
    1.85 +		strabon = new Strabon(databaseName,"postgres","postgres", 5432, "localhost", true);
    1.86 +		
    1.87 +		loadTestData();
    1.88 +	}
    1.89 +	
    1.90 +	@AfterClass
    1.91 +	public static void afterClass() throws SQLException
    1.92 +	{
    1.93 +		strabon.close();
    1.94 +		
    1.95 +		conn.close();
    1.96 +		String url = "jdbc:postgresql://"+serverName+":"+port+"/template1";
    1.97 +		conn = DriverManager.getConnection(url, username, password);
    1.98 +		Statement stmt = conn.createStatement();
    1.99 +		stmt.executeUpdate("DROP DATABASE \""+databaseName+"\"");
   1.100 +		conn.close();
   1.101 +	}
   1.102 +	
   1.103 +//	/**
   1.104 +//	 * @throws java.lang.Exception
   1.105 +//	 */
   1.106 +//	@Before
   1.107 +//	public void before()
   1.108 +//		throws Exception
   1.109 +//	{
   1.110 +//		
   1.111 +//	}
   1.112 +//
   1.113 +//	/**
   1.114 +//	 * @throws java.lang.Exception
   1.115 +//	 */
   1.116 +//	@After
   1.117 +//	public void after()
   1.118 +//		throws Exception
   1.119 +//	{
   1.120 +//		// Clean database
   1.121 +//		Statement stmt = conn.createStatement();
   1.122 +//		ResultSet results = stmt.executeQuery("SELECT table_name FROM information_schema.tables WHERE " +
   1.123 +//						"table_schema='public' and table_name <> 'spatial_ref_sys' " +
   1.124 +//						"and table_name <> 'geometry_columns' and " +
   1.125 +//						"table_name <> 'geography_columns' and table_name <> 'locked'");
   1.126 +//		while (results.next()) {
   1.127 +//			String table_name = results.getString("table_name");
   1.128 +//			Statement stmt2 = conn.createStatement();
   1.129 +//			stmt2.executeUpdate("DROP TABLE \""+table_name+"\"");
   1.130 +//			stmt2.close();
   1.131 +//		}
   1.132 +//			
   1.133 +//		stmt.close();
   1.134 +//	}
   1.135 +
   1.136 +	@Test
   1.137 +	public void testSimpleFilter() throws MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException, IOException, QueryEvaluationException
   1.138 +	{
   1.139 +	
   1.140 +		String query = 
   1.141 +			prefixes+
   1.142 +			"SELECT ?id "+ 
   1.143 +			"WHERE { "+
   1.144 +				"?s ex:id ?id . "+
   1.145 +				"?s ex:value ?value . "+
   1.146 +				"FILTER( ?id<2 ) . " +
   1.147 +			"}";
   1.148 +		
   1.149 +		@SuppressWarnings("unchecked")
   1.150 +		ArrayList<String> bindings = (ArrayList<String>) strabon.query(query,strabon.getSailRepoConnection());
   1.151 +		
   1.152 +		assertEquals(1, bindings.size());
   1.153 +		assertTrue(-1<bindings.indexOf("[id=\"1\"^^<http://www.w3.org/2001/XMLSchema#int>]"));
   1.154 +	}
   1.155 +	
   1.156 +	@Test
   1.157 +	public void testMatchLiteralWithWKTType() throws MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException, IOException, QueryEvaluationException
   1.158 +	{
   1.159 +		String query = 
   1.160 +			prefixes+
   1.161 +			"SELECT ?s2 "+
   1.162 +			"WHERE { "+
   1.163 +			" ?s2 noa:hasGeometry \"POINT(1 0)\"^^strdf:WKT . "+
   1.164 +			"}";
   1.165 +		
   1.166 +		ArrayList<String> bindings = (ArrayList<String>) strabon.query(query,strabon.getSailRepoConnection());
   1.167 +
   1.168 +		assertEquals(1, bindings.size());
   1.169 +		assertTrue(-1<bindings.indexOf("[s2=http://example.org/item1]"));
   1.170 +	}
   1.171 +	
   1.172 +//	@Test
   1.173 +//	public void testGroupConcatDistinct() {
   1.174 +//		StringBuilder query = new StringBuilder();
   1.175 +//		query.append(getNamespaceDeclarations());
   1.176 +//		query.append("SELECT (GROUP_CONCAT(DISTINCT ?l) AS ?concat)");
   1.177 +//		query.append("WHERE { ex:groupconcat-test ?p ?l . }");
   1.178 +//
   1.179 +//		TupleQuery tq = null;
   1.180 +//		try {
   1.181 +//			tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString());
   1.182 +//		}
   1.183 +//		catch (RepositoryException e) {
   1.184 +//			e.printStackTrace();
   1.185 +//			fail(e.getMessage());
   1.186 +//		}
   1.187 +//
   1.188 +//		catch (MalformedQueryException e) {
   1.189 +//			e.printStackTrace();
   1.190 +//			fail(e.getMessage());
   1.191 +//		}
   1.192 +//
   1.193 +//		try {
   1.194 +//			TupleQueryResult result = tq.evaluate();
   1.195 +//			assertNotNull(result);
   1.196 +//
   1.197 +//			while (result.hasNext()) {
   1.198 +//				BindingSet bs = result.next();
   1.199 +//				assertNotNull(bs);
   1.200 +//
   1.201 +//				Value concat = bs.getValue("concat");
   1.202 +//
   1.203 +//				assertTrue(concat instanceof Literal);
   1.204 +//
   1.205 +//				String lexValue = ((Literal)concat).getLabel();
   1.206 +//
   1.207 +//				int occ = countCharOccurrences(lexValue, 'a');
   1.208 +//				assertEquals(1, occ);
   1.209 +//				occ = countCharOccurrences(lexValue, 'b');
   1.210 +//				assertEquals(1, occ);
   1.211 +//				occ = countCharOccurrences(lexValue, 'c');
   1.212 +//				assertEquals(1, occ);
   1.213 +//				occ = countCharOccurrences(lexValue, 'd');
   1.214 +//				assertEquals(1, occ);
   1.215 +//			}
   1.216 +//			result.close();
   1.217 +//		}
   1.218 +//		catch (QueryEvaluationException e) {
   1.219 +//			e.printStackTrace();
   1.220 +//			fail(e.getMessage());
   1.221 +//		}
   1.222 +//
   1.223 +//	}
   1.224 +//	
   1.225 +//	@Test 
   1.226 +//	public void testPropertyPathInTree() {
   1.227 +//		StringBuilder query = new StringBuilder();
   1.228 +//		query.append(getNamespaceDeclarations());
   1.229 +//		query.append(" SELECT ?node ?name ");
   1.230 +//		query.append(" FROM ex:tree-graph ");
   1.231 +//		query.append(" WHERE { ?node ex:hasParent+ ex:b . ?node ex:name ?name . }");
   1.232 +//
   1.233 +//		TupleQuery tq = null;
   1.234 +//		try {
   1.235 +//			tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString());
   1.236 +//		}
   1.237 +//		catch (RepositoryException e) {
   1.238 +//			e.printStackTrace();
   1.239 +//			fail(e.getMessage());
   1.240 +//		}
   1.241 +//		catch (MalformedQueryException e) {
   1.242 +//			e.printStackTrace();
   1.243 +//			fail(e.getMessage());
   1.244 +//		}
   1.245 +//
   1.246 +//		try {
   1.247 +//			TupleQueryResult result = tq.evaluate();
   1.248 +//			assertNotNull(result);
   1.249 +//
   1.250 +//			while (result.hasNext()) {
   1.251 +//				BindingSet bs = result.next();
   1.252 +//				assertNotNull(bs);
   1.253 +//				
   1.254 +//				System.out.println(bs);
   1.255 +//				
   1.256 +//			}
   1.257 +//			result.close();
   1.258 +//		}
   1.259 +//		catch (QueryEvaluationException e) {
   1.260 +//			e.printStackTrace();
   1.261 +//			fail(e.getMessage());
   1.262 +//		}
   1.263 +//				
   1.264 +//	}
   1.265 +//
   1.266 +//	@Test
   1.267 +//	public void testGroupConcatNonDistinct() {
   1.268 +//		StringBuilder query = new StringBuilder();
   1.269 +//		query.append(getNamespaceDeclarations());
   1.270 +//		query.append("SELECT (GROUP_CONCAT(?l) AS ?concat)");
   1.271 +//		query.append("WHERE { ex:groupconcat-test ?p ?l . }");
   1.272 +//
   1.273 +//		TupleQuery tq = null;
   1.274 +//		try {
   1.275 +//			tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString());
   1.276 +//		}
   1.277 +//		catch (RepositoryException e) {
   1.278 +//			e.printStackTrace();
   1.279 +//			fail(e.getMessage());
   1.280 +//		}
   1.281 +//
   1.282 +//		catch (MalformedQueryException e) {
   1.283 +//			e.printStackTrace();
   1.284 +//			fail(e.getMessage());
   1.285 +//		}
   1.286 +//
   1.287 +//		try {
   1.288 +//			TupleQueryResult result = tq.evaluate();
   1.289 +//			assertNotNull(result);
   1.290 +//
   1.291 +//			while (result.hasNext()) {
   1.292 +//				BindingSet bs = result.next();
   1.293 +//				assertNotNull(bs);
   1.294 +//
   1.295 +//				Value concat = bs.getValue("concat");
   1.296 +//
   1.297 +//				assertTrue(concat instanceof Literal);
   1.298 +//
   1.299 +//				String lexValue = ((Literal)concat).getLabel();
   1.300 +//
   1.301 +//				int occ = countCharOccurrences(lexValue, 'a');
   1.302 +//				assertEquals(1, occ);
   1.303 +//				occ = countCharOccurrences(lexValue, 'b');
   1.304 +//				assertEquals(2, occ);
   1.305 +//				occ = countCharOccurrences(lexValue, 'c');
   1.306 +//				assertEquals(2, occ);
   1.307 +//				occ = countCharOccurrences(lexValue, 'd');
   1.308 +//				assertEquals(1, occ);
   1.309 +//			}
   1.310 +//			result.close();
   1.311 +//		}
   1.312 +//		catch (QueryEvaluationException e) {
   1.313 +//			e.printStackTrace();
   1.314 +//			fail(e.getMessage());
   1.315 +//		}
   1.316 +//
   1.317 +//	}
   1.318 +//
   1.319 +//	private int countCharOccurrences(String string, char ch) {
   1.320 +//		int count = 0;
   1.321 +//		for (int i = 0; i < string.length(); i++) {
   1.322 +//			if (string.charAt(i) == ch) {
   1.323 +//				count++;
   1.324 +//			}
   1.325 +//		}
   1.326 +//		return count;
   1.327 +//	}
   1.328 +//
   1.329 +//	/**
   1.330 +//	 * Get a set of useful namespace prefix declarations.
   1.331 +//	 * 
   1.332 +//	 * @return namespace prefix declarations for rdf, rdfs, dc, foaf and ex.
   1.333 +//	 */
   1.334 +//	protected String getNamespaceDeclarations() {
   1.335 +//		StringBuilder declarations = new StringBuilder();
   1.336 +//		declarations.append("PREFIX rdf: <" + RDF.NAMESPACE + "> \n");
   1.337 +//		declarations.append("PREFIX rdfs: <" + RDFS.NAMESPACE + "> \n");
   1.338 +//		declarations.append("PREFIX dc: <" + DC.NAMESPACE + "> \n");
   1.339 +//		declarations.append("PREFIX foaf: <" + FOAF.NAMESPACE + "> \n");
   1.340 +//		declarations.append("PREFIX ex: <" + EX_NS + "> \n");
   1.341 +//		declarations.append("\n");
   1.342 +//
   1.343 +//		return declarations.toString();
   1.344 +//	}
   1.345 +//
   1.346 +//	protected abstract Repository newRepository()
   1.347 +//		throws Exception;
   1.348 +
   1.349 +	protected static void loadTestData()
   1.350 +		throws RDFParseException, RepositoryException, IOException, RDFHandlerException, InvalidDatasetFormatFault
   1.351 +	{
   1.352 +		URL src = SimpleTests.class.getResource("/simple-tests.ntriples");
   1.353 +		strabon.storeInRepo(src, "NTRIPLES");
   1.354 +	}
   1.355 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/runtime/src/test/resources/simple-tests.ntriples	Thu May 17 15:07:24 2012 +0300
     2.3 @@ -0,0 +1,8 @@
     2.4 +<http://example.org/item1> <http://example.org/id> "1"^^<http://www.w3.org/2001/XMLSchema#int>.
     2.5 +<http://example.org/item2> <http://example.org/id> "2"^^<http://www.w3.org/2001/XMLSchema#int>.
     2.6 +<http://example.org/item3> <http://example.org/id> "3"^^<http://www.w3.org/2001/XMLSchema#int>.
     2.7 +<http://example.org/item1> <http://example.org/value> "10"^^<http://www.w3.org/2001/XMLSchema#int>.
     2.8 +<http://example.org/item2> <http://example.org/value> "20"^^<http://www.w3.org/2001/XMLSchema#int>.
     2.9 +<http://example.org/item1> <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#hasGeometry> "POINT(1 0)"^^<http://strdf.di.uoa.gr/ontology#WKT>.
    2.10 +<http://example.org/item2> <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#hasGeometry> "POINT(2 0)"^^<http://strdf.di.uoa.gr/ontology#WKT>.
    2.11 +<http://example.org/item3> <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#hasGeometry> "POINT(3 0)"^^<http://strdf.di.uoa.gr/ontology#WKT>.