1 | package negotiator.xml;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.ByteArrayOutputStream;
|
---|
6 | import java.util.LinkedHashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import javax.xml.stream.XMLStreamException;
|
---|
10 |
|
---|
11 | import org.junit.After;
|
---|
12 | import org.junit.Before;
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import genius.core.xml.Key;
|
---|
16 | import genius.core.xml.XmlWriteStream;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Test (partial) XML stream writing
|
---|
20 | */
|
---|
21 | public class XmlWriteStreamTest {
|
---|
22 | private XmlWriteStream stream;
|
---|
23 | private ByteArrayOutputStream output;
|
---|
24 |
|
---|
25 | @Before
|
---|
26 | public void before() throws XMLStreamException {
|
---|
27 | output = new ByteArrayOutputStream();
|
---|
28 | stream = new XmlWriteStream(output, "test");
|
---|
29 | }
|
---|
30 |
|
---|
31 | @After
|
---|
32 | public void after() {
|
---|
33 | System.out.println(output);
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Test
|
---|
37 | public void TestBasicWrite() throws XMLStreamException {
|
---|
38 | Map<Object, Object> result = basicResult();
|
---|
39 | stream.write("Outcome", result);
|
---|
40 | stream.close();
|
---|
41 |
|
---|
42 | assertEquals(
|
---|
43 | "<?xml version=\"1.0\" ?><test>\n<Outcome currentTime=\"2016-11-15 14:17:17\" timeOfAgreement=\"0.6666666666666666\" lastAction=\"Accept\" v=\"1\" v=\"2\">\n</Outcome></test>",
|
---|
44 | output.toString());
|
---|
45 | System.out.println(output);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Test
|
---|
49 | public void TestFullWrite() throws XMLStreamException {
|
---|
50 | Map<Object, Object> result = basicResult();
|
---|
51 |
|
---|
52 | result.put(new Key("resultOfAgent"), agentResult("A"));
|
---|
53 | result.put(new Key("resultOfAgent"), agentResult("B"));
|
---|
54 |
|
---|
55 | stream.write("Outcome", result);
|
---|
56 | stream.close();
|
---|
57 |
|
---|
58 | assertEquals(
|
---|
59 | "<?xml version=\"1.0\" ?><test>\n<Outcome currentTime=\"2016-11-15 14:17:17\" timeOfAgreement=\"0.6666666666666666\" lastAction=\"Accept\" v=\"1\" v=\"2\">\n<resultOfAgent agent=\"A\" agentName=\"QAgent\">\n</resultOfAgent>\n<resultOfAgent agent=\"B\" agentName=\"QAgent\">\n</resultOfAgent>\n</Outcome></test>",
|
---|
60 | output.toString());
|
---|
61 | }
|
---|
62 |
|
---|
63 | private Map<Object, Object> basicResult() {
|
---|
64 | // use linkedhashmap because the order is important for our exact string
|
---|
65 | // assertEquals...
|
---|
66 | Map<Object, Object> outcome = new LinkedHashMap<>();
|
---|
67 | outcome.put("currentTime", "2016-11-15 14:17:17");
|
---|
68 | outcome.put("timeOfAgreement", "0.6666666666666666");
|
---|
69 | outcome.put("lastAction", "Accept");
|
---|
70 | // check repeated values.
|
---|
71 | outcome.put(new Key("v"), 1);
|
---|
72 | outcome.put(new Key("v"), 2);
|
---|
73 | return outcome;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private Map<Object, Object> agentResult(String name) {
|
---|
77 | Map<Object, Object> outcome = new LinkedHashMap<>();
|
---|
78 | outcome.put(new Key("agent"), name);
|
---|
79 | outcome.put(new Key("agentName"), "QAgent");
|
---|
80 | return outcome;
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|