1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
package cgeo.geocaching.export;
import static org.assertj.core.api.Assertions.assertThat;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.files.GPX10Parser;
import cgeo.geocaching.files.ParserException;
import cgeo.geocaching.list.StoredList;
import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase;
import cgeo.geocaching.test.R;
import org.apache.commons.lang3.CharEncoding;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase {
public static void testWriteEmptyGPX() throws Exception {
final StringWriter writer = new StringWriter();
new GpxSerializer().writeGPX(Collections.<String> emptyList(), writer, null);
assertThat(writer.getBuffer().toString()).isEqualTo("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>" +
"<gpx version=\"1.0\" creator=\"c:geo - http://www.cgeo.org/\" " +
"xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd " +
"http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/1/cache.xsd " +
"http://www.gsak.net/xmlv1/6 http://www.gsak.net/xmlv1/6/gsak.xsd\" " +
"xmlns=\"http://www.topografix.com/GPX/1/0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:groundspeak=\"http://www.groundspeak.com/cache/1/0\" xmlns:gsak=\"http://www.gsak.net/xmlv1/6\" " +
"xmlns:cgeo=\"http://www.cgeo.org/wptext/1/0\" />");
}
public void testProgressReporting() throws IOException, ParserException {
final AtomicReference<Integer> importedCount = new AtomicReference<Integer>(0);
final StringWriter writer = new StringWriter();
final Geocache cache = loadCacheFromResource(R.raw.gc1bkp3_gpx101);
assertThat(cache).isNotNull();
new GpxSerializer().writeGPX(Collections.singletonList("GC1BKP3"), writer, new GpxSerializer.ProgressListener() {
@Override
public void publishProgress(final int countExported) {
importedCount.set(countExported);
}
});
assertEquals("Progress listener not called", 1, importedCount.get().intValue());
}
/**
* This test verifies that a loop of import, export, import leads to the same cache information.
*
*/
public void testStableExportImportExport() throws IOException, ParserException {
final String geocode = "GC1BKP3";
final int cacheResource = R.raw.gc1bkp3_gpx101;
final Geocache cache = loadCacheFromResource(cacheResource);
assertThat(cache).isNotNull();
final String gpxFirst = getGPXFromCache(geocode);
assertThat(gpxFirst.length() > 0).isTrue();
final GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST.id);
final InputStream stream = new ByteArrayInputStream(gpxFirst.getBytes(CharEncoding.UTF_8));
final Collection<Geocache> caches = parser.parse(stream, null);
assertThat(caches).isNotNull();
assertThat(caches).hasSize(1);
final String gpxSecond = getGPXFromCache(geocode);
assertThat(replaceLogIds(gpxSecond)).isEqualTo(replaceLogIds(gpxFirst));
}
private static String replaceLogIds(final String gpx) {
return gpx.replaceAll("log id=\"\\d*\"", "");
}
private static String getGPXFromCache(final String geocode) throws IOException {
final StringWriter writer = new StringWriter();
new GpxSerializer().writeGPX(Collections.singletonList(geocode), writer, null);
return writer.toString();
}
public static void testStateFromStateCountry() throws Exception {
final Geocache cache = withLocation("state, country");
assertThat(GpxSerializer.getState(cache)).isEqualTo("state");
}
public static void testCountryFromStateCountry() throws Exception {
final Geocache cache = withLocation("state, country");
assertThat(GpxSerializer.getCountry(cache)).isEqualTo("country");
}
public static void testCountryFromCountryOnly() throws Exception {
final Geocache cache = withLocation("somewhere");
assertThat(GpxSerializer.getCountry(cache)).isEqualTo("somewhere");
}
public static void testStateFromCountryOnly() throws Exception {
final Geocache cache = withLocation("somewhere");
assertThat(GpxSerializer.getState(cache)).isEmpty();
}
public static void testCountryFromExternalCommaString() throws Exception {
final Geocache cache = withLocation("first,second"); // this was not created by c:geo, therefore don't split it
assertThat(GpxSerializer.getState(cache)).isEmpty();
}
private static Geocache withLocation(final String location) {
final Geocache cache = new Geocache();
cache.setLocation(location);
return cache;
}
}
|