Improve XML parsing to handle attributes as well.
[cfb.git] / test / net / jaekl / qd / xml / ParseResultTest.java
index d58ab8c9a522484c4ada261145725e8a3ab486a3..7b0ea6fc57d0d6e29e7beb4437934e5d321e1a5f 100644 (file)
@@ -5,6 +5,7 @@ package net.jaekl.qd.xml;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Locale;
 
 import org.junit.Assert;
 
@@ -70,14 +71,14 @@ public class ParseResultTest {
                        + "<Route><RouteNo>4</RouteNo><DirectionID>1</DirectionID><Direction>Northbound</Direction>"
                        + "<RouteHeading>Rideau C / Ctr Rideau</RouteHeading>"
                        + "<Trips>"
-                       + "<Trip><TripDestination>Rideau Centre / Centre Rideau</TripDestination><TripStartTime>19:00</TripStartTime>"
+                       + "<Trip ghost=\"false\"><TripDestination>Rideau Centre / Centre Rideau</TripDestination><TripStartTime>19:00</TripStartTime>"
                        + "<AdjustedScheduleTime>16</AdjustedScheduleTime><AdjustmentAge>0.45</AdjustmentAge><LastTripOfSchedule/>"
                        + "<BusType>4LB - IN</BusType><Latitude>45.408957</Latitude><Longitude>-75.664125</Longitude>"
                        + "<GPSSpeed>66.4</GPSSpeed></Trip>"
-                       + "<Trip><TripDestination>Rideau Centre / Centre Rideau</TripDestination>"
+                       + "<Trip ghost=\"true\"><TripDestination>Rideau Centre / Centre Rideau</TripDestination>"
                        + "<TripStartTime>19:30</TripStartTime><AdjustedScheduleTime>40</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>"
                        + "<LastTripOfSchedule/><BusType>4LB - IN</BusType><Latitude/><Longitude/><GPSSpeed/></Trip>"
-                       + "<Trip><TripDestination>Rideau Centre / Centre Rideau</TripDestination><TripStartTime>20:00</TripStartTime>"
+                       + "<Trip ghost=\"true\"><TripDestination>Rideau Centre / Centre Rideau</TripDestination><TripStartTime>20:00</TripStartTime>"
                        + "<AdjustedScheduleTime>70</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge><LastTripOfSchedule/>"
                        + "<BusType>4LB - IN</BusType><Latitude/><Longitude/><GPSSpeed/></Trip>"
                        + "</Trips></Route></Routes></GetRouteSummaryForStopResult></GetRouteSummaryForStopResponse>"
@@ -94,7 +95,7 @@ public class ParseResultTest {
 
                @Override
                public void endContents(String uri, String localName, String qName,
-                               String chars, Attributes attr) throws XmlParseException 
+                               String chars) throws XmlParseException 
                {
                        Assert.fail("Should not have any contents to end.");
                }
@@ -197,7 +198,7 @@ public class ParseResultTest {
 
                @Override
                public void endContents(String uri, String localName, String qName,
-                               String chars, Attributes attr) throws XmlParseException 
+                               String chars) throws XmlParseException 
                {
                        if (localName.equals(ONE)) {
                                m_one = chars;
@@ -281,8 +282,7 @@ public class ParseResultTest {
                public RouteParse getRoute(int idx) { return m_routes.get(idx); }
 
                @Override
-               public void endContents(String uri, String localName, String qName,
-                               String chars, Attributes attr) throws XmlParseException 
+               public void endContents(String uri, String localName, String qName,     String chars) throws XmlParseException 
                {
                        if (localName.equals(STOP_NO)) {
                                m_stopNo = Integer.parseInt(chars);
@@ -344,7 +344,7 @@ public class ParseResultTest {
 
                @Override
                public void endContents(String uri, String localName, String qName,
-                               String chars, Attributes attr) throws XmlParseException 
+                               String chars) throws XmlParseException 
                {
                        if (localName.equals(ROUTE_NO)) {
                                m_routeNo = Integer.parseInt(chars);
@@ -376,6 +376,7 @@ public class ParseResultTest {
        }
        public static class TripParse extends ParseResult {
                private static final String TRIP = "Trip";
+               private static final String GHOST = "ghost";
                private static final String TRIP_DEST = "TripDestination";
                private static final String TRIP_START = "TripStartTime";
                private static final String ADJ_SCHED_TIME = "AdjustedScheduleTime";
@@ -386,22 +387,33 @@ public class ParseResultTest {
                // Data gleaned from parsing
                String m_dest;
                String m_startTime;
-               int m_adjSchedTime;;
+               int m_adjSchedTime;
+               boolean m_ghost;
                
                public TripParse() {
                        super(TRIP, INTERNAL, EXTERNAL);
                        
                        m_dest = m_startTime = null;
                        m_adjSchedTime = 0;
+                       m_ghost = false;
                }
                
                public String getDestination() { return m_dest; }
                public String getStartTime() { return m_startTime; }
                public int getAdjustedScheduleTime() { return m_adjSchedTime; }
+               public boolean ghostAttrSet() { return m_ghost; }
+               
+               @Override
+               public void handleMainAttributes(Attributes attr) throws MissingAttributeException
+               {
+                       String scratch = this.getRequiredAttr(TRIP, attr, GHOST);
+                       Assert.assertNotNull(scratch);
+                       m_ghost = scratch.toLowerCase(Locale.CANADA).equals("true");
+               }
 
                @Override
                public void endContents(String uri, String localName, String qName,
-                               String chars, Attributes attr) throws XmlParseException 
+                               String chars) throws XmlParseException 
                {
                        if (localName.equals(TRIP_DEST)) {
                                m_dest = chars;
@@ -502,18 +514,21 @@ public class ParseResultTest {
                        Assert.assertEquals("Rideau Centre / Centre Rideau", tp.getDestination());
                        Assert.assertEquals("19:00", tp.getStartTime());
                        Assert.assertEquals(16, tp.getAdjustedScheduleTime());
+                       Assert.assertEquals(false, tp.ghostAttrSet());
                        
                        tp = rp.getTrip(1);
                        Assert.assertNotNull(tp);
                        Assert.assertEquals("Rideau Centre / Centre Rideau", tp.getDestination());
                        Assert.assertEquals("19:30", tp.getStartTime());
                        Assert.assertEquals(40, tp.getAdjustedScheduleTime());
+                       Assert.assertEquals(true, tp.ghostAttrSet());
 
                        tp = rp.getTrip(2);
                        Assert.assertNotNull(tp);
                        Assert.assertEquals("Rideau Centre / Centre Rideau", tp.getDestination());
                        Assert.assertEquals("20:00", tp.getStartTime());
                        Assert.assertEquals(70, tp.getAdjustedScheduleTime());
+                       Assert.assertEquals(true, tp.ghostAttrSet());
                }
                finally {
                        if (null != bais) {