Skip to content

Commit

Permalink
JENA-1468: Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jan 27, 2018
1 parent 30eb931 commit 2586abf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
Expand Up @@ -24,7 +24,8 @@
@RunWith(Suite.class)
@Suite.SuiteClasses( {
// This package
TestAlgebraTranslate.class
TestTable.class
, TestAlgebraTranslate.class
, TestClassify.class
, TestOpAsQuery.class
, TestOpVars.class
Expand Down
Expand Up @@ -389,6 +389,25 @@ public void testMinus2() {
test_roundTripAlegbra(query) ;
}

@Test
public void testTable1() {
String query = "SELECT * WHERE { ?x ?p ?z . VALUES ?y { } }" ;
roundTripQuery(query);
}

@Test
public void testTable2() {
// JENA-1468 : op to string and back.
String qs = "SELECT * WHERE { ?x ?p ?z . VALUES ?y { } }" ;
Query query = QueryFactory.create(qs);
Op op = Algebra.compile(query);
String x = op.toString();
Op op1 = SSE.parseOp(x);
Query query2 = OpAsQuery.asQuery(op1);
assertEquals(query, query2);
}


@Test
public void testValues1() {
String query = "SELECT * { VALUES ?x {1 2} ?s ?p ?x }" ;
Expand Down Expand Up @@ -443,6 +462,7 @@ public static void test_equivalentQuery(String input, String expected) {

// Test for queries that do query->algebra->OpAsQuery->query
// to produce an output that is .equals the input.
/** query->algebra->OpAsQuery->query */
public static Query[] test_roundTripQuery(String query) {
Query[] r = roundTripQuery(query) ;
stripNamespacesAndBase(r[0]) ;
Expand Down Expand Up @@ -471,7 +491,7 @@ public void test_roundTripAlegbra(String query) {
Assert.assertEquals(a1, a2);
}

// algebra->OpAsQuery->query
/** algebra->OpAsQuery->query */
public static void test_AlgebraToQuery(String input, String expected) {
Op op = SSE.parseOp(input) ;
Query orig = QueryFactory.create(expected, Syntax.syntaxSPARQL_11);
Expand All @@ -480,7 +500,7 @@ public static void test_AlgebraToQuery(String input, String expected) {
Assert.assertEquals(orig, got) ;
}

// query->algebra->OpAsQuery->query
/** query->algebra->OpAsQuery->query **/
private static Query[] roundTripQuery(String query) {
Query orig = QueryFactory.create(query, Syntax.syntaxSPARQL_11);
Op toReconstruct = Algebra.compile(orig);
Expand All @@ -489,7 +509,7 @@ private static Query[] roundTripQuery(String query) {
return r;
}

// query->algebra/quads->OpAsQuery->query
/** query->algebra/quads->OpAsQuery->query */
private static Query[] roundTripQueryQuad(String query) {
Query orig = QueryFactory.create(query, Syntax.syntaxSPARQL_11);
Op toReconstruct = Algebra.compile(orig);
Expand Down
@@ -0,0 +1,68 @@
/*
* 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.sparql.algebra;

import static org.junit.Assert.assertEquals;

import org.apache.jena.atlas.io.IndentedLineBuffer;
import org.apache.jena.sparql.algebra.op.OpTable;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.engine.binding.BindingFactory;
import org.apache.jena.sparql.serializer.SerializationContext;
import org.apache.jena.sparql.sse.writers.WriterOp;
import org.junit.Test ;

public class TestTable {
@Test public void table_01() {
Table table = TableFactory.createEmpty();
Op opTable = OpTable.create(table);
String x = str(opTable);
assertEquals("(table empty)", x);
}

// JENA-1468: Table, no rows , with declared variables.
@Test public void table_02() {
Table table = TableFactory.create();
table.getVars().add(Var.alloc("a"));
Op opTable = OpTable.create(table);
String x = str(opTable);
assertEquals("(table (vars ?a))", x);
}

@Test public void table_03() {
Table table = TableFactory.create();
Binding b = BindingFactory.binding();
table.addBinding(b);
Op opTable = OpTable.create(table);
String x = str(opTable);
assertEquals("(table (vars) (row) )", x);
}

// String, no adornment
private static String str(Op op) {
SerializationContext sCxt = new SerializationContext();
IndentedLineBuffer out = new IndentedLineBuffer();
out.setFlatMode(true);
WriterOp.output(out, op, sCxt);
String x = out.asString();
return x.trim();
}
}

Expand Up @@ -907,7 +907,7 @@ public void oneOf6() {
// Tests use of multiple != conditions on same variable
testOp("(filter ((!= ?x <x>) (!= ?x <y>)) (bgp ( ?s ?p ?x)) )",
t_inequality,
"(minus (bgp (?s ?p ?x)) (table (vars ?p ?x) (row [?x <y>]) (row [?x <x>])))") ;
"(minus (bgp (?s ?p ?x)) (table (vars ?x) (row [?x <y>]) (row [?x <x>])))") ;
}

@Test public void inequality09() {
Expand Down

0 comments on commit 2586abf

Please sign in to comment.