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.test;
17  
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.kuali.common.util.xml.jaxb.JAXBXmlService;
25  import org.kuali.common.util.xml.jaxb.Sport;
26  import org.kuali.common.util.xml.jaxb.University;
27  import org.kuali.common.util.xml.service.XmlService;
28  
29  public class BasicTest {
30  	String encoding = "UTF-8";
31  
32  	static {
33  		String os = System.getProperty("os.name") + ", " + System.getProperty("os.version");
34  		String jdk = System.getProperty("java.vm.name") + ", " + System.getProperty("java.runtime.version");
35  		System.out.println(os);
36  		System.out.println(jdk);
37  	}
38  
39  	protected XmlService getService() {
40  		return new JAXBXmlService.Builder().useEclipseLinkMoxyProvider(true).build();
41  	}
42  
43  	@Test
44  	public void testIdenticalRoundTripXML() {
45  		XmlService service = getService();
46  		List<Sport> sports = Arrays.asList(new Sport("soccer"), new Sport("football"));
47  		List<String> colors = Arrays.asList("yellow", "green");
48  		University original = new University("Cal Poly", sports, colors);
49  		String originalXml = service.toXml(original, encoding);
50  		System.out.println(originalXml);
51  		University derived = service.getObjectFromXml(originalXml, encoding, University.class);
52  		String derivedXml = service.toXml(derived, encoding);
53  		System.out.println(derivedXml);
54  		Assert.assertEquals(originalXml, derivedXml);
55  	}
56  
57  	@Test
58  	public void testImmutability() {
59  		List<Sport> sports = Arrays.asList(new Sport("soccer"), new Sport("football"));
60  		List<String> colors = Arrays.asList("yellow", "green");
61  		University original = new University("Cal Poly", sports, colors);
62  		try {
63  			original.getColors().add("blue");
64  			Assert.fail("color's is mutable");
65  		} catch (UnsupportedOperationException e) {
66  			// ignore
67  		}
68  		try {
69  			original.getSports().add(new Sport("tennis"));
70  			Assert.fail("sports is mutable");
71  		} catch (UnsupportedOperationException e) {
72  			// ignore
73  		}
74  	}
75  
76  	@Test
77  	public void testMutabilityAfterMarshalling() {
78  		XmlService service = getService();
79  		List<Sport> sports = Arrays.asList(new Sport("soccer"), new Sport("football"));
80  		List<String> colors = Arrays.asList("yellow", "green");
81  		University original = new University("Cal Poly", sports, colors);
82  		String originalXml = service.toXml(original, encoding);
83  		University derived = service.getObjectFromXml(originalXml, encoding, University.class);
84  		try {
85  			derived.getSports().add(new Sport("tennis"));
86  			Assert.fail("sports is mutable");
87  		} catch (UnsupportedOperationException e) {
88  			// ignore
89  		}
90  		try {
91  			derived.getColors().add("blue");
92  			Assert.fail("color's is mutable");
93  		} catch (UnsupportedOperationException e) {
94  			// ignore
95  		}
96  	}
97  
98  	@Test
99  	public void testNullValuesAfterMarshalling() {
100 		XmlService service = getService();
101 		List<Sport> sports = Collections.emptyList();
102 		List<String> colors = Collections.emptyList();
103 		University original = new University("Cal Poly", sports, colors);
104 		String originalXml = service.toXml(original, encoding);
105 		System.out.println(originalXml);
106 		University derived = service.getObjectFromXml(originalXml, encoding, University.class);
107 		Assert.assertNotNull(derived.getColors());
108 		Assert.assertNotNull(derived.getSports());
109 		try {
110 			derived.getSports().add(new Sport("tennis"));
111 			Assert.fail("sports is mutable");
112 		} catch (UnsupportedOperationException e) {
113 			// ignore
114 		}
115 		try {
116 			derived.getColors().add("blue");
117 			Assert.fail("color's is mutable");
118 		} catch (UnsupportedOperationException e) {
119 			// ignore
120 		}
121 	}
122 
123 }