View Javadoc
1   package com.github.davidmoten.aws.lw.client.xml.builder;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.junit.Test;
6   
7   public class XmlTest {
8   
9       @Test
10      public void test() {
11          String xml = Xml //
12                  .create("CompleteMultipartUpload") //
13                  .a("xmlns", "http://s3.amazonaws.com/doc/2006-03-01/") //
14                  .a("weird", "&<>'\"") //
15                  .e("Part") //
16                  .e("ETag").content("1234&") //
17                  .up() //
18                  .e("PartNumber").content("1") //
19                  .toString();
20          assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
21                  + "<CompleteMultipartUpload weird=\"&amp;&lt;&gt;&apos;&quot;\" xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n"
22                  + "  <Part>\n" + "    <ETag>1234&amp;</ETag>\n" + "    <PartNumber>1</PartNumber>\n"
23                  + "  </Part>\n" + "</CompleteMultipartUpload>", xml);
24      }
25  
26      @Test(expected = IllegalArgumentException.class)
27      public void testContentAndChild() {
28          Xml.create("root").content("boo").element("child");
29      }
30  
31      @Test
32      public void testPrelude() {
33          assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<root>\n" + "</root>",
34                  Xml.create("root").toString());
35      }
36  
37      @Test
38      public void testNoPrelude() {
39          assertEquals("<root>\n" + "</root>", Xml.create("root").excludePrelude().toString());
40      }
41  
42      @Test
43      public void testNoPreludeOnChild() {
44          assertEquals("<root>\n  <thing></thing>\n" + "</root>",
45                  Xml.create("root").element("thing").content("").excludePrelude().toString());
46      }
47      
48      @Test(expected=IllegalArgumentException.class)
49      public void testNullName() {
50          Xml.create(null);
51      }
52      
53      @Test(expected=IllegalArgumentException.class)
54      public void testBlankName() {
55          Xml.create("  ");
56      }
57      
58      @Test
59      public void testUnusualCharacters1() {
60          assertEquals(
61                  "<root>&#x10461;bc</root>", Xml.create("root").excludePrelude().content("" + (char) 0xd801 + "abc").toString());
62          assertEquals(
63                  "<root>&#xfffd;</root>", Xml.create("root").excludePrelude().content("" + (char) 0xd801).toString());
64      }
65      
66      @Test
67      public void testUnusualCharacters2() {
68          assertEquals(
69                  "<root>&#xfffd;abc</root>", Xml.create("root").excludePrelude().content("" + (char) 0xdc00 + "abc").toString());
70      }
71      
72      @Test
73      public void testIllegalCharacters() {
74          assertEquals(
75                  "<root>&#xfffd;abc</root>", Xml.create("root").excludePrelude().content("" + (char) 0x00 + "abc").toString());
76      }
77      
78      @Test
79      public void testLegalWhitespace() {
80          assertEquals(
81                  "<root>\t\n\rabc</root>", Xml.create("root").excludePrelude().content("\t\n\rabc").toString());
82      }
83      
84      @Test
85      public void testUnusualCharacters3() {
86          assertEquals(
87                  "<root>&#x103ff;&#xfffd;&#xfffd;&#xefff;&#xd7ff;</root>", Xml.create("root").excludePrelude().content("" + (char) 0xd800  + (char) 0xdfff  + (char)0xfffe + (char) 0xffff + (char) 0xefff + (char) 0xd7ff).toString());
88      }
89  
90  
91  }