source: josm/branch/0.5/test/unit/org/openstreetmap/josm/tools/XmlObjectParserTest.java

Last change on this file was 298, checked in by imi, 19 years ago
  • added license description to head of each source file
File size: 2.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.io.StringReader;
5import java.util.NoSuchElementException;
6
7import org.openstreetmap.josm.tools.XmlObjectParser.Uniform;
8
9import junit.framework.TestCase;
10
11public class XmlObjectParserTest extends TestCase {
12
13 private XmlObjectParser parser;
14
15 public static class Foo {
16 public String bar;
17 }
18 public static class Bar {
19 private String ada;
20 public void setAda(String value) {
21 ada = value;
22 }
23 }
24
25 @Override protected void setUp() throws Exception {
26 super.setUp();
27 parser = new XmlObjectParser();
28 }
29
30 private XmlObjectParser createParser(String string) {
31 XmlObjectParser parser = new XmlObjectParser();
32 parser.map("foo", Foo.class);
33 parser.start(new StringReader(string));
34 return parser;
35 }
36
37 public void testSimpleStructWithAttributes() throws Exception {
38 parser = createParser("<xml><foo bar='foobar'/><foo bar='baz'/></xml>");
39
40 assertEquals("foobar", ((Foo)parser.next()).bar);
41 assertEquals("baz", ((Foo)parser.next()).bar);
42 assertFalse(parser.hasNext());
43 try {
44 parser.next();
45 fail();
46 } catch (NoSuchElementException e) {
47 }
48 }
49
50 public void testSubtagsWithCharacters() throws Exception {
51 parser = createParser("<foo><bar>asd</bar></foo>");
52 assertEquals("asd", ((Foo)parser.next()).bar);
53 }
54
55 public void testIterable() throws Exception {
56 parser = createParser("<xml><foo bar='yo'/><foo bar='yo'/><foo bar='yo'/></xml>");
57 for (Object o : parser)
58 assertEquals("yo", ((Foo)o).bar);
59 }
60
61 public void testUniformIterable() throws Exception {
62 XmlObjectParser.Uniform<Foo> p = new Uniform<Foo>(new StringReader("<xml><foo bar='sdf'/><foo bar='sdf'/></xml>"), "foo", Foo.class);
63 for (Foo foo : p)
64 assertEquals("sdf", foo.bar);
65 }
66
67
68 public void testObjectIntersection() throws Exception {
69 parser.map("foo", Foo.class);
70 parser.map("imi", Bar.class);
71 parser.start(new StringReader("<xml><foo bar='yo'><imi ada='123'/></foo></xml>"));
72
73 Object imi = parser.next();
74 Object foo = parser.next();
75 assertTrue(imi instanceof Bar);
76 assertTrue(foo instanceof Foo);
77 assertEquals("yo", ((Foo)foo).bar);
78 assertEquals("123", ((Bar)imi).ada);
79 }
80
81 public void testObjectIntersectionWithMapOnStart() throws Exception {
82 parser.mapOnStart("foo", Foo.class);
83 parser.map("imi", Bar.class);
84 parser.start(new StringReader("<xml><foo><imi/></foo></xml>"));
85
86 Object foo = parser.next();
87 Object imi = parser.next();
88 assertTrue(imi instanceof Bar);
89 assertTrue(foo instanceof Foo);
90 }
91
92 public void testMapReportsObjectsAndDoNotFillAttributes() throws Exception {
93 parser.map("foo", Foo.class);
94 parser.map("bar", Bar.class);
95 parser.start(new StringReader("<xml><foo><bar/></foo></xml>"));
96
97 assertTrue(parser.next() instanceof Bar);
98 Object foo = parser.next();
99 assertTrue(foo instanceof Foo);
100 assertNull(((Foo)foo).bar);
101 }
102}
Note: See TracBrowser for help on using the repository browser.