01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.resultio;
07:
08: import java.io.ByteArrayInputStream;
09: import java.io.ByteArrayOutputStream;
10: import java.io.IOException;
11: import java.util.Arrays;
12: import java.util.List;
13:
14: import junit.framework.TestCase;
15:
16: import org.openrdf.model.impl.BNodeImpl;
17: import org.openrdf.model.impl.LiteralImpl;
18: import org.openrdf.model.impl.URIImpl;
19: import org.openrdf.model.vocabulary.XMLSchema;
20: import org.openrdf.query.BindingSet;
21: import org.openrdf.query.QueryEvaluationException;
22: import org.openrdf.query.QueryResultUtil;
23: import org.openrdf.query.TupleQueryResult;
24: import org.openrdf.query.TupleQueryResultHandlerException;
25: import org.openrdf.query.impl.MapBindingSet;
26: import org.openrdf.query.impl.TupleQueryResultImpl;
27:
28: public class TupleQueryResultSerializationTest extends TestCase {
29:
30: public void testSPARQLResultFormat() throws IOException,
31: QueryResultParseException,
32: TupleQueryResultHandlerException,
33: UnsupportedQueryResultFormatException,
34: QueryEvaluationException {
35: testQueryResultFormat(TupleQueryResultFormat.SPARQL);
36: }
37:
38: public void testBinaryResultFormat() throws IOException,
39: QueryResultParseException,
40: TupleQueryResultHandlerException,
41: UnsupportedQueryResultFormatException,
42: QueryEvaluationException {
43: testQueryResultFormat(TupleQueryResultFormat.BINARY);
44: }
45:
46: private void testQueryResultFormat(TupleQueryResultFormat format)
47: throws IOException, QueryResultParseException,
48: TupleQueryResultHandlerException,
49: UnsupportedQueryResultFormatException,
50: QueryEvaluationException {
51: TupleQueryResult input = createQueryResult();
52: TupleQueryResult expected = createQueryResult();
53:
54: ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
55: QueryResultIO.write(input, format, out);
56: input.close();
57:
58: ByteArrayInputStream in = new ByteArrayInputStream(out
59: .toByteArray());
60: TupleQueryResult output = QueryResultIO.parse(in, format);
61:
62: assertTrue(QueryResultUtil.equals(expected, output));
63: }
64:
65: private TupleQueryResult createQueryResult() {
66: List<String> bindingNames = Arrays.asList("a", "b", "c");
67:
68: MapBindingSet solution1 = new MapBindingSet(bindingNames.size());
69: solution1.addBinding("a", new URIImpl("foo:bar"));
70: solution1.addBinding("b", new BNodeImpl("bnode"));
71: solution1.addBinding("c", new LiteralImpl("baz"));
72:
73: MapBindingSet solution2 = new MapBindingSet(bindingNames.size());
74: solution2.addBinding("a", new LiteralImpl("1",
75: XMLSchema.INTEGER));
76: solution2
77: .addBinding("c", new LiteralImpl("Hello World!", "en"));
78:
79: List<? extends BindingSet> bindingSetList = Arrays.asList(
80: solution1, solution2);
81:
82: TupleQueryResultImpl result = new TupleQueryResultImpl(
83: bindingNames, bindingSetList);
84:
85: return result;
86: }
87: }
|