JsonWriterTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. //
  2. // JsonWriterTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Text;
  31. using System.Runtime.Serialization.Json;
  32. using System.Xml;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Runtime.Serialization.Json
  35. {
  36. [TestFixture]
  37. public class JsonWriterTest
  38. {
  39. MemoryStream ms;
  40. XmlDictionaryWriter w;
  41. string ResultString {
  42. get { return Encoding.UTF8.GetString (ms.ToArray ()); }
  43. }
  44. [SetUp]
  45. public void Setup ()
  46. {
  47. ms = new MemoryStream ();
  48. w = JsonReaderWriterFactory.CreateJsonWriter (ms);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentNullException))]
  52. public void ConstructorNullStream ()
  53. {
  54. JsonReaderWriterFactory.CreateJsonWriter (null);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentNullException))]
  58. public void ConstructorNullEncoding ()
  59. {
  60. JsonReaderWriterFactory.CreateJsonWriter (new MemoryStream (), null);
  61. }
  62. [Test]
  63. [ExpectedException (typeof (XmlException))]
  64. public void SimpleElementNotRoot ()
  65. {
  66. w.WriteStartElement ("foo");
  67. }
  68. [Test]
  69. public void SimpleElement ()
  70. {
  71. w.WriteStartElement ("root");
  72. w.WriteEndElement ();
  73. w.Close ();
  74. // empty string literal ("")
  75. Assert.AreEqual ("\"\"", ResultString, "#1");
  76. }
  77. [Test]
  78. [ExpectedException (typeof (XmlException))]
  79. public void SimpleElement2 ()
  80. {
  81. w.WriteStartElement ("root");
  82. w.WriteStartElement ("foo");
  83. // type='array' or type='object' is required before writing immediate child of an element.
  84. }
  85. [Test]
  86. public void SimpleElement3 ()
  87. {
  88. w.WriteStartElement ("root");
  89. w.WriteAttributeString ("type", "object");
  90. w.WriteStartElement ("e1");
  91. w.WriteAttributeString ("type", "object");
  92. w.WriteStartElement ("e1_1");
  93. w.WriteEndElement (); // treated as a string literal
  94. w.WriteEndElement ();
  95. w.WriteStartElement ("e2");
  96. w.WriteString ("value");
  97. w.WriteEndElement ();
  98. w.WriteEndElement ();
  99. w.Close ();
  100. string json = "{\"e1\":{\"e1_1\":\"\"},\"e2\":\"value\"}";
  101. Assert.AreEqual (json, ResultString, "#1");
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentException))]
  105. public void AttributeNonType ()
  106. {
  107. w.WriteStartElement ("root");
  108. // only "type" attribute is expected.
  109. w.WriteStartAttribute ("a1");
  110. }
  111. [Test]
  112. [ExpectedException (typeof (XmlException))]
  113. public void TypeAttributeNonStandard ()
  114. {
  115. w.WriteStartElement ("root");
  116. w.WriteAttributeString ("type", "foo");
  117. }
  118. [Test]
  119. public void SimpleTypeAttribute ()
  120. {
  121. w.WriteStartElement ("root");
  122. w.WriteAttributeString ("type", "number");
  123. w.WriteEndElement ();
  124. w.Close ();
  125. Assert.AreEqual (String.Empty, ResultString, "#1");
  126. }
  127. [Test]
  128. public void SimpleTypeAttribute2 ()
  129. {
  130. w.WriteStartElement ("root");
  131. w.WriteAttributeString ("type", "object");
  132. w.WriteStartElement ("foo");
  133. w.WriteAttributeString ("type", "number");
  134. w.WriteString ("1");
  135. w.WriteEndElement ();
  136. w.Close ();
  137. Assert.AreEqual ("{\"foo\":1}", ResultString, "#1");
  138. }
  139. [Test]
  140. [ExpectedException (typeof (XmlException))]
  141. public void WriteStringForNull ()
  142. {
  143. w.WriteStartElement ("root");
  144. w.WriteStartElement ("foo");
  145. w.WriteAttributeString ("type", "null");
  146. w.WriteString ("1");
  147. }
  148. [Test]
  149. [ExpectedException (typeof (XmlException))]
  150. public void WriteStringForArray ()
  151. {
  152. w.WriteStartElement ("root");
  153. w.WriteAttributeString ("type", "object");
  154. w.WriteStartElement ("foo");
  155. w.WriteAttributeString ("type", "array");
  156. w.WriteString ("1");
  157. }
  158. [Test]
  159. // uh, no exception?
  160. public void WriteStringForBoolean ()
  161. {
  162. w.WriteStartElement ("root");
  163. w.WriteAttributeString ("type", "object");
  164. w.WriteStartElement ("foo");
  165. w.WriteAttributeString ("type", "boolean");
  166. w.WriteString ("xyz");
  167. w.WriteEndElement ();
  168. }
  169. [Test]
  170. [ExpectedException (typeof (XmlException))]
  171. public void WriteStringForObject ()
  172. {
  173. w.WriteStartElement ("root");
  174. w.WriteAttributeString ("type", "object");
  175. w.WriteString ("1");
  176. }
  177. [Test]
  178. [ExpectedException (typeof (XmlException))]
  179. public void WriteArrayNonItem ()
  180. {
  181. w.WriteStartElement ("root");
  182. w.WriteStartElement ("foo");
  183. w.WriteAttributeString ("type", "array");
  184. w.WriteStartElement ("bar");
  185. }
  186. [Test]
  187. public void WriteArray ()
  188. {
  189. w.WriteStartElement ("root"); // name is ignored
  190. w.WriteAttributeString ("type", "array");
  191. w.WriteElementString ("item", "v1");
  192. w.WriteElementString ("item", "v2");
  193. w.Close ();
  194. Assert.AreEqual (@"[""v1"",""v2""]", ResultString, "#1");
  195. }
  196. [Test]
  197. public void WriteArrayInObject ()
  198. {
  199. w.WriteStartElement ("root");
  200. w.WriteAttributeString ("type", "object");
  201. w.WriteStartElement ("foo");
  202. w.WriteAttributeString ("type", "array");
  203. w.WriteElementString ("item", "v1");
  204. w.WriteElementString ("item", "v2");
  205. w.Close ();
  206. Assert.AreEqual (@"{""foo"":[""v1"",""v2""]}", ResultString, "#1");
  207. }
  208. [Test]
  209. [ExpectedException (typeof (ArgumentException))]
  210. public void WriteStartElementNonEmptyNS ()
  211. {
  212. // namespaces are not allowed
  213. w.WriteStartElement (String.Empty, "x", "urn:foo");
  214. }
  215. [Test]
  216. [ExpectedException (typeof (ArgumentException))]
  217. public void WriteStartElementNonEmptyPrefix ()
  218. {
  219. // prefixes are not allowed
  220. w.WriteStartElement ("p", "x", "urn:foo");
  221. }
  222. [Test]
  223. [ExpectedException (typeof (XmlException))]
  224. public void WriteStartElementMultiTopLevel ()
  225. {
  226. w.WriteStartElement ("root");
  227. w.WriteEndElement ();
  228. // hmm...
  229. Assert.AreEqual (WriteState.Content, w.WriteState, "#1");
  230. // writing of multiple root elements is not supported
  231. w.WriteStartElement ("root2");
  232. w.Close ();
  233. Assert.AreEqual (String.Empty, ResultString, "#2");
  234. }
  235. [Test]
  236. [ExpectedException (typeof (ArgumentException))]
  237. public void WriteStartAttributeNonEmptyNS ()
  238. {
  239. // namespaces are not allowed
  240. w.WriteStartElement ("root");
  241. // well, empty prefix for a global attribute would be
  242. // replaced anyways ...
  243. w.WriteStartAttribute (String.Empty, "x", "urn:foo");
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentException))]
  247. public void WriteStartAttributeInXmlNamespace ()
  248. {
  249. // even "xml" namespace is not allowed (anyways only "type" is allowed ...)
  250. w.WriteStartElement ("root");
  251. w.WriteStartAttribute ("xml", "lang", "http://www.w3.org/XML/1998/namespace");
  252. }
  253. [Test]
  254. [ExpectedException (typeof (ArgumentNullException))]
  255. public void LookupPrefixNull ()
  256. {
  257. w.LookupPrefix (null);
  258. }
  259. [Test]
  260. public void LookupPrefix ()
  261. {
  262. // since namespaces are not allowed, it mostly makes no sense...
  263. Assert.AreEqual (String.Empty, w.LookupPrefix (String.Empty), "#1");
  264. Assert.IsNull (w.LookupPrefix ("urn:nonexistent"), "#2");
  265. Assert.AreEqual ("xml", w.LookupPrefix ("http://www.w3.org/XML/1998/namespace"), "#3");
  266. Assert.AreEqual ("xmlns", w.LookupPrefix ("http://www.w3.org/2000/xmlns/"), "#4");
  267. }
  268. [Test]
  269. public void WriteStartDocument ()
  270. {
  271. Assert.AreEqual (WriteState.Start, w.WriteState, "#1");
  272. w.WriteStartDocument ();
  273. Assert.AreEqual (WriteState.Start, w.WriteState, "#2");
  274. w.WriteStartDocument (true);
  275. Assert.AreEqual (WriteState.Start, w.WriteState, "#3");
  276. // So, it does nothing
  277. }
  278. [Test]
  279. public void WriteEndDocument ()
  280. {
  281. w.WriteEndDocument (); // so, it is completely wrong, but ignored.
  282. }
  283. [Test]
  284. [ExpectedException (typeof (NotSupportedException))]
  285. public void WriteDocType ()
  286. {
  287. w.WriteDocType (null, null, null, null);
  288. }
  289. [Test]
  290. [ExpectedException (typeof (NotSupportedException))]
  291. public void WriteComment ()
  292. {
  293. w.WriteComment ("test");
  294. }
  295. [Test]
  296. [ExpectedException (typeof (NotSupportedException))]
  297. public void WriteEntityRef ()
  298. {
  299. w.WriteEntityRef ("ent");
  300. }
  301. [Test]
  302. [ExpectedException (typeof (ArgumentException))]
  303. public void WriteProcessingInstruction ()
  304. {
  305. // since this method accepts case-insensitive "XML",
  306. // it throws ArgumentException.
  307. w.WriteProcessingInstruction ("T", "D");
  308. }
  309. [Test]
  310. public void WriteProcessingInstructionXML ()
  311. {
  312. // You might not know, but in some cases, things like
  313. // XmlWriter.WriteNode() is implemented to invoke
  314. // this method for writing XML declaration. This
  315. // check is (seems) case-insensitive.
  316. w.WriteProcessingInstruction ("XML", "foobar");
  317. // In this case, the data is simply ignored (as
  318. // WriteStartDocument() is).
  319. }
  320. [Test]
  321. public void WriteRaw ()
  322. {
  323. w.WriteStartElement ("root");
  324. w.WriteRaw ("sample");
  325. w.WriteRaw (new char [] {'0', '1', '2', '3'}, 1, 2);
  326. w.Close ();
  327. Assert.AreEqual ("\"sample12\"", ResultString);
  328. }
  329. [Test]
  330. public void WriteCData ()
  331. {
  332. w.WriteStartElement ("root");
  333. w.WriteCData ("]]>"); // this behavior is incompatible with ordinal XmlWriters.
  334. w.Close ();
  335. Assert.AreEqual ("\"]]>\"", ResultString);
  336. }
  337. [Test]
  338. public void WriteCharEntity ()
  339. {
  340. w.WriteStartElement ("root");
  341. w.WriteCharEntity ('>');
  342. w.Close ();
  343. Assert.AreEqual ("\">\"", ResultString);
  344. }
  345. [Test]
  346. public void WriteWhitespace ()
  347. {
  348. w.WriteStartElement ("root");
  349. w.WriteWhitespace ("\t \n\r");
  350. w.Close ();
  351. Assert.AreEqual (@"""\u0009 \u000a\u000d""", ResultString);
  352. }
  353. [Test]
  354. [ExpectedException (typeof (ArgumentException))]
  355. public void WriteWhitespaceNonWhitespace ()
  356. {
  357. w.WriteStartElement ("root");
  358. w.WriteWhitespace ("TEST");
  359. }
  360. [Test]
  361. [ExpectedException (typeof (InvalidOperationException))]
  362. public void WriteStringTopLevel ()
  363. {
  364. w.WriteString ("test");
  365. }
  366. [Test]
  367. [ExpectedException (typeof (XmlException))]
  368. public void WriteStartAttributeTopLevel ()
  369. {
  370. w.WriteStartAttribute ("test");
  371. }
  372. [Test]
  373. [ExpectedException (typeof (InvalidOperationException))]
  374. public void WriteStartDocumentAtClosed ()
  375. {
  376. w.Close ();
  377. w.WriteStartDocument ();
  378. }
  379. [Test]
  380. [ExpectedException (typeof (InvalidOperationException))]
  381. public void WriteStartElementAtClosed ()
  382. {
  383. w.Close ();
  384. w.WriteStartElement ("foo");
  385. }
  386. [Test]
  387. [ExpectedException (typeof (InvalidOperationException))]
  388. public void WriteProcessingInstructionAtClosed ()
  389. {
  390. w.Close ();
  391. w.WriteProcessingInstruction ("xml", "version='1.0'");
  392. }
  393. [Test]
  394. [ExpectedException (typeof (XmlException))]
  395. public void WriteMixedContent ()
  396. {
  397. w.WriteStartElement ("root");
  398. w.WriteString ("TEST");
  399. w.WriteStartElement ("mixed"); // is not allowed.
  400. }
  401. [Test]
  402. [ExpectedException (typeof (XmlException))]
  403. public void WriteStartElementInvalidTopLevelName ()
  404. {
  405. w.WriteStartElement ("anyname");
  406. }
  407. [Test]
  408. [ExpectedException (typeof (ArgumentNullException))]
  409. public void WriteStartElementNullName ()
  410. {
  411. w.WriteStartElement ("root");
  412. w.WriteAttributeString ("type", "object");
  413. w.WriteStartElement (null);
  414. }
  415. [Test]
  416. [ExpectedException (typeof (ArgumentException))]
  417. public void WriteStartElementEmptyName ()
  418. {
  419. w.WriteStartElement ("root");
  420. w.WriteAttributeString ("type", "object");
  421. w.WriteStartElement (String.Empty);
  422. // It is regarded as invalid name in JSON. However,
  423. // I don't think there is such limitation in JSON specification.
  424. }
  425. [Test]
  426. public void WriteStartElementWithRuntimeTypeName ()
  427. {
  428. w.WriteStartElement ("root");
  429. w.WriteAttributeString ("type", "object");
  430. w.WriteAttributeString ("__type", "FooType:#FooNamespace");
  431. w.Close ();
  432. Assert.AreEqual (@"{""__type"":""FooType:#FooNamespace""}", ResultString);
  433. }
  434. [Test]
  435. public void WriteStartElementWeirdName ()
  436. {
  437. w.WriteStartElement ("root");
  438. w.WriteAttributeString ("type", "object");
  439. w.WriteStartElement ("!!!");
  440. w.Close ();
  441. Assert.AreEqual (@"{""!!!"":""""}", ResultString);
  442. }
  443. [Test]
  444. public void WriteRootAsObject ()
  445. {
  446. w.WriteStartElement ("root");
  447. w.WriteStartAttribute ("type");
  448. w.WriteString ("object");
  449. w.WriteEndAttribute ();
  450. w.Close ();
  451. Assert.AreEqual ("{}", ResultString);
  452. }
  453. [Test]
  454. public void WriteRootAsArray ()
  455. {
  456. w.WriteStartElement ("root");
  457. w.WriteStartAttribute ("type");
  458. w.WriteString ("array");
  459. w.WriteEndAttribute ();
  460. w.Close ();
  461. Assert.AreEqual ("[]", ResultString);
  462. }
  463. [Test]
  464. public void WriteRootAsLiteral ()
  465. {
  466. w.WriteStartElement ("root");
  467. w.Close ();
  468. Assert.AreEqual ("\"\"", ResultString);
  469. }
  470. [Test]
  471. [ExpectedException (typeof (XmlException))]
  472. public void WriteEndElementOnAttribute ()
  473. {
  474. w.WriteStartElement ("root");
  475. w.WriteStartAttribute ("type");
  476. w.WriteString ("array");
  477. w.WriteEndElement ();
  478. }
  479. [Test]
  480. public void WriteAttributeAsSeparateStrings ()
  481. {
  482. w.WriteStartElement ("root");
  483. w.WriteStartAttribute ("type");
  484. w.WriteString ("arr");
  485. w.WriteString ("ay");
  486. w.WriteEndAttribute ();
  487. w.Close ();
  488. Assert.AreEqual ("[]", ResultString);
  489. }
  490. [Test]
  491. [ExpectedException (typeof (XmlException))]
  492. public void WriteStartAttributeInAttributeMode ()
  493. {
  494. w.WriteStartElement ("root");
  495. w.WriteStartAttribute ("type");
  496. w.WriteStartAttribute ("type");
  497. }
  498. [Test]
  499. [ExpectedException (typeof (XmlException))]
  500. public void WriteStartAttributeInContentMode ()
  501. {
  502. w.WriteStartElement ("root");
  503. w.WriteString ("TEST");
  504. w.WriteStartAttribute ("type");
  505. }
  506. [Test]
  507. [ExpectedException (typeof (XmlException))]
  508. public void WriteStartElementInAttributeMode ()
  509. {
  510. w.WriteStartElement ("root");
  511. w.WriteStartAttribute ("type");
  512. w.WriteStartElement ("child");
  513. }
  514. [Test]
  515. [ExpectedException (typeof (XmlException))]
  516. public void CloseAtAtributeState ()
  517. {
  518. w.WriteStartElement ("root");
  519. w.WriteStartAttribute ("type");
  520. w.WriteString ("array");
  521. // It calls WriteEndElement() without calling
  522. // WriteEndAttribute().
  523. w.Close ();
  524. }
  525. [Test]
  526. public void WriteSlashEscaped ()
  527. {
  528. w.WriteStartElement ("root");
  529. w.WriteString ("/my date/");
  530. w.WriteEndElement ();
  531. w.Close ();
  532. Assert.AreEqual ("\"\\/my date\\/\"", ResultString);
  533. }
  534. }
  535. }