XStreamXMLSerialization implements XMLSerialization { public <T> Serializer from(T object) { response.setContentType("application/xml"); return getSerializer().from(object); } XStreamXMLSerialization(HttpServletResponse response, TypeNameExtractor extractor, ProxyInitializer initializer, XStreamBuilder builder); boolean accepts(String format); Serializer from(T object); Serializer from(T object, String alias);  }
@Test public void shouldSerializeGenericClass() { String expectedResult = "<genericWrapper>\n <entityList class=\"list\">\n <client>\n <name>washington botelho</name>\n </client>\n <client>\n <name>washington botelho</name>\n </client>\n </entityList>\n <total>2</total>\n</genericWrapper>"; Collection<Client> entityList = new ArrayList<Client>(); entityList.add(new Client("washington botelho")); entityList.add(new Client("washington botelho")); GenericWrapper<Client> wrapper = new GenericWrapper<Client>(entityList, entityList.size()); serialization.from(wrapper).include("entityList").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeMaps() { String expectedResult = "<properties>\n <map>\n <entry>\n <string>test</string>\n <string>true</string>\n </entry>\n </map>\n</properties>"; serialization.from(new Properties("test", "true")).include("map").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeAllBasicFields() { String expectedResult = "<order>\n <price>15.0</price>\n <comments>pack it nicely, please</comments>\n</order>"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldUseAlias() { String expectedResult = "<customOrder>\n <price>15.0</price>\n <comments>pack it nicely, please</comments>\n</customOrder>"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order, "customOrder").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeEnumFields() { Order order = new BasicOrder(new Client("guilherme silveira"), 15.0, "pack it nicely, please", Type.basic); serialization.from(order).serialize(); String result = result(); assertThat(result, containsString("<type>basic</type>")); }
@Test public void shouldSerializeCollection() { String expectedResult = " <order>\n <price>15.0</price>\n <comments>pack it nicely, please</comments>\n </order>\n"; expectedResult += expectedResult; expectedResult = "<list>\n" + expectedResult + "</list>"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order)).serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeCollectionWithPrefixTag() { String expectedResult = " <order>\n <price>15.0</price>\n <comments>pack it nicely, please</comments>\n </order>\n"; expectedResult += expectedResult; expectedResult = "<orders>\n" + expectedResult + "</orders>"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order), "orders").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldIncludeFieldsFromACollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(Arrays.asList(order, order), "orders").include("items").serialize(); assertThat(result(), containsString("<items>")); assertThat(result(), containsString("<name>name</name>")); assertThat(result(), containsString("<price>12.99</price>")); assertThat(result(), containsString("</items>")); }
@Test public void shouldWorkWithEmptyCollections() { serialization.from(new ArrayList<Order>(), "orders").serialize(); assertThat(result(), containsString("<orders/>")); }
@Test public void shouldIncludeAllFieldsWhenRecursive() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(order).recursive().serialize(); assertThat(result(), containsString("<items>")); assertThat(result(), containsString("<name>name</name>")); assertThat(result(), containsString("<price>12.99</price>")); }
@Test public void shouldExcludeFieldsFromACollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order), "orders").exclude("price").serialize(); assertThat(result(), not(containsString("<price>"))); }
@Test(expected=IllegalArgumentException.class) public void shouldThrowAnExceptionWhenYouIncludeANonExistantField() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(order).include("wrongFieldName").serialize(); }
@Test(expected=IllegalArgumentException.class) public void shouldThrowAnExceptionWhenYouIncludeANonExistantFieldInsideOther() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(order).include("wrongFieldName.another").serialize(); }
@Test public void shouldSerializeParentFields() { Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).serialize(); assertThat(result(), containsString("<notes>complex package</notes>")); }
@Test public void shouldOptionallyExcludeFields() { String expectedResult = "<order>\n <comments>pack it nicely, please</comments>\n</order>"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).exclude("price").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldOptionallyIncludeFieldAndNotItsNonPrimitiveFields() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client").serialize(); assertThat(result(), containsString("<name>guilherme silveira</name>")); assertThat(result(), not(containsString("R. Vergueiro"))); }
@Test public void shouldOptionallyIncludeChildField() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client", "client.address").serialize(); assertThat(result(), containsString("<street>R. Vergueiro</street>")); }
@Test public void shouldOptionallyExcludeChildField() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).include("client").exclude("client.name").serialize(); assertThat(result(), containsString("<client/>")); assertThat(result(), not(containsString("<name>guilherme silveira</name>"))); }
@Test public void shouldOptionallyIncludeListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("any item", 12.99)); serialization.from(order).include("items").serialize(); assertThat(result(), containsString("<items>")); assertThat(result(), containsString("<name>any item</name>")); assertThat(result(), containsString("<price>12.99</price>")); assertThat(result(), containsString("</items>")); }
@Test public void shouldOptionallyExcludeFieldsFromIncludedListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("any item", 12.99)); serialization.from(order).include("items").exclude("items.price").serialize(); assertThat(result(), containsString("<items>")); assertThat(result(), containsString("<name>any item</name>")); assertThat(result(), not(containsString("12.99"))); assertThat(result(), containsString("</items>")); }
@Test public void shouldExcludeAllPrimitiveFields() { String expectedResult = "<order/>"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldExcludeAllPrimitiveParentFields() { String expectedResult = "<advancedOrder/>"; Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).excludeAll().serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldExcludeAllThanIncludeAndSerialize() { Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().include("price").serialize(); assertThat(result(), containsString("<order>")); assertThat(result(), containsString("<price>")); assertThat(result(), containsString("15.0")); assertThat(result(), containsString("</price>")); assertThat(result(), containsString("</order>")); }
@Test public void shouldAutomaticallyReadXStreamAnnotations() { WithAlias alias = new WithAlias(); alias.abc = "Duh!"; serialization.from(alias).serialize(); assertThat(result(), is("<withAlias>\n <def>Duh!</def>\n</withAlias>")); }
@Test public void shouldAutomaticallyReadXStreamAnnotationsForIncludedAttributes() { WithAlias alias = new WithAlias(); alias.abc = "Duh!"; WithAliasedAttribute attribute = new WithAliasedAttribute(); attribute.aliased = alias; serialization.from(attribute).include("aliased").serialize(); assertThat(result(), is("<withAliasedAttribute>\n <aliased>\n <def>Duh!</def>\n </aliased>\n</withAliasedAttribute>")); }
@Test public void shouldBeAbleToIncludeSubclassesFields() throws Exception { serialization.from(new B()).include("field2").serialize(); assertThat(result(), is("<b>\n <field2/>\n</b>")); }