1 /*----------------------------------------------------------------------------
  2 Name:      QosTest.cs
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Test cases for Qos.cs
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 Date:      12/2006
  8 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.html
  9 -----------------------------------------------------------------------------*/
 10 using System;
 11 using System.Collections.Generic;
 12 using System.Text;
 13 using NUnit.Framework;
 14 using System.Reflection;
 15 using System.Collections;
 16 
 17 namespace org.xmlBlaster.client
 18 {
 19    [TestFixture]
 20    public class QosTest
 21    {
 22       [Test]
 23       public void CheckComplete()
 24       {
 25          byte[] bytes = new byte[2];
 26          bytes[0] = (byte)199;
 27          bytes[1] = (byte)4;
 28          string base64 = System.Convert.ToBase64String(bytes);
 29          string qosStr = "<qos> <!-- UpdateQos -->"
 30             + "  <state id='ERASED'/>"
 31             + "  <sender>/node/heron/client/joe/-2  </sender>"
 32             + "  <priority>7  </priority>"
 33             + "  <subscribe id='__subId:heron-2'/>"
 34             + "  <rcvTimestamp nanos='1007764305862000002'>"
 35             + "    2001-12-07 23:31:45.862000002 <!-- The nanos from above but human readable -->"
 36             + "  </rcvTimestamp>"
 37             + "  <expiration lifeTime='129595811' remainingLife='1200'/>"
 38             + "  <queue index='3' size='6'/> <!-- If queued messages are flushed on login -->"
 39             + "  <redeliver> 4  </redeliver> <!-- Only sent if message sending had previous errors -->"
 40             + "  <clientProperty name='myTransactionId'>0x23345</clientProperty>"
 41             + "  <clientProperty name='myDescription' encoding='base64' charset='windows-1252'>QUUgaXMgJ8QnDQpPRSBpcyAn1icNCnNzIGlzICffJw==</clientProperty>"
 42             + "  <clientProperty name='myAge' type='int'>12</clientProperty>"
 43             + "  <clientProperty name='nothing'/>"
 44             + "  <clientProperty name='invalid' type='int'>ABC</clientProperty>"
 45             + "  <clientProperty name='somebytes' encoding='base64' type='byte[]'>" + base64 + "</clientProperty>"
 46             + "  <route> <!-- Routing information in cluster environment -->"
 47             + "    <node id='avalon' stratum='1' timestamp='1068026303739000001' dirtyRead='false'/>"
 48             + "    <node id='heron' stratum='0' timestamp='1068026303773000001' dirtyRead='false'/>"
 49             + "  </route>"
 50             + "</qos>";
 51          UpdateQos qos = new UpdateQos(qosStr);
 52          Assert.AreEqual("ERASED", qos.GetState());
 53          Assert.AreEqual(false, qos.IsOk());
 54          Assert.AreEqual(true, qos.IsErased());
 55          SessionName sessionName = qos.GetSender();
 56          Assert.AreEqual("/node/heron/client/joe/session/-2", sessionName.ToString());
 57          Assert.AreEqual(-2, sessionName.GetPublicSessionId());
 58          Assert.AreEqual("joe", sessionName.GetLoginName());
 59          Assert.AreEqual("client/joe/session/-2", sessionName.GetRelativeName());
 60          Assert.AreEqual(7, qos.GetPriority());
 61          Assert.AreEqual("__subId:heron-2", qos.GetSubscriptionId());
 62          Assert.AreEqual("2001-12-07 23:31:45.862000002", qos.GetRcvTime());
 63          Assert.AreEqual(1007764305862000002, qos.GetRcvTimeNanos());
 64          Assert.AreEqual(129595811, qos.GetLifeTime());
 65          Assert.AreEqual(3, qos.GetQueueIndex());
 66          Assert.AreEqual(6, qos.GetQueueSize());
 67          Assert.AreEqual(4, qos.GetRedeliver());
 68          Assert.AreEqual("0x23345", qos.GetClientProperty("myTransactionId", ""));
 69          Assert.AreEqual("AE is 'Ä'\r\nOE is 'Ö'\r\nss is 'ß'", qos.GetClientProperty("myDescription", ""));
 70          Assert.AreEqual(12, qos.GetClientProperty("myAge", 0));
 71          Assert.AreEqual("", qos.GetClientProperty("nothing", ""));
 72          Assert.AreEqual("", qos.GetClientProperty("aaa", ""));
 73          Assert.AreEqual(18, qos.GetClientProperty("invalid", 18));
 74          Assert.AreEqual(bytes, qos.GetClientProperty("somebytes", new byte[0]));
 75          Assert.AreEqual(6, qos.GetClientProperties().Count);
 76          ClientProperty myDescription = qos.GetClientProperty("myDescription");
 77          Assert.IsNotNull(myDescription);
 78          Assert.AreEqual(true, myDescription.IsBase64());
 79          Assert.AreEqual(ClientProperty.ENCODING_BASE64, myDescription.GetEncoding());
 80          Assert.AreEqual(ClientProperty.TYPE_STRING, myDescription.GetValueType());
 81          Assert.AreEqual("myDescription", myDescription.GetName());
 82       }
 83 
 84       [Test]
 85       public void CheckEmpty()
 86       {
 87          string qosStr = "";
 88          UpdateQos qos = new UpdateQos(qosStr);
 89          Assert.AreEqual("OK", qos.GetState());
 90          Assert.AreEqual(true, qos.IsOk());
 91          Assert.AreEqual(false, qos.IsErased());
 92          Assert.AreEqual(5, qos.GetPriority());
 93          SessionName sessionName = qos.GetSender();
 94          Assert.IsNull(sessionName);
 95          Assert.AreEqual("", qos.GetSubscriptionId());
 96          Assert.AreEqual("", qos.GetRcvTime());
 97          Assert.AreEqual(-1L, qos.GetLifeTime());
 98          Assert.AreEqual(0, qos.GetQueueIndex());
 99          Assert.AreEqual(0, qos.GetQueueSize());
100          Assert.AreEqual(0, qos.GetRedeliver());
101          Assert.AreEqual("", qos.GetClientProperty("myTransactionId", ""));
102          Assert.AreEqual(0, qos.GetClientProperties().Count);
103       }
104 
105       [ExpectedException(typeof(System.Exception))]
106       [Test]
107       public void CheckInvalid()
108       {
109          string qosStr = "<qos>"
110             + "  <sender>/node/heron/clXXXient/joe/-2</sender>"
111             + "</qos>";
112          UpdateQos qos = new UpdateQos(qosStr);
113          SessionName sessionName = qos.GetSender();
114          Assert.AreEqual("/node/heron/client/joe/session/-2", sessionName.ToString());
115       }
116 
117       [Test]
118       public void CheckSubscribeReturnQos()
119       {
120          string qosStr = "<qos>"
121             + "  <state id='OK' info='QUEUED'/>"
122             + "  <subscribe id='__subId:heron-2'/>"
123             + "</qos>";
124          SubscribeReturnQos qos = new SubscribeReturnQos(qosStr);
125          Assert.AreEqual("OK", qos.GetState());
126          Assert.AreEqual("QUEUED", qos.GetStateInfo());
127          Assert.AreEqual("__subId:heron-2", qos.GetSubscriptionId());
128       }
129 
130       [Test]
131       public void CheckUnSubscribeReturnQos()
132       {
133          string qosStr = "<qos>"
134             + "  <state id='OK' info='QUEUED'/>"
135             + "  <subscribe id='__subId:heron-2'/>"
136             + "</qos>";
137          UnSubscribeReturnQos qos = new UnSubscribeReturnQos(qosStr);
138          Assert.AreEqual("OK", qos.GetState());
139          Assert.AreEqual("QUEUED", qos.GetStateInfo());
140          Assert.AreEqual("__subId:heron-2", qos.GetSubscriptionId());
141       }
142 
143       [Test]
144       public void CheckEraseReturnQos()
145       {
146          string qosStr = "<qos>"
147             + "  <state id='OK' info='QUEUED'/>"
148             + "  <key oid='MyTopic'/>"
149             + "</qos>";
150          EraseReturnQos qos = new EraseReturnQos(qosStr);
151          Assert.AreEqual("OK", qos.GetState());
152          Assert.AreEqual("QUEUED", qos.GetStateInfo());
153          Assert.AreEqual("MyTopic", qos.GetKeyOid());
154       }
155 
156       [Test]
157       public void CheckPublishReturnQos()
158       {
159          string qosStr = "<qos>"
160             + "  <state id='OK' info='QUEUED'/>"
161             + "  <key oid='MyTopic'/>"
162             + "  <rcvTimestamp nanos='1007764305862000002'>"
163             + "    2001-12-07 23:31:45.862000002"
164             + "  </rcvTimestamp>"
165             + "</qos>";
166          PublishReturnQos qos = new PublishReturnQos(qosStr);
167          Assert.AreEqual("OK", qos.GetState());
168          Assert.AreEqual("QUEUED", qos.GetStateInfo());
169          Assert.AreEqual("MyTopic", qos.GetKeyOid());
170          Assert.AreEqual("2001-12-07 23:31:45.862000002", qos.GetRcvTime());
171          Assert.AreEqual(1007764305862000002, qos.GetRcvTimeNanos());
172       }
173 
174       [Test]
175       public void CheckConnectReturnQos()
176       {
177          string qosStr = "<qos>"
178          + "   <securityService type='htpasswd' version='1.0'>"
179          + "     <![CDATA["
180          + "     <user>fred</user>"
181          + "     <passwd>secret</passwd>"
182          + "     ]]>"
183          + "   </securityService>"
184          + ""
185          + "   <session name='joe/3' timeout='3600000' maxSessions='10'"
186          + "               clearSessions='false' reconnectSameClientOnly='false' sessionId='4e56890ghdFzj0'/>"
187          + ""
188          + "   <!-- Recoverable session after server crash / restart -->"
189          + "   <persistent/>"
190          + "   <reconnected>false</reconnected>"
191          + "   <ptp>true</ptp>"
192          + ""
193          + "   <duplicateUpdates>false</duplicateUpdates>"
194          + ""
195          + "   <!-- Setting to control client side behavior, used for cluster configuration -->"
196          + "   <queue relating='connection' maxEntries='10000000' maxEntriesCache='1000'>"
197          + "      <address type='IOR' bootstrapPort='7600' dispatchPlugin='undef'/>"
198          + "   </queue>"
199          + ""
200          + "   <!-- Configure the server side subject queue (one for each login name) -->"
201          + "   <queue relating='subject' type='CACHE' version='1.0'"
202          + "             maxEntries='5000' maxBytes='1000000'"
203          + "             maxEntriesCache='100' maxBytesCache='100000'"
204          + "             onOverflow='deadMessage'/>"
205          + ""
206          + "   <!-- Configure the server side callback queue (one for each login session) -->"
207          + "   <queue relating='callback' maxEntries='1000' maxBytes='4000000'"
208          + "                                                   onOverflow='deadMessage'>"
209          + "      <callback type='IOR' sessionId='4e56890ghdFzj0' pingInterval='10000'"
210          + "          retries='-1' delay='10000' oneway='false' dispatcherActive='true' dispatchPlugin='undef'>"
211          + "         IOR:10000010033200000099000010...."
212          + "         <burstMode collectTime='400' maxEntries='20' maxBytes='-1' />"
213          + "         <compress type='gzip' minSize='3000'/> <!-- only implemented for SOCKET protocol -->"
214          + "         <ptp>true</ptp>"
215          + "         <attribute name='key1' type='int'>2005</attribute>"
216          + "      </callback>"
217          + "   </queue>"
218          + ""
219          + "   <!-- a client specific property: here it could be the bean to invoke on updates -->"
220          + "   <clientProperty name='onMessageDefault'>beanName</clientProperty>"
221          + "</qos>";
222          ConnectReturnQos qos = new ConnectReturnQos(qosStr);
223          Assert.AreEqual("fred", qos.GetSecurityServiceUser());
224          Assert.AreEqual("secret", qos.GetSecurityServicePasswd());
225          SessionName sessionName = qos.GetSessionName();
226          Assert.IsNotNull(sessionName);
227          Assert.AreEqual("client/joe/session/3", sessionName.GetRelativeName());
228          Assert.AreEqual(true, qos.IsPtp());
229          Assert.AreEqual(true, qos.IsPersistent());
230          Assert.AreEqual("beanName", qos.GetClientProperty("onMessageDefault", ""));
231          Assert.AreEqual(1, qos.GetClientProperties().Count);
232       }
233    }
234 }


syntax highlighted by Code2HTML, v. 0.9.1