jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cla...@apache.org
Subject [2/3] jena git commit: Added ValuesHandler and associated tests. Fixes JENA-1295
Date Sun, 26 Feb 2017 17:42:59 GMT
Added ValuesHandler and associated tests. Fixes JENA-1295

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/e8692389
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/e8692389
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/e8692389

Branch: refs/heads/master
Commit: e86923895d4b34256c71d6717c8ad7aa3ab3b055
Parents: b8df146
Author: Claude Warren <claude@apache.org>
Authored: Sun Feb 26 17:11:09 2017 +0000
Committer: Claude Warren <claude@apache.org>
Committed: Sun Feb 26 17:23:43 2017 +0000

----------------------------------------------------------------------
 .../arq/querybuilder/AbstractQueryBuilder.java  |  30 +-
 .../arq/querybuilder/clauses/ValuesClause.java  |  62 ++++
 .../arq/querybuilder/handlers/HandlerBlock.java |  23 ++
 .../querybuilder/handlers/ValuesHandler.java    | 142 +++++++++
 .../jena/arq/AbstractRegexpBasedTest.java       |   1 +
 .../querybuilder/clauses/ValuesClauseTest.java  |  93 ++++++
 .../querybuilder/clauses/WhereClauseTest.java   |   2 +-
 .../handlers/ValuesHandlerTest.java             | 319 +++++++++++++++++++
 8 files changed, 670 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
index 08dc923..fa11df9 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
@@ -17,11 +17,15 @@
  */
 package org.apache.jena.arq.querybuilder;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.jena.arq.querybuilder.clauses.PrologClause;
+import org.apache.jena.arq.querybuilder.clauses.ValuesClause;
 import org.apache.jena.arq.querybuilder.handlers.HandlerBlock;
 import org.apache.jena.arq.querybuilder.handlers.PrologHandler;
+import org.apache.jena.arq.querybuilder.handlers.ValuesHandler;
 import org.apache.jena.graph.FrontsNode ;
 import org.apache.jena.graph.Node ;
 import org.apache.jena.graph.NodeFactory ;
@@ -52,7 +56,7 @@ import org.apache.jena.sparql.util.NodeFactoryExtra ;
  *            The derived class type. Used for return types.
  */
 public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
-		implements Cloneable, PrologClause<T> {
+		implements Cloneable, PrologClause<T>, ValuesClause<T> {
 
 	// the query this builder is building
 	protected Query query;
@@ -297,6 +301,10 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 		return getHandlerBlock().getPrologHandler();
 	}
 
+	@Override
+	public ValuesHandler getValuesHandler() {
+		return getHandlerBlock().getValueHandler();
+	}
 
 	/**
 	 * Set a variable replacement. During build all instances of var in the
@@ -376,6 +384,26 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 		setBase(makeNode(base).getURI());
 		return (T) this;
 	}
+	
+	
+	@SuppressWarnings("unchecked")
+	@Override
+	public T addValueVar(Object var) {
+		getValuesHandler().addValueVar( makeVar( var ));
+		return (T) this;
+	}
+
+	@SuppressWarnings("unchecked")
+	@Override
+	public T addDataBlock(Collection<Object> values) {
+		ArrayList<Node> nodeValues = new ArrayList<Node>(values.size());
+		for (Object o : values )
+		{
+			nodeValues.add( makeNode( o ));
+		}
+		getValuesHandler().addDataBlock(nodeValues);
+		return (T) this;
+	}
 
 	@Override
 	public String toString() {

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/ValuesClause.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/ValuesClause.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/ValuesClause.java
new file mode 100644
index 0000000..b6ee5ff
--- /dev/null
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/ValuesClause.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jena.arq.querybuilder.clauses;
+
+import java.util.Collection;
+
+import org.apache.jena.arq.querybuilder.AbstractQueryBuilder;
+import org.apache.jena.arq.querybuilder.handlers.ValuesHandler;
+
+/**
+ * Interface that defines the ValueClause as per
+ * https://www.w3.org/TR/sparql11-query/#rValuesClause
+ * 
+ * @param <T>
+ *            The Builder type that the clause is part of.
+ */
+public interface ValuesClause<T extends AbstractQueryBuilder<T>> {
+	
+	/**
+	 * Get the value handler for this clause. 
+	 * @return The ValueHandler this clause is using.
+	 */
+	public ValuesHandler getValuesHandler();
+	
+	/**
+	 * Add a variable to the value statement.
+	 * 
+	 * A variable may only be added once. Attempting to add the same variable
+	 * multiple times will be silently ignored.
+	 * 
+	 * @param var
+	 *            The variable to add.
+	 * @return The builder for chaining.
+	 */
+	public T addValueVar(Object var);
+
+	/**
+	 * Add the values for the variables.  There must be one value for each value var.
+	 * 
+	 * @param values
+	 *            the collection of values to add.
+	 * @return The builder for chaining.
+	 */
+	public T addDataBlock(Collection<Object> values);
+
+
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/HandlerBlock.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/HandlerBlock.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/HandlerBlock.java
index 41c7e42..5e84e15 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/HandlerBlock.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/HandlerBlock.java
@@ -35,6 +35,7 @@ public class HandlerBlock {
 	private final SelectHandler selectHandler;
 	private final SolutionModifierHandler modifierHandler;
 	private final WhereHandler whereHandler;
+	private final ValuesHandler valueHandler;
 
 	/**
 	 * Constructor.
@@ -48,6 +49,7 @@ public class HandlerBlock {
 		whereHandler = new WhereHandler(query);
 		datasetHandler = new DatasetHandler(query);
 		modifierHandler = new SolutionModifierHandler(query);
+		valueHandler = new ValuesHandler(query);
 		/*
 		 * selecthandler and constructhandler may be null so processthem
 		 * accordingly
@@ -131,6 +133,15 @@ public class HandlerBlock {
 	}
 
 	/**
+	 * Get the value handler.
+	 * 
+	 * @return the value handler.
+	 */
+	public ValuesHandler getValueHandler() {
+		return valueHandler;
+	}
+	
+	/**
 	 * Add the prolog handler contents to this prolog handler.
 	 * 
 	 * @param handler
@@ -208,6 +219,16 @@ public class HandlerBlock {
 	}
 
 	/**
+	 * Add the values handler contents to this prolog handler.
+	 * 
+	 * @param handler
+	 *            The values handler to add to this one.
+	 */
+	public void addAll(ValuesHandler handler) {
+		valueHandler.addAll(handler);
+	}
+	
+	/**
 	 * Add all of the handlers in the handler block to this one. Any handler
 	 * that is null or is null in the handler argument are properly skipped.
 	 * 
@@ -222,6 +243,7 @@ public class HandlerBlock {
 		addAll(handler.modifierHandler);
 		addAll(handler.prologHandler);
 		addAll(handler.whereHandler);
+		addAll(handler.valueHandler);
 	}
 
 	/**
@@ -264,5 +286,6 @@ public class HandlerBlock {
 		modifierHandler.build();
 		whereHandler.build();
 		aggHandler.build();
+		valueHandler.build();
 	}
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandler.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandler.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandler.java
new file mode 100644
index 0000000..679d08b
--- /dev/null
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandler.java
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jena.arq.querybuilder.handlers;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jena.graph.Node;
+import org.apache.jena.query.Query;
+import org.apache.jena.query.QueryBuildException;
+import org.apache.jena.sparql.core.Var;
+import org.apache.jena.sparql.engine.binding.Binding;
+import org.apache.jena.sparql.engine.binding.BindingHashMap;
+
+public class ValuesHandler implements Handler {
+
+	private final List<Var> variables;
+	
+	private final List<Collection<Node>> values;
+	
+	private Map<Var,Node> valueMap;
+	
+	// the query to modify
+		private final Query query;
+
+		/**
+		 * Constructor.
+		 * 
+		 * @param query
+		 *            The query to manipulate.
+		 */
+		public ValuesHandler(Query query) {
+			this.query = query;
+			this.variables = new ArrayList<Var>();
+			this.values  = new ArrayList<Collection<Node>>();
+			this.valueMap = Collections.emptyMap();
+		}
+
+	
+	@Override
+	public void setVars(Map<Var, Node> values) {
+		valueMap = values;
+	}
+
+	@Override
+	public void build() {
+		List<Var> vars = new ArrayList<Var>( variables );
+		vars.removeAll( valueMap.keySet());
+		if (vars.isEmpty())
+		{
+			return;
+		}
+		
+		List<Binding> bindings = new ArrayList<Binding>();
+		for (Collection<Node> col : values)
+		{
+			BindingHashMap b = new BindingHashMap();
+			if (col.size() != variables.size())
+			{
+				throw new QueryBuildException(
+						String.format( "The number of variables (%s) does not match the number of nodes in
the data block (%s): %s",
+								variables.size(), col.size(), variables)) ;
+			}
+			Iterator<Node> iter = col.iterator();
+			for (int i=0;i<variables.size();i++)
+			{
+				Var v = variables.get(i);
+				Node n = iter.next();
+				if (valueMap.containsKey(v))
+				{
+					continue;
+				}
+				if (valueMap.containsKey(n))
+				{
+					n = valueMap.get(n);
+				}
+				b.add(v, n);
+			}
+			if (!b.isEmpty())
+			{
+				bindings.add(b);
+			}
+		}
+		if (!bindings.isEmpty()) {
+			query.setValuesDataBlock(vars, bindings);
+		}
+	}
+
+	/**
+	 * Add a variable to the value statement.
+	 * 
+	 * A variable may only be added once. Attempting to add the same variable
+	 * multiple times will be silently ignored.
+	 * 
+	 * @param var
+	 *            The variable to add.
+	 */
+	public void addValueVar(Var var) {
+		variables.add(var);
+	}
+
+	/**
+	 * Add the values for the variables.  There must be one value for each value var.
+	 * 
+	 * @param values
+	 *            the collection of values to add.
+	 * @return The builder for chaining.
+	 */
+	public void addDataBlock(Collection<Node> values) {
+		this.values.add(values);
+	}
+	
+	/**
+	 * Add the ValuesHandler values to this values Handler.
+	 * @param handler the handler that has the values to add.
+	 */
+	public void addAll( ValuesHandler handler )
+	{
+		this.values.addAll( handler.values );
+		this.variables.addAll( handler.variables );
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/AbstractRegexpBasedTest.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/AbstractRegexpBasedTest.java
b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/AbstractRegexpBasedTest.java
index da93255..ce7cf11 100644
--- a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/AbstractRegexpBasedTest.java
+++ b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/AbstractRegexpBasedTest.java
@@ -50,6 +50,7 @@ public abstract class AbstractRegexpBasedTest {
 	protected static final String OPTIONAL = "OPTIONAL" + SPACE;
 	protected static final String BIND = "BIND";
 	protected static final String SEMI = OPT_SPACE+"\\;";
+	protected static final String VALUES = "VALUES" + SPACE;
 
 	protected static String quote(String s) {
 		return String.format("%s%s%s", QUOTE, s, QUOTE);

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/ValuesClauseTest.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/ValuesClauseTest.java
b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/ValuesClauseTest.java
new file mode 100644
index 0000000..2bf05fb
--- /dev/null
+++ b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/ValuesClauseTest.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jena.arq.querybuilder.clauses;
+
+import java.util.Arrays;
+
+import org.apache.jena.arq.querybuilder.AbstractQueryBuilder;
+import org.apache.jena.sparql.core.Var;
+import org.junit.After;
+import org.junit.Before;
+import org.xenei.junit.contract.Contract;
+import org.xenei.junit.contract.ContractTest;
+import org.xenei.junit.contract.IProducer;
+
+@Contract(ValuesClause.class)
+public class ValuesClauseTest<T extends ValuesClause<?>> extends AbstractClauseTest
{
+
+	// the producer we will user
+	private IProducer<T> producer;
+	
+	private T valuesClause;
+
+	@Contract.Inject
+	// define the method to set producer.
+	public final void setProducer(IProducer<T> producer) {
+		this.producer = producer;
+	}
+
+	protected final IProducer<T> getProducer() {
+		return producer;
+	}
+	
+	@Before
+	public final void setupValuesClauseTest()
+	{
+		valuesClause = producer.newInstance();
+	}
+
+	@After
+	public final void cleanupValuesClauseTest() {
+		getProducer().cleanUp(); // clean up the producer for the next run
+	}
+
+	@ContractTest
+	public void testSetOneVar() {
+		
+		AbstractQueryBuilder<?> builder = valuesClause.addValueVar( "?x");
+		builder.addDataBlock( Arrays.asList( "foo"));
+		assertContainsRegex(VALUES + var("x")+OPT_SPACE+OPEN_CURLY + quote("foo")
+				+ OPT_SPACE + CLOSE_CURLY, builder.buildString());
+	}
+	
+	@ContractTest
+	public void testSetOneVarTwoValues() {
+		
+		AbstractQueryBuilder<?> builder = valuesClause.addValueVar( "?x");
+		builder.addDataBlock( Arrays.asList( "foo"));
+		builder.addDataBlock( Arrays.asList( "bar"));		
+		assertContainsRegex(VALUES + var("x")+OPT_SPACE+OPEN_CURLY + quote("foo") + SPACE +
+				quote("bar")
+				+ OPT_SPACE + CLOSE_CURLY, builder.buildString());
+	}
+	
+	@ContractTest
+	public void testSetTwoVarTwoValues() {
+		
+		AbstractQueryBuilder<?> builder = valuesClause.addValueVar( "?x");
+		builder.addValueVar( "?y");
+		builder.addDataBlock( Arrays.asList( "foo", "fu"));
+		builder.addDataBlock( Arrays.asList( "bar", "bear"));
+		
+		String s = builder.buildString();
+		
+		assertContainsRegex(VALUES + OPEN_PAREN+ var("x")+ SPACE + var("y")+ CLOSE_PAREN + OPT_SPACE+OPEN_CURLY
+
+				OPEN_PAREN + quote("foo") + SPACE + quote("fu") +CLOSE_PAREN + OPT_SPACE +
+				OPEN_PAREN + quote("bar") + SPACE + quote("bear") +CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
+	}
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
index a7896dc..ba43404 100644
--- a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
+++ b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
@@ -65,7 +65,7 @@ public class WhereClauseTest<T extends WhereClause<?>> extends
 	}
 
 	@After
-	public final void cleanupDatasetClauseTest() {
+	public final void cleanupWhereClauseTest() {
 		getProducer().cleanUp(); // clean up the producer for the next run
 	}
 

http://git-wip-us.apache.org/repos/asf/jena/blob/e8692389/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandlerTest.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandlerTest.java
b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandlerTest.java
new file mode 100644
index 0000000..8bdc79e
--- /dev/null
+++ b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/ValuesHandlerTest.java
@@ -0,0 +1,319 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jena.arq.querybuilder.handlers;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.query.Query;
+import org.apache.jena.query.QueryBuildException;
+import org.apache.jena.sparql.core.Var;
+import org.apache.jena.sparql.engine.binding.Binding;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ValuesHandlerTest extends AbstractHandlerTest {
+
+	private Query query;
+	private ValuesHandler handler;
+
+	@Before
+	public void setup() {
+		query = new Query();
+		handler = new ValuesHandler(query);
+	}
+	
+	@Test
+	public void noChangeTest()
+	{
+		handler.build();
+		assertNull( query.getValuesVariables());
+		assertNull( query.getValuesData() );
+	}
+	
+	@Test
+	public void oneVarNoData() {
+		handler.addValueVar( Var.alloc( "x" ));
+		handler.build();
+		assertNull( query.getValuesVariables());
+		assertNull( query.getValuesData() );
+	}
+	
+	@Test
+	public void noVarOneData() {
+		handler.addDataBlock( Arrays.asList( NodeFactory.createLiteral( "hello")));
+		handler.build();
+		assertNull( query.getValuesVariables());
+		assertNull( query.getValuesData() );
+	}
+	
+	@Test
+	public void oneVarOneData() {
+		Node n =NodeFactory.createLiteral( "hello");
+		handler.addDataBlock( Arrays.asList( n));
+		Var v = Var.alloc( "x" );
+		handler.addValueVar( v );
+		handler.build();
+		
+		List<Var> vars = query.getValuesVariables();
+		assertEquals( 1, vars.size());
+		assertEquals( v, vars.get(0));
+		
+		
+		assertNotNull( query.getValuesData() );
+		List<Binding> lb = query.getValuesData();
+		assertEquals( 1, lb.size());
+		Binding b = lb.get(0);
+		
+		assertTrue( b.contains(v));
+		assertEquals( n, b.get(v) );
+	}
+	
+	@Test
+	public void oneVarTwoBlocks() {
+		Node n =NodeFactory.createLiteral( "hello");
+		Node n2 = NodeFactory.createLiteral( "there");
+		handler.addDataBlock( Arrays.asList( n ));
+		handler.addDataBlock( Arrays.asList( n2 ));
+		
+		Var v = Var.alloc( "x" );
+		handler.addValueVar( v );
+		handler.build();
+		
+		List<Var> vars = query.getValuesVariables();
+		assertEquals( 1, vars.size());
+		assertEquals( v, vars.get(0));
+		
+		
+		
+		assertNotNull( query.getValuesData() );
+		List<Binding> lb = query.getValuesData();
+		assertEquals( 2, lb.size());
+		
+		List<Node> ln = new ArrayList<Node>();
+		ln.add(n);
+		ln.add(n2);
+		for (Binding b : lb ) {
+			assertTrue( b.contains(v));
+		
+			assertTrue( ln.contains( b.get(v) ) );
+			ln.remove( b.get(v));
+		}
+	}
+	
+	@Test
+	public void twoVarOneData() {
+		Node n =NodeFactory.createLiteral( "hello");
+		handler.addDataBlock( Arrays.asList( n));
+		Var v = Var.alloc( "x" );
+		Var v2 = Var.alloc( "x" );
+		handler.addValueVar( v );
+		handler.addValueVar( v2 );
+		try {
+			handler.build();
+			fail( "Shoud have thrown QueryBuildException");
+		}
+		catch (QueryBuildException expected)
+		{
+			// do nothing.
+		}
+	}
+	
+	@Test
+	public void oneVarTwoData() {
+		Node n =NodeFactory.createLiteral( "hello");
+		Node n2 = NodeFactory.createLiteral( "there");
+		handler.addDataBlock( Arrays.asList( n, n2 ));		
+		Var v = Var.alloc( "x" );
+		handler.addValueVar( v );
+		
+		try {
+			handler.build();
+			fail( "Shoud have thrown QueryBuildException");
+		}
+		catch (QueryBuildException expected)
+		{
+			// do nothing.
+		}
+		
+	}
+	
+	
+	@Test
+	public void twoVarTwoBlocks() {
+		Node n =NodeFactory.createLiteral( "hello");
+		Node nn = NodeFactory.createLiteral( "hola");
+		Node n2 = NodeFactory.createLiteral( "there");
+		Node nn2 = NodeFactory.createLiteral( "aqui");
+		handler.addDataBlock( Arrays.asList( n, nn ));
+		handler.addDataBlock( Arrays.asList( n2, nn2 ));
+		
+		Var v = Var.alloc( "x" );
+		Var v2 = Var.alloc( "y");
+		
+		handler.addValueVar( v );
+		handler.addValueVar(v2);
+	
+
+		handler.build();
+		
+		List<Var> vars = query.getValuesVariables();
+		assertEquals( 2, vars.size());
+		assertTrue( vars.contains(v));
+		assertTrue( vars.contains(v2));
+		
+		assertNotNull( query.getValuesData() );
+		List<Binding> lb = query.getValuesData();
+		assertEquals( 2, lb.size());
+		
+		List<Node> ln = new ArrayList<Node>();
+		ln.add(n);
+		ln.add(n2);
+		
+		List<Node> ln2 = new ArrayList<Node>();
+		ln2.add(nn);
+		ln2.add(nn2);
+		
+		
+		for (Binding b : lb ) {
+			assertTrue( b.contains(v));
+			assertTrue( ln.contains( b.get(v) ) );
+			ln.remove( b.get(v));
+			assertTrue( b.contains(v2));
+			assertTrue( ln2.contains( b.get(v2) ) );
+			ln2.remove( b.get(v2));
+		}
+	}
+	
+	@Test
+	public void twoVarTwoBlocksWithVarReplacement() {
+		Node n =NodeFactory.createLiteral( "hello");
+		Node nn = NodeFactory.createLiteral( "hola");
+		Node n2 = NodeFactory.createLiteral( "there");
+		Node nn2 = NodeFactory.createLiteral( "aqui");
+		handler.addDataBlock( Arrays.asList( n, nn ));
+		handler.addDataBlock( Arrays.asList( n2, nn2 ));
+		
+		Var v = Var.alloc( "x" );
+		Var v2 = Var.alloc( "y");
+		
+		handler.addValueVar( v );
+		handler.addValueVar(v2);
+		
+		Map<Var,Node> replaceVars = new HashMap<Var,Node>();
+		replaceVars.put( v2, NodeFactory.createBlankNode());
+		
+		handler.setVars(replaceVars);
+		handler.build();
+		
+		List<Var> vars = query.getValuesVariables();
+		assertEquals( 1, vars.size());
+		assertEquals( v, vars.get(0));
+		
+		assertNotNull( query.getValuesData() );
+		List<Binding> lb = query.getValuesData();
+		assertEquals( 2, lb.size());
+		
+		List<Node> ln = new ArrayList<Node>();
+		ln.add(n);
+		ln.add(n2);
+		for (Binding b : lb ) {
+			assertTrue( b.contains(v));
+			assertFalse( b.contains(v2));
+			assertTrue( ln.contains( b.get(v) ) );
+			ln.remove( b.get(v));			
+		}
+	}
+	
+	@Test
+	public void twoVarTwoBlocksReplaceDataVar() {
+		Node n =NodeFactory.createLiteral( "hello");
+		Node nn = NodeFactory.createLiteral( "hola");
+		Node n2 = NodeFactory.createLiteral( "there");
+		Var nn2 = Var.alloc( "z");
+		handler.addDataBlock( Arrays.asList( n, nn ));
+		handler.addDataBlock( Arrays.asList( n2, nn2 ));
+		
+		Var v = Var.alloc( "x" );
+		Var v2 = Var.alloc( "y");
+		
+		handler.addValueVar( v );
+		handler.addValueVar(v2);
+	
+
+		Node rep = NodeFactory.createLiteral( "aqui");
+		Map<Var,Node> replaceVars = new HashMap<Var,Node>();
+		replaceVars.put( nn2, rep);
+		
+		handler.setVars(replaceVars);
+		handler.build();
+		
+		List<Var> vars = query.getValuesVariables();
+		assertEquals( 2, vars.size());
+		assertTrue( vars.contains(v));
+		assertTrue( vars.contains(v2));
+		
+		assertNotNull( query.getValuesData() );
+		List<Binding> lb = query.getValuesData();
+		assertEquals( 2, lb.size());
+		
+		List<Node> ln = new ArrayList<Node>();
+		ln.add(n);
+		ln.add(n2);
+		
+		List<Node> ln2 = new ArrayList<Node>();
+		ln2.add(nn);
+		ln2.add(rep);
+			
+		for (Binding b : lb ) {
+			assertTrue( b.contains(v));
+			assertTrue( ln.contains( b.get(v) ) );
+			ln.remove( b.get(v));
+			assertTrue( b.contains(v2));
+			assertTrue( ln2.contains( b.get(v2) ) );
+			ln2.remove( b.get(v2));
+		}
+	}
+	
+	@Test
+	public void oneVarTwoBlocksWithReplacement() {
+		Node n =NodeFactory.createLiteral( "hello");
+		Node n2 = NodeFactory.createLiteral( "there");
+		handler.addDataBlock( Arrays.asList( n ));
+		handler.addDataBlock( Arrays.asList( n2 ));
+		
+		Var v = Var.alloc( "x" );
+		handler.addValueVar( v );
+		
+		Map<Var,Node> replaceVars = new HashMap<Var,Node>();
+		replaceVars.put( v, NodeFactory.createAnon());
+		
+		handler.setVars(replaceVars);
+		handler.build();
+		
+		assertNull( query.getValuesVariables());
+		assertNull( query.getValuesData() );
+	}
+}


Mime
View raw message