⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customserializationtest.java

📁 xstream是一个把java object序列化成xml文件的开源库,轻便好用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            nothing = fields.get("theNothing", null);        }    }    public void testAllowsNamedFields() {        ObjectWithNamedFields obj = new ObjectWithNamedFields();        obj.name = "Joe";        obj.number = 99;        obj.someSoftware = new Software("tw", "xs");        obj.polymorphic = new Hardware("small", "ipod");        obj.nothing = null;        xstream.alias("with-named-fields", ObjectWithNamedFields.class);        xstream.alias("software", Software.class);        String expectedXml = ""                + "<with-named-fields serialization=\"custom\">\n"                + "  <with-named-fields>\n"                + "    <default>\n"                + "      <theName>Joe</theName>\n"                + "      <theNumber>99</theNumber>\n"                + "      <theSoftware>\n"                + "        <vendor>tw</vendor>\n"                + "        <name>xs</name>\n"                + "      </theSoftware>\n"                + "      <thePolymorphic class=\"com.thoughtworks.acceptance.objects.Hardware\">\n"                + "        <arch>small</arch>\n"                + "        <name>ipod</name>\n"                + "      </thePolymorphic>\n"                + "    </default>\n"                + "  </with-named-fields>\n"                + "</with-named-fields>";        assertBothWays(obj, expectedXml);    }    public void testUsesDefaultIfNamedFieldNotFound() {        xstream.alias("with-named-fields", ObjectWithNamedFields.class);        xstream.alias("software", Software.class);        String inputXml = ""                + "<with-named-fields serialization=\"custom\">\n"                + "  <with-named-fields>\n"                + "    <default>\n"                + "      <theSoftware>\n"                + "        <vendor>tw</vendor>\n"                + "        <name>xs</name>\n"                + "      </theSoftware>\n"                + "      <thePolymorphic class=\"com.thoughtworks.acceptance.objects.Hardware\">\n"                + "        <arch>small</arch>\n"                + "        <name>ipod</name>\n"                + "      </thePolymorphic>\n"                + "    </default>\n"                + "  </with-named-fields>\n"                + "</with-named-fields>";        ObjectWithNamedFields result = (ObjectWithNamedFields) xstream.fromXML(inputXml);        assertEquals(-1, result.number);        assertEquals("unknown", result.name);        assertEquals(new Software("tw", "xs"), result.someSoftware);    }    public void testCustomStreamWithNestedCustomStream() {        ObjectWithNamedFields outer = new ObjectWithNamedFields();        outer.name = "Joe";        outer.someSoftware = new Software("tw", "xs");        outer.nothing = null;        ObjectWithNamedFields inner = new ObjectWithNamedFields();        inner.name = "Thing";        outer.polymorphic = inner;        xstream.alias("with-named-fields", ObjectWithNamedFields.class);        xstream.alias("software", Software.class);        String expectedXml = ""                + "<with-named-fields serialization=\"custom\">\n"                + "  <with-named-fields>\n"                + "    <default>\n"                + "      <theName>Joe</theName>\n"                + "      <theNumber>0</theNumber>\n"                + "      <theSoftware>\n"                + "        <vendor>tw</vendor>\n"                + "        <name>xs</name>\n"                + "      </theSoftware>\n"                + "      <thePolymorphic class=\"with-named-fields\" serialization=\"custom\">\n"                + "        <with-named-fields>\n"                + "          <default>\n"                + "            <theName>Thing</theName>\n"                + "            <theNumber>0</theNumber>\n"                + "          </default>\n"                + "        </with-named-fields>\n"                + "      </thePolymorphic>\n"                + "    </default>\n"                + "  </with-named-fields>\n"                + "</with-named-fields>";        assertBothWays(outer, expectedXml);    }    public static class NoDefaultFields extends StandardObject implements Serializable {        private transient int something;        public NoDefaultFields() {        }        public NoDefaultFields(int something) {            this.something = something;        }        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {            in.defaultReadObject();            something = in.readInt();        }        private void writeObject(ObjectOutputStream out) throws IOException {            out.defaultWriteObject();            out.writeInt(something);        }    }    public void testObjectWithCallToDefaultWriteButNoDefaultFields() {        xstream.alias("x", NoDefaultFields.class);        String expectedXml = ""                + "<x serialization=\"custom\">\n"                + "  <x>\n"                + "    <default/>\n"                + "    <int>77</int>\n"                + "  </x>\n"                + "</x>";        assertBothWays(new NoDefaultFields(77), expectedXml);    }    public void testMaintainsBackwardsCompatabilityWithXStream1_1_0FieldFormat() {        ObjectWithNamedFields outer = new ObjectWithNamedFields();        outer.name = "Joe";        outer.someSoftware = new Software("tw", "xs");        outer.nothing = null;        ObjectWithNamedFields inner = new ObjectWithNamedFields();        inner.name = "Thing";        outer.polymorphic = inner;        xstream.alias("with-named-fields", ObjectWithNamedFields.class);        xstream.alias("software", Software.class);        String oldFormatOfXml = ""                + "<with-named-fields serialization=\"custom\">\n"                + "  <with-named-fields>\n"                + "    <fields>\n"                + "      <field name=\"theName\" class=\"string\">Joe</field>\n"                + "      <field name=\"theNumber\" class=\"int\">0</field>\n"                + "      <field name=\"theSoftware\" class=\"software\">\n"                + "        <vendor>tw</vendor>\n"                + "        <name>xs</name>\n"                + "      </field>\n"                + "      <field name=\"thePolymorphic\" class=\"with-named-fields\" serialization=\"custom\">\n"                + "        <with-named-fields>\n"                + "          <fields>\n"                + "            <field name=\"theName\" class=\"string\">Thing</field>\n"                + "            <field name=\"theNumber\" class=\"int\">0</field>\n"                + "          </fields>\n"                + "        </with-named-fields>\n"                + "      </field>\n"                + "    </fields>\n"                + "  </with-named-fields>\n"                + "</with-named-fields>";        assertEquals(outer, xstream.fromXML(oldFormatOfXml));    }    public static class ObjectWithNamedThatMatchRealFields extends StandardObject implements Serializable {        private String name;        private int number;        private void writeObject(ObjectOutputStream out) throws IOException {            ObjectOutputStream.PutField fields = out.putFields();            fields.put("name", name.toUpperCase());            fields.put("number", number * 100);            out.writeFields();        }        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {            ObjectInputStream.GetField fields = in.readFields();            name = ((String) fields.get("name", "unknown")).toLowerCase();            number = fields.get("number", 10000) / 100;        }    }    public void testSupportsWritingFieldsForObjectsThatDoNotExplicitlyDefineThem() {        xstream.alias("an-object", ObjectWithNamedThatMatchRealFields.class);        ObjectWithNamedThatMatchRealFields input = new ObjectWithNamedThatMatchRealFields();        input.name = "a name";        input.number = 5;        String expectedXml = ""                + "<an-object serialization=\"custom\">\n"                + "  <an-object>\n"                + "    <default>\n"                + "      <name>A NAME</name>\n"                + "      <number>500</number>\n"                + "    </default>\n"                + "  </an-object>\n"                + "</an-object>";        assertBothWays(input, expectedXml);    }    public static class ObjectThatReadsCustomFieldsButDoesNotWriteThem extends StandardObject implements Serializable {        private String name;        private int number;        private void writeObject(ObjectOutputStream out) throws IOException {            out.defaultWriteObject();        }        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {            ObjectInputStream.GetField fields = in.readFields();            name = ((String) fields.get("name", "unknown"));            number = fields.get("number", 10000);        }    }    public void testSupportsGetFieldsWithoutPutFields() {        xstream.alias("an-object", ObjectThatReadsCustomFieldsButDoesNotWriteThem.class);        ObjectThatReadsCustomFieldsButDoesNotWriteThem input = new ObjectThatReadsCustomFieldsButDoesNotWriteThem();        input.name = "a name";        input.number = 5;        String expectedXml = ""                + "<an-object serialization=\"custom\">\n"                + "  <an-object>\n"                + "    <default>\n"                + "      <number>5</number>\n"                + "      <name>a name</name>\n"                + "    </default>\n"                + "  </an-object>\n"                + "</an-object>";        assertBothWays(input, expectedXml);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -