Skip to content

Commit b1f822f

Browse files
committed
Moar testing!
1 parent 64bdf3f commit b1f822f

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ static class NsAttrBean {
2929
public String lang = "en";
3030
}
3131

32+
// Root element in its own namespace
33+
@JsonRootName(value = "Root", namespace = "urn:ns:root")
34+
static class RootNsBean {
35+
public String value = "v";
36+
}
37+
38+
// Root element in its own namespace, plus an attribute on the root
39+
// in a *different* namespace
40+
@JsonRootName(value = "Root", namespace = "urn:ns:root")
41+
static class RootNsWithAttrBean {
42+
@JacksonXmlProperty(isAttribute = true, namespace = "urn:ns:attr", localName = "id")
43+
public String id = "42";
44+
public String value = "v";
45+
}
46+
3247
private final XmlMapper MAPPER = newMapper();
3348

3449
// // [dataformat-xml#150]: DTD writing -- ok cases
@@ -408,6 +423,50 @@ public void testUnusedNamespaceBindingHasNoEffect() throws Exception
408423
assertEquals(EXPECTED, w.writeValueAsString(new Ingredients()));
409424
}
410425

426+
// Root element's own namespace can be bound as the default namespace
427+
// (Jackson's root-element serializer prefers default-namespace form regardless,
428+
// so this just verifies the binding does not interfere)
429+
@Test
430+
public void testRootElementDefaultNamespaceBinding() throws Exception
431+
{
432+
ObjectWriter w = _writer(new XmlGeneratorInitializer()
433+
.addDefaultNamespace("urn:ns:root"));
434+
assertEquals(a2q("<Root xmlns='urn:ns:root'><value xmlns=''>v</value></Root>"),
435+
w.writeValueAsString(new RootNsBean()));
436+
}
437+
438+
// An attribute on the root element, in a namespace, should honor the bound prefix
439+
@Test
440+
public void testRootElementAttributeNamespaceBinding() throws Exception
441+
{
442+
// Without binding: Woodstox assigns wstxns1 (sanity baseline)
443+
assertEquals(a2q("<Root xmlns='urn:ns:root' xmlns:wstxns1='urn:ns:attr' wstxns1:id='42'>"
444+
+"<value xmlns=''>v</value>"
445+
+"</Root>"),
446+
MAPPER.writeValueAsString(new RootNsWithAttrBean()));
447+
448+
// With binding: caller-supplied prefix is used
449+
ObjectWriter w = _writer(new XmlGeneratorInitializer()
450+
.addNamespace("a", "urn:ns:attr"));
451+
assertEquals(a2q("<Root xmlns='urn:ns:root' xmlns:a='urn:ns:attr' a:id='42'>"
452+
+"<value xmlns=''>v</value>"
453+
+"</Root>"),
454+
w.writeValueAsString(new RootNsWithAttrBean()));
455+
}
456+
457+
// Bindings for root namespace AND root attribute namespace can coexist
458+
@Test
459+
public void testRootElementAndAttributeBindingsCombined() throws Exception
460+
{
461+
ObjectWriter w = _writer(new XmlGeneratorInitializer()
462+
.addDefaultNamespace("urn:ns:root")
463+
.addNamespace("a", "urn:ns:attr"));
464+
assertEquals(a2q("<Root xmlns='urn:ns:root' xmlns:a='urn:ns:attr' a:id='42'>"
465+
+"<value xmlns=''>v</value>"
466+
+"</Root>"),
467+
w.writeValueAsString(new RootNsWithAttrBean()));
468+
}
469+
411470
// // [dataformat-xml#207]: namespace binding -- failing cases
412471

413472
@Test

0 commit comments

Comments
 (0)