|
| 1 | +package scala.xml |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.junit.Ignore |
| 5 | +import org.junit.runner.RunWith |
| 6 | +import org.junit.runners.JUnit4 |
| 7 | +import org.junit.Assert.assertTrue |
| 8 | +import org.junit.Assert.assertFalse |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | + |
| 11 | +class XMLTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + def nodeSeq: Unit = { |
| 15 | + val p = <foo> |
| 16 | + <bar gt='ga' value="3"/> |
| 17 | + <baz bazValue="8"/> |
| 18 | + <bar value="5" gi='go'/> |
| 19 | + </foo> |
| 20 | + |
| 21 | + val pelems_1 = for (x <- p \ "bar"; y <- p \ "baz" ) yield { |
| 22 | + Text(x.attributes("value").toString + y.attributes("bazValue").toString+ "!") |
| 23 | + }; |
| 24 | + |
| 25 | + val pelems_2 = new NodeSeq { val theSeq = List(Text("38!"),Text("58!")) }; |
| 26 | + assertTrue(pelems_1 sameElements pelems_2) |
| 27 | + assertTrue(Text("8") sameElements (p \\ "@bazValue")) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + def queryBooks: Unit = { |
| 32 | + val books = |
| 33 | + <bks> |
| 34 | + <book><title>Blabla</title></book> |
| 35 | + <book><title>Blubabla</title></book> |
| 36 | + <book><title>Baaaaaaalabla</title></book> |
| 37 | + </bks>; |
| 38 | + |
| 39 | + val reviews = |
| 40 | + <reviews> |
| 41 | + <entry><title>Blabla</title> |
| 42 | + <remarks> |
| 43 | + Hallo Welt. |
| 44 | + </remarks> |
| 45 | + </entry> |
| 46 | + <entry><title>Blubabla</title> |
| 47 | + <remarks> |
| 48 | + Hello Blu |
| 49 | + </remarks> |
| 50 | + </entry> |
| 51 | + <entry><title>Blubabla</title> |
| 52 | + <remarks> |
| 53 | + rem 2 |
| 54 | + </remarks> |
| 55 | + </entry> |
| 56 | + </reviews>; |
| 57 | + |
| 58 | + val results1 = new scala.xml.PrettyPrinter(80, 5).formatNodes ( |
| 59 | + for (t <- books \\ "title"; |
| 60 | + r <- reviews \\ "entry" |
| 61 | + if (r \ "title") xml_== t) yield |
| 62 | + <result> |
| 63 | + { t } |
| 64 | + { r \ "remarks" } |
| 65 | + </result> |
| 66 | + ); |
| 67 | + val results1Expected = """<result> |
| 68 | + | <title>Blabla</title> |
| 69 | + | <remarks> Hallo Welt. </remarks> |
| 70 | + |</result><result> |
| 71 | + | <title>Blubabla</title> |
| 72 | + | <remarks> Hello Blu </remarks> |
| 73 | + |</result><result> |
| 74 | + | <title>Blubabla</title> |
| 75 | + | <remarks> rem 2 </remarks> |
| 76 | + |</result>""".stripMargin |
| 77 | + assertEquals(results1Expected, results1) |
| 78 | + |
| 79 | + { |
| 80 | + val actual = for (t @ <book><title>Blabla</title></book> <- new NodeSeq { val theSeq = books.child }.toList) |
| 81 | + yield t |
| 82 | + val expected = List(<book><title>Blabla</title></book>) |
| 83 | + assertEquals(expected, actual) |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + def queryPhoneBook: Unit = { |
| 90 | + val phoneBook = |
| 91 | + <phonebook> |
| 92 | + <descr> |
| 93 | + This is the <b>phonebook</b> of the |
| 94 | + <a href="http://acme.org">ACME</a> corporation. |
| 95 | + </descr> |
| 96 | + <entry> |
| 97 | + <name>John</name> |
| 98 | + <phone where="work"> +41 21 693 68 67</phone> |
| 99 | + <phone where="mobile">+41 79 602 23 23</phone> |
| 100 | + </entry> |
| 101 | + </phonebook>; |
| 102 | + |
| 103 | + val addrBook = |
| 104 | + <addrbook> |
| 105 | + <descr> |
| 106 | + This is the <b>addressbook</b> of the |
| 107 | + <a href="http://acme.org">ACME</a> corporation. |
| 108 | + </descr> |
| 109 | + <entry> |
| 110 | + <name>John</name> |
| 111 | + <street> Elm Street</street> |
| 112 | + <city>Dolphin City</city> |
| 113 | + </entry> |
| 114 | + </addrbook>; |
| 115 | + |
| 116 | + val actual: String = new scala.xml.PrettyPrinter(80, 5).formatNodes ( |
| 117 | + for (t <- addrBook \\ "entry"; |
| 118 | + r <- phoneBook \\ "entry" |
| 119 | + if (t \ "name") xml_== (r \ "name")) yield |
| 120 | + <result> |
| 121 | + { t.child } |
| 122 | + { r \ "phone" } |
| 123 | + </result> |
| 124 | + ) |
| 125 | + val expected = """|<result> |
| 126 | + | <name>John</name> |
| 127 | + | <street> Elm Street</street> |
| 128 | + | <city>Dolphin City</city> |
| 129 | + | <phone where="work"> +41 21 693 68 67</phone> |
| 130 | + | <phone where="mobile">+41 79 602 23 23</phone> |
| 131 | + |</result>""".stripMargin |
| 132 | + assertEquals(expected, actual) |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + def namespaces: Unit = { |
| 137 | + val cuckoo = <cuckoo xmlns="http://cuckoo.com"> |
| 138 | + <foo/> |
| 139 | + <bar/> |
| 140 | + </cuckoo>; |
| 141 | + assertEquals("http://cuckoo.com", cuckoo.namespace) |
| 142 | + for (n <- cuckoo \ "_" ) { |
| 143 | + assertEquals("http://cuckoo.com", n.namespace) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + def validationOfElements: Unit = { |
| 149 | + val vtor = new scala.xml.dtd.ElementValidator(); |
| 150 | + { |
| 151 | + import scala.xml.dtd.ELEMENTS |
| 152 | + import scala.xml.dtd.ContentModel._ |
| 153 | + vtor.setContentModel( |
| 154 | + ELEMENTS( |
| 155 | + Sequ( |
| 156 | + Letter(ElemName("bar")), |
| 157 | + Star(Letter(ElemName("baz"))) ))); |
| 158 | + } |
| 159 | + assertTrue(vtor(<foo><bar/><baz/><baz/></foo>)) |
| 160 | + |
| 161 | + { |
| 162 | + import scala.xml.dtd.MIXED |
| 163 | + import scala.xml.dtd.ContentModel._ |
| 164 | + |
| 165 | + vtor.setContentModel( |
| 166 | + MIXED( |
| 167 | + Alt(Letter(ElemName("bar")), |
| 168 | + Letter(ElemName("baz")), |
| 169 | + Letter(ElemName("bal"))))); |
| 170 | + } |
| 171 | + |
| 172 | + assertTrue(vtor(<foo><bar/><baz/><baz/></foo> )) |
| 173 | + assertTrue(vtor(<foo>ab<bar/>cd<baz/>ed<baz/>gh</foo> )) |
| 174 | + assertFalse(vtor(<foo> <ugha/> <bugha/> </foo> )) |
| 175 | + } |
| 176 | + |
| 177 | + def validationfOfAttributes: Unit = { |
| 178 | + val vtor = new scala.xml.dtd.ElementValidator(); |
| 179 | + vtor.setContentModel(null) |
| 180 | + vtor.setMetaData(List()) |
| 181 | + assertFalse(vtor( <foo bar="hello"/> )) |
| 182 | + |
| 183 | + { |
| 184 | + import scala.xml.dtd._ |
| 185 | + vtor setMetaData List(AttrDecl("bar", "CDATA", IMPLIED)) |
| 186 | + } |
| 187 | + assertFalse(vtor(<foo href="http://foo.com" bar="hello"/>)) |
| 188 | + assertTrue(vtor(<foo bar="hello"/>)) |
| 189 | + |
| 190 | + { |
| 191 | + import scala.xml.dtd._ |
| 192 | + vtor.setMetaData(List(AttrDecl("bar","CDATA",REQUIRED))) |
| 193 | + } |
| 194 | + assertFalse(vtor( <foo href="http://foo.com" /> )) |
| 195 | + assertTrue( vtor( <foo bar="http://foo.com" /> )) |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments