1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
67 }
68 try {
69 original.getSports().add(new Sport("tennis"));
70 Assert.fail("sports is mutable");
71 } catch (UnsupportedOperationException e) {
72
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
89 }
90 try {
91 derived.getColors().add("blue");
92 Assert.fail("color's is mutable");
93 } catch (UnsupportedOperationException e) {
94
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
114 }
115 try {
116 derived.getColors().add("blue");
117 Assert.fail("color's is mutable");
118 } catch (UnsupportedOperationException e) {
119
120 }
121 }
122
123 }