clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject svn commit: r1478708 - in /clerezza/trunk: rdf.core/ rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/ rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/ rdf.core/src/test/java/org/apache/clerezza/rdf/core/access/ rdf.jena.sparql/src...
Date Fri, 03 May 2013 10:08:54 GMT
Author: misl
Date: Fri May  3 10:08:54 2013
New Revision: 1478708

URL: http://svn.apache.org/r1478708
Log:
CLEREZZA-468: Added support for string based queries, but still using the QueryEngine instead
of QueryableTcProvider.

Added:
    clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/BaseTdbTcProvider.java
Modified:
    clerezza/trunk/rdf.core/pom.xml
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/QueryableTcProvider.java
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/QueryEngine.java
    clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/access/TcManagerTest.java
    clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
    clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
    clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/TdbTcProvider.java

Modified: clerezza/trunk/rdf.core/pom.xml
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/pom.xml?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/pom.xml (original)
+++ clerezza/trunk/rdf.core/pom.xml Fri May  3 10:08:54 2013
@@ -54,6 +54,12 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+		    <dependency>
+			      <groupId>org.mockito</groupId>
+			      <artifactId>mockito-core</artifactId>
+			      <version>1.9.5</version>
+			      <scope>test</scope>
+		    </dependency>
     </dependencies>
     <build>
         <plugins>

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/QueryableTcProvider.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/QueryableTcProvider.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/QueryableTcProvider.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/QueryableTcProvider.java
Fri May  3 10:08:54 2013
@@ -19,6 +19,7 @@
 package org.apache.clerezza.rdf.core.access;
 
 import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.sparql.query.Query;
 
 /**
  * Extends the TcProvider interface for providers that support sparql queries.
@@ -37,4 +38,17 @@ public interface QueryableTcProvider ext
      * @return the resulting ResultSet, Graph or Boolean value
      */
     public Object executeSparqlQuery(String query, TripleCollection defaultGraph);
+
+    /**
+     * Executes any sparql query. The type of the result object will vary
+     * depending on the type of the query.
+     * 
+     * @param query
+     *            the sparql query to execute
+     * @param defaultGraph
+     *            the default graph against which to execute the query if not
+     *            FROM clause is present
+     * @return the resulting ResultSet, Graph or Boolean value
+     */
+    public Object executeSparqlQuery(Query query, TripleCollection defaultGraph);
 }

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
Fri May  3 10:08:54 2013
@@ -18,9 +18,6 @@
  */
 package org.apache.clerezza.rdf.core.access;
 
-import org.apache.clerezza.rdf.core.impl.WriteBlockedMGraph;
-import org.apache.clerezza.rdf.core.impl.WriteBlockedTripleCollection;
-
 import java.security.AccessControlException;
 import java.util.Collection;
 import java.util.Collections;
@@ -33,28 +30,30 @@ import java.util.Properties;
 import java.util.ServiceLoader;
 import java.util.Set;
 
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.component.ComponentContext;
 import org.apache.clerezza.rdf.core.Graph;
 import org.apache.clerezza.rdf.core.MGraph;
 import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.core.access.security.TcAccessController;
+import org.apache.clerezza.rdf.core.impl.WriteBlockedMGraph;
+import org.apache.clerezza.rdf.core.impl.WriteBlockedTripleCollection;
+import org.apache.clerezza.rdf.core.sparql.NoQueryEngineException;
+import org.apache.clerezza.rdf.core.sparql.QueryEngine;
+import org.apache.clerezza.rdf.core.sparql.ResultSet;
 import org.apache.clerezza.rdf.core.sparql.query.AskQuery;
 import org.apache.clerezza.rdf.core.sparql.query.ConstructQuery;
 import org.apache.clerezza.rdf.core.sparql.query.DescribeQuery;
-import org.apache.clerezza.rdf.core.sparql.NoQueryEngineException;
 import org.apache.clerezza.rdf.core.sparql.query.Query;
-import org.apache.clerezza.rdf.core.sparql.QueryEngine;
-import org.apache.clerezza.rdf.core.sparql.ResultSet;
 import org.apache.clerezza.rdf.core.sparql.query.SelectQuery;
+import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleStringQuerySerializer;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.felix.scr.annotations.Service;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
 
 /**
  * This class implements <code>TcManager</code>, delegating the actual
@@ -277,6 +276,26 @@ public class TcManager extends TcProvide
      *            FROM clause is present
      * @return the resulting ResultSet, Graph or Boolean value
      */
+    public Object executeSparqlQuery(String query, TripleCollection defaultGraph) {
+        final QueryEngine queryEngine = this.queryEngine;
+        if (queryEngine != null) {
+            return queryEngine.execute(this, defaultGraph, query);
+        } else {
+            throw new NoQueryEngineException();
+        }
+    }
+
+    /**
+     * Executes any sparql query. The type of the result object will vary
+     * depending on the type of the query.
+     * 
+     * @param query
+     *            the sparql query to execute
+     * @param defaultGraph
+     *            the default graph against which to execute the query if not
+     *            FROM clause is present
+     * @return the resulting ResultSet, Graph or Boolean value
+     */
     public Object executeSparqlQuery(Query query, TripleCollection defaultGraph) {
         final QueryEngine queryEngine = this.queryEngine;
         if (queryEngine != null) {
@@ -298,12 +317,7 @@ public class TcManager extends TcProvide
      */
     public ResultSet executeSparqlQuery(SelectQuery query,
             TripleCollection defaultGraph) {
-        final QueryEngine queryEngine = this.queryEngine;
-        if (queryEngine != null) {
-            return (ResultSet) queryEngine.execute(this, defaultGraph, query);
-        } else {
-            throw new NoQueryEngineException();
-        }
+        return (ResultSet) executeSparqlQuery((Query)query, defaultGraph);
     }
 
     /**
@@ -318,12 +332,7 @@ public class TcManager extends TcProvide
      */
     public boolean executeSparqlQuery(AskQuery query,
             TripleCollection defaultGraph) {
-        final QueryEngine queryEngine = this.queryEngine;
-        if (queryEngine != null) {
-            return (Boolean) queryEngine.execute(this, defaultGraph, query);
-        } else {
-            throw new NoQueryEngineException();
-        }
+        return (Boolean) executeSparqlQuery((Query)query, defaultGraph);
     }
 
     /**
@@ -338,12 +347,7 @@ public class TcManager extends TcProvide
      */
     public Graph executeSparqlQuery(DescribeQuery query,
             TripleCollection defaultGraph) {
-        final QueryEngine queryEngine = this.queryEngine;
-        if (queryEngine != null) {
-            return (Graph) queryEngine.execute(this, defaultGraph, query);
-        } else {
-            throw new NoQueryEngineException();
-        }
+        return (Graph) executeSparqlQuery((Query)query, defaultGraph);
     }
 
     /**
@@ -358,12 +362,7 @@ public class TcManager extends TcProvide
      */
     public Graph executeSparqlQuery(ConstructQuery query,
             TripleCollection defaultGraph) {
-        final QueryEngine queryEngine = this.queryEngine;
-        if (queryEngine != null) {
-            return (Graph) queryEngine.execute(this, defaultGraph, query);
-        } else {
-            throw new NoQueryEngineException();
-        }
+        return (Graph) executeSparqlQuery((Query)query, defaultGraph);
     }
 
     /**

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/QueryEngine.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/QueryEngine.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/QueryEngine.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/QueryEngine.java
Fri May  3 10:08:54 2013
@@ -18,18 +18,46 @@
  */
 package org.apache.clerezza.rdf.core.sparql;
 
-import org.apache.clerezza.rdf.core.sparql.query.Query;
 import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.access.TcManager;
+import org.apache.clerezza.rdf.core.sparql.query.Query;
 
 /**
  * A QueryEngine can process SPARQL queries against an arbitrary set of graphs.
- *
+ * 
  * @author rbn
  */
 public interface QueryEngine {
 
-    public Object execute(TcManager tcManager, TripleCollection dafaultGraph, Query query);
-
+	/**
+	 * Executes any sparql query. The type of the result object will vary
+	 * depending on the type of the query.
+	 * 
+	 * @param tcManager
+	 *            where the query originates.
+	 * @param defaultGraph
+	 *            the default graph against which to execute the query if no
+	 *            FROM clause is present
+	 * @param query
+	 *            Query object to be executed
+	 * @return the resulting ResultSet, Graph or Boolean value
+	 */
+	public Object execute(TcManager tcManager, TripleCollection defaultGraph,
+			Query query);
 
+	/**
+	 * Executes any sparql query. The type of the result object will vary
+	 * depending on the type of the query.
+	 * 
+	 * @param tcManager
+	 *            where the query originates.
+	 * @param defaultGraph
+	 *            the default graph against which to execute the query if no
+	 *            FROM clause is present
+	 * @param query
+	 *            string to be executed.
+	 * @return the resulting ResultSet, Graph or Boolean value
+	 */
+	public Object execute(TcManager tcManager, TripleCollection defaultGraph,
+			String query);
 }

Modified: clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/access/TcManagerTest.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/access/TcManagerTest.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/access/TcManagerTest.java
(original)
+++ clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/access/TcManagerTest.java
Fri May  3 10:08:54 2013
@@ -18,10 +18,12 @@
  */
 package org.apache.clerezza.rdf.core.access;
 
+import java.lang.reflect.Field;
 import java.util.Iterator;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.apache.clerezza.rdf.core.Graph;
 import org.apache.clerezza.rdf.core.Triple;
 import org.apache.clerezza.rdf.core.TripleCollection;
@@ -30,90 +32,271 @@ import org.apache.clerezza.rdf.core.acce
 import org.apache.clerezza.rdf.core.access.providers.WeightedA1;
 import org.apache.clerezza.rdf.core.access.providers.WeightedAHeavy;
 import org.apache.clerezza.rdf.core.access.providers.WeightedBlight;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
 import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.core.sparql.NoQueryEngineException;
+import org.apache.clerezza.rdf.core.sparql.QueryEngine;
+import org.apache.clerezza.rdf.core.sparql.query.AskQuery;
+import org.apache.clerezza.rdf.core.sparql.query.ConstructQuery;
+import org.apache.clerezza.rdf.core.sparql.query.DescribeQuery;
+import org.apache.clerezza.rdf.core.sparql.query.Query;
+import org.apache.clerezza.rdf.core.sparql.query.SelectQuery;
+
 import static org.junit.Assert.*;
 
 /**
- *
+ * 
  * @author reto
  */
 public class TcManagerTest {
 
-    public static UriRef uriRefAHeavy = new UriRef("http://example.org/aHeavy");
-    public static UriRef uriRefB = new UriRef("http://example.org/b");
-    ;
-    public static final UriRef uriRefA = new UriRef("http://example.org/a");
-    public static final UriRef uriRefA1 = new UriRef("http://example.org/a1");
-    private TcManager graphAccess;
-    private final WeightedA weightedA = new WeightedA();
-    private final WeightedA1 weightedA1 = new WeightedA1();
-    private WeightedTcProvider weightedBlight = new WeightedBlight();
-
-    @Before
-    public void setUp() {
-        graphAccess = TcManager.getInstance();
-        graphAccess.bindWeightedTcProvider(weightedA);
-        graphAccess.bindWeightedTcProvider(weightedA1);
-        graphAccess.bindWeightedTcProvider(weightedBlight);
-    }
-
-    @After
-    public void tearDown() {
-        graphAccess = TcManager.getInstance();
-        graphAccess.unbindWeightedTcProvider(weightedA);
-        graphAccess.unbindWeightedTcProvider(weightedA1);
-        graphAccess.unbindWeightedTcProvider(weightedBlight);
-    }
-
-    @Test
-    public void getGraphFromA() {
-        Graph graphA = graphAccess.getGraph(uriRefA);
-        Iterator<Triple> iterator = graphA.iterator();
-        assertEquals(new TripleImpl(uriRefA, uriRefA, uriRefA), iterator.next());
-        assertFalse(iterator.hasNext());
-        TripleCollection triplesA = graphAccess.getTriples(uriRefA);
-        iterator = triplesA.iterator();
-        assertEquals(new TripleImpl(uriRefA, uriRefA, uriRefA), iterator.next());
-        assertFalse(iterator.hasNext());
-    }
-
-    @Test
-    public void getGraphFromB() {
-        Graph graphA = graphAccess.getGraph(uriRefB);
-        Iterator<Triple> iterator = graphA.iterator();
-        assertEquals(new TripleImpl(uriRefB, uriRefB, uriRefB), iterator.next());
-        assertFalse(iterator.hasNext());
-        TripleCollection triplesA = graphAccess.getTriples(uriRefB);
-        iterator = triplesA.iterator();
-        assertEquals(new TripleImpl(uriRefB, uriRefB, uriRefB), iterator.next());
-        assertFalse(iterator.hasNext());
-    }
-
-    @Test
-    public void getGraphFromAAfterUnbinding() {
-        graphAccess.unbindWeightedTcProvider(weightedA);
-        Graph graphA = graphAccess.getGraph(uriRefA);
-        Iterator<Triple> iterator = graphA.iterator();
-        assertEquals(new TripleImpl(uriRefA1, uriRefA1, uriRefA1), iterator.next());
-        assertFalse(iterator.hasNext());
-        TripleCollection triplesA = graphAccess.getTriples(uriRefA);
-        iterator = triplesA.iterator();
-        assertEquals(new TripleImpl(uriRefA1, uriRefA1, uriRefA1), iterator.next());
-        assertFalse(iterator.hasNext());
-    }
-
-    @Test
-    public void getGraphFromAWithHeavy() {
-        final WeightedAHeavy weightedAHeavy = new WeightedAHeavy();
-        graphAccess.bindWeightedTcProvider(weightedAHeavy);
-        Graph graphA = graphAccess.getGraph(uriRefA);
-        Iterator<Triple> iterator = graphA.iterator();
-        assertEquals(new TripleImpl(uriRefAHeavy, uriRefAHeavy, uriRefAHeavy), iterator.next());
-        assertFalse(iterator.hasNext());
-        TripleCollection triplesA = graphAccess.getTriples(uriRefA);
-        iterator = triplesA.iterator();
-        assertEquals(new TripleImpl(uriRefAHeavy, uriRefAHeavy, uriRefAHeavy), iterator.next());
-        assertFalse(iterator.hasNext());
-        graphAccess.unbindWeightedTcProvider(weightedAHeavy);
-    }
+	public static UriRef uriRefAHeavy = new UriRef("http://example.org/aHeavy");
+	public static UriRef uriRefB = new UriRef("http://example.org/b");;
+	public static final UriRef uriRefA = new UriRef("http://example.org/a");
+	public static final UriRef uriRefA1 = new UriRef("http://example.org/a1");
+	private TcManager graphAccess;
+	private QueryEngine queryEngine;
+	private final WeightedA weightedA = new WeightedA();
+	private final WeightedA1 weightedA1 = new WeightedA1();
+	private WeightedTcProvider weightedBlight = new WeightedBlight();
+
+	@Before
+	public void setUp() {
+		graphAccess = TcManager.getInstance();
+		graphAccess.bindWeightedTcProvider(weightedA);
+		graphAccess.bindWeightedTcProvider(weightedA1);
+		graphAccess.bindWeightedTcProvider(weightedBlight);
+
+		queryEngine = Mockito.mock(QueryEngine.class);
+	}
+
+	@After
+	public void tearDown() {
+		graphAccess = TcManager.getInstance();
+		graphAccess.unbindWeightedTcProvider(weightedA);
+		graphAccess.unbindWeightedTcProvider(weightedA1);
+		graphAccess.unbindWeightedTcProvider(weightedBlight);
+
+		queryEngine = null;
+	}
+
+	@Test
+	public void getGraphFromA() {
+		Graph graphA = graphAccess.getGraph(uriRefA);
+		Iterator<Triple> iterator = graphA.iterator();
+		assertEquals(new TripleImpl(uriRefA, uriRefA, uriRefA), iterator.next());
+		assertFalse(iterator.hasNext());
+		TripleCollection triplesA = graphAccess.getTriples(uriRefA);
+		iterator = triplesA.iterator();
+		assertEquals(new TripleImpl(uriRefA, uriRefA, uriRefA), iterator.next());
+		assertFalse(iterator.hasNext());
+	}
+
+	@Test
+	public void getGraphFromB() {
+		Graph graphA = graphAccess.getGraph(uriRefB);
+		Iterator<Triple> iterator = graphA.iterator();
+		assertEquals(new TripleImpl(uriRefB, uriRefB, uriRefB), iterator.next());
+		assertFalse(iterator.hasNext());
+		TripleCollection triplesA = graphAccess.getTriples(uriRefB);
+		iterator = triplesA.iterator();
+		assertEquals(new TripleImpl(uriRefB, uriRefB, uriRefB), iterator.next());
+		assertFalse(iterator.hasNext());
+	}
+
+	@Test
+	public void getGraphFromAAfterUnbinding() {
+		graphAccess.unbindWeightedTcProvider(weightedA);
+		Graph graphA = graphAccess.getGraph(uriRefA);
+		Iterator<Triple> iterator = graphA.iterator();
+		assertEquals(new TripleImpl(uriRefA1, uriRefA1, uriRefA1),
+				iterator.next());
+		assertFalse(iterator.hasNext());
+		TripleCollection triplesA = graphAccess.getTriples(uriRefA);
+		iterator = triplesA.iterator();
+		assertEquals(new TripleImpl(uriRefA1, uriRefA1, uriRefA1),
+				iterator.next());
+		assertFalse(iterator.hasNext());
+	}
+
+	@Test
+	public void getGraphFromAWithHeavy() {
+		final WeightedAHeavy weightedAHeavy = new WeightedAHeavy();
+		graphAccess.bindWeightedTcProvider(weightedAHeavy);
+		Graph graphA = graphAccess.getGraph(uriRefA);
+		Iterator<Triple> iterator = graphA.iterator();
+		assertEquals(new TripleImpl(uriRefAHeavy, uriRefAHeavy, uriRefAHeavy),
+				iterator.next());
+		assertFalse(iterator.hasNext());
+		TripleCollection triplesA = graphAccess.getTriples(uriRefA);
+		iterator = triplesA.iterator();
+		assertEquals(new TripleImpl(uriRefAHeavy, uriRefAHeavy, uriRefAHeavy),
+				iterator.next());
+		assertFalse(iterator.hasNext());
+		graphAccess.unbindWeightedTcProvider(weightedAHeavy);
+	}
+
+	@Test(expected = NoQueryEngineException.class)
+	public void executeSparqlQueryNoEngineWithString() throws Exception {
+		// Prepare
+		injectQueryEngine(null);
+
+		// Execute
+		graphAccess.executeSparqlQuery("", new SimpleMGraph());
+	}
+
+	@Test(expected = NoQueryEngineException.class)
+	public void executeSparqlQueryNoEngineWithQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(null);
+
+		// Execute
+		graphAccess.executeSparqlQuery((Query) null, new SimpleMGraph());
+	}
+
+	@Test(expected = NoQueryEngineException.class)
+	public void executeSparqlQueryNoEngineWithSelectQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(null);
+
+		// Execute
+		graphAccess.executeSparqlQuery((SelectQuery) null, new SimpleMGraph());
+	}
+
+	@Test(expected = NoQueryEngineException.class)
+	public void executeSparqlQueryNoEngineWithAskQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(null);
+
+		// Execute
+		graphAccess.executeSparqlQuery((AskQuery) null, new SimpleMGraph());
+	}
+
+	@Test(expected = NoQueryEngineException.class)
+	public void executeSparqlQueryNoEngineWithDescribeQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(null);
+
+		// Execute
+		graphAccess
+				.executeSparqlQuery((DescribeQuery) null, new SimpleMGraph());
+	}
+
+	@Test(expected = NoQueryEngineException.class)
+	public void executeSparqlQueryNoEngineWithConstructQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(null);
+
+		// Execute
+		graphAccess.executeSparqlQuery((ConstructQuery) null,
+				new SimpleMGraph());
+	}
+
+	@Test
+	public void executeSparqlQueryWithEngineWithString() throws Exception {
+		// Prepare
+		injectQueryEngine(queryEngine);
+		TripleCollection tripleCollection = new SimpleMGraph();
+
+		// Execute
+		graphAccess.executeSparqlQuery("", tripleCollection);
+
+		// Verify
+		Mockito.verify(queryEngine).execute(graphAccess, tripleCollection, "");
+		Mockito.verify(queryEngine, Mockito.never()).execute(
+				(TcManager) Mockito.anyObject(),
+				(TripleCollection) Mockito.anyObject(),
+				(Query) Mockito.anyObject());
+	}
+
+	@Test
+	public void executeSparqlQueryWithEngineWithSelectQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(queryEngine);
+		TripleCollection tripleCollection = new SimpleMGraph();
+		SelectQuery query = Mockito.mock(SelectQuery.class);
+
+		// Execute
+		graphAccess.executeSparqlQuery(query, tripleCollection);
+
+		// Verify
+		Mockito.verify(queryEngine).execute(graphAccess, tripleCollection,
+				(Query) query);
+		Mockito.verify(queryEngine, Mockito.never()).execute(
+				(TcManager) Mockito.anyObject(),
+				(TripleCollection) Mockito.anyObject(), Mockito.anyString());
+	}
+
+	@Test
+	public void executeSparqlQueryWithEngineWithAskQuery() throws Exception {
+		// Prepare
+		injectQueryEngine(queryEngine);
+		TripleCollection tripleCollection = new SimpleMGraph();
+		AskQuery query = Mockito.mock(AskQuery.class);
+
+		Mockito.when(
+				queryEngine.execute((TcManager) Mockito.anyObject(),
+						(TripleCollection) Mockito.anyObject(),
+						(Query) Mockito.anyObject())).thenReturn(Boolean.TRUE);
+
+		// Execute
+		graphAccess.executeSparqlQuery(query, tripleCollection);
+
+		// Verify
+		Mockito.verify(queryEngine).execute(graphAccess, tripleCollection,
+				(Query) query);
+		Mockito.verify(queryEngine, Mockito.never()).execute(
+				(TcManager) Mockito.anyObject(),
+				(TripleCollection) Mockito.anyObject(), Mockito.anyString());
+	}
+
+	@Test
+	public void executeSparqlQueryWithEngineWithDescribeQuery()
+			throws Exception {
+		// Prepare
+		injectQueryEngine(queryEngine);
+		TripleCollection tripleCollection = new SimpleMGraph();
+		DescribeQuery query = Mockito.mock(DescribeQuery.class);
+
+		// Execute
+		graphAccess.executeSparqlQuery(query, tripleCollection);
+
+		// Verify
+		Mockito.verify(queryEngine).execute(graphAccess, tripleCollection,
+				(Query) query);
+		Mockito.verify(queryEngine, Mockito.never()).execute(
+				(TcManager) Mockito.anyObject(),
+				(TripleCollection) Mockito.anyObject(), Mockito.anyString());
+	}
+
+	@Test
+	public void executeSparqlQueryWithEngineWithConstructQuery()
+			throws Exception {
+		// Prepare
+		injectQueryEngine(queryEngine);
+		TripleCollection tripleCollection = new SimpleMGraph();
+		ConstructQuery query = Mockito.mock(ConstructQuery.class);
+
+		// Execute
+		graphAccess.executeSparqlQuery(query, tripleCollection);
+
+		// Verify
+		Mockito.verify(queryEngine).execute(graphAccess, tripleCollection,
+				(Query) query);
+		Mockito.verify(queryEngine, Mockito.never()).execute(
+				(TcManager) Mockito.anyObject(),
+				(TripleCollection) Mockito.anyObject(), Mockito.anyString());
+	}
+
+	// ------------------------------------------------------------------------
+	// Implementing QueryableTcProvider
+	// ------------------------------------------------------------------------
+
+	private void injectQueryEngine(QueryEngine engine)
+			throws NoSuchFieldException, IllegalAccessException {
+		Field queryEngineField = TcManager.class
+				.getDeclaredField("queryEngine");
+		queryEngineField.setAccessible(true);
+		queryEngineField.set(graphAccess, engine);
+	}
 }
\ No newline at end of file

Modified: clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
(original)
+++ clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
Fri May  3 10:08:54 2013
@@ -18,19 +18,23 @@
  */
 package org.apache.clerezza.rdf.jena.sparql;
 
-import com.hp.hpl.jena.query.Dataset;
-import com.hp.hpl.jena.query.QueryExecException;
-import com.hp.hpl.jena.query.QueryExecution;
-import com.hp.hpl.jena.query.QueryExecutionFactory;
-import com.hp.hpl.jena.query.QueryFactory;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+
 import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.access.TcManager;
 import org.apache.clerezza.rdf.core.sparql.QueryEngine;
+import org.apache.clerezza.rdf.core.sparql.query.AskQuery;
+import org.apache.clerezza.rdf.core.sparql.query.ConstructQuery;
+import org.apache.clerezza.rdf.core.sparql.query.DescribeQuery;
 import org.apache.clerezza.rdf.core.sparql.query.Query;
 import org.apache.clerezza.rdf.jena.storage.JenaGraphAdaptor;
 
+import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.query.QueryExecException;
+import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.query.QueryExecutionFactory;
+import com.hp.hpl.jena.query.QueryFactory;
 
 /**
  * 
@@ -41,48 +45,89 @@ import org.apache.clerezza.rdf.jena.stor
  */
 public class JenaSparqlEngine implements QueryEngine {
 
-    @Override
-    public Object execute(TcManager tcManager, TripleCollection defaultGraph,
-            final Query query) {
-        final Dataset dataset = new TcDataset(tcManager, defaultGraph);
-
-        // Missing permission (java.lang.RuntimePermission getClassLoader)
-        // when calling QueryFactory.create causes ExceptionInInitializerError
-        // to be thrown.
-        // QueryExecutionFactory.create requires
-        // (java.io.FilePermission [etc/]location-mapping.* read)
-        // Thus, they are placed within doPrivileged
-        QueryExecution qexec = AccessController.doPrivileged (
-                new PrivilegedAction<QueryExecution>() {
-
-            @Override
-            public QueryExecution run() {
-                com.hp.hpl.jena.query.Query jenaQuery =
-                        QueryFactory.create(query.toString());
-                return QueryExecutionFactory.create(jenaQuery, dataset);
-            }
-        });
-
-        try {
-            try {
-                return new ResultSetWrapper(qexec.execSelect());
-            } catch (QueryExecException e) {
-                try {
-                    return Boolean.valueOf(qexec.execAsk());
-                } catch (QueryExecException e2) {
-                    try {
-                        return new JenaGraphAdaptor(
-                                qexec.execDescribe().getGraph()).getGraph();
-                    } catch (QueryExecException e3) {
-                        return new JenaGraphAdaptor(
-                                qexec.execConstruct().getGraph()).getGraph();
-                    }
-                }
-            }
-        } finally {
-            qexec.close();
-        }
-
-    }
-
+	// ------------------------------------------------------------------------
+	// Implementing QueryEngine
+	// ------------------------------------------------------------------------
+
+	@Override
+	public Object execute(TcManager tcManager, TripleCollection defaultGraph,
+			final Query query) {
+		final Dataset dataset = new TcDataset(tcManager, defaultGraph);
+
+		// Missing permission (java.lang.RuntimePermission getClassLoader)
+		// when calling QueryFactory.create causes ExceptionInInitializerError
+		// to be thrown.
+		// QueryExecutionFactory.create requires
+		// (java.io.FilePermission [etc/]location-mapping.* read)
+		// Thus, they are placed within doPrivileged
+		QueryExecution qexec = AccessController
+				.doPrivileged(new PrivilegedAction<QueryExecution>() {
+
+					@Override
+					public QueryExecution run() {
+						com.hp.hpl.jena.query.Query jenaQuery = QueryFactory
+								.create(query.toString());
+						return QueryExecutionFactory.create(jenaQuery, dataset);
+					}
+				});
+
+		try {
+			if (query instanceof AskQuery) {
+				return Boolean.valueOf(qexec.execAsk());
+			} else if (query instanceof DescribeQuery) {
+				return new JenaGraphAdaptor(qexec.execDescribe().getGraph())
+						.getGraph();
+			} else if (query instanceof ConstructQuery) {
+				return new JenaGraphAdaptor(qexec.execConstruct().getGraph())
+						.getGraph();
+			} else {
+				return new ResultSetWrapper(qexec.execSelect());
+			}
+		} finally {
+			qexec.close();
+		}
+	}
+
+	@Override
+	public Object execute(TcManager tcManager, TripleCollection defaultGraph,
+			final String query) {
+		final Dataset dataset = new TcDataset(tcManager, defaultGraph);
+
+		// Missing permission (java.lang.RuntimePermission getClassLoader)
+		// when calling QueryFactory.create causes ExceptionInInitializerError
+		// to be thrown.
+		// QueryExecutionFactory.create requires
+		// (java.io.FilePermission [etc/]location-mapping.* read)
+		// Thus, they are placed within doPrivileged
+		QueryExecution qexec = AccessController
+				.doPrivileged(new PrivilegedAction<QueryExecution>() {
+
+					@Override
+					public QueryExecution run() {
+						com.hp.hpl.jena.query.Query jenaQuery = QueryFactory
+								.create(query);
+						return QueryExecutionFactory.create(jenaQuery, dataset);
+					}
+				});
+
+		try {
+			try {
+				return new ResultSetWrapper(qexec.execSelect());
+			} catch (QueryExecException e) {
+				try {
+					return Boolean.valueOf(qexec.execAsk());
+				} catch (QueryExecException e2) {
+					try {
+						return new JenaGraphAdaptor(qexec.execDescribe()
+								.getGraph()).getGraph();
+					} catch (QueryExecException e3) {
+						return new JenaGraphAdaptor(qexec.execConstruct()
+								.getGraph()).getGraph();
+					}
+				}
+			}
+		} finally {
+			qexec.close();
+		}
+	}
 }

Modified: clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
(original)
+++ clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
Fri May  3 10:08:54 2013
@@ -39,6 +39,22 @@ import org.apache.clerezza.rdf.ontologie
 public class SimpleTest {
 
     @Test
+    public void simpleStringQuery() throws ParseException {
+        SimpleMGraph data = new SimpleMGraph();
+        final String titleValue = "SPARQL Tutorial";
+        data.add(new TripleImpl(new UriRef("http://example.org/book/book1"),
+                DC.title, new PlainLiteralImpl(titleValue)));
+        String query = "SELECT ?title WHERE" +
+                "{" +
+                "    <http://example.org/book/book1> <"+DC.title.getUnicodeString()+">
?title ." +
+                "}";
+        ResultSet resultSet = (ResultSet) TcManager.getInstance().executeSparqlQuery(
+                query, data);
+        Assert.assertEquals(titleValue, 
+                ((Literal)resultSet.next().get("title")).getLexicalForm());
+    }
+
+    @Test
     public void simpleSelectQuery() throws ParseException {
         SimpleMGraph data = new SimpleMGraph();
         final String titleValue = "SPARQL Tutorial";

Added: clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/BaseTdbTcProvider.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/BaseTdbTcProvider.java?rev=1478708&view=auto
==============================================================================
--- clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/BaseTdbTcProvider.java
(added)
+++ clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/BaseTdbTcProvider.java
Fri May  3 10:08:54 2013
@@ -0,0 +1,23 @@
+package org.apache.clerezza.rdf.jena.tdb.storage;
+
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.access.QueryableTcProvider;
+import org.apache.clerezza.rdf.core.sparql.query.Query;
+
+abstract class BaseTdbTcProvider implements QueryableTcProvider{
+
+	// ------------------------------------------------------------------------
+	// Implementing QueryableTcProvider
+	// ------------------------------------------------------------------------
+
+    @Override
+    public Object executeSparqlQuery(String query, TripleCollection defaultGraph) {
+    	// TODO Auto-generated method stub
+    	return null;
+    }
+    
+    @Override
+    public Object executeSparqlQuery(Query query, TripleCollection defaultGraph) {
+    	return executeSparqlQuery(query.toString(), defaultGraph);
+    }
+}

Modified: clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/TdbTcProvider.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/TdbTcProvider.java?rev=1478708&r1=1478707&r2=1478708&view=diff
==============================================================================
--- clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/TdbTcProvider.java
(original)
+++ clerezza/trunk/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/TdbTcProvider.java
Fri May  3 10:08:54 2013
@@ -69,7 +69,7 @@ import org.apache.felix.scr.annotations.
 @Component(metatype=true, immediate=true)
 @Service(WeightedTcProvider.class)
 @Property(name="weight", intValue=105)
-public class TdbTcProvider implements WeightedTcProvider {
+public class TdbTcProvider extends BaseTdbTcProvider implements WeightedTcProvider {
 
     static {
         //not sure what the perfomance implication of this is
@@ -94,7 +94,7 @@ public class TdbTcProvider implements We
     private final Map<File, Dataset> dir2Dataset = new HashMap<File, Dataset>();
     private static final Logger log = LoggerFactory.getLogger(TdbTcProvider.class);
     private int weight = 105;
-
+    
     class SyncThread extends Thread {
         private boolean stopRequested = false;
 



Mime
View raw message