/* * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007. * * Licensed under the Aduna BSD-style license. */ package org.openrdf.query.resultio; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; import org.openrdf.model.impl.BNodeImpl; import org.openrdf.model.impl.LiteralImpl; import org.openrdf.model.impl.URIImpl; import org.openrdf.model.vocabulary.XMLSchema; import org.openrdf.query.BindingSet; import org.openrdf.query.QueryEvaluationException; import org.openrdf.query.QueryResultUtil; import org.openrdf.query.TupleQueryResult; import org.openrdf.query.TupleQueryResultHandlerException; import org.openrdf.query.impl.MapBindingSet; import org.openrdf.query.impl.TupleQueryResultImpl; public class TupleQueryResultSerializationTest extends TestCase { public void testSPARQLResultFormat() throws IOException, QueryResultParseException, TupleQueryResultHandlerException, UnsupportedQueryResultFormatException, QueryEvaluationException { testQueryResultFormat(TupleQueryResultFormat.SPARQL); } public void testBinaryResultFormat() throws IOException, QueryResultParseException, TupleQueryResultHandlerException, UnsupportedQueryResultFormatException, QueryEvaluationException { testQueryResultFormat(TupleQueryResultFormat.BINARY); } private void testQueryResultFormat(TupleQueryResultFormat format) throws IOException, QueryResultParseException, TupleQueryResultHandlerException, UnsupportedQueryResultFormatException, QueryEvaluationException { TupleQueryResult input = createQueryResult(); TupleQueryResult expected = createQueryResult(); ByteArrayOutputStream out = new ByteArrayOutputStream(4096); QueryResultIO.write(input, format, out); input.close(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); TupleQueryResult output = QueryResultIO.parse(in, format); assertTrue(QueryResultUtil.equals(expected, output)); } private TupleQueryResult createQueryResult() { List<String> bindingNames = Arrays.asList("a", "b", "c"); MapBindingSet solution1 = new MapBindingSet(bindingNames.size()); solution1.addBinding("a", new URIImpl("foo:bar")); solution1.addBinding("b", new BNodeImpl("bnode")); solution1.addBinding("c", new LiteralImpl("baz")); MapBindingSet solution2 = new MapBindingSet(bindingNames.size()); solution2.addBinding("a", new LiteralImpl("1", XMLSchema.INTEGER)); solution2.addBinding("c", new LiteralImpl("Hello World!", "en")); List<? extends BindingSet> bindingSetList = Arrays.asList(solution1, solution2); TupleQueryResultImpl result = new TupleQueryResultImpl(bindingNames, bindingSetList); return result; } }