void runTestSelect(Query query, QueryExecution qe) throws Exception { // Do the query! ResultSetRewindable resultsActual = ResultSetFactory.makeRewindable(qe.execSelect()); qe.close(); if (results == null) return; // Assumes resultSetCompare can cope with full isomorphism possibilities. ResultSetRewindable resultsExpected; if (results.isResultSet()) resultsExpected = ResultSetFactory.makeRewindable(results.getResultSet()); else if (results.isModel()) resultsExpected = ResultSetFactory.makeRewindable(results.getModel()); else { fail("Wrong result type for SELECT query"); resultsExpected = null; // Keep the compiler happy } if (query.isReduced()) { // Reduced - best we can do is DISTINCT resultsExpected = unique(resultsExpected); resultsActual = unique(resultsActual); } // Hack for CSV : tests involving bNodes need manually checking. if (testItem.getResultFile().endsWith(".csv")) { resultsActual = convertToStrings(resultsActual); resultsActual.reset(); int nActual = ResultSetFormatter.consume(resultsActual); int nExpected = ResultSetFormatter.consume(resultsExpected); resultsActual.reset(); resultsExpected.reset(); assertEquals("CSV: Different number of rows", nExpected, nActual); boolean b = resultSetEquivalent(query, resultsExpected, resultsActual); if (!b) System.out.println("Manual check of CSV results required: " + testItem.getName()); return; } boolean b = resultSetEquivalent(query, resultsExpected, resultsActual); if (!b) { resultsExpected.reset(); resultsActual.reset(); boolean b2 = resultSetEquivalent(query, resultsExpected, resultsActual); printFailedResultSetTest(query, qe, resultsExpected, resultsActual); } assertTrue("Results do not match: " + testItem.getName(), b); return; }
private static ResultSetRewindable unique(ResultSetRewindable results) { // VERY crude. Utilises the fact that bindings have value equality. List<Binding> x = new ArrayList<Binding>(); Set<Binding> seen = new HashSet<Binding>(); for (; results.hasNext(); ) { Binding b = results.nextBinding(); if (seen.contains(b)) continue; seen.add(b); x.add(b); } QueryIterator qIter = new QueryIterPlainWrapper(x.iterator()); ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter); return ResultSetFactory.makeRewindable(rs); }
private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) { List<Binding> bindings = new ArrayList<Binding>(); while (resultsActual.hasNext()) { Binding b = resultsActual.nextBinding(); BindingMap b2 = BindingFactory.create(); for (String vn : resultsActual.getResultVars()) { Var v = Var.alloc(vn); Node n = b.get(v); String s; if (n == null) s = ""; else if (n.isBlank()) s = "_:" + n.getBlankNodeLabel(); else s = NodeFunctions.str(n); b2.add(v, NodeFactory.createLiteral(s)); } bindings.add(b2); } ResultSet rs = new ResultSetStream( resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator())); return ResultSetFactory.makeRewindable(rs); }