File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\jenkins\util\xstream\XStreamDOMTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2012, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package jenkins.util.xstream;
25
26 import hudson.util.XStream2;
27 import org.apache.commons.io.IOUtils;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import static org.junit.Assert.*;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.StringReader;
36 import java.io.StringWriter;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 /**
43 * @author Kohsuke Kawaguchi
44 */
45 public class XStreamDOMTest {
46
47 private XStream2 xs;
48
49 public static class Foo {
50 XStreamDOM bar;
51 XStreamDOM zot;
52 }
53
54 @Before
55 public void setUp() throws Exception {
56 xs = new XStream2();
57 xs.alias("foo", Foo.class);
58 }
59
60 @Test
61 public void testMarshal() throws IOException {
62 Foo foo = createSomeFoo();
63 String xml = xs.toXML(foo);
64 System.out.println(xml);
65 assertEquals(getTestData1().trim(), xml.trim());
66 }
67
68 private String getTestData1() throws IOException {
69 return getTestData("XStreamDOMTest.data1.xml");
70 }
71
72 private String getTestData(String resourceName) throws IOException {
73 return IOUtils.toString(getClass().getResourceAsStream(resourceName)).replaceAll("\r\n", "\n");
74 }
75
76
77 private Foo createSomeFoo() {
78 Foo foo = new Foo();
79 foo.bar = new XStreamDOM("test1", Collections.singletonMap("key", "value"),"text!");
80 foo.zot = new XStreamDOM("test2", Collections.singletonMap("key","value"),Collections.singletonList(foo.bar));
81 return foo;
82 }
83
84 @Test
85 public void testUnmarshal() throws Exception {
86 InputStream is = XStreamDOMTest.class.getResourceAsStream("XStreamDOMTest.data1.xml");
87 Foo foo;
88 try {
89 foo = (Foo) xs.fromXML(is);
90 } finally {
91 is.close();
92 }
93 assertEquals("test1",foo.bar.getTagName());
94 assertEquals("value",foo.bar.getAttribute("key"));
95 assertEquals("text!",foo.bar.getValue());
96 }
97
98 @Test
99 public void testWriteToDOM() throws Exception {
100 // roundtrip via DOM
101 XStreamDOM dom = XStreamDOM.from(xs, createSomeFoo());
102 Foo foo = dom.unmarshal(xs);
103
104 String xml = xs.toXML(foo);
105 System.out.println(xml);
106 assertEquals(getTestData1().trim(), xml.trim());
107 }
108
109 @Test
110 public void testNoChild() {
111 String[] in = new String[0];
112 XStreamDOM dom = XStreamDOM.from(xs, in);
113 System.out.println(xs.toXML(dom));
114 String[] out = dom.unmarshal(xs);
115 assertEquals(in.length, out.length);
116 }
117
118 @Test
119 public void testNameEscape() {
120 Object o = new Name_That_Gets_Escaped();
121 XStreamDOM dom = XStreamDOM.from(xs, o);
122 System.out.println(xs.toXML(dom));
123 Object out = dom.unmarshal(xs);
124 assertEquals(o.getClass(),out.getClass());
125 }
126
127 public static class Name_That_Gets_Escaped {}
128
129 public static class DomInMap {
130 Map<String,XStreamDOM> values = new HashMap<String, XStreamDOM>();
131 }
132
133 @Test
134 public void testDomInMap() {
135 DomInMap v = new DomInMap();
136 v.values.put("foo",createSomeFoo().bar);
137 String xml = xs.toXML(v);
138 Object v2 = xs.fromXML(xml);
139 assertTrue(v2 instanceof DomInMap);
140 assertXStreamDOMEquals(v.values.get("foo"), ((DomInMap)v2).values.get("foo"));
141 }
142
143 private void assertXStreamDOMEquals(XStreamDOM expected, XStreamDOM actual) {
144 assertEquals(expected.getTagName(), actual.getTagName());
145 assertEquals(expected.getValue(), actual.getValue());
146
147 assertEquals(expected.getAttributeCount(), actual.getAttributeCount());
148 for (int i=0; i<expected.getAttributeCount(); i++) {
149 assertEquals(expected.getAttributeName(i), actual.getAttributeName(i));
150 assertEquals(expected.getAttribute(i), actual.getAttribute(i));
151 }
152
153 if (expected.getChildren() == null) {
154 assertNull(actual.getChildren());
155 } else {
156 assertEquals(expected.getChildren().size(), actual.getChildren().size());
157 int childrenCount = expected.getChildren().size();
158 for (int i=0; i<childrenCount; i++) {
159 assertXStreamDOMEquals(expected.getChildren().get(i), actual.getChildren().get(i));
160 }
161 }
162 }
163
164 @Test
165 public void readFromInputStream() throws Exception {
166 for (String name : new String[]{"XStreamDOMTest.data1.xml","XStreamDOMTest.data2.xml"}) {
167 String input = getTestData(name);
168 XStreamDOM dom = XStreamDOM.from(new StringReader(input));
169 StringWriter sw = new StringWriter();
170 dom.writeTo(sw);
171 assertEquals(input.trim(),sw.toString().trim());
172 }
173 }
174
175 /**
176 * Regardless of how we read XML into XStreamDOM, XStreamDOM should retain the raw XML infoset,
177 * which means escaped names.
178 */
179 @Test
180 public void escapeHandling() throws Exception {
181 String input = getTestData("XStreamDOMTest.data3.xml");
182
183 XStreamDOM dom = XStreamDOM.from(new StringReader(input));
184 List<XStreamDOM> children = dom.getChildren().get(0).getChildren().get(0).getChildren();
185 assertNamesAreEscaped(children);
186
187 Foo foo = (Foo) xs.fromXML(new StringReader(input));
188 assertNamesAreEscaped(foo.bar.getChildren());
189
190 StringWriter sw = new StringWriter();
191 dom.writeTo(sw);
192 assertTrue(sw.toString().contains("bar_-bar"));
193 assertTrue(sw.toString().contains("zot__bar"));
194
195 String s = xs.toXML(foo);
196 assertTrue(s.contains("bar_-bar"));
197 assertTrue(s.contains("zot__bar"));
198 }
199
200 private void assertNamesAreEscaped(List<XStreamDOM> children) {
201 assertEquals("bar_-bar", children.get(0).getTagName());
202 assertEquals("zot__bar", children.get(1).getTagName());
203 }
204 }
205