From 5b0dbd5d30fd098acd6a9a791d2f884aff0175ec Mon Sep 17 00:00:00 2001 From: Jared Casper Date: Wed, 9 Feb 2011 16:23:55 -0800 Subject: [PATCH] Be more explicit in testing the XML output of GpxTrackWriter. Instead of producing the comparison strings in the tester just as they are produced in the tested code, use string literals, thus actually testing that the formatting code is working as expected. --- .../apps/mytracks/io/GpxTrackWriter.java | 6 +- .../apps/mytracks/io/GpxTrackWriterTest.java | 83 +++++++------------ 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java index f1216d3cb..65636ce54 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java @@ -37,10 +37,10 @@ import java.util.TimeZone; */ public class GpxTrackWriter implements TrackFormatWriter { - static final int LATLONG_FORMAT = Location.FORMAT_DEGREES; - static final DecimalFormat ELEVATION_FORMATTER = + private static final int LATLONG_FORMAT = Location.FORMAT_DEGREES; + private static final DecimalFormat ELEVATION_FORMATTER = new DecimalFormat("#.#"); - static final SimpleDateFormat TIMESTAMP_FORMATTER = + private static final SimpleDateFormat TIMESTAMP_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); static { TIMESTAMP_FORMATTER.setTimeZone(TimeZone.getTimeZone("UTC")); diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java index 96c3f9cf2..c9c3e7f42 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java @@ -30,62 +30,41 @@ public class GpxTrackWriterTest extends TrackFormatWriterTest { assertEquals(Long.toString(TRACK_ID), getChildTextValue(trackTag, "number")); List segmentTags = getChildElements(trackTag, "trkseg", 2); - Element segment1Tag = segmentTags.get(0); - Element segment2Tag = segmentTags.get(1); - List seg1PointTags = getChildElements(segment1Tag, "trkpt", 2); - List seg2PointTags = getChildElements(segment2Tag, "trkpt", 2); - assertTagsMatchPoints(seg1PointTags, location1, location2); - assertTagsMatchPoints(seg2PointTags, location3, location4); + List segPointTags = getChildElements(segmentTags.get(0), "trkpt", 2); + assertTagMatchesLocation(segPointTags.get(0), + "0", "0", "1970-01-01T02:46:40Z", "0"); + assertTagMatchesLocation(segPointTags.get(1), + "1", "-1", "1970-01-01T02:46:41Z", "5000000"); + + segPointTags = getChildElements(segmentTags.get(1), "trkpt", 2); + assertTagMatchesLocation(segPointTags.get(0), + "2", "-2", "1970-01-01T02:46:42Z", "10000000"); + assertTagMatchesLocation(segPointTags.get(1), + "3", "-3", "1970-01-01T02:46:43Z", "15000000"); + List waypointTags = getChildElements(gpxTag, "wpt", 2); - assertTagsMatchWaypoints(waypointTags, wp1, wp2); + Element wptTag = waypointTags.get(0); + assertEquals(WAYPOINT1_NAME, getChildTextValue(wptTag, "name")); + assertEquals(WAYPOINT1_DESCRIPTION, getChildTextValue(wptTag, "desc")); + assertTagMatchesLocation(wptTag, + "1", "-1", "1970-01-01T02:46:41Z", "5000000"); + + wptTag = waypointTags.get(1); + assertEquals(WAYPOINT2_NAME, getChildTextValue(wptTag, "name")); + assertEquals(WAYPOINT2_DESCRIPTION, getChildTextValue(wptTag, "desc")); + assertTagMatchesLocation(wptTag, + "2", "-2", "1970-01-01T02:46:42Z", "10000000"); } /** - * Asserts that the given tags describe the given waypoints, in the same - * order. + * Asserts that the given tag describes the location given by the + * Strings lat, lon, time, and ele. */ - protected void assertTagsMatchWaypoints(List tags, Waypoint... wps) { - assertEquals(wps.length, tags.size()); - for (int i = 0; i < wps.length; i++) { - Element tag = tags.get(i); - Waypoint wp = wps[i]; - Location loc = wp.getLocation(); - - assertTagMatchesLocation(tag, loc); - - assertEquals(wp.getName(), getChildTextValue(tag, "name")); - assertEquals(wp.getDescription(), getChildTextValue(tag, "desc")); - } - } - - /** - * Asserts that the given tags describe the given points, in the same order. - */ - protected void assertTagsMatchPoints(List tags, Location... locs) { - assertEquals(locs.length, tags.size()); - for (int i = 0; i < locs.length; i++) { - Element tag = tags.get(i); - Location loc = locs[i]; - - assertTagMatchesLocation(tag, loc); - } - } - - /** - * Asserts that the given tag describes the given location. - */ - private void assertTagMatchesLocation(Element tag, Location loc) { - assertEquals( - Location.convert(loc.getLatitude(), GpxTrackWriter.LATLONG_FORMAT), - tag.getAttribute("lat")); - assertEquals( - Location.convert(loc.getLongitude(), GpxTrackWriter.LATLONG_FORMAT), - tag.getAttribute("lon")); - assertEquals( - GpxTrackWriter.TIMESTAMP_FORMATTER.format(new Date(loc.getTime())), - getChildTextValue(tag, "time")); - assertEquals( - GpxTrackWriter.ELEVATION_FORMATTER.format(loc.getAltitude()), - getChildTextValue(tag, "ele")); + private void assertTagMatchesLocation(Element tag, String lat, + String lon, String time, String ele) { + assertEquals(lat, tag.getAttribute("lat")); + assertEquals(lon, tag.getAttribute("lon")); + assertEquals(time, getChildTextValue(tag, "time")); + assertEquals(ele, getChildTextValue(tag, "ele")); } }