1 package org.apache.maven.shared.utils.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.utils.StringUtils;
23 import org.apache.maven.shared.utils.xml.pull.XmlPullParserException;
24
25 import org.junit.Test;
26
27 import java.io.IOException;
28 import java.io.StringReader;
29 import java.io.StringWriter;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.fail;
33
34
35
36
37
38 public class Xpp3DomBuilderTest
39 {
40
41 private static final String LS = System.getProperty("line.separator");
42 private static final String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
43
44 @Test
45 public void selfClosingTag()
46 throws Exception
47 {
48
49
50 String domString = selfClosingTagSource();
51
52 Xpp3Dom dom = Xpp3DomBuilder.build(new StringReader(domString));
53
54 String expected = expectedSelfClosingTag();
55 String dom1Str = dom.toString();
56 assertEquals( "check DOMs match", expected, dom1Str);
57 }
58
59 @Test
60 public void trimming()
61 throws Exception
62 {
63 String domString = createDomString();
64
65 Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( domString ), true );
66
67 assertEquals( "element1value", dom.getChild( "element1" ).getValue() );
68
69 dom = Xpp3DomBuilder.build( new StringReader( domString ), false );
70
71 assertEquals( " element1value\n ", dom.getChild( "element1" ).getValue() );
72 }
73
74 @Test(expected = XmlPullParserException.class)
75 public void malformedXml()
76 {
77 Xpp3DomBuilder.build( new StringReader("<newRoot>" + createDomString()) );
78 fail("We're supposed to fail");
79 }
80
81 @Test
82 public void attributeEscaping()
83 throws IOException, XmlPullParserException
84 {
85 String s = getAttributeEncodedString();
86 Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( s ) );
87
88 assertEquals( "<foo>", dom.getChild( "el" ).getAttribute("att") );
89 StringWriter w = new StringWriter();
90 Xpp3DomWriter.write( w, dom );
91 String newString = w.toString();
92 assertEquals( newString, s );
93 }
94
95 @Test
96 public void contentEscaping()
97 throws IOException, XmlPullParserException
98 {
99 Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( getEncodedString() ) );
100
101 assertEquals( "\"msg\"", dom.getChild( "a1" ).getValue() );
102 assertEquals( "<b>\"msg\"</b>", dom.getChild( "a2" ).getValue() );
103 assertEquals( "<b>\"msg\"</b>", dom.getChild( "a3" ).getValue() );
104
105 StringWriter w = new StringWriter();
106 Xpp3DomWriter.write( w, dom );
107 assertEquals( getExpectedString(), w.toString() );
108 }
109
110
111 private static String getAttributeEncodedString()
112 {
113 StringBuilder domString = new StringBuilder();
114 domString.append( "<root>" );
115 domString.append(LS);
116 domString.append( " <el att=\"<foo>\">bar</el>" );
117 domString.append(LS);
118 domString.append( "</root>" );
119
120 return domString.toString();
121 }
122
123 private static String getEncodedString()
124 {
125 StringBuilder domString = new StringBuilder();
126 domString.append( "<root>\n" );
127 domString.append( " <a1>\"msg\"</a1>\n" );
128 domString.append( " <a2><![CDATA[<b>\"msg\"</b>]]></a2>\n" );
129 domString.append( " <a3><b>"msg"</b></a3>\n" );
130 domString.append( "</root>" );
131
132 return domString.toString();
133 }
134
135 private static String getExpectedString()
136 {
137 StringBuilder domString = new StringBuilder();
138 domString.append( "<root>" );
139 domString.append( LS );
140 domString.append( " <a1>\"msg\"</a1>" );
141 domString.append( LS );
142 domString.append( " <a2><b>\"msg\"</b></a2>" );
143 domString.append( LS );
144 domString.append( " <a3><b>\"msg\"</b></a3>" );
145 domString.append( LS );
146 domString.append( "</root>" );
147 return domString.toString();
148 }
149
150 private static String createDomString()
151 {
152 StringBuilder buf = new StringBuilder();
153 buf.append( "<root>\n" );
154 buf.append( " <element1> element1value\n </element1>\n" );
155 buf.append( " <element2 att2='attribute2 nextline'>\n" );
156 buf.append( " <element3 att3='attribute3'>element3value</element3>\n" );
157 buf.append( " </element2>\n" );
158 buf.append( " <element4></element4>\n" );
159 buf.append( " <element5/>\n" );
160 buf.append( "</root>\n" );
161
162 return buf.toString();
163 }
164
165 private static String selfClosingTagSource()
166 {
167 StringBuilder buf = new StringBuilder();
168 buf.append( "<root>\n" );
169 buf.append( " <el4></el4>\n" );
170 buf.append( " <el5></el5>\n" );
171 buf.append( "</root>" );
172 return StringUtils.unifyLineSeparators(buf.toString());
173 }
174 private static String expectedSelfClosingTag()
175 {
176 return StringUtils.unifyLineSeparators(xmlDeclaration + selfClosingTagSource());
177 }
178
179 }