forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationTests.cs
More file actions
568 lines (495 loc) · 19.4 KB
/
SerializationTests.cs
File metadata and controls
568 lines (495 loc) · 19.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using BruTile;
using BruTile.Cache;
using BruTile.MbTiles;
using BruTile.Predefined;
using BruTile.Web;
using BruTile.Wmts;
using NUnit.Framework;
using SharpMap.Utilities;
namespace UnitTests.Serialization
{
[Category("BruTile")]
public class SerializationTests
{
[Test]
public void TestExtent()
{
var e1 = new Extent(-10, -10, 10, 10);
var e2 = SandD(e1);
Assert.AreEqual(e1.CenterX, e2.CenterX);
Assert.AreEqual(e1.CenterY, e2.CenterY);
Assert.AreEqual(e1.Width, e2.Width);
Assert.AreEqual(e1.Height, e2.Height);
}
[Test]
public void TestResolution()
{
var r1 = new Resolution("XXX", 1245, 999,999, 5, 5, 4, 4, 0.1);
var r2 = SandD(r1);
Assert.AreEqual(r1.Id, r2.Id);
Assert.AreEqual(r1.UnitsPerPixel, r2.UnitsPerPixel);
Assert.AreEqual(r1.TileWidth, r2.TileWidth);
Assert.AreEqual(r1.TileHeight, r2.TileHeight);
Assert.AreEqual(r1.MatrixWidth, r2.MatrixWidth);
Assert.AreEqual(r1.MatrixHeight, r2.MatrixHeight);
Assert.AreEqual(r1.Top, r2.Top);
Assert.AreEqual(r1.Left, r2.Left);
Assert.AreEqual(r1.ScaleDenominator, r2.ScaleDenominator);
}
[Test]
public void TestGlobalMercatorSchema()
{
string message;
var s1 = new GlobalMercator("png", 2, 11);
var s2 = SandD(s1);
var equal = EqualTileSchemas(s1, s2, out message);
Assert.IsTrue(equal, message);
}
[Test]
public void TestGlobalSphericalMercatorSchema()
{
string message;
var s1 = new GlobalSphericalMercator("png", YAxis.OSM, 2, 11);
var s2 = SandD(s1);
var equal = EqualTileSchemas(s1, s2, out message);
Assert.IsTrue(equal, message);
}
[Test]
public void TestWkstNederlandSchema()
{
string message;
var s1 = new WkstNederlandSchema();
var s2 = SandD(s1);
var equal = EqualTileSchemas(s1, s2, out message);
Assert.IsTrue(equal, message);
}
#region Caches
[Test]
public void TestMemoryCacheBytes()
{
var c1 = new MemoryCache<byte[]>(17, 34);
var c2 = SandD(c1);
Assert.NotNull(c2);
#if DEBUG
Assert.AreEqual(c1.MinTiles, c2.MinTiles);
Assert.AreEqual(c1.MaxTiles, c2.MaxTiles);
#endif
}
[Test]
public void TestMemoryCacheBitmap()
{
var c1 = new BruTile.Cache.MemoryCache<System.Drawing.Bitmap>(17, 34);
var c2 = SandD(c1);
Assert.NotNull(c2);
#if DEBUG
Assert.AreEqual(c1.MinTiles, c2.MinTiles);
Assert.AreEqual(c1.MaxTiles, c2.MaxTiles);
//Assert.IsTrue(c1.EqualSetup(c2));
#endif
}
[Test]
public void FileCache()
{
var c1 = new FileCache(Path.Combine(Path.GetTempPath(), "_test"), "jpg", new TimeSpan(8, 22, 19, 35));
var c2 = SandD(c1);
Assert.NotNull(c2);
#if DEBUG
Assert.AreEqual(ReflectionTests.GetFieldValue(c1, "_cacheExpireTime"),
ReflectionTests.GetFieldValue(c2, "_cacheExpireTime"), "CacheExpireTimes not equal");
Assert.AreEqual(ReflectionTests.GetFieldValue(c1, "_directory"),
ReflectionTests.GetFieldValue(c2, "_directory"), "Directories not equal");
Assert.AreEqual(ReflectionTests.GetFieldValue(c1, "_format"),
ReflectionTests.GetFieldValue(c2, "_format"), "Formats not equal");
//ToDo: Test by reflection
//Assert.IsTrue(c1.EqualSetup(c2));
#endif
}
#endregion
#region Tile sources
[Ignore("Needs internet connection")]
[Category("Internet")]
[TestCase("http://tiles.geoservice.dlr.de/service/wmts?SERVICE=WMTS&REQUEST=GetCapabilities")]
public void TestWmtsTileSource(string url)
{
IEnumerable<ITileSource> sources;
try
{
var req = WebRequest.Create(url);
using (var resp = req.GetResponse())
sources = WmtsParser.Parse(resp.GetResponseStream());
}
catch (Exception ex)
{
throw new IgnoreException("Getting the response stream failed", ex);
}
if (sources == null)
throw new IgnoreException("Failed to get capabilities");
var equal = true;
var messages = new List<string>();
System.Diagnostics.Trace.WriteLine($"Testing {url}");
foreach (var srcS in sources)
{
var srcD = SandD(srcS);
Assert.NotNull(srcD);
System.Diagnostics.Trace.WriteLine($"{srcS}");
string message;
if (!EqualTileSources(srcS, srcD, out message))
{
messages.Add(message);
equal = false;
}
}
Assert.IsTrue(equal, string.Join("\n", messages));
}
[Test]
public void TestOsmTileSource()
{
var ts1 = KnownTileSources.Create(KnownTileSource.StamenTonerLite, null, new FakePersistentCache<byte[]>());
var ts2 = SandD(ts1);
Assert.NotNull(ts2);
string message;
var equal = EqualTileSources(ts1, ts2, out message);
Assert.IsTrue(equal, message);
}
[TestCase(@"C:\Users\obe.IVV-AACHEN\Downloads\geography-class.mbtiles")]
[Ignore("Test a path to a folder on a specific machine")]
public void TestMbTiles(string mbTilesFile)
{
if (!File.Exists(mbTilesFile))
throw new IgnoreException(string.Format("File '{0}' does not exist.", mbTilesFile));
var cn = new SQLite.SQLiteConnectionString(mbTilesFile, false);
var p1 = new MbTilesTileSource(cn);
var p2 = SandD(p1);
Assert.IsNotNull(p2);
//Assert.AreEqual(p1.Format, p2.Format, "MbTiles Format not equal");
Assert.AreEqual(p1.Type, p2.Type, "MbTiles Type not equal");
string msg;
Assert.IsTrue(EqualTileSources(p1, p2, out msg), msg);
//Assert.IsTrue(EqualTileSchemas(p1.Schema, p2.Schema, out msg), msg);
}
[Test]
public void TestKnownTileSources()
{
foreach (KnownTileSource kts in Enum.GetValues(typeof(KnownTileSource)))
{
try
{
var srcS = KnownTileSources.Create(kts);
var srcD = SandD(srcS);
Assert.IsNotNull(srcD);
string message;
Assert.IsTrue(EqualTileSources(srcS, srcD, out message));
}
catch
{
}
}
}
[Test]
public void TestHttpTileProvider()
{
var srcS = new HttpTileProvider(new BasicRequest("http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png", new [] {"a", "b"}), userAgent: "HUHU");
var srcD = SandD(srcS);
Assert.IsNotNull(srcD);
}
[Test]
public void TestNullCache()
{
var srcS = new NullCache();
var srcD = SandD(srcS);
Assert.IsNotNull(srcD);
}
[TestCase("http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png", new[] { "a", "b" }, null)]
public void TestBasicRequest(string urlFormatter, IEnumerable<string> serverNodes, string apiKey)
{
var srcS = new BasicRequest(urlFormatter, serverNodes, apiKey);
var srcD = SandD(srcS);
Assert.IsNotNull(srcD);
}
#endregion
#region private helper methods
private static bool EqualTileSources(ITileSource ts1, ITileSource ts2, out string message)
{
if (!ReferenceEquals(ts1, ts2))
{
if (ts1 == null)
{
message = "The reference tile source is null!";
return false;
}
if (ts2 == null)
{
message = "One of the tile sources is null, and the other not";
return false;
}
if (!EqualTileSchemas(ts1.Schema, ts2.Schema, out message))
return false;
if (!EqualTileProviders(ts1.Schema, (ITileProvider)ts1, (ITileProvider)ts2, out message))
return false;
}
message = "Tile sources seem to be equal";
return true;
}
private static bool EqualTileProviders(ITileSchema schema, ITileProvider tp1, ITileProvider tp2, out string message)
{
if (!ReferenceEquals(tp1, tp2))
{
if (tp1 == null)
{
message = "The reference tile provider is null!";
return false;
}
if (tp2 == null)
{
message = "One of the tile providers is null, and the other not";
return false;
}
if (tp1.GetType() != tp2.GetType())
{
message = "Tile providers are of different type";
return false;
}
var keys = schema.Resolutions.Keys.ToArray();
var minkey = GetIndex(keys[0]);
var maxKey = GetIndex(keys[keys.Length - 1]);
var prefix = GetPrefix(keys[0]);
for (var i = 0; i < 3; i++)
{
TileInfo ti = null;
try
{
ti = RandomTileInfo(prefix + "{0}", minkey, Math.Min(maxKey, 12));
var req1 = tp1 as IRequest;
var req2 = tp2 as IRequest;
if (req1 != null && req2 != null)
{
if (req1.GetUri(ti) != req2.GetUri(ti))
{
message = "Request builders produce different uris for the same request";
return false;
}
}
else
{
var t1 = tp1.GetTile(ti);
var t2 = tp2.GetTile(ti);
if (!TilesEqual(t1, t2))
{
message = "Request builders produce different results for same tile request";
return false;
}
}
}
catch (TimeoutException ex)
{
System.Diagnostics.Trace.WriteLine(string.Format(
"TileInfo({4}): {0}, {1}, {2}\n{3}",
ti.Index.Level, ti.Index.Col, ti.Index.Row, ex.Message, i));
}
catch(WebException ex)
{
if (ti == null)
System.Diagnostics.Trace.WriteLine("No tile info!");
else
System.Diagnostics.Trace.WriteLine(string.Format(
"TileInfo({5}): {0}, {1}, {2}\n{3}\n{4}",
ti.Index.Level, ti.Index.Col, ti.Index.Row,
ex.Message, ex.Response.ResponseUri, i));
}
}
}
message = "Tile providers appear to be equal";
return true;
}
private static int GetIndex(string s)
{
var pos = s.LastIndexOf(":", StringComparison.Ordinal);
if (pos == 0)
return int.Parse(s, NumberFormatInfo.InvariantInfo);
return int.Parse(s.Substring(pos + 1), NumberFormatInfo.InvariantInfo);
}
private static string GetPrefix(string s)
{
var pos = s.LastIndexOf(":", StringComparison.Ordinal);
if (pos == -1)
return string.Empty;
return s.Substring(0, pos+1);
}
private static bool TilesEqual(IEnumerable<byte> t1, IList<byte> t2)
{
if (ReferenceEquals(t1, t2))
return true;
if (t1 == null ^ t2 == null)
return false;
if (t1 == null)
return false;
return !t1.Where((t, i) => t != t2[i]).Any();
}
/*
private static bool EqualRequestBuilders(IRequest ts1, IRequest ts2, out string message)
{
if (!ReferenceEquals(ts1, ts2))
{
if (ts1 == null ^ ts2 == null)
{
message = "One of the request builders is null, and the other not";
return false;
}
if (ts1 == null)
{
message = "Reference request builder is null";
return false;
}
for (var i = 0; i < 50; i++)
{
var rnd = RandomTileInfo();
var uri1 = ts1.GetUri(rnd);
var uri2 = ts2.GetUri(rnd);
if (uri1 != uri2)
{
message = "Request builders produce different results for same tile request";
return false;
}
}
}
message = "Request builder seem to be equal";
return true;
}
*/
private static readonly Random Rnd = new Random();
private static TileInfo RandomTileInfo(string format = "{0}", int minLevel = 3, int maxLevel = 12)
{
var level = Rnd.Next(minLevel, maxLevel);
var max = (int)Math.Pow(2, level) - 1;
return new TileInfo
{
Extent = new Extent(),
Index = new TileIndex(Rnd.Next(0, max), Rnd.Next(0, max), string.Format(CultureInfo.InvariantCulture, format, level))
};
}
private static bool EqualTileSchemas(ITileSchema ts1, ITileSchema ts2, out string message)
{
if (ts1.Name != ts2.Name)
{
message = "Names don't match";
return false;
}
if (ts1.Srs != ts2.Srs)
{
message = "Srs' don't match";
return false;
}
if (ts1.Format != ts2.Format)
{
message = "Formats don't match";
return false;
}
if (ts1.Extent != ts2.Extent)
{
message = "Extents don't match";
return false;
}
var tts1 = ts1 as TileSchema;
var tts2 = ts2 as TileSchema;
if (tts1 != null && tts2 != null)
{
if (tts1.Resolutions.Count != tts2.Resolutions.Count)
{
message = "Number of resolutions don't match";
return false;
}
if (tts1.OriginX != tts2.OriginX)
{
message = "OriginX' don't match";
return false;
}
if (tts1.OriginY != tts2.OriginY)
{
message = "OriginYs don't match";
return false;
}
foreach(var res1 in tts1.Resolutions.Values)
{
var res2 = tts2.Resolutions[res1.Id];
if (tts1.GetTileHeight(res1.Id) != tts2.GetTileHeight(res2.Id))
{
message = "Heights don't match";
return false;
}
if (tts1.GetTileWidth(res1.Id) != tts2.GetTileWidth(res2.Id))
{
message = "Widths don't match";
return false;
}
}
}
var wts1 = ts1 as WmtsTileSchema;
var wts2 = ts2 as WmtsTileSchema;
if (wts1 != null && wts2 != null)
{
if (wts1.Abstract != wts2.Abstract)
{
message = "Abstracts don't match";
return false;
}
if (wts1.Style != wts2.Style)
{
message = "Styles don't match";
return false;
}
if (wts1.Identifier != wts2.Identifier)
{
message = "Identifiers don't match";
return false;
}
if (wts1.SupportedSRS.ToString() != wts2.SupportedSRS.ToString())
{
message = "SupportedSRS' don't match";
return false;
}
}
if (ts1.Resolutions.Count != ts2.Resolutions.Count)
{
message = "Number of resolutions doesn't match";
return false;
}
foreach (var key in ts1.Resolutions.Keys)
{
var r1 = ts1.Resolutions[key];
var r2 = ts2.Resolutions[key];
//!!! compare other resultion parameters.
if (r1.Id != r2.Id || r1.UnitsPerPixel != r2.UnitsPerPixel)
{
message = string.Format("Resolution doesn't match at index {0}", key);
return false;
}
}
message = "Schemas are equal!";
return true;
}
private static T SandD<T>(T obj, IFormatter formatter = null)
{
if (formatter == null)
{
formatter = new BinaryFormatter();
formatter.AddBruTileSurrogates();
}
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(ms);
}
}
#endregion
}
}