View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.xml;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.collections.*;
23  import org.junit.jupiter.api.*;
24  
25  class XmlParser_Test extends TestBase {
26  
27  	@Test void a01_genericAttributes() throws Exception {
28  		var xml = "<A b='1'><c>2</c></A>";
29  		var m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
30  		assertEquals("{b:'1',c:'2'}", m.toString());
31  	}
32  
33  	@Test void a02_genericWithChildElements() throws Exception {
34  
35  		var xml = "<A><B><C>c</C></B></A>";
36  		var m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
37  		assertEquals("{B:{C:'c'}}", m.toString());
38  
39  		xml = "<A><B><C1>c1</C1><C2>c2</C2></B></A>";
40  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
41  		assertEquals("{B:{C1:'c1',C2:'c2'}}", m.toString());
42  
43  		xml = "<A><B><C><D1>d1</D1><D2>d2</D2></C></B></A>";
44  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
45  		assertEquals("{B:{C:{D1:'d1',D2:'d2'}}}", m.toString());
46  
47  		xml = "<A><B><C><D1 d1a='d1av'><E1>e1</E1></D1><D2 d2a='d2av'><E2>e2</E2></D2></C></B></A>";
48  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
49  		assertEquals("{B:{C:{D1:{d1a:'d1av',E1:'e1'},D2:{d2a:'d2av',E2:'e2'}}}}", m.toString());
50  
51  		xml = "<A><B b='b'><C>c</C></B></A>";
52  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
53  		assertEquals("{B:{b:'b',C:'c'}}", m.toString());
54  
55  		xml = "<A><B b='b'>c</B></A>";
56  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
57  		assertEquals("{B:{b:'b',contents:'c'}}", m.toString());
58  
59  		xml = "<A><B>b1</B><B>b2</B></A>";
60  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
61  		assertEquals("{B:['b1','b2']}", m.toString());
62  
63  		xml = "<A><B><C>c1</C><C>c2</C></B></A>";
64  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
65  		assertEquals("{B:{C:['c1','c2']}}", m.toString());
66  
67  		xml = "<A><B v='v1'>b1</B><B v='v2'>b2</B></A>";
68  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
69  		assertEquals("{B:[{v:'v1',contents:'b1'},{v:'v2',contents:'b2'}]}", m.toString());
70  
71  		xml = "<A><B><C v='v1'>c1</C><C v='v2'>c2</C></B></A>";
72  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
73  		assertEquals("{B:{C:[{v:'v1',contents:'c1'},{v:'v2',contents:'c2'}]}}", m.toString());
74  
75  		xml = "<A><B c='c1'><c>c2</c></B></A>";
76  		m = XmlParser.DEFAULT.parse(xml, JsonMap.class);
77  		assertEquals("{B:{c:['c1','c2']}}", m.toString());
78  	}
79  
80  	@Test void a03_preserveRootElement() throws Exception {
81  		var p = XmlParser.create().preserveRootElement().build();
82  
83  		var xml = "<A><B><C>c</C></B></A>";
84  		var m = p.parse(xml, JsonMap.class);
85  		assertEquals("{A:{B:{C:'c'}}}", m.toString());
86  
87  		xml = "<A></A>";
88  		m = p.parse(xml, JsonMap.class);
89  		assertEquals("{A:{}}", m.toString());
90  	}
91  }