source: josm/test/org/openstreetmap/josm/tools/XmlObjectParserTest.java@ 267

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