KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > test > XmlBufferTest


1 package org.sapia.util.xml.idefix.test;
2
3 import junit.framework.TestCase;
4 import junit.textui.TestRunner;
5 import org.sapia.util.xml.CData;
6 import org.sapia.util.xml.idefix.XmlBuffer;
7
8 /**
9  *
10  *
11  * @author Jean-Cedric Desrochers
12  *
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class XmlBufferTest extends TestCase {
20
21   public static void main(String JavaDoc[] args) {
22     TestRunner.run(XmlBufferTest.class);
23   }
24
25   public XmlBufferTest(String JavaDoc aName) {
26     super(aName);
27   }
28
29   public void testClosingInvalidElement() throws Exception JavaDoc {
30     String JavaDoc aNamespaceURI = "http://schemas.sapia.org/idefix";
31     String JavaDoc anotherNamespaceURI = "http://schemas.sapia.org/another";
32
33     XmlBuffer aBuffer = new XmlBuffer();
34     aBuffer.startElement("A").endElement("A");
35
36     /////////////////////////////////////////////////////////////////////////////
37
try {
38       aBuffer = new XmlBuffer();
39       aBuffer.startElement("A").endElement("B");
40       fail();
41     } catch (IllegalArgumentException JavaDoc iae) {
42     }
43
44     /////////////////////////////////////////////////////////////////////////////
45
try {
46       aBuffer = new XmlBuffer();
47       aBuffer.addNamespace(aNamespaceURI, "P");
48       aBuffer.startElement(aNamespaceURI, "A").endElement("A");
49       fail();
50     } catch (IllegalArgumentException JavaDoc iae) {
51     }
52
53     /////////////////////////////////////////////////////////////////////////////
54
aBuffer = new XmlBuffer();
55     aBuffer.addNamespace(aNamespaceURI, "P");
56     aBuffer.startElement(aNamespaceURI, "A").endElement(aNamespaceURI, "A");
57
58     /////////////////////////////////////////////////////////////////////////////
59
try {
60       aBuffer = new XmlBuffer();
61       aBuffer.addNamespace(aNamespaceURI, "P");
62       aBuffer.startElement(aNamespaceURI, "A").endElement("B");
63       fail();
64     } catch (IllegalArgumentException JavaDoc iae) {
65     }
66
67     /////////////////////////////////////////////////////////////////////////////
68
try {
69       aBuffer = new XmlBuffer();
70       aBuffer.addNamespace(aNamespaceURI, "P");
71       aBuffer.startElement(aNamespaceURI, "A").endElement(aNamespaceURI, "B");
72       fail();
73     } catch (IllegalArgumentException JavaDoc iae) {
74     }
75
76     /////////////////////////////////////////////////////////////////////////////
77
try {
78       aBuffer = new XmlBuffer();
79       aBuffer.addNamespace(aNamespaceURI, "P");
80       aBuffer.startElement(aNamespaceURI, "A").endElement(anotherNamespaceURI, "B");
81       fail();
82     } catch (IllegalArgumentException JavaDoc iae) {
83     }
84
85     /////////////////////////////////////////////////////////////////////////////
86
try {
87       aBuffer = new XmlBuffer();
88       aBuffer.addNamespace(aNamespaceURI, "P");
89       aBuffer.addNamespace(anotherNamespaceURI, "Q");
90       aBuffer.startElement(aNamespaceURI, "A").endElement(anotherNamespaceURI, "B");
91       fail();
92     } catch (IllegalArgumentException JavaDoc iae) {
93     }
94
95     /////////////////////////////////////////////////////////////////////////////
96
try {
97       aBuffer = new XmlBuffer();
98       aBuffer.addNamespace(aNamespaceURI, "P");
99       aBuffer.addNamespace(anotherNamespaceURI, "Q");
100       aBuffer.startElement(aNamespaceURI, "A").endElement(anotherNamespaceURI, "A");
101       fail();
102     } catch (IllegalArgumentException JavaDoc iae) {
103     }
104   }
105
106   public void testEmptyElementNoNSWithoutAttribute() throws Exception JavaDoc {
107     XmlBuffer aBuffer = new XmlBuffer();
108     aBuffer.startElement("foo").endElement("foo");
109
110     assertEquals("The string content of the xml buffer is invalid",
111                  "<foo />", aBuffer.toString());
112
113     /////////////////////////////////////////////////////////////////////////////
114
XmlBuffer anotherBuffer = new XmlBuffer(true);
115     anotherBuffer.startElement("foo").endElement("foo");
116
117     assertEquals("The string content of the xml buffer is invalid",
118                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><foo />", anotherBuffer.toString());
119
120     /////////////////////////////////////////////////////////////////////////////
121
anotherBuffer = new XmlBuffer("UTF-16");
122     anotherBuffer.startElement("foo").endElement("foo");
123
124     assertEquals("The string content of the xml buffer is invalid",
125                  "<?xml version=\"1.0\" encoding=\"UTF-16\" ?><foo />", anotherBuffer.toString());
126   }
127
128   public void testInvalidEndAttribute() throws Exception JavaDoc {
129     try {
130       XmlBuffer aBuffer = new XmlBuffer(false);
131       aBuffer.endAttribute();
132       fail();
133     } catch (IllegalStateException JavaDoc ise) {
134     }
135
136     /////////////////////////////////////////////////////////////////////////////
137
try {
138       XmlBuffer aBuffer = new XmlBuffer(false);
139       aBuffer.startElement("foo").
140               endElement("foo").
141               endAttribute();
142       fail();
143     } catch (IllegalStateException JavaDoc ise) {
144     }
145
146     /////////////////////////////////////////////////////////////////////////////
147
try {
148       XmlBuffer aBuffer = new XmlBuffer(false);
149       aBuffer.startElement("foo").
150               addAttribute("bar", "bar").
151               endAttribute().
152               addAttribute("bar2", "bar").
153               endElement("foo");
154       fail();
155     } catch (IllegalStateException JavaDoc ise) {
156     }
157   }
158
159   public void testEmptyElementNoNSWithAttribute() throws Exception JavaDoc {
160     XmlBuffer aBuffer = new XmlBuffer(false);
161     aBuffer.startElement("foo").
162             addAttribute("bar1", "value1").
163             addAttribute("bar2", "value2").
164             endElement("foo");
165
166     assertEquals("The string content of the xml buffer is invalid",
167                  "<foo bar1=\"value1\" bar2=\"value2\" />", aBuffer.toString());
168
169     /////////////////////////////////////////////////////////////////////////////
170
aBuffer = new XmlBuffer(false);
171     aBuffer.startElement("foo").
172             addAttribute("bar1", "value1").
173             addAttribute("bar2", "value2").
174             endAttribute().
175             endElement("foo");
176
177     assertEquals("The string content of the xml buffer is invalid",
178                  "<foo bar1=\"value1\" bar2=\"value2\" />", aBuffer.toString());
179
180     /////////////////////////////////////////////////////////////////////////////
181
XmlBuffer anotherBuffer = new XmlBuffer(true);
182     anotherBuffer.startElement("foo").
183             addAttribute("bar1", "value1").
184             addAttribute("bar2", "value2").
185             endElement("foo");
186
187     assertEquals("The string content of the xml buffer is invalid",
188                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
189                  "<foo bar1=\"value1\" bar2=\"value2\" />", anotherBuffer.toString());
190
191     /////////////////////////////////////////////////////////////////////////////
192
anotherBuffer = new XmlBuffer(true);
193     anotherBuffer.startElement("foo").
194             addAttribute("bar1", "value1").
195             addAttribute("bar2", "value2").
196             endAttribute().
197             endElement("foo");
198
199     assertEquals("The string content of the xml buffer is invalid",
200                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
201                  "<foo bar1=\"value1\" bar2=\"value2\" />", anotherBuffer.toString());
202   }
203
204
205   public void testEmptyElementWithNSWithoutAttribute() throws Exception JavaDoc {
206     XmlBuffer aBuffer = new XmlBuffer(false);
207     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
208             startElement("http://schemas.sapia.org/idefix", "foo").
209             endElement("http://schemas.sapia.org/idefix", "foo");
210
211     assertEquals("The string content of the xml buffer is invalid",
212                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\" />", aBuffer.toString());
213
214     /////////////////////////////////////////////////////////////////////////////
215
aBuffer = new XmlBuffer(false);
216     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
217             startElement("http://schemas.sapia.org/idefix", "foo").
218             endAttribute().
219             endElement("http://schemas.sapia.org/idefix", "foo");
220
221     assertEquals("The string content of the xml buffer is invalid",
222                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\" />", aBuffer.toString());
223
224     /////////////////////////////////////////////////////////////////////////////
225
XmlBuffer anotherBuffer = new XmlBuffer(true);
226     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
227             startElement("http://schemas.sapia.org/idefix", "foo").
228             endElement("http://schemas.sapia.org/idefix", "foo");
229
230     assertEquals("The string content of the xml buffer is invalid",
231                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
232                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\" />", anotherBuffer.toString());
233
234     /////////////////////////////////////////////////////////////////////////////
235
anotherBuffer = new XmlBuffer(true);
236     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
237             startElement("http://schemas.sapia.org/idefix", "foo").
238             endAttribute().
239             endElement("http://schemas.sapia.org/idefix", "foo");
240
241     assertEquals("The string content of the xml buffer is invalid",
242                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
243                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\" />", anotherBuffer.toString());
244   }
245
246
247   public void testEmptyElementWithNSWithAttribute() throws Exception JavaDoc {
248     XmlBuffer aBuffer = new XmlBuffer(false);
249     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
250             addNamespace("http://another.namespace.uri/m", "M").
251             startElement("http://schemas.sapia.org/idefix", "foo").
252             addAttribute("bar1", "value1").
253             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
254             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
255             endElement("http://schemas.sapia.org/idefix", "foo");
256
257     assertEquals("The string content of the xml buffer is invalid",
258                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
259                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
260                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\" />", aBuffer.toString());
261
262     /////////////////////////////////////////////////////////////////////////////
263
aBuffer = new XmlBuffer(false);
264     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
265             addNamespace("http://another.namespace.uri/m", "M").
266             startElement("http://schemas.sapia.org/idefix", "foo").
267             addAttribute("bar1", "value1").
268             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
269             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
270             endAttribute().
271             endElement("http://schemas.sapia.org/idefix", "foo");
272
273     assertEquals("The string content of the xml buffer is invalid",
274                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
275                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
276                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\" />", aBuffer.toString());
277
278     /////////////////////////////////////////////////////////////////////////////
279
XmlBuffer anotherBuffer = new XmlBuffer(true);
280     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
281             addNamespace("http://another.namespace.uri/m", "M").
282             startElement("http://schemas.sapia.org/idefix", "foo").
283             addAttribute("bar1", "value1").
284             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
285             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
286             endElement("http://schemas.sapia.org/idefix", "foo");
287
288     assertEquals("The string content of the xml buffer is invalid",
289                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
290                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
291                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
292                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\" />", anotherBuffer.toString());
293
294     /////////////////////////////////////////////////////////////////////////////
295
anotherBuffer = new XmlBuffer(true);
296     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
297             addNamespace("http://another.namespace.uri/m", "M").
298             startElement("http://schemas.sapia.org/idefix", "foo").
299             addAttribute("bar1", "value1").
300             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
301             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
302             endAttribute().
303             endElement("http://schemas.sapia.org/idefix", "foo");
304
305     assertEquals("The string content of the xml buffer is invalid",
306                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
307                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
308                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
309                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\" />", anotherBuffer.toString());
310   }
311
312
313   public void testElementsWithDefaultNSWithAttribute() throws Exception JavaDoc {
314     XmlBuffer aBuffer = new XmlBuffer(false);
315     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "").
316             addNamespace("http://another.namespace.uri/m", "M").
317             startElement("http://schemas.sapia.org/idefix", "foo").
318             addAttribute("bar1", "value1").
319             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
320             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
321             endElement("http://schemas.sapia.org/idefix", "foo");
322
323     assertEquals("The string content of the xml buffer is invalid",
324                  "<foo xmlns=\"http://schemas.sapia.org/idefix\"" +
325                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
326                  " bar2=\"value2\" M:bar3=\"value3\" />", aBuffer.toString());
327
328     /////////////////////////////////////////////////////////////////////////////
329
aBuffer = new XmlBuffer(false);
330     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "").
331             addNamespace("http://another.namespace.uri/m", "M").
332             startElement("http://schemas.sapia.org/idefix", "foo").
333             addAttribute("bar1", "value1").
334             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
335             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
336             endAttribute().
337             endElement("http://schemas.sapia.org/idefix", "foo");
338
339     assertEquals("The string content of the xml buffer is invalid",
340                  "<foo xmlns=\"http://schemas.sapia.org/idefix\"" +
341                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
342                  " bar2=\"value2\" M:bar3=\"value3\" />", aBuffer.toString());
343
344     /////////////////////////////////////////////////////////////////////////////
345
XmlBuffer anotherBuffer = new XmlBuffer(true);
346     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "").
347             addNamespace("http://another.namespace.uri/m", "M").
348             startElement("http://schemas.sapia.org/idefix", "foo").
349             addAttribute("bar1", "value1").
350             addContent("foobar").
351             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
352             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
353             endElement("http://schemas.sapia.org/idefix", "foo");
354
355     assertEquals("The string content of the xml buffer is invalid",
356                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
357                  "<foo xmlns=\"http://schemas.sapia.org/idefix\"" +
358                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
359                  " bar2=\"value2\" M:bar3=\"value3\">foobar</foo>", anotherBuffer.toString());
360
361     /////////////////////////////////////////////////////////////////////////////
362
anotherBuffer = new XmlBuffer(true);
363     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "").
364             addNamespace("http://another.namespace.uri/m", "M").
365             startElement("http://schemas.sapia.org/idefix", "foo").
366             addAttribute("bar1", "value1").
367             addContent("foobar").
368             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
369             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
370             endAttribute().
371             endElement("http://schemas.sapia.org/idefix", "foo");
372
373     assertEquals("The string content of the xml buffer is invalid",
374                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
375                  "<foo xmlns=\"http://schemas.sapia.org/idefix\"" +
376                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
377                  " bar2=\"value2\" M:bar3=\"value3\">foobar</foo>", anotherBuffer.toString());
378
379     /////////////////////////////////////////////////////////////////////////////
380
XmlBuffer yetAnotherBuffer = new XmlBuffer(true);
381     yetAnotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "").
382             addNamespace("http://another.namespace.uri/m", "M").
383             startElement("http://schemas.sapia.org/idefix", "foo").
384             addAttribute("bar1", "value1").
385             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
386             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
387             addContent("foobar").
388             endElement("http://schemas.sapia.org/idefix", "foo");
389
390     assertEquals("The string content of the xml buffer is invalid",
391                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
392                  "<foo xmlns=\"http://schemas.sapia.org/idefix\"" +
393                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
394                  " bar2=\"value2\" M:bar3=\"value3\">foobar</foo>", yetAnotherBuffer.toString());
395
396     /////////////////////////////////////////////////////////////////////////////
397
yetAnotherBuffer = new XmlBuffer(true);
398     yetAnotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "").
399             addNamespace("http://another.namespace.uri/m", "M").
400             startElement("http://schemas.sapia.org/idefix", "foo").
401             addAttribute("bar1", "value1").
402             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
403             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
404             endAttribute().
405             addContent("foobar").
406             endElement("http://schemas.sapia.org/idefix", "foo");
407
408     assertEquals("The string content of the xml buffer is invalid",
409                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
410                  "<foo xmlns=\"http://schemas.sapia.org/idefix\"" +
411                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
412                  " bar2=\"value2\" M:bar3=\"value3\">foobar</foo>", yetAnotherBuffer.toString());
413   }
414
415
416   public void testSimpleElementNoNSWithoutAttribute() throws Exception JavaDoc {
417     XmlBuffer aBuffer = new XmlBuffer();
418     aBuffer.startElement("foo").addContent("bar").endElement("foo");
419
420     assertEquals("The string content of the xml buffer is invalid",
421                  "<foo>bar</foo>", aBuffer.toString());
422
423     /////////////////////////////////////////////////////////////////////////////
424
aBuffer = new XmlBuffer();
425     aBuffer.startElement("foo").endAttribute().addContent("bar").endElement("foo");
426
427     assertEquals("The string content of the xml buffer is invalid",
428                  "<foo>bar</foo>", aBuffer.toString());
429
430     /////////////////////////////////////////////////////////////////////////////
431
XmlBuffer anotherBuffer = new XmlBuffer(true);
432     anotherBuffer.startElement("foo").addContent("bar").endElement("foo");
433
434     assertEquals("The string content of the xml buffer is invalid",
435                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
436                  "<foo>bar</foo>", anotherBuffer.toString());
437
438     /////////////////////////////////////////////////////////////////////////////
439
anotherBuffer = new XmlBuffer(true);
440     anotherBuffer.startElement("foo").addContent("bar").endAttribute().endElement("foo");
441
442     assertEquals("The string content of the xml buffer is invalid",
443                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
444                  "<foo>bar</foo>", anotherBuffer.toString());
445   }
446
447
448   public void testSimpleElementNoNSWithAttribute() throws Exception JavaDoc {
449     XmlBuffer aBuffer = new XmlBuffer(false);
450     aBuffer.startElement("foo").
451             addAttribute("bar1", "value1").
452             addContent("foobar").
453             addAttribute("bar2", "value2").
454             endElement("foo");
455
456     assertEquals("The string content of the xml buffer is invalid",
457                  "<foo bar1=\"value1\" bar2=\"value2\">foobar</foo>", aBuffer.toString());
458
459     /////////////////////////////////////////////////////////////////////////////
460
aBuffer = new XmlBuffer(false);
461     aBuffer.startElement("foo").
462             addAttribute("bar1", "value1").
463             addContent("foobar").
464             addAttribute("bar2", "value2").
465             endAttribute().
466             endElement("foo");
467
468     assertEquals("The string content of the xml buffer is invalid",
469                  "<foo bar1=\"value1\" bar2=\"value2\">foobar</foo>", aBuffer.toString());
470
471     /////////////////////////////////////////////////////////////////////////////
472
XmlBuffer anotherBuffer = new XmlBuffer(true);
473     anotherBuffer.startElement("foo").
474             addContent("foobar").
475             addAttribute("bar1", "value1").
476             addAttribute("bar2", "value2").
477             endElement("foo");
478
479     assertEquals("The string content of the xml buffer is invalid",
480                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
481                  "<foo bar1=\"value1\" bar2=\"value2\">foobar</foo>", anotherBuffer.toString());
482
483     /////////////////////////////////////////////////////////////////////////////
484
anotherBuffer = new XmlBuffer(true);
485     anotherBuffer.startElement("foo").
486             addAttribute("bar1", "value1").
487             addAttribute("bar2", "value2").
488             endAttribute().
489             addContent("foobar").
490             endElement("foo");
491
492     assertEquals("The string content of the xml buffer is invalid",
493                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
494                  "<foo bar1=\"value1\" bar2=\"value2\">foobar</foo>", anotherBuffer.toString());
495   }
496
497   public void testSimpleElementWithNSWithAttribute() throws Exception JavaDoc {
498     XmlBuffer aBuffer = new XmlBuffer(false);
499     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
500             addNamespace("http://another.namespace.uri/m", "M").
501             startElement("http://schemas.sapia.org/idefix", "foo").
502             addAttribute("bar1", "value1").
503             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
504             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
505             addContent("foobar").
506             endElement("http://schemas.sapia.org/idefix", "foo");
507
508     assertEquals("The string content of the xml buffer is invalid",
509                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
510                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
511                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\">foobar</IDEFIX:foo>", aBuffer.toString());
512
513     /////////////////////////////////////////////////////////////////////////////
514
aBuffer = new XmlBuffer(false);
515     aBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
516             addNamespace("http://another.namespace.uri/m", "M").
517             startElement("http://schemas.sapia.org/idefix", "foo").
518             addAttribute("bar1", "value1").
519             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
520             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
521             endAttribute().
522             addContent("foobar").
523             endElement("http://schemas.sapia.org/idefix", "foo");
524
525     assertEquals("The string content of the xml buffer is invalid",
526                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
527                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
528                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\">foobar</IDEFIX:foo>", aBuffer.toString());
529
530     /////////////////////////////////////////////////////////////////////////////
531
XmlBuffer anotherBuffer = new XmlBuffer(true);
532     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
533             addNamespace("http://another.namespace.uri/m", "M").
534             startElement("http://schemas.sapia.org/idefix", "foo").
535             addContent("foobar").
536             addAttribute("bar1", "value1").
537             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
538             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
539             endElement("http://schemas.sapia.org/idefix", "foo");
540
541     assertEquals("The string content of the xml buffer is invalid",
542                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
543                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
544                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
545                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\">foobar</IDEFIX:foo>", anotherBuffer.toString());
546
547     /////////////////////////////////////////////////////////////////////////////
548
anotherBuffer = new XmlBuffer(true);
549     anotherBuffer.addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
550             addNamespace("http://another.namespace.uri/m", "M").
551             startElement("http://schemas.sapia.org/idefix", "foo").
552             addContent("foobar").
553             addAttribute("bar1", "value1").
554             addAttribute("http://schemas.sapia.org/idefix", "bar2", "value2").
555             addAttribute("http://another.namespace.uri/m", "bar3", "value3").
556             endAttribute().
557             endElement("http://schemas.sapia.org/idefix", "foo");
558
559     assertEquals("The string content of the xml buffer is invalid",
560                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
561                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\"" +
562                  " xmlns:M=\"http://another.namespace.uri/m\" bar1=\"value1\""+
563                  " IDEFIX:bar2=\"value2\" M:bar3=\"value3\">foobar</IDEFIX:foo>", anotherBuffer.toString());
564   }
565
566   public void testNestedElementNoNSNoAttribute() throws Exception JavaDoc {
567     XmlBuffer aBuffer = new XmlBuffer(false);
568     aBuffer.startElement("foo").
569             startElement("bar").
570             endElement("bar").
571             endElement("foo");
572
573     assertEquals("The string content of the xml buffer is invalid",
574                  "<foo><bar /></foo>", aBuffer.toString());
575
576     /////////////////////////////////////////////////////////////////////////////
577
aBuffer = new XmlBuffer(false);
578     aBuffer.startElement("foo").
579             endAttribute().
580             startElement("bar").
581             endAttribute().
582             endElement("bar").
583             endElement("foo");
584
585     assertEquals("The string content of the xml buffer is invalid",
586                  "<foo><bar /></foo>", aBuffer.toString());
587
588     /////////////////////////////////////////////////////////////////////////////
589
XmlBuffer anotherBuffer = new XmlBuffer(false);
590     anotherBuffer.startElement("foo").
591             startElement("bar").
592             startElement("too").
593             endElement("too").
594             endElement("bar").
595             endElement("foo");
596
597     assertEquals("The string content of the xml buffer is invalid",
598                  "<foo><bar><too /></bar></foo>", anotherBuffer.toString());
599
600     /////////////////////////////////////////////////////////////////////////////
601
anotherBuffer = new XmlBuffer(false);
602     anotherBuffer.
603             startElement("foo").
604             startElement("bar").
605             startElement("too").
606             endAttribute().
607             endElement("too").
608             endElement("bar").
609             endElement("foo");
610
611     assertEquals("The string content of the xml buffer is invalid",
612                  "<foo><bar><too /></bar></foo>", anotherBuffer.toString());
613
614     /////////////////////////////////////////////////////////////////////////////
615
anotherBuffer = new XmlBuffer(false);
616     anotherBuffer.
617             startElement("foo").
618             startElement("bar").
619             endAttribute().
620             startElement("too").
621             endElement("too").
622             endElement("bar").
623             endElement("foo");
624
625     assertEquals("The string content of the xml buffer is invalid",
626                  "<foo><bar><too /></bar></foo>", anotherBuffer.toString());
627
628     /////////////////////////////////////////////////////////////////////////////
629
anotherBuffer = new XmlBuffer(false);
630     anotherBuffer.
631             startElement("foo").
632             startElement("bar").
633             startElement("too").
634             endElement("too").
635             endAttribute().
636             endElement("bar").
637             endElement("foo");
638
639     assertEquals("The string content of the xml buffer is invalid",
640                  "<foo><bar><too /></bar></foo>", anotherBuffer.toString());
641   }
642
643   public void testNestedElementNoNSWithAttribute() throws Exception JavaDoc {
644     XmlBuffer aBuffer = new XmlBuffer();
645     aBuffer.startElement("foo").
646             addAttribute("bar1", "value1").
647             addAttribute("bar2", "value2").
648             startElement("bar").
649             endElement("bar").
650             endElement("foo");
651
652     assertEquals("The string content of the xml buffer is invalid",
653                  "<foo bar1=\"value1\" bar2=\"value2\"><bar /></foo>", aBuffer.toString());
654
655     /////////////////////////////////////////////////////////////////////////////
656
aBuffer = new XmlBuffer();
657     aBuffer.startElement("foo").
658             addAttribute("bar1", "value1").
659             addAttribute("bar2", "value2").
660             endAttribute().
661             startElement("bar").
662             endElement("bar").
663             endElement("foo");
664
665     assertEquals("The string content of the xml buffer is invalid",
666                  "<foo bar1=\"value1\" bar2=\"value2\"><bar /></foo>", aBuffer.toString());
667
668     /////////////////////////////////////////////////////////////////////////////
669
aBuffer = new XmlBuffer();
670     aBuffer.startElement("foo").
671             addAttribute("bar1", "value1").
672             startElement("bar").
673             endElement("bar").
674             addAttribute("bar2", "value2").
675             endElement("foo");
676
677     assertEquals("The string content of the xml buffer is invalid",
678                  "<foo bar1=\"value1\" bar2=\"value2\"><bar /></foo>", aBuffer.toString());
679
680     /////////////////////////////////////////////////////////////////////////////
681
aBuffer = new XmlBuffer();
682     aBuffer.startElement("foo").
683             addAttribute("bar1", "value1").
684             startElement("bar").
685             endElement("bar").
686             addAttribute("bar2", "value2").
687             endAttribute().
688             endElement("foo");
689
690     assertEquals("The string content of the xml buffer is invalid",
691                  "<foo bar1=\"value1\" bar2=\"value2\"><bar /></foo>", aBuffer.toString());
692
693     /////////////////////////////////////////////////////////////////////////////
694
aBuffer = new XmlBuffer();
695     aBuffer.startElement("foo").
696             startElement("bar").
697             endElement("bar").
698             addAttribute("bar1", "value1").
699             addAttribute("bar2", "value2").
700             endElement("foo");
701
702     assertEquals("The string content of the xml buffer is invalid",
703                  "<foo bar1=\"value1\" bar2=\"value2\"><bar /></foo>", aBuffer.toString());
704
705     /////////////////////////////////////////////////////////////////////////////
706
aBuffer = new XmlBuffer();
707     aBuffer.startElement("foo").
708             startElement("bar").
709             endElement("bar").
710             addAttribute("bar1", "value1").
711             addAttribute("bar2", "value2").
712             endAttribute().
713             endElement("foo");
714
715     assertEquals("The string content of the xml buffer is invalid",
716                  "<foo bar1=\"value1\" bar2=\"value2\"><bar /></foo>", aBuffer.toString());
717
718     /////////////////////////////////////////////////////////////////////////////
719
XmlBuffer anotherBuffer = new XmlBuffer(true);
720     anotherBuffer.startElement("foo").
721             addAttribute("bar1", "value1").
722             startElement("bar").
723             addAttribute("bar2", "value2").
724             endElement("bar").
725             endElement("foo");
726
727     assertEquals("The string content of the xml buffer is invalid",
728                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
729                  "<foo bar1=\"value1\"><bar bar2=\"value2\" /></foo>", anotherBuffer.toString());
730
731     /////////////////////////////////////////////////////////////////////////////
732
anotherBuffer = new XmlBuffer(true);
733     anotherBuffer.startElement("foo").
734             addAttribute("bar1", "value1").
735             startElement("bar").
736             addAttribute("bar2", "value2").
737             endAttribute().
738             endElement("bar").
739             endElement("foo");
740
741     assertEquals("The string content of the xml buffer is invalid",
742                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
743                  "<foo bar1=\"value1\"><bar bar2=\"value2\" /></foo>", anotherBuffer.toString());
744
745     /////////////////////////////////////////////////////////////////////////////
746
anotherBuffer = new XmlBuffer(true);
747     anotherBuffer.startElement("foo").
748             startElement("bar").
749             addAttribute("bar2", "value2").
750             endElement("bar").
751             addAttribute("bar1", "value1").
752             endElement("foo");
753
754     assertEquals("The string content of the xml buffer is invalid",
755                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
756                  "<foo bar1=\"value1\"><bar bar2=\"value2\" /></foo>", anotherBuffer.toString());
757
758     /////////////////////////////////////////////////////////////////////////////
759
anotherBuffer = new XmlBuffer(true);
760     anotherBuffer.startElement("foo").
761             startElement("bar").
762             addAttribute("bar2", "value2").
763             endElement("bar").
764             addAttribute("bar1", "value1").
765             endAttribute().
766             endElement("foo");
767
768     assertEquals("The string content of the xml buffer is invalid",
769                  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
770                  "<foo bar1=\"value1\"><bar bar2=\"value2\" /></foo>", anotherBuffer.toString());
771
772     /////////////////////////////////////////////////////////////////////////////
773
anotherBuffer = new XmlBuffer("ISO-8859-1");
774     anotherBuffer.startElement("foo").
775             startElement("bar").
776             addAttribute("bar1", "value1").
777             addAttribute("bar2", "value2").
778             endElement("bar").
779             endElement("foo");
780
781     assertEquals("The string content of the xml buffer is invalid",
782                  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" +
783                  "<foo><bar bar1=\"value1\" bar2=\"value2\" /></foo>", anotherBuffer.toString());
784
785     /////////////////////////////////////////////////////////////////////////////
786
anotherBuffer = new XmlBuffer("ISO-8859-1");
787     anotherBuffer.startElement("foo").
788             startElement("bar").
789             addAttribute("bar1", "value1").
790             addAttribute("bar2", "value2").
791             endElement("bar").
792             endAttribute().
793             endElement("foo");
794
795     assertEquals("The string content of the xml buffer is invalid",
796                  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" +
797                  "<foo><bar bar1=\"value1\" bar2=\"value2\" /></foo>", anotherBuffer.toString());
798   }
799
800   public void testSimpleNestedElementNoNSWithAttribute() throws Exception JavaDoc {
801     XmlBuffer aBuffer = new XmlBuffer();
802     aBuffer.startElement("foo").
803                 addContent("bonjour").
804                 addAttribute("bar1", "value1").
805                 addAttribute("bar2", "value2").
806                 startElement("bar").
807                     addContent("ola!").
808                 endElement("bar").
809             endElement("foo");
810
811     assertEquals("The string content of the xml buffer is invalid",
812                  "<foo bar1=\"value1\" bar2=\"value2\"><bar>ola!</bar>bonjour</foo>", aBuffer.toString());
813
814     /////////////////////////////////////////////////////////////////////////////
815
aBuffer = new XmlBuffer();
816     aBuffer.startElement("foo").
817                 addContent("bonjour").
818                 addAttribute("bar1", "value1").
819                 addAttribute("bar2", "value2").
820                 startElement("bar").
821                     endAttribute().
822                     addContent("ola!").
823                 endElement("bar").
824             endElement("foo");
825
826     assertEquals("The string content of the xml buffer is invalid",
827                  "<foo bar1=\"value1\" bar2=\"value2\"><bar>ola!</bar>bonjour</foo>", aBuffer.toString());
828
829     /////////////////////////////////////////////////////////////////////////////
830
aBuffer = new XmlBuffer();
831     aBuffer.startElement("foo").
832                 startElement("bar").
833                     addContent("ola!").
834                     addAttribute("bar2", "value2").
835                 endElement("bar").
836                 addContent("bonjour").
837                 addAttribute("bar1", "value1").
838             endElement("foo");
839
840     assertEquals("The string content of the xml buffer is invalid",
841                  "<foo bar1=\"value1\"><bar bar2=\"value2\">ola!</bar>bonjour</foo>", aBuffer.toString());
842
843     /////////////////////////////////////////////////////////////////////////////
844
aBuffer = new XmlBuffer();
845     aBuffer.startElement("foo").
846                 startElement("bar").
847                     addContent("ola!").
848                     addAttribute("bar2", "value2").
849                     endAttribute().
850                 endElement("bar").
851                 addContent("bonjour").
852                 addAttribute("bar1", "value1").
853                 endAttribute().
854             endElement("foo");
855
856     assertEquals("The string content of the xml buffer is invalid",
857                  "<foo bar1=\"value1\"><bar bar2=\"value2\">ola!</bar>bonjour</foo>", aBuffer.toString());
858
859     /////////////////////////////////////////////////////////////////////////////
860
aBuffer = new XmlBuffer();
861     aBuffer.startElement("foo").
862                 startElement("bar").
863                     addContent("ola!").
864                     addAttribute("bar2", "value2").
865                     addContent("bonjour").
866                     addAttribute("bar1", "value1").
867                 endElement("bar").
868             endElement("foo");
869
870     assertEquals("The string content of the xml buffer is invalid",
871                  "<foo><bar bar2=\"value2\" bar1=\"value1\">ola!bonjour</bar></foo>", aBuffer.toString());
872
873     /////////////////////////////////////////////////////////////////////////////
874
aBuffer = new XmlBuffer();
875     aBuffer.startElement("foo").
876                 startElement("bar").
877                     addContent("ola!").
878                     addAttribute("bar2", "value2").
879                     addContent("bonjour").
880                     addAttribute("bar1", "value1").
881                     endAttribute().
882                 endElement("bar").
883                 endAttribute().
884             endElement("foo");
885
886     assertEquals("The string content of the xml buffer is invalid",
887                  "<foo><bar bar2=\"value2\" bar1=\"value1\">ola!bonjour</bar></foo>", aBuffer.toString());
888   }
889
890
891   public void testNestedElementWithNSNoAttributes() throws Exception JavaDoc {
892     XmlBuffer aBuffer = new XmlBuffer().
893             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
894             addNamespace("http://another.namespace.uri/m", "M");
895
896     aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
897                 startElement("http://schemas.sapia.org/idefix", "bar").
898                     addContent("bonjour").
899                 endElement("http://schemas.sapia.org/idefix", "bar").
900             endElement("http://schemas.sapia.org/idefix", "foo");
901
902     assertEquals("The string content of the xml buffer is invalid",
903                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
904                  "<IDEFIX:bar>bonjour</IDEFIX:bar></IDEFIX:foo>", aBuffer.toString());
905
906     /////////////////////////////////////////////////////////////////////////////
907
aBuffer = new XmlBuffer().
908             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
909             addNamespace("http://another.namespace.uri/m", "M");
910
911     aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
912                 startElement("http://schemas.sapia.org/idefix", "bar").
913                     endAttribute().
914                     addContent("bonjour").
915                 endElement("http://schemas.sapia.org/idefix", "bar").
916             endElement("http://schemas.sapia.org/idefix", "foo");
917
918     assertEquals("The string content of the xml buffer is invalid",
919                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
920                  "<IDEFIX:bar>bonjour</IDEFIX:bar></IDEFIX:foo>", aBuffer.toString());
921
922     /////////////////////////////////////////////////////////////////////////////
923
aBuffer = new XmlBuffer().
924             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
925             addNamespace("http://another.namespace.uri/m", "M");
926
927     aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
928                 startElement("http://another.namespace.uri/m", "bar").
929                     addContent("bonjour").
930                 endElement("http://another.namespace.uri/m", "bar").
931             endElement("http://schemas.sapia.org/idefix", "foo");
932
933     assertEquals("The string content of the xml buffer is invalid",
934                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
935                  "<M:bar xmlns:M=\"http://another.namespace.uri/m\">bonjour</M:bar></IDEFIX:foo>", aBuffer.toString());
936
937     /////////////////////////////////////////////////////////////////////////////
938
aBuffer = new XmlBuffer().
939             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
940             addNamespace("http://another.namespace.uri/m", "M");
941
942     aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
943                 startElement("http://another.namespace.uri/m", "bar").
944                     addContent("bonjour").
945                 endElement("http://another.namespace.uri/m", "bar").
946                 endAttribute().
947             endElement("http://schemas.sapia.org/idefix", "foo");
948
949     assertEquals("The string content of the xml buffer is invalid",
950                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
951                  "<M:bar xmlns:M=\"http://another.namespace.uri/m\">bonjour</M:bar></IDEFIX:foo>", aBuffer.toString());
952
953     /////////////////////////////////////////////////////////////////////////////
954
aBuffer = new XmlBuffer().
955             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
956             addNamespace("http://another.namespace.uri/m", "M");
957
958     aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
959                 startElement("http://another.namespace.uri/m", "bar").
960                     addContent("bonjour").
961                 endElement("http://another.namespace.uri/m", "bar").
962                 startElement("http://another.namespace.uri/m", "too").
963                     addContent("ola!").
964                 endElement("http://another.namespace.uri/m", "too").
965             endElement("http://schemas.sapia.org/idefix", "foo");
966
967     assertEquals("The string content of the xml buffer is invalid",
968                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
969                  "<M:bar xmlns:M=\"http://another.namespace.uri/m\">bonjour</M:bar>" +
970                  "<M:too xmlns:M=\"http://another.namespace.uri/m\">ola!</M:too></IDEFIX:foo>", aBuffer.toString());
971
972     /////////////////////////////////////////////////////////////////////////////
973
aBuffer = new XmlBuffer().
974             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX").
975             addNamespace("http://another.namespace.uri/m", "M");
976
977     aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
978                 endAttribute().
979                 startElement("http://another.namespace.uri/m", "bar").
980                     endAttribute().
981                     addContent("bonjour").
982                 endElement("http://another.namespace.uri/m", "bar").
983                 startElement("http://another.namespace.uri/m", "too").
984                     endAttribute().
985                     addContent("ola!").
986                 endElement("http://another.namespace.uri/m", "too").
987             endElement("http://schemas.sapia.org/idefix", "foo");
988
989     assertEquals("The string content of the xml buffer is invalid",
990                  "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
991                  "<M:bar xmlns:M=\"http://another.namespace.uri/m\">bonjour</M:bar>" +
992                  "<M:too xmlns:M=\"http://another.namespace.uri/m\">ola!</M:too></IDEFIX:foo>", aBuffer.toString());
993   }
994
995
996   public void testOverridingNSPrefix() throws Exception JavaDoc {
997     /////////////////////////////////////////////////////////////////////////////
998
XmlBuffer aBuffer = new XmlBuffer().
999             addNamespace("http://schemas.sapia.org/idefix", "IDEFIX");
1000
1001    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo");
1002    aBuffer.addNamespace("http://schemas.sapia.org", "IDEFIX");
1003    aBuffer.startElement("http://schemas.sapia.org", "bar").
1004                addContent("bonjour").
1005            endElement("http://schemas.sapia.org", "bar");
1006    aBuffer.removeNamespace("http://schemas.sapia.org");
1007    aBuffer.endElement("http://schemas.sapia.org/idefix", "foo");
1008
1009    assertEquals("The string content of the xml buffer is invalid",
1010                 "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
1011                 "<IDEFIX:bar xmlns:IDEFIX=\"http://schemas.sapia.org\">bonjour</IDEFIX:bar></IDEFIX:foo>", aBuffer.toString());
1012
1013    /////////////////////////////////////////////////////////////////////////////
1014
aBuffer = new XmlBuffer().
1015            addNamespace("http://schemas.sapia.org/idefix", "IDEFIX");
1016
1017    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo");
1018    aBuffer.addNamespace("http://schemas.sapia.org", "IDEFIX");
1019    aBuffer.startElement("http://schemas.sapia.org", "bar").
1020                endAttribute().
1021                addContent("bonjour").
1022            endElement("http://schemas.sapia.org", "bar");
1023    aBuffer.removeNamespace("http://schemas.sapia.org");
1024    aBuffer.endElement("http://schemas.sapia.org/idefix", "foo");
1025
1026    assertEquals("The string content of the xml buffer is invalid",
1027                 "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
1028                 "<IDEFIX:bar xmlns:IDEFIX=\"http://schemas.sapia.org\">bonjour</IDEFIX:bar></IDEFIX:foo>", aBuffer.toString());
1029
1030    /////////////////////////////////////////////////////////////////////////////
1031
aBuffer = new XmlBuffer().
1032            addNamespace("http://schemas.sapia.org/idefix", "");
1033
1034    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo");
1035    aBuffer.addNamespace("http://schemas.sapia.org", "");
1036    aBuffer.startElement("http://schemas.sapia.org", "bar").
1037                addContent("bonjour").
1038            endElement("http://schemas.sapia.org", "bar");
1039    aBuffer.removeNamespace("http://schemas.sapia.org");
1040    aBuffer.endElement("http://schemas.sapia.org/idefix", "foo");
1041
1042    assertEquals("The string content of the xml buffer is invalid",
1043                 "<foo xmlns=\"http://schemas.sapia.org/idefix\">" +
1044                 "<bar xmlns=\"http://schemas.sapia.org\">bonjour</bar></foo>", aBuffer.toString());
1045
1046    /////////////////////////////////////////////////////////////////////////////
1047
aBuffer = new XmlBuffer().
1048            addNamespace("http://schemas.sapia.org/idefix", "");
1049
1050    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").
1051            endAttribute();
1052    aBuffer.addNamespace("http://schemas.sapia.org", "");
1053    aBuffer.startElement("http://schemas.sapia.org", "bar").
1054                addContent("bonjour").
1055            endElement("http://schemas.sapia.org", "bar");
1056    aBuffer.removeNamespace("http://schemas.sapia.org");
1057    aBuffer.endElement("http://schemas.sapia.org/idefix", "foo");
1058
1059    assertEquals("The string content of the xml buffer is invalid",
1060                 "<foo xmlns=\"http://schemas.sapia.org/idefix\">" +
1061                 "<bar xmlns=\"http://schemas.sapia.org\">bonjour</bar></foo>", aBuffer.toString());
1062  }
1063
1064  public void testOverridingNSURI() throws Exception JavaDoc {
1065    XmlBuffer aBuffer = new XmlBuffer().
1066            addNamespace("http://schemas.sapia.org/idefix", "IDEFIX");
1067
1068    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo");
1069    aBuffer.addNamespace("http://schemas.sapia.org/idefix", "SAPIA");
1070    aBuffer.startElement("http://schemas.sapia.org/idefix", "bar").
1071                addContent("bonjour").
1072            endElement("http://schemas.sapia.org/idefix", "bar");
1073    aBuffer.removeNamespace("http://schemas.sapia.org/idefix");
1074    aBuffer.endElement("http://schemas.sapia.org/idefix", "foo");
1075
1076    assertEquals("The string content of the xml buffer is invalid",
1077                 "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
1078                 "<SAPIA:bar xmlns:SAPIA=\"http://schemas.sapia.org/idefix\">bonjour</SAPIA:bar></IDEFIX:foo>", aBuffer.toString());
1079
1080    /////////////////////////////////////////////////////////////////////////////
1081
aBuffer = new XmlBuffer().
1082            addNamespace("http://schemas.sapia.org/idefix", "IDEFIX");
1083
1084    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo").endAttribute();
1085    aBuffer.addNamespace("http://schemas.sapia.org/idefix", "SAPIA");
1086    aBuffer.startElement("http://schemas.sapia.org/idefix", "bar").
1087              endAttribute().
1088              addContent("bonjour").
1089            endElement("http://schemas.sapia.org/idefix", "bar");
1090    aBuffer.removeNamespace("http://schemas.sapia.org/idefix");
1091    aBuffer.endElement("http://schemas.sapia.org/idefix", "foo");
1092
1093    assertEquals("The string content of the xml buffer is invalid",
1094                 "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
1095                 "<SAPIA:bar xmlns:SAPIA=\"http://schemas.sapia.org/idefix\">bonjour</SAPIA:bar></IDEFIX:foo>", aBuffer.toString());
1096
1097    /////////////////////////////////////////////////////////////////////////////
1098
aBuffer = new XmlBuffer().
1099            addNamespace("http://schemas.sapia.org/idefix", "IDEFIX");
1100
1101    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo");
1102    aBuffer.addNamespace("http://schemas.sapia.org/idefix", "SAPIA");
1103    aBuffer.startElement("http://schemas.sapia.org/idefix", "bar").
1104                addContent("bonjour").
1105            endElement("http://schemas.sapia.org/idefix", "bar");
1106    aBuffer.removeNamespace("http://schemas.sapia.org/idefix");
1107    aBuffer.startElement("http://schemas.sapia.org/idefix", "bar").
1108                addContent("ola!").
1109            endElement("http://schemas.sapia.org/idefix", "bar").
1110          endElement("http://schemas.sapia.org/idefix", "foo");
1111
1112    assertEquals("The string content of the xml buffer is invalid",
1113                 "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
1114                 "<SAPIA:bar xmlns:SAPIA=\"http://schemas.sapia.org/idefix\">bonjour</SAPIA:bar>" +
1115                 "<IDEFIX:bar>ola!</IDEFIX:bar></IDEFIX:foo>", aBuffer.toString());
1116
1117    /////////////////////////////////////////////////////////////////////////////
1118
aBuffer = new XmlBuffer().
1119            addNamespace("http://schemas.sapia.org/idefix", "IDEFIX");
1120
1121    aBuffer.startElement("http://schemas.sapia.org/idefix", "foo");
1122    aBuffer.addNamespace("http://schemas.sapia.org/idefix", "SAPIA");
1123    aBuffer.startElement("http://schemas.sapia.org/idefix", "bar").
1124                endAttribute().
1125                addContent("bonjour").
1126            endElement("http://schemas.sapia.org/idefix", "bar");
1127    aBuffer.removeNamespace("http://schemas.sapia.org/idefix");
1128    aBuffer.startElement("http://schemas.sapia.org/idefix", "bar").
1129                endAttribute().
1130                addContent("ola!").
1131            endElement("http://schemas.sapia.org/idefix", "bar").
1132          endElement("http://schemas.sapia.org/idefix", "foo");
1133
1134    assertEquals("The string content of the xml buffer is invalid",
1135                 "<IDEFIX:foo xmlns:IDEFIX=\"http://schemas.sapia.org/idefix\">" +
1136                 "<SAPIA:bar xmlns:SAPIA=\"http://schemas.sapia.org/idefix\">bonjour</SAPIA:bar>" +
1137                 "<IDEFIX:bar>ola!</IDEFIX:bar></IDEFIX:foo>", aBuffer.toString());
1138  }
1139
1140
1141  public void testEndAttributeScenarios() throws Exception JavaDoc {
1142    XmlBuffer aBuffer = new XmlBuffer();
1143    aBuffer.startElement("A").
1144              startElement("B").
1145                addAttribute("b", "b").
1146                startElement("C").
1147                  addAttribute("c", "c").
1148                endElement("C").
1149                startElement("D").
1150                  addAttribute("d", "d").
1151                  startElement("E").
1152                    addAttribute("e", "e").
1153                  endElement("E").
1154                endElement("D").
1155              endElement("B").
1156            endElement("A");
1157    
1158    assertEquals("The string content of the xml buffer is invalid",
1159                 "<A><B b=\"b\"><C c=\"c\" /><D d=\"d\"><E e=\"e\" /></D></B></A>", aBuffer.toString());
1160
1161    /////////////////////////////////////////////////////////////////////////////
1162
aBuffer = new XmlBuffer();
1163    aBuffer.startElement("A").
1164              endAttribute().
1165              startElement("B").
1166                addAttribute("b", "b").
1167                endAttribute().
1168                startElement("C").
1169                  addAttribute("c", "c").
1170                  endAttribute().
1171                endElement("C").
1172                startElement("D").
1173                  addAttribute("d", "d").
1174                  endAttribute().
1175                  startElement("E").
1176                    addAttribute("e", "e").
1177                    endAttribute().
1178                  endElement("E").
1179                endElement("D").
1180              endElement("B").
1181            endElement("A");
1182
1183    assertEquals("The string content of the xml buffer is invalid",
1184                 "<A><B b=\"b\"><C c=\"c\" /><D d=\"d\"><E e=\"e\" /></D></B></A>", aBuffer.toString());
1185
1186    /////////////////////////////////////////////////////////////////////////////
1187
aBuffer = new XmlBuffer();
1188    aBuffer.startElement("A").
1189              startElement("B").
1190                addAttribute("b", "b").
1191                endAttribute().
1192                startElement("C").
1193                  addAttribute("c", "c").
1194                endElement("C").
1195                startElement("D").
1196                  addAttribute("d", "d").
1197                  startElement("E").
1198                    addAttribute("e", "e").
1199                    endAttribute().
1200                  endElement("E").
1201                endElement("D").
1202              endElement("B").
1203            endElement("A");
1204
1205    assertEquals("The string content of the xml buffer is invalid",
1206                 "<A><B b=\"b\"><C c=\"c\" /><D d=\"d\"><E e=\"e\" /></D></B></A>", aBuffer.toString());
1207
1208    /////////////////////////////////////////////////////////////////////////////
1209
aBuffer = new XmlBuffer();
1210    aBuffer.startElement("A").
1211              endAttribute().
1212              startElement("B").
1213                addAttribute("b", "b").
1214                startElement("C").
1215                  addAttribute("c", "c").
1216                  endAttribute().
1217                endElement("C").
1218                startElement("D").
1219                  addAttribute("d", "d").
1220                  endAttribute().
1221                  startElement("E").
1222                    addAttribute("e", "e").
1223                  endElement("E").
1224                endElement("D").
1225              endElement("B").
1226            endElement("A");
1227
1228    assertEquals("The string content of the xml buffer is invalid",
1229                 "<A><B b=\"b\"><C c=\"c\" /><D d=\"d\"><E e=\"e\" /></D></B></A>", aBuffer.toString());
1230  }
1231  
1232
1233  public void testXMLReservedCharacters() throws Exception JavaDoc {
1234    XmlBuffer aBuffer = new XmlBuffer();
1235    aBuffer.startElement("foo").
1236                addContent("<\"o\" & \'la!\'>").
1237                addAttribute("bar1", "<\"o\" & \'la!\'>").
1238                startElement("bar").
1239                    addContent("<\"bon\" & \'jour\'>").
1240                    addAttribute("bar2", "<\"bon\" & \'jour\'>").
1241                endElement("bar").
1242            endElement("foo");
1243
1244    assertEquals("The string content of the xml buffer is invalid",
1245                 "<foo bar1=\"&lt;&quot;o&quot; &amp; &apos;la!&apos;&gt;\">" +
1246                 "<bar bar2=\"&lt;&quot;bon&quot; &amp; &apos;jour&apos;&gt;\">" +
1247                 "&lt;&quot;bon&quot; &amp; &apos;jour&apos;&gt;</bar>" +
1248                 "&lt;&quot;o&quot; &amp; &apos;la!&apos;&gt;</foo>", aBuffer.toString());
1249  }
1250
1251  public void testSimpleElementWithCData() throws Exception JavaDoc {
1252    XmlBuffer aBuffer = new XmlBuffer();
1253    aBuffer.startElement("foo").
1254              addAttribute("bar1", "value1").
1255              addContent(new CData("bonjour")).
1256            endElement("foo");
1257
1258    assertEquals("The string content of the xml buffer is invalid",
1259                 "<foo bar1=\"value1\">" +
1260                 "<![CDATA[bonjour]]>" +
1261                 "</foo>", aBuffer.toString());
1262  }
1263}
1264
Popular Tags