/src/test/java/com/alibaba/json/bvt/StringFieldTest_special_2.java

https://github.com/alibaba/fastjson · Java · 72 lines · 54 code · 15 blank · 3 comment · 7 complexity · 96f28253a542e24597aa5d9ef5fdd3ff MD5 · raw file

  1. package com.alibaba.json.bvt;
  2. import org.junit.Assert;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import junit.framework.TestCase;
  6. public class StringFieldTest_special_2 extends TestCase {
  7. public void test_special() throws Exception {
  8. Model model = new Model();
  9. StringBuilder buf = new StringBuilder();
  10. for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
  11. buf.append((char) i);
  12. }
  13. model.name = buf.toString();
  14. String text = JSON.toJSONString(model);
  15. Model model2 = JSON.parseObject(text, Model.class);
  16. Assert.assertEquals(model.name, model2.name);
  17. }
  18. public void test_special_browsecue() throws Exception {
  19. Model model = new Model();
  20. StringBuilder buf = new StringBuilder();
  21. for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
  22. buf.append((char) i);
  23. }
  24. model.name = buf.toString();
  25. String text = JSON.toJSONString(model, SerializerFeature.BrowserSecure);
  26. text = text.replaceAll("&lt;", "<");
  27. text = text.replaceAll("&gt;", ">");
  28. // text = text.replaceAll("\\\\/", "/");
  29. Model model2 = JSON.parseObject(text, Model.class);
  30. for (int i = 0; i < model.name.length() && i < model2.name.length(); ++i) {
  31. char c1 = model.name.charAt(i);
  32. char c2 = model.name.charAt(i);
  33. if (c1 != c2) {
  34. System.out.println("diff : " + c1 + " -> " + c2);
  35. break;
  36. }
  37. }
  38. // String str = model2.name.substring(65535);
  39. // System.out.println(str);
  40. Assert.assertEquals(model.name.length(), model2.name.length());
  41. Assert.assertEquals(model.name, model2.name);
  42. }
  43. public void test_special_browsecompatible() throws Exception {
  44. Model model = new Model();
  45. StringBuilder buf = new StringBuilder();
  46. for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
  47. buf.append((char) i);
  48. }
  49. model.name = buf.toString();
  50. String text = JSON.toJSONString(model, SerializerFeature.BrowserCompatible);
  51. Model model2 = JSON.parseObject(text, Model.class);
  52. Assert.assertEquals(model.name, model2.name);
  53. }
  54. private static class Model {
  55. public String name;
  56. }
  57. }