1 /*----------------------------------------------------------------------------
  2 Name:      ServiceTOTest.cs
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Test cases for service XML markup
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 Date:      04/2007
  8 See:       http://www.xmlblaster.org/
  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.contrib.service {
 18    [TestFixture]
 19    public class ServiceTOTest {
 20 
 21       public ServiceTOTest() {
 22       }
 23 
 24       [SetUp]
 25       public void Init() {
 26       }
 27 
 28       [TearDown]
 29       public void Dispose() { /* ... */ }
 30 
 31       /*
 32        * using (XmlReader reader = XmlReader.Create("2books.xml")) {
 33 
 34   // Move the reader to the second book node.
 35   reader.MoveToContent(); 
 36   reader.ReadToDescendant("book");
 37   reader.Skip(); //Skip the first book.
 38 
 39   // Parse the file starting with the second book node.
 40   do {
 41      switch (reader.NodeType) {
 42         case XmlNodeType.Element:
 43            Console.Write("<{0}", reader.Name);
 44            while (reader.MoveToNextAttribute()) {
 45                Console.Write(" {0}='{1}'", reader.Name, reader.Value);
 46            }
 47            Console.Write(">");
 48            break;
 49         case XmlNodeType.Text:
 50            Console.Write(reader.Value);
 51            break;
 52         case XmlNodeType.EndElement:
 53            Console.Write("</{0}>", reader.Name);
 54            break;
 55      }       
 56   }  while (reader.Read());    
 57 
 58 }
 59 
 60 */
 61 
 62       [Test]
 63       public void CheckToXml() {
 64          ServiceListTO serviceList = new ServiceListTO();
 65          {
 66             ServiceTO service = new ServiceTO();
 67             serviceList.addService(service);
 68             service.addProp(new PropTO(PropTO.KEY_SERVICENAME, "buddy"));
 69          }
 70          byte[] bytes = serviceList.ToXml();
 71          string xml = serviceList.ToXmlStr();
 72          Console.WriteLine("CheckToXml: " + xml);
 73 
 74          {
 75             ServiceListTO serviceList2 = ServiceListTO.parseStr(xml);
 76             Console.WriteLine(serviceList2.ToXmlStr());
 77             Assert.AreEqual(1, serviceList2.getServices().Count);
 78             foreach (ServiceTO service in serviceList2.getServices()) {
 79                Assert.AreEqual("buddy", service.getPropValue(PropTO.KEY_SERVICENAME));
 80             }
 81          }
 82          {
 83             ServiceListTO serviceList2 = ServiceListTO.parse(bytes);
 84             Console.WriteLine(serviceList2.ToXmlStr());
 85             Assert.AreEqual(1, serviceList2.getServices().Count);
 86             foreach (ServiceTO service in serviceList2.getServices()) {
 87                Assert.AreEqual("buddy", service.getPropValue(PropTO.KEY_SERVICENAME));
 88             }
 89          }
 90          Console.WriteLine("CheckToXml");
 91       }
 92 
 93       [Test]
 94       public void CheckBase64() {
 95          byte[] bytes = org.xmlBlaster.util.Serialization.StringToUTF8ByteArray("Hello");
 96          PropTO prop = new PropTO(PropTO.KEY_DATA, bytes);
 97          Assert.AreEqual("SGVsbG8=", prop.GetValueRaw());
 98          Assert.AreEqual("Hello", prop.GetValue());
 99       }
100 
101 
102       [Test]
103       public void CheckToXmlParsing2() {
104          string xmlService =
105          "<sc>" +
106            "<s>" +
107              "<p k=\"serviceName\">buddy</p>" +
108              "<p k=\"task\">getBuddyList</p>" +
109              "<p k=\"resultMime\">application/watchee.service.buddy.buddylist</p>" +
110              "<p k=\"resultEncoding\">base64</p>" +
111              "<p k=\"result\">PGJ1ZGR5bGlzdCBsb2dpbk5hbWU9ImpvZUBteWNvbXAuaW5mbyIgdHlwZT0iYWxsIj4KICA8YnVkZHk+CiAgICA8bG9naW5OYW1lPmphY2tAc29tZS5vcmc8L2xvZ2luTmFtZT4KICAgIDxhbGlhcz5qYWNrPC9hbGlhcz4KICAgIDxwZXJtaXNzaW9uIG5hbWU9ImdwcyIgZGVzY3JpcHRpb249IlNob3cgbXkgcG9zaXRpb24iLz4KICAgIDxwZXJtaXNzaW9uIG5hbWU9InhzbXMiIGRlc2NyaXB0aW9uPSJTZW5kL1JlY2VpdmUgbWFpbHMiLz4KICA8L2J1ZGR5Pgo8L2J1ZGR5bGlzdD4=</p>" +
112            "</s>" +
113          "</sc>";
114          ServiceListTO serviceList = ServiceListTO.parseStr(xmlService);
115          Assert.AreEqual(1, serviceList.getServices().Count);
116          foreach (ServiceTO service in serviceList.getServices()) {
117             Assert.AreEqual(5, service.getProps().Count);
118             string resultMime = service.getPropValue(PropTO.KEY_RESULTMIME);
119             string xml = service.getPropValue(PropTO.KEY_RESULT);
120             Console.WriteLine("Handling resultMime=" + resultMime + " xml=" + xml);
121          }
122       }
123 
124       [Test]
125       public void CheckXmlParsing() {
126          {
127             string xml = "<?xml version='1.0' encoding='utf-8'?>\r\n<s>"
128                + "<p k='serviceName'>track</p>"
129                + "<p k='taskType'>named</p>"
130                + "<p k='task'>myStandardTrackIdQuery('Summer')</p>"
131                + "<p k='resultMime'>application/service-buddy</p>"
132                + "</s>";
133             ServiceTO service = ServiceTO.parse(xml);
134             Assert.IsNotNull(service);
135             Assert.AreEqual(4, service.getProps().Count);
136             Assert.AreEqual("track", service.getPropValue(PropTO.KEY_SERVICENAME));
137             Assert.AreEqual("named", service.getPropValue(PropTO.KEY_TASKTYPE));
138             Assert.AreEqual("myStandardTrackIdQuery('Summer')", service.getPropValue(PropTO.KEY_TASK));
139             Assert.AreEqual("application/service-buddy", service.getPropValue(PropTO.KEY_RESULTMIME));
140          }
141 
142          {
143             string xml = "<?xml version='1.0' encoding='utf-8'?>\r\n"
144                + "<sc><s>"
145                + "<p k='serviceName'>track</p>"
146                + "<p k='taskType'>named</p>"
147                + "<p k='task'>myStandardTrackIdQuery('Summer')</p>"
148                + "<p k='resultMime'>application/watchee-service-buddy-</p>"
149                + "</s></sc>";
150             ServiceListTO serviceList = ServiceListTO.parseStr(xml);
151             Assert.IsNotNull(serviceList);
152             Assert.AreEqual(1, 1);
153          }
154          Console.WriteLine("CheckXmlParsing");
155       }
156 
157       [Test]
158       public void CheckXmlSubtagsParsing() {
159          {
160             string xml = "<s>"
161                + "<p k='serviceName'>track</p>"
162                + "<p k='result'><A><B>Hallo</B><C /></A></p>"
163                + "</s>";
164             ServiceTO service = ServiceTO.parse(xml);
165             Assert.IsNotNull(service);
166             Assert.AreEqual(2, service.getProps().Count);
167             Assert.AreEqual("<A><B>Hallo</B><C /></A>", service.getPropValue(PropTO.KEY_RESULT));
168          }
169 
170          {
171             string xml = "<s>"
172                + "<p k='serviceName'>track</p>"
173                + "<p k='result'>&lt;A&gt;&lt;B&gt;Hallo&amp;&lt;/B&gt;&lt;C /&gt;&lt;/A&gt;</p>"
174                + "</s>";
175             ServiceTO service = ServiceTO.parse(xml);
176             Assert.IsNotNull(service);
177             Assert.AreEqual(2, service.getProps().Count);
178             Assert.AreEqual("<A><B>Hallo&</B><C /></A>", service.getPropValue(PropTO.KEY_RESULT));
179          }
180 
181          {
182             string xml = "<s>"
183                + "<p k='serviceName'>track</p>"
184                + "<p k='result'><![CDATA[<A><B>Hallo</B><C /></A>]]></p>"
185                + "</s>";
186             ServiceTO service = ServiceTO.parse(xml);
187             Assert.IsNotNull(service);
188             Assert.AreEqual(2, service.getProps().Count);
189             Assert.AreEqual("<A><B>Hallo</B><C /></A>", service.getPropValue(PropTO.KEY_RESULT));
190          }
191 
192          Console.WriteLine("CheckXmlSubtagsParsing");
193       }
194 
195       [Test]
196       public void CheckXmlBase64Parsing() {
197          {
198             string xml = "<s>"
199                + "<p k='serviceName'>track</p>"
200                + "<p k='resultEncoding'>base64</p>"
201                + "<p k='result'>QmxhPEJsYUJsYQ==</p>"
202                + "</s>";
203             ServiceTO service = ServiceTO.parse(xml);
204             Assert.IsNotNull(service);
205             Assert.AreEqual(3, service.getProps().Count);
206             //string tmp = service.getPropValue(PropTO.KEY_RESULT);

207             Assert.AreEqual("Bla<BlaBla", service.getPropValue(PropTO.KEY_RESULT));
208          }
209 
210          Console.WriteLine("CheckXmlBase64Parsing");
211       }
212    }
213 }


syntax highlighted by Code2HTML, v. 0.9.1