001package org.kuali.common.util.xml.jaxb.issue415;
002
003import javax.xml.bind.JAXBContext;
004import javax.xml.bind.Marshaller;
005import javax.xml.bind.annotation.XmlAccessType;
006import javax.xml.bind.annotation.XmlAccessorType;
007import javax.xml.bind.annotation.XmlElement;
008import javax.xml.bind.annotation.XmlRootElement;
009import javax.xml.bind.annotation.adapters.XmlAdapter;
010import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
011
012import org.junit.Assert;
013import org.junit.Test;
014
015/**
016 * Unit test for https://java.net/jira/browse/JAXB-415
017 * 
018 * Marshaller.marshall throws NPE if an adapter adapts a non-null bound value to null
019 */
020@XmlRootElement
021@XmlAccessorType(XmlAccessType.FIELD)
022public class JAXBIssue415Test {
023
024        @XmlElement
025        @XmlJavaTypeAdapter(JAXBIssue415TestAdapter.class)
026        private String value = "foo"; // Bound value is always initialized to "foo"
027
028        @Test
029        public void testIssue415() throws Exception {
030                String os = System.getProperty("os.name") + ", " + System.getProperty("os.version");
031                String jdk = System.getProperty("java.vm.name") + ", " + System.getProperty("java.runtime.version");
032                try {
033                        Marshaller m = JAXBContext.newInstance(JAXBIssue415Test.class).createMarshaller();
034                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
035                        m.marshal(new JAXBIssue415Test(), System.out);
036                } catch (NullPointerException e) {
037                        Assert.fail("JAXB issue 415 is still occurring on [" + os + "] [" + jdk + "]");
038                }
039        }
040
041        public static class JAXBIssue415TestAdapter extends XmlAdapter<String, String> {
042
043                @Override
044                public String marshal(String value) {
045                        return null; // Ignore the bound value and just always return null
046                }
047
048                @Override
049                public String unmarshal(String value) {
050                        return null;
051                }
052        }
053}