1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * Created on Oct 7, 2004
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 }