|
| 1 | +package org.jvnet.jaxb.tests.namespace; |
| 2 | + |
| 3 | +import jakarta.xml.bind.JAXBContext; |
| 4 | +import jakarta.xml.bind.JAXBElement; |
| 5 | +import jakarta.xml.bind.JAXBException; |
| 6 | +import jakarta.xml.bind.annotation.XmlNs; |
| 7 | +import jakarta.xml.bind.annotation.XmlSchema; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | +import org.jvnet.jaxb.test.namespace.a.A1Type; |
| 11 | +import org.jvnet.jaxb.test.namespace.c.C1Type; |
| 12 | +import org.jvnet.jaxb.test.namespace.c.CType; |
| 13 | + |
| 14 | +import javax.xml.namespace.QName; |
| 15 | +import java.io.StringWriter; |
| 16 | +import java.util.Arrays; |
| 17 | + |
| 18 | +public class CTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testC() throws JAXBException { |
| 22 | + CType c = new CType(); |
| 23 | + XmlSchema schema = c.getClass().getPackage().getAnnotation(XmlSchema.class); |
| 24 | + XmlNs[] namespaces = schema.xmlns(); |
| 25 | + Assert.assertEquals(3, namespaces.length); |
| 26 | + XmlNs aNs = Arrays.stream(namespaces).filter(ns -> "aprefix".equals(ns.prefix())).findFirst().orElse(null); |
| 27 | + Assert.assertNotNull(aNs); |
| 28 | + Assert.assertEquals("a", aNs.namespaceURI()); |
| 29 | + Assert.assertEquals("aprefix", aNs.prefix()); |
| 30 | + XmlNs bNs = Arrays.stream(namespaces).filter(ns -> "bprefix".equals(ns.prefix())).findFirst().orElse(null); |
| 31 | + Assert.assertNotNull(bNs); |
| 32 | + Assert.assertEquals("b", bNs.namespaceURI()); |
| 33 | + Assert.assertEquals("bprefix", bNs.prefix()); |
| 34 | + XmlNs cNs = Arrays.stream(namespaces).filter(ns -> "creal".equals(ns.prefix())).findFirst().orElse(null); |
| 35 | + Assert.assertNotNull(cNs); |
| 36 | + Assert.assertEquals("c", cNs.namespaceURI()); |
| 37 | + Assert.assertEquals("creal", cNs.prefix()); |
| 38 | + |
| 39 | + c.setA(new A1Type()); |
| 40 | + c.setC(new C1Type()); |
| 41 | + |
| 42 | + JAXBElement<CType> jaxbElement |
| 43 | + = new JAXBElement<>( new QName("c", "cPart"), CType.class, c); |
| 44 | + JAXBContext context = JAXBContext.newInstance(CType.class.getPackage().getName()); |
| 45 | + StringWriter writer = new StringWriter(); |
| 46 | + context.createMarshaller().marshal(jaxbElement, writer); |
| 47 | + String marshalled = writer.toString(); |
| 48 | + |
| 49 | + Assert.assertNotNull(marshalled); |
| 50 | + String expectedA = "xmlns:aprefix=\"a\""; |
| 51 | + Assert.assertTrue(marshalled.contains(expectedA)); |
| 52 | + String expectedB = "xmlns:bprefix=\"b\""; |
| 53 | + Assert.assertTrue(marshalled.contains(expectedB)); |
| 54 | + String expectedC = "xmlns:creal=\"c\""; |
| 55 | + Assert.assertTrue(marshalled.contains(expectedC)); |
| 56 | + } |
| 57 | +} |
0 commit comments