Skip to content

Commit 1ab3482

Browse files
committed
testing
1 parent 1871355 commit 1ab3482

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/test/java/tools/jackson/dataformat/xml/ser/XmlGeneratorInitializerTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import java.util.Arrays;
6+
import java.util.List;
7+
58
import com.fasterxml.jackson.annotation.JsonRootName;
69

710
import tools.jackson.databind.ObjectWriter;
811
import tools.jackson.dataformat.xml.XmlMapper;
912
import tools.jackson.dataformat.xml.XmlTestUtil;
1013
import tools.jackson.dataformat.xml.XmlWriteFeature;
14+
import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
1115
import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;
1216

1317
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -44,6 +48,14 @@ static class RootNsWithAttrBean {
4448
public String value = "v";
4549
}
4650

51+
// Collection field in a namespace, with wrapper and item elements both namespaced
52+
@JsonRootName("Box")
53+
static class FruitBox {
54+
@JacksonXmlElementWrapper(namespace = "urn:produce:fruit", localName = "fruits")
55+
@JacksonXmlProperty(namespace = "urn:produce:fruit", localName = "fruit")
56+
public List<String> fruits = Arrays.asList("apple", "banana", "cherry");
57+
}
58+
4759
private final XmlMapper MAPPER = newMapper();
4860

4961
// // [dataformat-xml#150]: DTD writing -- ok cases
@@ -467,6 +479,46 @@ public void testRootElementAndAttributeBindingsCombined() throws Exception
467479
w.writeValueAsString(new RootNsWithAttrBean()));
468480
}
469481

482+
// Collection of namespaced items under a namespaced wrapper: binding should be
483+
// declared once on the wrapper element and inherited by every item
484+
@Test
485+
public void testNamespaceBindingOnCollection() throws Exception
486+
{
487+
// Without binding: auto-generated prefix (sanity baseline)
488+
assertEquals(a2q("<Box>"
489+
+"<wstxns1:fruits xmlns:wstxns1='urn:produce:fruit'>"
490+
+"<wstxns1:fruit>apple</wstxns1:fruit>"
491+
+"<wstxns1:fruit>banana</wstxns1:fruit>"
492+
+"<wstxns1:fruit>cherry</wstxns1:fruit>"
493+
+"</wstxns1:fruits>"
494+
+"</Box>"),
495+
MAPPER.writeValueAsString(new FruitBox()));
496+
497+
// With prefix binding: declared once on the wrapper, inherited by items
498+
ObjectWriter w = _writer(new XmlGeneratorInitializer()
499+
.addNamespace("f", "urn:produce:fruit"));
500+
assertEquals(a2q("<Box>"
501+
+"<f:fruits xmlns:f='urn:produce:fruit'>"
502+
+"<f:fruit>apple</f:fruit>"
503+
+"<f:fruit>banana</f:fruit>"
504+
+"<f:fruit>cherry</f:fruit>"
505+
+"</f:fruits>"
506+
+"</Box>"),
507+
w.writeValueAsString(new FruitBox()));
508+
509+
// With default namespace binding: wrapper uses unprefixed xmlns, items inherit
510+
w = _writer(new XmlGeneratorInitializer()
511+
.addDefaultNamespace("urn:produce:fruit"));
512+
assertEquals(a2q("<Box>"
513+
+"<fruits xmlns='urn:produce:fruit'>"
514+
+"<fruit>apple</fruit>"
515+
+"<fruit>banana</fruit>"
516+
+"<fruit>cherry</fruit>"
517+
+"</fruits>"
518+
+"</Box>"),
519+
w.writeValueAsString(new FruitBox()));
520+
}
521+
470522
// // [dataformat-xml#207]: namespace binding -- failing cases
471523

472524
@Test

0 commit comments

Comments
 (0)