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