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

Last change on this file since 193 was 193, checked in by imi, 17 years ago
  • added new XmlObjectParser that should replace the direct use of MinML2 in the future
  • changed AnnotationPreset to use the new parser
File size: 3.7 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 testManyTags() throws Exception {
55 StringBuilder b = new StringBuilder("<all>");
56 for (int i = 0; i < 50000; ++i) {
57 if (Math.random() > 0.5) {
58 b.append("<foo bar='blob");
59 b.append(i);
60 b.append("'/>");
61 } else {
62 b.append("<foo><bar>yuppel");
63 b.append(i);
64 b.append("</bar></foo>");
65 }
66 }
67 b.append("</all>");
68
69 System.gc();
70 long memBefore = Runtime.getRuntime().freeMemory();
71 parser = createParser(b.toString());
72 Thread.sleep(300);
73 System.gc();
74 long memAfter = Runtime.getRuntime().freeMemory();
75 assertTrue("2MB should be more than enough. "+(memAfter-memBefore), memAfter-memBefore < 2*1024*1024);
76
77 for (int i = 0; i < 50000; ++i) {
78 Foo f = (Foo)parser.next();
79 assertTrue(f.bar.equals("blob"+i) || f.bar.equals("yuppel"+i));
80 }
81 assertFalse(parser.hasNext());
82 }
83
84 public void testIterable() throws Exception {
85 parser = createParser("<xml><foo bar='yo'/><foo bar='yo'/><foo bar='yo'/></xml>");
86 for (Object o : parser)
87 assertEquals("yo", ((Foo)o).bar);
88 }
89
90 public void testUniformIterable() throws Exception {
91 XmlObjectParser.Uniform<Foo> p = new Uniform<Foo>(new StringReader("<xml><foo bar='sdf'/><foo bar='sdf'/></xml>"), "foo", Foo.class);
92 for (Foo foo : p)
93 assertEquals("sdf", foo.bar);
94 }
95
96
97 public void testObjectIntersection() throws Exception {
98 parser.map("foo", Foo.class);
99 parser.map("imi", Bar.class);
100 parser.start(new StringReader("<xml><foo bar='yo'><imi ada='123'/></foo></xml>"));
101
102 Object imi = parser.next();
103 Object foo = parser.next();
104 assertTrue(imi instanceof Bar);
105 assertTrue(foo instanceof Foo);
106 assertEquals("yo", ((Foo)foo).bar);
107 assertEquals("123", ((Bar)imi).ada);
108 }
109
110 public void testObjectIntersectionWithMapOnStart() throws Exception {
111 parser.mapOnStart("foo", Foo.class);
112 parser.map("imi", Bar.class);
113 parser.start(new StringReader("<xml><foo><imi/></foo></xml>"));
114
115 Object foo = parser.next();
116 Object imi = parser.next();
117 assertTrue(imi instanceof Bar);
118 assertTrue(foo instanceof Foo);
119 }
120
121 public void testMapReportsObjectsAndDoNotFillAttributes() throws Exception {
122 parser.map("foo", Foo.class);
123 parser.map("bar", Bar.class);
124 parser.start(new StringReader("<xml><foo><bar/></foo></xml>"));
125
126 assertTrue(parser.next() instanceof Bar);
127 Object foo = parser.next();
128 assertTrue(foo instanceof Foo);
129 assertNull(((Foo)foo).bar);
130 }
131}
Note: See TracBrowser for help on using the repository browser.