1 /*
  2 @file     XmlBlasterAccessTest.cs
  3 @comment  Access xmlBlaster from C# (Csharp)
  4 @author   mr@marcelruff.info
  5 @see      http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.csharp.html
  6 */
  7 using System;
  8 using NUnit.Framework;
  9 using org.xmlBlaster.client;
 10 using System.Collections;
 11 
 12 [TestFixture]
 13 public class XmlBlasterAccessTest : I_Callback, I_LoggingCallback
 14 {
 15    private MsgUnitUpdate msgUnitUpdate = null;
 16 
 17    [SetUp]
 18    public void Init()
 19    {
 20       this.msgUnitUpdate = null;
 21    }
 22 
 23    [TearDown]
 24    public void Dispose()
 25    { /* ... */ }
 26 
 27    [Test]
 28    public void CheckInvalid()
 29    {
 30       I_XmlBlasterAccess xb = XmlBlasterAccessFactory.CreateInstance();
 31       xb.AddLoggingListener(null);
 32       xb.RemoveLoggingListener(null);
 33       string version = xb.GetVersion();
 34       Assert.IsNotNull(version);
 35       Console.WriteLine(version);
 36       string usage = xb.GetUsage();
 37       Assert.IsNotNull(usage);
 38       Console.WriteLine(usage);
 39    }
 40 
 41    [ExpectedException(typeof(XmlBlasterException))]
 42    [Test]
 43    public void CheckInvalidDisconnect()
 44    {
 45       I_XmlBlasterAccess xb = XmlBlasterAccessFactory.CreateInstance();
 46       xb.Disconnect(null);
 47       //xb.Connect(null, null);

 48    }
 49 
 50    [ExpectedException(typeof(XmlBlasterException))]
 51    [Test]
 52    public void CheckInvalidConnect()
 53    {
 54       I_XmlBlasterAccess xb = XmlBlasterAccessFactory.CreateInstance();
 55       xb.Connect(null, null);
 56    }
 57 
 58    //[Platform("NET-2.0")]

 59    /// <summary>

 60    /// This test expects a runngin xmlBlaster server!

 61    /// </summary>

 62    [Test]
 63    public void CheckMethods()
 64    {
 65       I_XmlBlasterAccess xb = XmlBlasterAccessFactory.CreateInstance();
 66       Hashtable properties = null;
 67       xb.Initialize(properties);
 68       string connectQos =
 69          "<qos>\n" +
 70          " <securityService type='htpasswd' version='1.0'>\n" +
 71          "   <user>fritz</user>\n" +
 72          "   <passwd>secret</passwd>\n" +
 73          " </securityService>\n" +
 74          "</qos>";
 75       ConnectReturnQos crq = xb.Connect(connectQos, this);
 76       Assert.IsNotNull(crq);
 77 
 78       SubscribeReturnQos srq = xb.Subscribe("<key oid='C#_test'/>", "<qos><updateOneway/></qos>");
 79       Assert.AreEqual("OK", srq.GetState());
 80       PublishReturnQos prq = xb.Publish("<key oid='C#_test'/>", "publish-1", "<qos/>");
 81       Assert.AreEqual("C#_test", prq.GetKeyOid());
 82 
 83       System.Threading.Thread.Sleep(1000);
 84       Assert.IsNotNull(this.msgUnitUpdate, "No update arrived");
 85       Assert.AreEqual("C#_test", this.msgUnitUpdate.GetUpdateKey().GetOid());
 86 
 87       UnSubscribeReturnQos[] urq = xb.UnSubscribe("<key oid='"+srq.GetSubscriptionId()+"'/>", "<qos/>");
 88       Assert.AreEqual(1, urq.Length);
 89       Assert.AreEqual(srq.GetSubscriptionId(), urq[0].GetSubscriptionId());
 90 
 91       {
 92          MsgUnitGet[] arr = xb.Get("<key oid='C#_test'/>", null);
 93          Assert.AreEqual(1, arr.Length);
 94          Assert.AreEqual("C#_test", arr[0].GetGetKey().GetOid());
 95       }
 96 
 97       EraseReturnQos[] erq = xb.Erase("<key oid='C#_test'/>", "<qos/>");
 98       Assert.AreEqual(1, erq.Length);
 99       Assert.AreEqual("C#_test", erq[0].GetKeyOid());
100 
101       {
102          MsgUnitGet[] arr = xb.Get("<key oid='C#_test'/>", null);
103          Assert.AreEqual(0, arr.Length);
104       }
105 
106       xb.Disconnect("<qos/>");
107    }
108 
109    #region I_Callback Members

110    public string OnUpdate(string cbSessionId, MsgUnitUpdate msgUnit)
111    {
112       this.msgUnitUpdate = msgUnit;
113       return "";
114    }
115    #endregion

116 
117    #region I_LoggingCallback Members

118    public void OnLogging(XmlBlasterLogLevel logLevel, string location, string message)
119    {
120       log(logLevel.ToString() + " " + location + ": " + message);
121    }
122    #endregion

123 
124    void log(String str)
125    {
126       Console.WriteLine(str);
127       System.Diagnostics.Debug.WriteLine(str);
128    }
129 }


syntax highlighted by Code2HTML, v. 0.9.1