1 // Copyright (C) 2004, 2014 Christian Jaekl
3 package net.jaekl.qd.xml;
5 import java.io.ByteArrayInputStream;
6 import java.io.IOException;
7 import java.util.ArrayList;
9 import org.junit.Assert;
11 import org.junit.Test;
12 import org.xml.sax.InputSource;
13 import org.xml.sax.SAXException;
14 import org.xml.sax.XMLReader;
15 import org.xml.sax.helpers.XMLReaderFactory;
17 public class ParseResultTest {
18 // Some samples of XML that we're going to (try to) parse\
19 private static final String MINIMAL_XML =
21 private static final String MINIMAL_XML_WITH_PROLOGUE =
22 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root/>";
23 private static final String XML_WITH_MINOR_CONTENT =
24 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root><One/></Root>";
25 private static final String ROOT_INSIDE_SECONDARY_ELEMENT =
26 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Secondary><Root><One/></Root></Secondary>";
27 private static final String PROLOGUE_AND_SECONDARY_ELEMENT =
28 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Secondary/>";
29 private static final String SIMPLE_INTERNAL_TAGS =
30 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root><One/><Two>content of two</Two><Three>3</Three></Root>";
31 private static final String ROUTE_SUMMARY_FOR_STOP =
32 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
33 + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
34 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
35 + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
37 + " <GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">\n"
38 + " <GetRouteSummaryForStopResult>\n"
39 + " <StopNo xmlns=\"http://tempuri.org/\">1234</StopNo>\n"
41 + " xmlns=\"http://tempuri.org/\">ONE-TWO-THREE-FOUR</StopDescription>\n"
42 + " <Error xmlns=\"http://tempuri.org/\"/>\n"
43 + " <Routes xmlns=\"http://tempuri.org/\">\n"
45 + " <RouteNo>123</RouteNo>\n"
46 + " <DirectionID>0</DirectionID>\n"
47 + " <Direction>NORTH</Direction>\n"
48 + " <RouteHeading>First Mall</RouteHeading>\n"
51 + " <RouteNo>123</RouteNo>\n"
52 + " <DirectionID>1</DirectionID>\n"
53 + " <Direction>SOUTH</Direction>\n"
54 + " <RouteHeading>Second Mall</RouteHeading>\n"
57 + " </GetRouteSummaryForStopResult>\n"
58 + " </GetRouteSummaryForStopResponse>\n"
60 + "</soap:Envelope>\n";
61 private static final String NEXT_TRIPS_FOR_STOP =
62 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
63 + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
64 + "<soap:Body><GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">"
65 + "<GetRouteSummaryForStopResult>"
66 + "<StopNo xmlns=\"http://tempuri.org/\">2438</StopNo>"
67 + "<StopDescription xmlns=\"http://tempuri.org/\">BRONSON SUNNYSIDE</StopDescription>"
68 + "<Error xmlns=\"http://tempuri.org/\"/><Routes xmlns=\"http://tempuri.org/\">"
69 + "<Route><RouteNo>4</RouteNo><DirectionID>1</DirectionID><Direction>Northbound</Direction>"
70 + "<RouteHeading>Rideau C / Ctr Rideau</RouteHeading>"
72 + "<Trip><TripDestination>Rideau Centre / Centre Rideau</TripDestination><TripStartTime>19:00</TripStartTime>"
73 + "<AdjustedScheduleTime>16</AdjustedScheduleTime><AdjustmentAge>0.45</AdjustmentAge><LastTripOfSchedule/>"
74 + "<BusType>4LB - IN</BusType><Latitude>45.408957</Latitude><Longitude>-75.664125</Longitude>"
75 + "<GPSSpeed>66.4</GPSSpeed></Trip>"
76 + "<Trip><TripDestination>Rideau Centre / Centre Rideau</TripDestination>"
77 + "<TripStartTime>19:30</TripStartTime><AdjustedScheduleTime>40</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>"
78 + "<LastTripOfSchedule/><BusType>4LB - IN</BusType><Latitude/><Longitude/><GPSSpeed/></Trip>"
79 + "<Trip><TripDestination>Rideau Centre / Centre Rideau</TripDestination><TripStartTime>20:00</TripStartTime>"
80 + "<AdjustedScheduleTime>70</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge><LastTripOfSchedule/>"
81 + "<BusType>4LB - IN</BusType><Latitude/><Longitude/><GPSSpeed/></Trip>"
82 + "</Trips></Route></Routes></GetRouteSummaryForStopResult></GetRouteSummaryForStopResponse>"
83 + "</soap:Body></soap:Envelope>";
85 // Do the least possible parsing: check for the <Root/> element only.
86 public static class MinimalParse extends ParseResult {
87 private static final String[] INTERNAL = {};
88 private static final Object[][] EXTERNAL = {} ;
90 public MinimalParse() {
91 super("Root", INTERNAL, EXTERNAL);
95 public void endContents(String uri, String localName, String qName,
96 String chars) throws XmlParseException
98 Assert.fail("Should not have any contents to end.");
102 public void endExternal(String uri, String localName, String qName)
103 throws XmlParseException
105 Assert.fail("Should not have any external tags to end.");
109 // Check that we can parse a minimal document without errors.
110 // Because there's no content being parsed (beyond the root element), there is
111 // no "correct" behaviour to assert. The test is to confirm that we
112 // don't do anything incorrect--no calls to endContent() nor endExternal(),
113 // and no exceptions thrown along the way.
115 public void test_withMinimalParse() throws IOException, SAXException {
116 MinimalParse mp = new MinimalParse();
117 ByteArrayInputStream bais = null;
121 MINIMAL_XML_WITH_PROLOGUE,
122 XML_WITH_MINOR_CONTENT,
123 ROOT_INSIDE_SECONDARY_ELEMENT
126 for (String datum : data) {
128 bais = new ByteArrayInputStream(datum.getBytes("UTF-8"));
129 XMLReader reader = XMLReaderFactory.createXMLReader();
130 ParseHandler ph = new ParseHandler(mp);
131 reader.setContentHandler(ph);
132 reader.parse(new InputSource(bais));
142 // If we parse something that doesn't have the expected root element, we should generate an exception
144 public void test_minimalParseWithMismatchedRootElement() throws IOException {
145 MinimalParse mp = new MinimalParse();
146 ByteArrayInputStream bais = null;
148 String[] data = { PROLOGUE_AND_SECONDARY_ELEMENT };
150 for (String datum : data) {
152 bais = new ByteArrayInputStream(datum.getBytes("UTF-8"));
153 XMLReader reader = XMLReaderFactory.createXMLReader();
154 ParseHandler ph = new ParseHandler(mp);
155 reader.setContentHandler(ph);
156 reader.parse(new InputSource(bais));
157 Assert.fail("Should have thrown an exception.");
159 catch ( SAXException se ) {
160 Throwable cause = se.getCause();
161 Assert.assertNotNull(cause);
162 Assert.assertTrue(cause instanceof MissingInfoException);
163 MissingInfoException mie = (MissingInfoException) cause;
164 Assert.assertEquals("Root", mie.getTagName());
174 // Do the some simple parsing: <Root/> and some subtags that are processed internally
175 public static class SimpleParse extends ParseResult {
176 private static final String ONE = "One";
177 private static final String TWO = "Two";
178 private static final String THREE = "Three";
180 private static final String[] INTERNAL = {ONE, TWO, THREE};
181 private static final Object[][] EXTERNAL = {} ;
187 public SimpleParse() {
188 super("Root", INTERNAL, EXTERNAL);
190 m_one = m_two = m_three = null;
193 public String getOne() { return m_one; }
194 public String getTwo() { return m_two; }
195 public String getThree() { return m_three; }
198 public void endContents(String uri, String localName, String qName,
199 String chars) throws XmlParseException
201 if (localName.equals(ONE)) {
204 else if (localName.equals(TWO)) {
207 else if (localName.equals(THREE)) {
213 public void endExternal(String uri, String localName, String qName)
214 throws XmlParseException
216 Assert.fail("Should not have any external tags to end.");
220 // Parse some XML containing subtags that are handled internally by SimpleParse
222 public void test_parseWithInternalSubtags() throws IOException, SAXException
224 SimpleParse sp = new SimpleParse();
225 ByteArrayInputStream bais = null;
231 for (String datum : data) {
233 bais = new ByteArrayInputStream(datum.getBytes("UTF-8"));
234 XMLReader reader = XMLReaderFactory.createXMLReader();
235 ParseHandler ph = new ParseHandler(sp);
236 reader.setContentHandler(ph);
237 reader.parse(new InputSource(bais));
239 Assert.assertEquals("", sp.getOne());
240 Assert.assertEquals("content of two", sp.getTwo());
241 Assert.assertEquals("3", sp.getThree());
251 // Parse sub-tags, handling some internally and some externally
252 public static class RouteSummaryParse extends ParseResult {
253 private static final String STOP_NO = "StopNo";
254 private static final String STOP_DESCR = "StopDescription";
255 private static final String ERROR = "Error";
256 private static final String ROUTES = "Routes";
257 private static final String ROUTE = "Route";
259 private static final String[] INTERNAL = {STOP_NO, STOP_DESCR, ERROR, ROUTES};
260 private static final Object[][] EXTERNAL = { {ROUTE, RouteParse.class} };
262 // Data gleaned from parsing
266 ArrayList<RouteParse> m_routes;
268 public RouteSummaryParse() {
269 super("GetRouteSummaryForStopResult", INTERNAL, EXTERNAL);
272 m_stopDescr = m_error = null;
273 m_routes = new ArrayList<RouteParse>();
276 public int getStopNo() { return m_stopNo; }
277 public String getStopDescription() { return m_stopDescr; }
278 public String getError() { return m_error; }
279 public int getNumRoutes() { return m_routes.size(); }
280 public RouteParse getRoute(int idx) { return m_routes.get(idx); }
283 public void endContents(String uri, String localName, String qName,
284 String chars) throws XmlParseException
286 if (localName.equals(STOP_NO)) {
287 m_stopNo = Integer.parseInt(chars);
289 else if (localName.equals(STOP_DESCR)) {
292 else if (localName.equals(ERROR)) {
298 public void endExternal(String uri, String localName, String qName)
299 throws XmlParseException
301 if (localName.equals(ROUTE)) {
302 ParseResult[] collected = collectParsedChildren(RouteParse.class);
303 for (ParseResult pr : collected) {
304 Assert.assertTrue(pr instanceof RouteParse);
305 m_routes.add((RouteParse)pr);
310 public static class RouteParse extends ParseResult {
311 private static final String ROUTE = "Route";
312 private static final String ROUTE_NO = "RouteNo";
313 private static final String DIR_ID = "DirectionID";
314 private static final String DIR = "Direction";
315 private static final String HEADING = "RouteHeading";
316 private static final String TRIPS = "Trips";
317 private static final String TRIP = "Trip";
319 private static final String[] INTERNAL = {ROUTE_NO, DIR_ID, DIR, HEADING, TRIPS};
320 private static final Object[][] EXTERNAL = { {TRIP, TripParse.class} };
322 // Data gleaned from parsing
327 ArrayList<TripParse> m_trips;
329 public RouteParse() {
330 super(ROUTE, INTERNAL, EXTERNAL);
332 m_routeNo = m_dirID = 0;
333 m_dir = m_heading = null;
334 m_trips = new ArrayList<TripParse>();
337 public int getRouteNo() { return m_routeNo; }
338 public int getDirectionID() { return m_dirID; }
339 public String getDirection() { return m_dir; }
340 public String getHeading() { return m_heading; }
341 public int getNumTrips() { return m_trips.size(); }
342 public TripParse getTrip(int idx) { return m_trips.get(idx); }
345 public void endContents(String uri, String localName, String qName,
346 String chars) throws XmlParseException
348 if (localName.equals(ROUTE_NO)) {
349 m_routeNo = Integer.parseInt(chars);
351 else if (localName.equals(DIR_ID)) {
352 m_dirID = Integer.parseInt(chars);
354 else if (localName.equals(DIR)) {
357 else if (localName.equals(HEADING)) {
363 public void endExternal(String uri, String localName, String qName)
364 throws XmlParseException
366 if (localName.equals(TRIP)) {
367 ParseResult[] collected = collectParsedChildren(TripParse.class);
368 for (ParseResult pr : collected) {
369 Assert.assertTrue(pr instanceof TripParse);
370 m_trips.add((TripParse)pr);
376 public static class TripParse extends ParseResult {
377 private static final String TRIP = "Trip";
378 private static final String TRIP_DEST = "TripDestination";
379 private static final String TRIP_START = "TripStartTime";
380 private static final String ADJ_SCHED_TIME = "AdjustedScheduleTime";
382 private static final String[] INTERNAL = {TRIP_DEST, TRIP_START, ADJ_SCHED_TIME };
383 private static final Object[][] EXTERNAL = { };
385 // Data gleaned from parsing
391 super(TRIP, INTERNAL, EXTERNAL);
393 m_dest = m_startTime = null;
397 public String getDestination() { return m_dest; }
398 public String getStartTime() { return m_startTime; }
399 public int getAdjustedScheduleTime() { return m_adjSchedTime; }
402 public void endContents(String uri, String localName, String qName,
403 String chars) throws XmlParseException
405 if (localName.equals(TRIP_DEST)) {
408 else if (localName.equals(TRIP_START)) {
411 else if (localName.equals(ADJ_SCHED_TIME)) {
412 m_adjSchedTime = Integer.parseInt(chars);
417 public void endExternal(String uri, String localName, String qName)
418 throws XmlParseException
420 Assert.fail("Should not be attempting to parse external tags.");
424 // Parse some XML containing subtags that are handled both internally and externally
426 public void test_parseRouteSummary() throws IOException, SAXException
428 RouteSummaryParse rsp = new RouteSummaryParse();
429 ByteArrayInputStream bais = null;
434 bais = new ByteArrayInputStream(ROUTE_SUMMARY_FOR_STOP.getBytes("UTF-8"));
435 XMLReader reader = XMLReaderFactory.createXMLReader();
436 ParseHandler ph = new ParseHandler(rsp);
437 reader.setContentHandler(ph);
438 reader.parse(new InputSource(bais));
440 Assert.assertEquals(1234, rsp.getStopNo());
441 Assert.assertEquals("ONE-TWO-THREE-FOUR", rsp.getStopDescription());
442 Assert.assertEquals("", rsp.getError());
444 Assert.assertEquals(2, rsp.getNumRoutes());
446 rp = rsp.getRoute(0);
447 Assert.assertNotNull(rp);
448 Assert.assertEquals(123, rp.getRouteNo());
449 Assert.assertEquals(0, rp.getDirectionID());
450 Assert.assertEquals("NORTH", rp.getDirection());
451 Assert.assertEquals("First Mall", rp.getHeading());
453 rp = rsp.getRoute(1);
454 Assert.assertNotNull(rp);
455 Assert.assertEquals(123, rp.getRouteNo());
456 Assert.assertEquals(1, rp.getDirectionID());
457 Assert.assertEquals("SOUTH", rp.getDirection());
458 Assert.assertEquals("Second Mall", rp.getHeading());
467 // Parse a 3-level external-tag hierarchy: RouteSummary contains Routes contains Trips
469 public void test_parseThreeLevels() throws IOException, SAXException
471 RouteSummaryParse rsp = new RouteSummaryParse();
472 ByteArrayInputStream bais = null;
478 bais = new ByteArrayInputStream(NEXT_TRIPS_FOR_STOP.getBytes("UTF-8"));
479 XMLReader reader = XMLReaderFactory.createXMLReader();
480 ParseHandler ph = new ParseHandler(rsp);
481 reader.setContentHandler(ph);
482 reader.parse(new InputSource(bais));
484 Assert.assertEquals(2438, rsp.getStopNo());
485 Assert.assertEquals("BRONSON SUNNYSIDE", rsp.getStopDescription());
486 Assert.assertEquals("", rsp.getError());
488 Assert.assertEquals(1, rsp.getNumRoutes());
490 rp = rsp.getRoute(0);
491 Assert.assertNotNull(rp);
492 Assert.assertEquals(4, rp.getRouteNo());
493 Assert.assertEquals(1, rp.getDirectionID());
494 Assert.assertEquals("Northbound", rp.getDirection());
495 Assert.assertEquals("Rideau C / Ctr Rideau", rp.getHeading());
497 Assert.assertEquals(3, rp.getNumTrips());
500 Assert.assertNotNull(tp);
501 Assert.assertEquals("Rideau Centre / Centre Rideau", tp.getDestination());
502 Assert.assertEquals("19:00", tp.getStartTime());
503 Assert.assertEquals(16, tp.getAdjustedScheduleTime());
506 Assert.assertNotNull(tp);
507 Assert.assertEquals("Rideau Centre / Centre Rideau", tp.getDestination());
508 Assert.assertEquals("19:30", tp.getStartTime());
509 Assert.assertEquals(40, tp.getAdjustedScheduleTime());
512 Assert.assertNotNull(tp);
513 Assert.assertEquals("Rideau Centre / Centre Rideau", tp.getDestination());
514 Assert.assertEquals("20:00", tp.getStartTime());
515 Assert.assertEquals(70, tp.getAdjustedScheduleTime());