From 7423e836f2f6e994e5823ab38a7327b8e590f624 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Fri, 31 Mar 2006 09:10:11 +0000 Subject: added test case for xml lib --- test/files/jvm/xmlstuff.check | 36 ++++++ test/files/jvm/xmlstuff.scala | 258 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 test/files/jvm/xmlstuff.check create mode 100644 test/files/jvm/xmlstuff.scala (limited to 'test') diff --git a/test/files/jvm/xmlstuff.check b/test/files/jvm/xmlstuff.check new file mode 100644 index 0000000000..1963498001 --- /dev/null +++ b/test/files/jvm/xmlstuff.check @@ -0,0 +1,36 @@ +NodeSeq +passed ok +passed ok + + Blabla + Hallo Welt. + + Blubabla + Hello Blu + + Blubabla + rem 2 + +List(Blabla) + + John + Elm Street + Dolphin City + +41 21 693 68 67 + +41 79 602 23 23 + +namespaces +passed ok +passed ok +passed ok +validation - elements +passed ok +passed ok +passed ok +passed ok +validation - attributes +passed ok +passed ok +passed ok +passed ok +passed ok diff --git a/test/files/jvm/xmlstuff.scala b/test/files/jvm/xmlstuff.scala new file mode 100644 index 0000000000..0d1aad5fac --- /dev/null +++ b/test/files/jvm/xmlstuff.scala @@ -0,0 +1,258 @@ +import java.io.StringReader; +import org.xml.sax.InputSource; +import scala.testing.UnitTest._ ; +import scala.xml.{Node, NodeSeq, Elem, Text, XML}; + +object Test { + + /** returns true if exception was thrown */ + def catcher(att:Function1[Unit,scala.xml.MetaData]): Boolean = { + var ex = false; + try { + val x = att.apply({}); + } catch { + case scala.xml.MalformedAttributeException(msg) => + Console.println(msg); + ex = true; + } + return ex; + } + + def main(args:Array[String]) = { + + //val e: scala.xml.MetaData = null; //Node.NoAttributes; + //val sc: scala.xml.NamespaceBinding = null; + + // ------------------------------------------ tests for class NodeSeq + + /** + Console.println("checking wellformed attributes"); + { + import scala.xml.{ UnprefixedAttribute, Null } + assertTrue(catcher {x:Unit => new UnprefixedAttribute("key", "<", Null)}); // < illegal + assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "&", Null))); // & illegal + assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "a&a", Null))); // & illegal + assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "a&a;&", Null))); // 2nd & + + assertFalse(catcher(x:Unit => new UnprefixedAttribute("key", "a&a; <<", Null))); + } +*/ + +/* +checking wellformed attributes +< not allowed in attribute value +passed ok +malformed entity reference in attribute value [&] +passed ok +malformed entity reference in attribute value [a&a] +passed ok +malformed entity reference in attribute value [a&a;&] +passed ok +passed ok +*/ + + Console.println("NodeSeq"); + import scala.xml.Utility.view ; + + val p = + + + + ; + + val pelems_1 = for( val x <- p \ "bar"; val y <- p \ "baz" ) yield { + Text(x.attribute("value").toString() + y.attribute("bazValue").toString()+ "!") + }; + val pelems_2 = new NodeSeq { val theSeq = List(Text("38!"),Text("58!")) }; + assertSameElements(pelems_1, pelems_2); + assertSameElements( + p \\ "@value", new NodeSeq { val theSeq = List(Text("3"), Text("5")) } + ); + + + /* // more test cases !!! + val test = ; + + Console.println(test \ "@name"); + + val x = test.attributes.nodes; + Console.println("trying to print"); + val it = x.elements; + while(it.hasNext) { + val c = it.next; + Console.println(c); + Console.println("c.label == @name? "+(c.label == "@name")); + } + */ + + /* + for(val c <- x) { + Console.println(x); + } + */ + + val books = + + Blabla + Blubabla + Baaaaaaalabla + ; + + val reviews = + + Blabla + + Hallo Welt. + + + Blubabla + + Hello Blu + + + Blubabla + + rem 2 + + + ; + + Console.println( new scala.xml.PrettyPrinter(80, 5).formatNodes ( + for( val t <- books \\ "title"; + val r <- reviews \\ "entry"; + r \ "title" == t) yield + + { t } + { r \ "remarks" } + + )); + + // example + Console.println( + for( val t @ Blabla <- new NodeSeq { val theSeq = books.child }.toList) + yield t + ); +val phoneBook = + + + This is the phonebook of the + ACME corporation. + + + John + +41 21 693 68 67 + +41 79 602 23 23 + + ; + + +val addrBook = + + + This is the addressbook of the + ACME corporation. + + + John + Elm Street + Dolphin City + + ; + + Console.println( new scala.xml.PrettyPrinter(80, 5).formatNodes ( + for( val t <- addrBook \\ "entry"; + val r <- phoneBook \\ "entry"; + t \ "name" == r \ "name") yield + + { t.child } + { r \ "phone" } + + )); + + + /* namespaces */ + // begin tmp + Console.println("namespaces"); + val cuckoo = + + + ; + assertEquals( cuckoo.namespace, "http://cuckoo.com"); + for( val n <- cuckoo \ "_" ) { + //Console.println("n = "+n); + //Console.println("n.prefix = "+n.prefix); + //Console.println("n.scope = "+n.scope); + assertEquals( n.namespace, "http://cuckoo.com"); + } + // end tmp + /* + +DEPRECATED, don't support namespaces in pattern match anymore +*/ + /* + assertEquals( true, cuckoo match { + case + + + => true; + case _ => false; }); +*/ + /* + // begin tmp + assertEquals( false, cuckoo match { + case + + + => true; + case _ => false; }); + // end tmp + */ + Console.println("validation - elements"); + val vtor = new scala.xml.dtd.ElementValidator(); + { + import scala.xml.dtd.ELEMENTS; + import scala.xml.dtd.ContentModel._; + vtor.setContentModel( + ELEMENTS( + Sequ( + Letter(ElemName("bar")), + Star(Letter(ElemName("baz"))) ))); + + } + assertEquals( vtor( ), true ); + { + import scala.xml.dtd.MIXED; + import scala.xml.dtd.ContentModel._; + + vtor.setContentModel( + MIXED( + Alt(Letter(ElemName("bar")), + Letter(ElemName("baz")), + Letter(ElemName("bal"))))); + } + + assertEquals( vtor( ), true ); + assertEquals( vtor( abcdedgh ), true ); + assertEquals( vtor( ), false ); + + Console.println("validation - attributes"); + vtor.setContentModel(null); + vtor.setMetaData(List()); + assertEquals( vtor( ), false ); + + { + import scala.xml.dtd._ ; + vtor.setMetaData(List(AttrDecl("bar","CDATA",IMPLIED))); + } + assertEquals( vtor( ), false ); + assertEquals( vtor( ), true ); + + { + import scala.xml.dtd._ ; + vtor.setMetaData(List(AttrDecl("bar","CDATA",REQUIRED))); + } + assertEquals( vtor( ), false ); + assertEquals( vtor( ), true ); + + } +} -- cgit v1.2.3