1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package com.reuters.msgtest;
37
38 import com.reuters.msgtest.RvTestCase;
39 import com.tibco.tibrv.TibrvException;
40 import com.tibco.tibrv.TibrvMsg;
41 import junit.framework.AssertionFailedError;
42 import java.util.Set;
43 import java.util.TreeSet;
44
45
46 /***
47 * @author Peter Ryan
48 *
49 * This test is present to provide an example of using RvTestCase
50 * to perform comparisons of TibrvMsgs.
51 *
52 */
53 public class MessageComparisonTest extends RvTestCase {
54 public MessageComparisonTest(String name) {
55 super(name);
56 }
57
58 public void testMessageIsEqual() throws Exception {
59 TibrvMsg reference = new TibrvMsg();
60 TibrvMsg copy = new TibrvMsg(reference);
61 assertEquals("These message should be equal", reference, copy);
62 }
63
64 public void testMessagesAreNotEqual() throws Exception {
65 TibrvMsg reference = getReferenceMessage();
66 TibrvMsg alternate = new TibrvMsg();
67 alternate.add("other", "field", TibrvMsg.STRING);
68
69 try {
70 assertEquals(reference, alternate);
71 } catch (AssertionFailedError e) {
72 assertEquals("Wrong message",
73 "expected:<{ key(18)=1234 filename=\"myfile.txt\" nested={ leaf(9)=true } }> but was:<{ other=\"field\" }>",
74 e.getMessage());
75 }
76 }
77
78 public void testMessagesIgnoresExtraFields() throws Exception {
79 TibrvMsg expectedMessage = getReferenceMessage();
80 TibrvMsg actualMessage = getReferenceMessage();
81 actualMessage.add("project", "rvtest", TibrvMsg.STRING);
82
83 Set ignoredFields = new TreeSet();
84 ignoredFields.add("project");
85 assertEquals("The Actual Message should not contain the project field",
86 expectedMessage, actualMessage, ignoredFields, true);
87 }
88
89 public void testMessageIgnoresBadValuesInIgnoredFields()
90 throws Exception {
91 TibrvMsg expectedMessage = getReferenceMessage();
92 TibrvMsg actualMessage = getReferenceMessage();
93 actualMessage.update("filename", "newfilename");
94
95 Set ignoredFields = new TreeSet();
96 ignoredFields.add("filename");
97 assertEquals("Different filename values should be ignored",
98 expectedMessage, actualMessage, ignoredFields);
99 }
100
101 public TibrvMsg getReferenceMessage() throws TibrvException {
102 TibrvMsg msg = new TibrvMsg();
103 msg.add("key", 1234, TibrvMsg.I32);
104 msg.add("filename", "myfile.txt", TibrvMsg.STRING);
105
106 TibrvMsg nestedField = new TibrvMsg();
107 nestedField.add("leaf", true, TibrvMsg.BOOL);
108
109 msg.add("nested", nestedField, TibrvMsg.MSG);
110
111 return msg;
112 }
113 }