View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.xml.jaxb.issue415;
17  
18  import javax.xml.bind.JAXBContext;
19  import javax.xml.bind.Marshaller;
20  import javax.xml.bind.annotation.XmlAccessType;
21  import javax.xml.bind.annotation.XmlAccessorType;
22  import javax.xml.bind.annotation.XmlElement;
23  import javax.xml.bind.annotation.XmlRootElement;
24  import javax.xml.bind.annotation.adapters.XmlAdapter;
25  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
26  
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Unit test for https://java.net/jira/browse/JAXB-415
32   * 
33   * Marshaller.marshall throws NPE if an adapter adapts a non-null bound value to null
34   */
35  @XmlRootElement
36  @XmlAccessorType(XmlAccessType.FIELD)
37  public class JAXBIssue415Test {
38  
39  	@XmlElement
40  	@XmlJavaTypeAdapter(JAXBIssue415TestAdapter.class)
41  	private String value = "foo"; // Bound value is always initialized to "foo"
42  
43  	@Test
44  	public void testIssue415() throws Exception {
45  		String os = System.getProperty("os.name") + ", " + System.getProperty("os.version");
46  		String jdk = System.getProperty("java.vm.name") + ", " + System.getProperty("java.runtime.version");
47  		try {
48  			Marshaller m = JAXBContext.newInstance(JAXBIssue415Test.class).createMarshaller();
49  			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
50  			m.marshal(new JAXBIssue415Test(), System.out);
51  		} catch (NullPointerException e) {
52  			Assert.fail("JAXB issue 415 is still occurring on [" + os + "] [" + jdk + "]");
53  		}
54  	}
55  
56  	public static class JAXBIssue415TestAdapter extends XmlAdapter<String, String> {
57  
58  		@Override
59  		public String marshal(String value) {
60  			return null; // Ignore the bound value and just always return null
61  		}
62  
63  		@Override
64  		public String unmarshal(String value) {
65  			return null;
66  		}
67  	}
68  }